fix: bring back support for middleware Array (#3434)
Our validation didn't account for `_middleware` files exporting an Array of middlewares. Fixes https://github.com/denoland/fresh/issues/3400
This commit is contained in:
@@ -39,11 +39,15 @@ export interface FsRouteFile<State> {
|
|||||||
css: string[];
|
css: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
// deno-lint-ignore no-explicit-any
|
function isFreshFile<State>(
|
||||||
function isFreshFile<State>(mod: any): mod is FreshFsMod<State> {
|
// deno-lint-ignore no-explicit-any
|
||||||
|
mod: any,
|
||||||
|
commandType: CommandType,
|
||||||
|
): mod is FreshFsMod<State> {
|
||||||
if (mod === null || typeof mod !== "object") return false;
|
if (mod === null || typeof mod !== "object") return false;
|
||||||
|
|
||||||
return typeof mod.default === "function" ||
|
return typeof mod.default === "function" ||
|
||||||
|
commandType === CommandType.Middleware && Array.isArray(mod.default) ||
|
||||||
typeof mod.config === "object" || typeof mod.handlers === "object" ||
|
typeof mod.config === "object" || typeof mod.handlers === "object" ||
|
||||||
typeof mod.handlers === "function" || typeof mod.handler === "object" ||
|
typeof mod.handlers === "function" || typeof mod.handler === "object" ||
|
||||||
typeof mod.handler === "function";
|
typeof mod.handler === "function";
|
||||||
@@ -73,7 +77,7 @@ export function fsItemsToCommands<State>(
|
|||||||
switch (type) {
|
switch (type) {
|
||||||
case CommandType.Middleware: {
|
case CommandType.Middleware: {
|
||||||
if (isLazy(rawMod)) continue;
|
if (isLazy(rawMod)) continue;
|
||||||
const { handlers, mod } = validateFsMod(filePath, rawMod);
|
const { handlers, mod } = validateFsMod(filePath, rawMod, type);
|
||||||
|
|
||||||
let middlewares = (handlers ?? mod.default) as unknown as
|
let middlewares = (handlers ?? mod.default) as unknown as
|
||||||
| Middleware<State>
|
| Middleware<State>
|
||||||
@@ -97,7 +101,7 @@ export function fsItemsToCommands<State>(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case CommandType.Layout: {
|
case CommandType.Layout: {
|
||||||
const { handlers, mod } = validateFsMod<State>(filePath, rawMod);
|
const { handlers, mod } = validateFsMod<State>(filePath, rawMod, type);
|
||||||
if (handlers !== null) {
|
if (handlers !== null) {
|
||||||
warnInvalidRoute("Layout does not support handlers");
|
warnInvalidRoute("Layout does not support handlers");
|
||||||
}
|
}
|
||||||
@@ -107,7 +111,7 @@ export function fsItemsToCommands<State>(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case CommandType.Error: {
|
case CommandType.Error: {
|
||||||
const { handlers, mod } = validateFsMod<State>(filePath, rawMod);
|
const { handlers, mod } = validateFsMod<State>(filePath, rawMod, type);
|
||||||
commands.push(newErrorCmd(
|
commands.push(newErrorCmd(
|
||||||
pattern,
|
pattern,
|
||||||
{
|
{
|
||||||
@@ -121,7 +125,7 @@ export function fsItemsToCommands<State>(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case CommandType.NotFound: {
|
case CommandType.NotFound: {
|
||||||
const { handlers, mod } = validateFsMod<State>(filePath, rawMod);
|
const { handlers, mod } = validateFsMod<State>(filePath, rawMod, type);
|
||||||
commands.push(newNotFoundCmd({
|
commands.push(newNotFoundCmd({
|
||||||
config: mod.config,
|
config: mod.config,
|
||||||
component: mod.default,
|
component: mod.default,
|
||||||
@@ -131,7 +135,7 @@ export function fsItemsToCommands<State>(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
case CommandType.App: {
|
case CommandType.App: {
|
||||||
const { mod } = validateFsMod<State>(filePath, rawMod);
|
const { mod } = validateFsMod<State>(filePath, rawMod, type);
|
||||||
if (mod.default === undefined) continue;
|
if (mod.default === undefined) continue;
|
||||||
|
|
||||||
commands.push(newAppCmd(mod.default));
|
commands.push(newAppCmd(mod.default));
|
||||||
@@ -148,7 +152,7 @@ export function fsItemsToCommands<State>(
|
|||||||
}, async (span) => {
|
}, async (span) => {
|
||||||
try {
|
try {
|
||||||
const result = await rawMod();
|
const result = await rawMod();
|
||||||
return normalizeRoute(filePath, result, routePattern);
|
return normalizeRoute(filePath, result, routePattern, type);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
recordSpanError(span, err);
|
recordSpanError(span, err);
|
||||||
throw err;
|
throw err;
|
||||||
@@ -162,7 +166,7 @@ export function fsItemsToCommands<State>(
|
|||||||
config.routeOverride = item.overrideConfig?.routeOverride ??
|
config.routeOverride = item.overrideConfig?.routeOverride ??
|
||||||
routePattern;
|
routePattern;
|
||||||
} else {
|
} else {
|
||||||
normalized = normalizeRoute(filePath, rawMod, routePattern);
|
normalized = normalizeRoute(filePath, rawMod, routePattern, type);
|
||||||
if (rawMod.config) {
|
if (rawMod.config) {
|
||||||
config = rawMod.config;
|
config = rawMod.config;
|
||||||
}
|
}
|
||||||
@@ -301,11 +305,12 @@ function getRoutePathScore(char: string, s: string, i: number): number {
|
|||||||
export function validateFsMod<State>(
|
export function validateFsMod<State>(
|
||||||
filePath: string,
|
filePath: string,
|
||||||
mod: unknown,
|
mod: unknown,
|
||||||
|
commandType: CommandType,
|
||||||
): {
|
): {
|
||||||
handlers: RouteHandler<unknown, State> | HandlerFn<unknown, State>[] | null;
|
handlers: RouteHandler<unknown, State> | HandlerFn<unknown, State>[] | null;
|
||||||
mod: FreshFsMod<State>;
|
mod: FreshFsMod<State>;
|
||||||
} {
|
} {
|
||||||
if (!isFreshFile<State>(mod)) {
|
if (!isFreshFile<State>(mod, commandType)) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
`Expected a route, middleware, layout or error template, but couldn't find relevant exports in: ${filePath}`,
|
`Expected a route, middleware, layout or error template, but couldn't find relevant exports in: ${filePath}`,
|
||||||
);
|
);
|
||||||
@@ -325,8 +330,9 @@ function normalizeRoute<State>(
|
|||||||
filePath: string,
|
filePath: string,
|
||||||
rawMod: FreshFsMod<State>,
|
rawMod: FreshFsMod<State>,
|
||||||
routePattern: string,
|
routePattern: string,
|
||||||
|
commandType: CommandType,
|
||||||
): Route<State> {
|
): Route<State> {
|
||||||
const { handlers, mod } = validateFsMod<State>(filePath, rawMod);
|
const { handlers, mod } = validateFsMod<State>(filePath, rawMod, commandType);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
config: {
|
config: {
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
import type { Middleware } from "@fresh/core";
|
||||||
|
|
||||||
|
const middleware1: Middleware<{ text: string }> = async (ctx) => {
|
||||||
|
ctx.state.text = "A";
|
||||||
|
return await ctx.next();
|
||||||
|
};
|
||||||
|
|
||||||
|
const middleware2: Middleware<{ text: string }> = async (ctx) => {
|
||||||
|
ctx.state.text += "B";
|
||||||
|
return await ctx.next();
|
||||||
|
};
|
||||||
|
|
||||||
|
export default [middleware1, middleware2];
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
import type { HandlerFn } from "@fresh/core";
|
||||||
|
|
||||||
|
export const handler: HandlerFn<null, { text: string }> = (ctx) =>
|
||||||
|
new Response(ctx.state.text);
|
||||||
@@ -474,3 +474,19 @@ Deno.test({
|
|||||||
sanitizeOps: false,
|
sanitizeOps: false,
|
||||||
sanitizeResources: false,
|
sanitizeResources: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "vite build - support _middleware Array",
|
||||||
|
fn: async () => {
|
||||||
|
await launchProd(
|
||||||
|
{ cwd: viteResult.tmp },
|
||||||
|
async (address) => {
|
||||||
|
const res = await fetch(`${address}/tests/middlewares`);
|
||||||
|
const text = await res.text();
|
||||||
|
expect(text).toEqual("AB");
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
sanitizeOps: false,
|
||||||
|
sanitizeResources: false,
|
||||||
|
});
|
||||||
|
|||||||
@@ -419,3 +419,14 @@ Deno.test({
|
|||||||
sanitizeOps: false,
|
sanitizeOps: false,
|
||||||
sanitizeResources: false,
|
sanitizeResources: false,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test({
|
||||||
|
name: "vite dev - support _middleware Array",
|
||||||
|
fn: async () => {
|
||||||
|
const res = await fetch(`${demoServer.address()}/tests/middlewares`);
|
||||||
|
const text = await res.text();
|
||||||
|
expect(text).toEqual("AB");
|
||||||
|
},
|
||||||
|
sanitizeOps: false,
|
||||||
|
sanitizeResources: false,
|
||||||
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user