From 6c2291f0def3e515de614e7733d5fabacda33d6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elias=20Sj=C3=B6green?= Date: Tue, 23 Aug 2022 23:33:11 +0200 Subject: [PATCH] chore: use x/rutt instead of crux.land/router (#647) Co-authored-by: Luca Casonato --- src/server/context.ts | 28 ++++++++++++++-------------- src/server/deps.ts | 4 ++-- src/server/types.ts | 4 ++-- 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/server/context.ts b/src/server/context.ts index 54812ccb..4ccec269 100644 --- a/src/server/context.ts +++ b/src/server/context.ts @@ -3,7 +3,7 @@ import { extname, fromFileUrl, RequestHandler, - router, + rutt, Status, toFileUrl, typeByExtension, @@ -171,7 +171,7 @@ export class ServerContext { url, name, component, - handler: handler ?? ((req) => router.defaultOtherHandler(req)), + handler: handler ?? ((req) => rutt.defaultOtherHandler(req)), csp: Boolean(config?.csp ?? false), }; } else if ( @@ -190,7 +190,7 @@ export class ServerContext { name, component, handler: handler ?? - ((req, ctx) => router.defaultErrorHandler(req, ctx, ctx.error)), + ((req, ctx) => rutt.defaultErrorHandler(req, ctx, ctx.error)), csp: Boolean(config?.csp ?? false), }; } @@ -279,7 +279,7 @@ export class ServerContext { * by fresh, including static files. */ handler(): RequestHandler { - const inner = router.router(...this.#handlers()); + const inner = rutt.router(...this.#handlers()); const withMiddlewares = this.#composeMiddlewares(this.#middlewares); return function handler(req: Request, connInfo: ConnInfo) { // Redirect requests that end with a trailing slash @@ -302,7 +302,7 @@ export class ServerContext { return ( req: Request, connInfo: ConnInfo, - inner: router.Handler, + inner: rutt.Handler, ) => { // identify middlewares to apply, if any. // middlewares should be already sorted from deepest to shallow layer @@ -335,11 +335,11 @@ export class ServerContext { * path-to-regex, to handler mapping. */ #handlers(): [ - router.Routes, - router.Handler, - router.ErrorHandler, + rutt.Routes, + rutt.Handler, + rutt.ErrorHandler, ] { - const routes: router.Routes = {}; + const routes: rutt.Routes = {}; routes[`${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/:path*`] = this .#bundleAssetRoute(); @@ -484,7 +484,7 @@ export class ServerContext { } } - const unknownHandler: router.Handler = ( + const unknownHandler: rutt.Handler = ( req, ctx, ) => @@ -500,7 +500,7 @@ export class ServerContext { this.#error, Status.InternalServerError, ); - const errorHandler: router.ErrorHandler = ( + const errorHandler: rutt.ErrorHandler = ( req, ctx, error, @@ -528,7 +528,7 @@ export class ServerContext { size: number, contentType: string, etag: string, - ): router.MatchHandler { + ): rutt.MatchHandler { return async (req: Request) => { const url = new URL(req.url); const key = url.searchParams.get(ASSET_CACHE_BUST_KEY); @@ -566,7 +566,7 @@ export class ServerContext { * Returns a router that contains all fresh routes. Should be mounted at * constants.INTERNAL_PREFIX */ - #bundleAssetRoute = (): router.MatchHandler => { + #bundleAssetRoute = (): rutt.MatchHandler => { return async (_req, _ctx, params) => { const path = `/${params.path}`; const file = await this.#bundler.get(path); @@ -606,7 +606,7 @@ const DEFAULT_NOT_FOUND: UnknownPage = { pattern: "", url: "", name: "_404", - handler: (req) => router.defaultOtherHandler(req), + handler: (req) => rutt.defaultOtherHandler(req), csp: false, }; diff --git a/src/server/deps.ts b/src/server/deps.ts index 0f96d6b8..2fca03b2 100644 --- a/src/server/deps.ts +++ b/src/server/deps.ts @@ -16,8 +16,8 @@ export { typeByExtension, } from "https://deno.land/std@0.150.0/media_types/mod.ts"; -// -- router -- -export * as router from "https://crux.land/router@0.0.11"; +// -- rutt -- +export * as rutt from "https://deno.land/x/rutt@0.0.13/mod.ts"; // -- esbuild -- // @deno-types="https://deno.land/x/esbuild@v0.14.51/mod.d.ts" diff --git a/src/server/types.ts b/src/server/types.ts index d797ad34..77f46dd5 100644 --- a/src/server/types.ts +++ b/src/server/types.ts @@ -1,5 +1,5 @@ import { ComponentType } from "preact"; -import { ConnInfo, router, ServeInit } from "./deps.ts"; +import { ConnInfo, rutt, ServeInit } from "./deps.ts"; import { InnerRenderFunction, RenderContext } from "./render.tsx"; // --- APPLICATION CONFIGURATION --- @@ -78,7 +78,7 @@ export type Handler> = ( // deno-lint-ignore no-explicit-any export type Handlers> = { - [K in typeof router.METHODS[number]]?: Handler; + [K in typeof rutt.METHODS[number]]?: Handler; }; export interface RouteModule {