fix(server): merge competing PluginMiddleware handlers together (#1545)
This commit is contained in:
+41
-15
@@ -2,6 +2,7 @@ import {
|
|||||||
dirname,
|
dirname,
|
||||||
extname,
|
extname,
|
||||||
fromFileUrl,
|
fromFileUrl,
|
||||||
|
groupBy,
|
||||||
join,
|
join,
|
||||||
JSONC,
|
JSONC,
|
||||||
Status,
|
Status,
|
||||||
@@ -1269,24 +1270,49 @@ async function readDenoConfig(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatMiddlewarePath(path: string): string {
|
||||||
|
let formattedPath = path;
|
||||||
|
if (!path.startsWith("/")) {
|
||||||
|
formattedPath = `/${formattedPath}`;
|
||||||
|
}
|
||||||
|
if (!formattedPath.endsWith("/")) {
|
||||||
|
formattedPath = `${formattedPath}/`;
|
||||||
|
}
|
||||||
|
return formattedPath;
|
||||||
|
}
|
||||||
|
|
||||||
function getMiddlewareRoutesFromPlugins(
|
function getMiddlewareRoutesFromPlugins(
|
||||||
plugins: Plugin[],
|
plugins: Plugin[],
|
||||||
): Record<string, MiddlewareModule> {
|
): Record<string, MiddlewareModule> {
|
||||||
return (Object.assign(
|
// 1. Collect all middlewares from all plugins
|
||||||
{},
|
const allPluginMiddlewares = plugins.flatMap((plugin) =>
|
||||||
...[
|
plugin.middlewares || []
|
||||||
...new Set(
|
);
|
||||||
([] as PluginMiddleware[]).concat(
|
|
||||||
...plugins.map((p) => p.middlewares || []),
|
// 2. Group PluginMiddleware by their path
|
||||||
),
|
const groupedPluginMiddlewares: Record<string, PluginMiddleware[]> = {
|
||||||
),
|
...(groupBy(
|
||||||
]
|
allPluginMiddlewares,
|
||||||
.map((middleware: PluginMiddleware) => ({
|
(middleware) =>
|
||||||
[`./routes${middleware.path}_middleware.ts`]: {
|
`./routes${formatMiddlewarePath(middleware.path)}_middleware.ts`,
|
||||||
handler: middleware.middleware.handler,
|
) as Record<string, PluginMiddleware[]>),
|
||||||
},
|
};
|
||||||
})) || [],
|
|
||||||
));
|
// 3. Convert the grouped PluginMiddleware to MiddlewareModule
|
||||||
|
// this involves merging the Handler or Handler[]
|
||||||
|
const middlewareRoutes: Record<string, MiddlewareModule> = Object.fromEntries(
|
||||||
|
Object.entries(groupedPluginMiddlewares).map(([key, pluginMiddlewares]) => {
|
||||||
|
const handler = pluginMiddlewares.flatMap((pluginMiddleware) => {
|
||||||
|
const middlewareHandler = pluginMiddleware.middleware.handler;
|
||||||
|
return Array.isArray(middlewareHandler)
|
||||||
|
? middlewareHandler
|
||||||
|
: [middlewareHandler];
|
||||||
|
});
|
||||||
|
return [key, { handler }];
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
return middlewareRoutes;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getRoutesFromPlugins(plugins: Plugin[]): Record<string, RouteModule> {
|
function getRoutesFromPlugins(plugins: Plugin[]): Record<string, RouteModule> {
|
||||||
|
|||||||
@@ -25,3 +25,4 @@ export {
|
|||||||
assertSpyCalls,
|
assertSpyCalls,
|
||||||
spy,
|
spy,
|
||||||
} from "https://deno.land/std@0.193.0/testing/mock.ts";
|
} from "https://deno.land/std@0.193.0/testing/mock.ts";
|
||||||
|
export { groupBy } from "https://deno.land/std@0.193.0/collections/group_by.ts";
|
||||||
|
|||||||
@@ -2,16 +2,18 @@
|
|||||||
// This file SHOULD be checked into source version control.
|
// This file SHOULD be checked into source version control.
|
||||||
// This file is automatically updated during development when running `dev.ts`.
|
// This file is automatically updated during development when running `dev.ts`.
|
||||||
|
|
||||||
import * as $0 from "./routes/static.tsx";
|
import * as $0 from "./routes/lots-of-middleware/index.tsx";
|
||||||
import * as $1 from "./routes/test.tsx";
|
import * as $1 from "./routes/static.tsx";
|
||||||
import * as $2 from "./routes/with-island.tsx";
|
import * as $2 from "./routes/test.tsx";
|
||||||
|
import * as $3 from "./routes/with-island.tsx";
|
||||||
import * as $$0 from "./islands/Island.tsx";
|
import * as $$0 from "./islands/Island.tsx";
|
||||||
|
|
||||||
const manifest = {
|
const manifest = {
|
||||||
routes: {
|
routes: {
|
||||||
"./routes/static.tsx": $0,
|
"./routes/lots-of-middleware/index.tsx": $0,
|
||||||
"./routes/test.tsx": $1,
|
"./routes/static.tsx": $1,
|
||||||
"./routes/with-island.tsx": $2,
|
"./routes/test.tsx": $2,
|
||||||
|
"./routes/with-island.tsx": $3,
|
||||||
},
|
},
|
||||||
islands: {
|
islands: {
|
||||||
"./islands/Island.tsx": $$0,
|
"./islands/Island.tsx": $$0,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import cssInjectPlugin from "./utils/css-inject-plugin.ts";
|
|||||||
import jsInjectPlugin from "./utils/js-inject-plugin.ts";
|
import jsInjectPlugin from "./utils/js-inject-plugin.ts";
|
||||||
import cssInjectPluginAsync from "./utils/css-inject-plugin-async.ts";
|
import cssInjectPluginAsync from "./utils/css-inject-plugin-async.ts";
|
||||||
import routePlugin from "./utils/route-plugin.ts";
|
import routePlugin from "./utils/route-plugin.ts";
|
||||||
|
import secondMiddlewarePlugin from "$fresh/tests/fixture_plugin/utils/second-middleware-plugin.ts";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
plugins: [
|
plugins: [
|
||||||
@@ -10,5 +11,6 @@ export default {
|
|||||||
jsInjectPlugin,
|
jsInjectPlugin,
|
||||||
cssInjectPluginAsync,
|
cssInjectPluginAsync,
|
||||||
routePlugin({ title: "Title Set From Plugin Config" }),
|
routePlugin({ title: "Title Set From Plugin Config" }),
|
||||||
|
secondMiddlewarePlugin(),
|
||||||
],
|
],
|
||||||
} as FreshOptions;
|
} as FreshOptions;
|
||||||
|
|||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { Handlers, PageProps } from "$fresh/server.ts";
|
||||||
|
import { PluginMiddlewareState } from "../../utils/route-plugin.ts";
|
||||||
|
|
||||||
|
export const handler: Handlers = {
|
||||||
|
async GET(_req, ctx) {
|
||||||
|
const resp = await ctx.render();
|
||||||
|
return resp;
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default function Home(props: PageProps<unknown, PluginMiddlewareState>) {
|
||||||
|
const value = props.state.num;
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<h1>{value}</h1>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Plugin } from "$fresh/server.ts";
|
import { MiddlewareHandlerContext, Plugin } from "$fresh/server.ts";
|
||||||
import { handler as testMiddleware } from "./sample_routes/_middleware.ts";
|
import { handler as testMiddleware } from "./sample_routes/_middleware.ts";
|
||||||
import { AppBuilder } from "./sample_routes/AppBuilder.tsx";
|
import { AppBuilder } from "./sample_routes/AppBuilder.tsx";
|
||||||
export type { Options };
|
export type { Options };
|
||||||
@@ -6,15 +6,41 @@ export type { Options };
|
|||||||
interface Options {
|
interface Options {
|
||||||
title: string;
|
title: string;
|
||||||
}
|
}
|
||||||
|
export type PluginMiddlewareState = {
|
||||||
|
num: number;
|
||||||
|
test: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
const twoPointlessMiddlewares = [
|
||||||
|
async (
|
||||||
|
_req: Request,
|
||||||
|
ctx: MiddlewareHandlerContext<PluginMiddlewareState>,
|
||||||
|
) => {
|
||||||
|
ctx.state.num = ctx.state.num === undefined ? 1 : ctx.state.num + 1;
|
||||||
|
return await ctx.next();
|
||||||
|
},
|
||||||
|
async (
|
||||||
|
_req: Request,
|
||||||
|
ctx: MiddlewareHandlerContext<PluginMiddlewareState>,
|
||||||
|
) => {
|
||||||
|
ctx.state.num = ctx.state.num === undefined ? 1 : ctx.state.num + 1;
|
||||||
|
return await ctx.next();
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
export default function routePlugin(
|
export default function routePlugin(
|
||||||
options: Options,
|
options: Options,
|
||||||
): Plugin<{ test: string }> {
|
): Plugin<PluginMiddlewareState> {
|
||||||
return {
|
return {
|
||||||
name: "routePlugin",
|
name: "routePlugin",
|
||||||
middlewares: [{
|
middlewares: [{
|
||||||
middleware: { handler: testMiddleware },
|
middleware: { handler: testMiddleware },
|
||||||
path: "/",
|
path: "/",
|
||||||
|
}, {
|
||||||
|
middleware: {
|
||||||
|
handler: twoPointlessMiddlewares,
|
||||||
|
},
|
||||||
|
path: "lots-of-middleware",
|
||||||
}],
|
}],
|
||||||
routes: [{
|
routes: [{
|
||||||
path: "/_app",
|
path: "/_app",
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
import { MiddlewareHandlerContext } from "$fresh/server.ts";
|
||||||
|
import { PluginMiddlewareState } from "../../utils/route-plugin.ts";
|
||||||
|
|
||||||
export async function handler(
|
export async function handler(
|
||||||
_req: Request,
|
_req: Request,
|
||||||
ctx: MiddlewareHandlerContext<{ test: string }>,
|
ctx: MiddlewareHandlerContext<PluginMiddlewareState>,
|
||||||
) {
|
) {
|
||||||
ctx.state.test = "look, i'm set from a plugin!";
|
ctx.state.test = "look, i'm set from a plugin!";
|
||||||
const resp = await ctx.next();
|
const resp = await ctx.next();
|
||||||
|
|||||||
@@ -0,0 +1,36 @@
|
|||||||
|
import { MiddlewareHandlerContext, Plugin } from "$fresh/server.ts";
|
||||||
|
import { PluginMiddlewareState } from "./route-plugin.ts";
|
||||||
|
|
||||||
|
export default function secondMiddlewarePlugin(): Plugin<
|
||||||
|
PluginMiddlewareState
|
||||||
|
> {
|
||||||
|
return {
|
||||||
|
name: "secondMiddlewarePlugin",
|
||||||
|
middlewares: [{
|
||||||
|
middleware: {
|
||||||
|
handler: async (_req: Request, ctx: MiddlewareHandlerContext) => {
|
||||||
|
return await ctx.next();
|
||||||
|
},
|
||||||
|
},
|
||||||
|
path: "/", // this is the root route
|
||||||
|
}, {
|
||||||
|
middleware: {
|
||||||
|
handler: async (
|
||||||
|
_req: Request,
|
||||||
|
ctx: MiddlewareHandlerContext<PluginMiddlewareState>,
|
||||||
|
) => {
|
||||||
|
ctx.state.num = ctx.state.num === undefined ? 1 : ctx.state.num + 1;
|
||||||
|
return await ctx.next();
|
||||||
|
},
|
||||||
|
},
|
||||||
|
path: "lots-of-middleware/",
|
||||||
|
}, {
|
||||||
|
middleware: {
|
||||||
|
handler: async (_req: Request, ctx: MiddlewareHandlerContext) => {
|
||||||
|
return await ctx.next();
|
||||||
|
},
|
||||||
|
},
|
||||||
|
path: "", // this also goes to the root route, as of 1.4
|
||||||
|
}],
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -68,6 +68,19 @@ Deno.test("plugin routes and middleware", async () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Deno.test("plugin middleware multiple handlers", async () => {
|
||||||
|
const resp = await router(
|
||||||
|
new Request("https://fresh.deno.dev/lots-of-middleware"),
|
||||||
|
);
|
||||||
|
assert(resp);
|
||||||
|
assertEquals(resp.status, Status.OK);
|
||||||
|
const body = await resp.text();
|
||||||
|
assertStringIncludes(
|
||||||
|
body,
|
||||||
|
`<h1>3</h1>`,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
Deno.test({
|
Deno.test({
|
||||||
name: "/with-island hydration",
|
name: "/with-island hydration",
|
||||||
async fn(t) {
|
async fn(t) {
|
||||||
|
|||||||
Reference in New Issue
Block a user