feat: layered and scoped middleware and context state (#139)
Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
co-authored by
Luca Casonato
parent
b36f2c1a43
commit
8d4e77f4ac
@@ -20,4 +20,7 @@ declare global {
|
||||
|
||||
// deno-lint-ignore no-var
|
||||
var __FRSH_BUILD_ID: string;
|
||||
|
||||
// deno-lint-ignore no-explicit-any no-var
|
||||
var URLPattern: any;
|
||||
}
|
||||
|
||||
+106
-30
@@ -11,6 +11,7 @@ import {
|
||||
import { Manifest } from "./mod.ts";
|
||||
import { Bundler } from "./bundle.ts";
|
||||
import { ALIVE_URL, BUILD_ID, JS_PREFIX, REFRESH_JS_URL } from "./constants.ts";
|
||||
|
||||
import DefaultErrorHandler from "./default_error_page.tsx";
|
||||
import {
|
||||
AppModule,
|
||||
@@ -20,10 +21,12 @@ import {
|
||||
Island,
|
||||
Middleware,
|
||||
MiddlewareModule,
|
||||
MiddlewareRoute,
|
||||
Page,
|
||||
PageModule,
|
||||
Renderer,
|
||||
RendererModule,
|
||||
RouterState,
|
||||
UnknownPage,
|
||||
UnknownPageModule,
|
||||
} from "./types.ts";
|
||||
@@ -52,7 +55,7 @@ export class ServerContext {
|
||||
#staticFiles: StaticFile[];
|
||||
#bundler: Bundler;
|
||||
#renderer: Renderer;
|
||||
#middleware: Middleware;
|
||||
#middlewares: MiddlewareRoute[];
|
||||
#app: AppModule;
|
||||
#notFound: UnknownPage;
|
||||
#error: ErrorPage;
|
||||
@@ -62,7 +65,7 @@ export class ServerContext {
|
||||
islands: Island[],
|
||||
staticFiles: StaticFile[],
|
||||
renderer: Renderer,
|
||||
middleware: Middleware,
|
||||
middlewares: MiddlewareRoute[],
|
||||
app: AppModule,
|
||||
notFound: UnknownPage,
|
||||
error: ErrorPage,
|
||||
@@ -71,7 +74,7 @@ export class ServerContext {
|
||||
this.#islands = islands;
|
||||
this.#staticFiles = staticFiles;
|
||||
this.#renderer = renderer;
|
||||
this.#middleware = middleware;
|
||||
this.#middlewares = middlewares;
|
||||
this.#app = app;
|
||||
this.#notFound = notFound;
|
||||
this.#error = error;
|
||||
@@ -90,7 +93,7 @@ export class ServerContext {
|
||||
const pages: Page[] = [];
|
||||
const islands: Island[] = [];
|
||||
let renderer: Renderer = DEFAULT_RENDERER;
|
||||
let middleware: Middleware = DEFAULT_MIDDLEWARE;
|
||||
const middlewares: MiddlewareRoute[] = [];
|
||||
let app: AppModule = DEFAULT_APP;
|
||||
let notFound: UnknownPage = DEFAULT_NOT_FOUND;
|
||||
let error: ErrorPage = DEFAULT_ERROR;
|
||||
@@ -102,7 +105,10 @@ export class ServerContext {
|
||||
const path = url.substring(baseUrl.length).substring("routes".length);
|
||||
const baseRoute = path.substring(1, path.length - extname(path).length);
|
||||
const name = baseRoute.replace("/", "-");
|
||||
if (!path.startsWith("/_")) {
|
||||
const isMiddleware = path.endsWith("/_middleware.tsx") ||
|
||||
path.endsWith("/_middleware.ts") || path.endsWith("/_middleware.jsx") ||
|
||||
path.endsWith("/_middleware.js");
|
||||
if (!path.startsWith("/_") && !isMiddleware) {
|
||||
const { default: component, config } = (module as PageModule);
|
||||
let route = pathToRoute(baseRoute);
|
||||
if (config?.routeOverride) {
|
||||
@@ -130,11 +136,11 @@ export class ServerContext {
|
||||
path === "/_render.jsx" || path === "/_render.js"
|
||||
) {
|
||||
renderer = module as RendererModule;
|
||||
} else if (
|
||||
path === "/_middleware.tsx" || path === "/_middleware.ts" ||
|
||||
path === "/_middleware.jsx" || path === "/_middleware.js"
|
||||
) {
|
||||
middleware = module as MiddlewareModule;
|
||||
} else if (isMiddleware) {
|
||||
middlewares.push({
|
||||
...middlewarePathToPattern(baseRoute),
|
||||
...module as MiddlewareModule,
|
||||
});
|
||||
} else if (
|
||||
path === "/_app.tsx" || path === "/_app.ts" ||
|
||||
path === "/_app.jsx" || path === "/_app.js"
|
||||
@@ -180,6 +186,7 @@ export class ServerContext {
|
||||
}
|
||||
}
|
||||
sortRoutes(pages);
|
||||
sortRoutes(middlewares);
|
||||
|
||||
for (const [self, module] of Object.entries(manifest.islands)) {
|
||||
const url = new URL(self, baseUrl).href;
|
||||
@@ -237,12 +244,13 @@ export class ServerContext {
|
||||
throw err;
|
||||
}
|
||||
}
|
||||
|
||||
return new ServerContext(
|
||||
pages,
|
||||
islands,
|
||||
staticFiles,
|
||||
renderer,
|
||||
middleware,
|
||||
middlewares,
|
||||
app,
|
||||
notFound,
|
||||
error,
|
||||
@@ -254,8 +262,8 @@ export class ServerContext {
|
||||
* by fresh, including static files.
|
||||
*/
|
||||
handler(): RequestHandler {
|
||||
const inner = router.router(...this.#routes());
|
||||
const middleware = this.#middleware;
|
||||
const inner = router.router<RouterState>(...this.#routes());
|
||||
const withMiddlewares = this.#composeMiddlewares(this.#middlewares);
|
||||
return function handler(req: Request, connInfo: ConnInfo) {
|
||||
// Redirect requests that end with a trailing slash
|
||||
// to their non-trailing slash counterpart.
|
||||
@@ -265,8 +273,43 @@ export class ServerContext {
|
||||
url.pathname = url.pathname.slice(0, -1);
|
||||
return Response.redirect(url.href, 307);
|
||||
}
|
||||
const handle = () => Promise.resolve(inner(req, connInfo));
|
||||
return middleware.handler(req, { handle, ...connInfo });
|
||||
return withMiddlewares(req, connInfo, inner);
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Identify which middlewares should be applied for a request,
|
||||
* chain them and return a handler response
|
||||
*/
|
||||
#composeMiddlewares(middlewares: MiddlewareRoute[]) {
|
||||
return (
|
||||
req: Request,
|
||||
connInfo: ConnInfo,
|
||||
inner: router.Handler<RouterState>,
|
||||
) => {
|
||||
// identify middlewares to apply, if any.
|
||||
// middlewares should be already sorted from deepest to shallow layer
|
||||
const mws = selectMiddlewares(req.url, middlewares);
|
||||
|
||||
const handlers: (() => Response | Promise<Response>)[] = [];
|
||||
|
||||
const ctx = {
|
||||
next() {
|
||||
const handler = handlers.shift()!;
|
||||
return Promise.resolve(handler());
|
||||
},
|
||||
...connInfo,
|
||||
state: {},
|
||||
};
|
||||
|
||||
for (const mw of mws) {
|
||||
handlers.push(() => mw.handler(req, ctx));
|
||||
}
|
||||
|
||||
handlers.push(() => inner(req, ctx));
|
||||
|
||||
const handler = handlers.shift()!;
|
||||
return handler();
|
||||
};
|
||||
}
|
||||
|
||||
@@ -274,8 +317,12 @@ export class ServerContext {
|
||||
* This function returns all routes required by fresh as an extended
|
||||
* path-to-regex, to handler mapping.
|
||||
*/
|
||||
#routes(): [router.Routes, RequestHandler, router.ErrorHandler] {
|
||||
const routes: router.Routes = {};
|
||||
#routes(): [
|
||||
router.Routes<RouterState>,
|
||||
router.Handler<RouterState>,
|
||||
router.ErrorHandler<RouterState>,
|
||||
] {
|
||||
const routes: router.Routes<RouterState> = {};
|
||||
|
||||
routes[`${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/:path*`] = this
|
||||
.#bundleAssetRoute();
|
||||
@@ -396,17 +443,17 @@ export class ServerContext {
|
||||
for (const page of this.#pages) {
|
||||
const createRender = genRender(page, 200);
|
||||
if (typeof page.handler === "function") {
|
||||
routes[page.route] = (req, connInfo, params) =>
|
||||
routes[page.route] = (req, ctx, params) =>
|
||||
(page.handler as Handler)(req, {
|
||||
...connInfo,
|
||||
...ctx,
|
||||
params,
|
||||
render: createRender(req, params),
|
||||
});
|
||||
} else {
|
||||
for (const [method, handler] of Object.entries(page.handler)) {
|
||||
routes[`${method}@${page.route}`] = (req, connInfo, params) =>
|
||||
routes[`${method}@${page.route}`] = (req, ctx, params) =>
|
||||
handler(req, {
|
||||
...connInfo,
|
||||
...ctx,
|
||||
params,
|
||||
render: createRender(req, params),
|
||||
});
|
||||
@@ -415,17 +462,24 @@ export class ServerContext {
|
||||
}
|
||||
|
||||
const unknownHandlerRender = genRender(this.#notFound, 404);
|
||||
const unknownHandler = (req: Request, connInfo: ConnInfo) =>
|
||||
const unknownHandler: router.Handler<RouterState> = (
|
||||
req,
|
||||
ctx,
|
||||
) =>
|
||||
this.#notFound.handler(
|
||||
req,
|
||||
{
|
||||
...connInfo,
|
||||
...ctx,
|
||||
render: unknownHandlerRender(req, {}),
|
||||
},
|
||||
);
|
||||
|
||||
const errorHandlerRender = genRender(this.#error, 500);
|
||||
const errorHandler = (req: Request, connInfo: ConnInfo, error: unknown) => {
|
||||
const errorHandler: router.ErrorHandler<RouterState> = (
|
||||
req,
|
||||
ctx,
|
||||
error,
|
||||
) => {
|
||||
console.error(
|
||||
"%cAn error occured during route handling or page rendering.",
|
||||
"color:red",
|
||||
@@ -434,7 +488,7 @@ export class ServerContext {
|
||||
return this.#error.handler(
|
||||
req,
|
||||
{
|
||||
...connInfo,
|
||||
...ctx,
|
||||
error,
|
||||
render: errorHandlerRender(req, {}, error),
|
||||
},
|
||||
@@ -478,7 +532,7 @@ export class ServerContext {
|
||||
* constants.INTERNAL_PREFIX
|
||||
*/
|
||||
#bundleAssetRoute = (): router.MatchHandler => {
|
||||
return async (_req, _connInfo, params) => {
|
||||
return async (_req, _ctx, params) => {
|
||||
const path = `/${params.path}`;
|
||||
const file = await this.#bundler.get(path);
|
||||
let res;
|
||||
@@ -511,10 +565,6 @@ const DEFAULT_RENDERER: Renderer = {
|
||||
},
|
||||
};
|
||||
|
||||
const DEFAULT_MIDDLEWARE: Middleware = {
|
||||
handler: (_, ctx) => ctx.handle(),
|
||||
};
|
||||
|
||||
const DEFAULT_APP: AppModule = {
|
||||
default: ({ Component }) => h(Component, {}),
|
||||
};
|
||||
@@ -536,6 +586,25 @@ const DEFAULT_ERROR: ErrorPage = {
|
||||
csp: false,
|
||||
};
|
||||
|
||||
/**
|
||||
* Return a list of middlewares that needs to be applied for request url
|
||||
* @param url the request url
|
||||
* @param middlewares Array of middlewares handlers and their routes as path-to-regexp style
|
||||
*/
|
||||
export function selectMiddlewares(url: string, middlewares: MiddlewareRoute[]) {
|
||||
const selectedMws: Middleware[] = [];
|
||||
const reqURL = new URL(url);
|
||||
|
||||
for (const { pattern, handler } of middlewares) {
|
||||
const res = pattern.exec(reqURL);
|
||||
if (res) {
|
||||
selectedMws.push({ handler });
|
||||
}
|
||||
}
|
||||
|
||||
return selectedMws;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sort pages by their relative routing priority, based on the parts in the
|
||||
* route matcher
|
||||
@@ -612,3 +681,10 @@ function serializeCSPDirectives(csp: ContentSecurityPolicyDirectives): string {
|
||||
})
|
||||
.join("; ");
|
||||
}
|
||||
|
||||
export function middlewarePathToPattern(baseRoute: string) {
|
||||
baseRoute = baseRoute.slice(0, -"_middleware".length);
|
||||
const regRoute = pathToRoute(baseRoute);
|
||||
const pattern = new URLPattern({ pathname: `${regRoute}*` });
|
||||
return { route: regRoute, pattern };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
import { assert } from "../../tests/deps.ts";
|
||||
import { middlewarePathToPattern, selectMiddlewares } from "./context.ts";
|
||||
import { MiddlewareRoute } from "./types.ts";
|
||||
|
||||
Deno.test("selectMiddlewares", () => {
|
||||
const url = "https://fresh.deno.dev/api/abc/def";
|
||||
const middlewaresPath = [
|
||||
// should select
|
||||
"_middleware",
|
||||
"api/_middleware",
|
||||
"api/[id]/_middleware",
|
||||
|
||||
// should not select
|
||||
"api/xyz/_middleware",
|
||||
"api/[id]/xyz/_middleware",
|
||||
"api/[id]/[path]/_middleware",
|
||||
"api/[id]/[path]/foo/_middleware",
|
||||
];
|
||||
const mwRoutes = middlewaresPath.map((path) =>
|
||||
middlewarePathToPattern(path)
|
||||
) as MiddlewareRoute[];
|
||||
const mws = selectMiddlewares(url, mwRoutes);
|
||||
assert(mws.length === 3);
|
||||
});
|
||||
+1
-1
@@ -16,7 +16,7 @@ export type {
|
||||
} from "https://deno.land/std@0.128.0/http/server.ts";
|
||||
|
||||
// -- router --
|
||||
export * as router from "https://crux.land/router@0.0.6";
|
||||
export * as router from "https://crux.land/router@0.0.7";
|
||||
|
||||
// -- media types --
|
||||
export { lookup as mediaTypeLookup } from "https://deno.land/x/media_types@v2.10.2/mod.ts";
|
||||
|
||||
@@ -17,7 +17,7 @@ import {
|
||||
import { HEAD_CONTEXT } from "../runtime/head.ts";
|
||||
import { CSP_CONTEXT, nonce, NONE, UNSAFE_INLINE } from "../runtime/csp.ts";
|
||||
import { ContentSecurityPolicy } from "../runtime/csp.ts";
|
||||
import { BUILD_ID, bundleAssetUrl } from "./constants.ts";
|
||||
import { bundleAssetUrl } from "./constants.ts";
|
||||
|
||||
export interface RenderOptions<Data> {
|
||||
page: Page<Data> | UnknownPage | ErrorPage;
|
||||
|
||||
+41
-15
@@ -9,11 +9,16 @@ import {
|
||||
} from "../runtime/types.ts";
|
||||
import { RenderContext, RenderFn } from "./render.tsx";
|
||||
|
||||
export interface RouterState {
|
||||
state: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export type StartOptions = ServeInit;
|
||||
|
||||
export interface PageModule {
|
||||
default?: ComponentType<PageProps>;
|
||||
handler?: Handler | Handlers;
|
||||
// deno-lint-ignore no-explicit-any
|
||||
handler?: Handler<any, any> | Handlers<any, any>;
|
||||
config?: PageConfig;
|
||||
}
|
||||
|
||||
@@ -34,28 +39,36 @@ export interface IslandModule {
|
||||
default: ComponentType<any>;
|
||||
}
|
||||
|
||||
export interface HandlerContext<T = unknown> extends ConnInfo {
|
||||
export interface HandlerContext<Data = unknown, State = Record<string, unknown>>
|
||||
extends ConnInfo {
|
||||
params: Record<string, string>;
|
||||
render: (data?: T) => Response | Promise<Response>;
|
||||
render: (data?: Data) => Response | Promise<Response>;
|
||||
state: State;
|
||||
}
|
||||
|
||||
export interface UnknownHandlerContext extends ConnInfo {
|
||||
export interface UnknownHandlerContext<State = Record<string, unknown>>
|
||||
extends ConnInfo {
|
||||
render: () => Response | Promise<Response>;
|
||||
state: State;
|
||||
}
|
||||
|
||||
export interface ErrorHandlerContext extends ConnInfo {
|
||||
export interface ErrorHandlerContext<State = Record<string, unknown>>
|
||||
extends ConnInfo {
|
||||
error: unknown;
|
||||
render: () => Response | Promise<Response>;
|
||||
state: State;
|
||||
}
|
||||
|
||||
export interface MiddlewareHandlerContext extends ConnInfo {
|
||||
handle: () => Promise<Response>;
|
||||
export interface MiddlewareHandlerContext<State = Record<string, unknown>>
|
||||
extends ConnInfo {
|
||||
next: () => Promise<Response>;
|
||||
state: State;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export type Handler<T = any> = (
|
||||
export type Handler<T = any, State = Record<string, unknown>> = (
|
||||
req: Request,
|
||||
ctx: HandlerContext<T>,
|
||||
ctx: HandlerContext<T, State>,
|
||||
) => Response | Promise<Response>;
|
||||
export type UnknownHandler = (
|
||||
req: Request,
|
||||
@@ -67,8 +80,8 @@ export type ErrorHandler = (
|
||||
) => Response | Promise<Response>;
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export type Handlers<T = any> = {
|
||||
[K in typeof router.METHODS[number]]?: Handler<T>;
|
||||
export type Handlers<T = any, State = Record<string, unknown>> = {
|
||||
[K in typeof router.METHODS[number]]?: Handler<T, State>;
|
||||
};
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
@@ -107,20 +120,33 @@ export interface Renderer {
|
||||
render(ctx: RenderContext, render: RenderFn): void | Promise<void>;
|
||||
}
|
||||
|
||||
export interface MiddlewareModule {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export interface MiddlewareModule<State = any> {
|
||||
handler(
|
||||
req: Request,
|
||||
ctx: MiddlewareHandlerContext,
|
||||
ctx: MiddlewareHandlerContext<State>,
|
||||
): Response | Promise<Response>;
|
||||
}
|
||||
|
||||
export interface Middleware {
|
||||
export interface Middleware<State = Record<string, unknown>> {
|
||||
handler(
|
||||
req: Request,
|
||||
ctx: MiddlewareHandlerContext,
|
||||
ctx: MiddlewareHandlerContext<State>,
|
||||
): Response | Promise<Response>;
|
||||
}
|
||||
|
||||
export interface MiddlewareRoute extends Middleware {
|
||||
/**
|
||||
* path-to-regexp style url path
|
||||
*/
|
||||
route: string;
|
||||
/**
|
||||
* URLPattern of the route
|
||||
*/
|
||||
// deno-lint-ignore no-explicit-any
|
||||
pattern: any;
|
||||
}
|
||||
|
||||
export interface AppModule {
|
||||
default: ComponentType<AppProps>;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user