chore: use x/rutt instead of crux.land/router (#647)
Co-authored-by: Luca Casonato <hello@lcas.dev>
This commit is contained in:
co-authored by
Luca Casonato
parent
7e0278bbb3
commit
6c2291f0de
+14
-14
@@ -3,7 +3,7 @@ import {
|
|||||||
extname,
|
extname,
|
||||||
fromFileUrl,
|
fromFileUrl,
|
||||||
RequestHandler,
|
RequestHandler,
|
||||||
router,
|
rutt,
|
||||||
Status,
|
Status,
|
||||||
toFileUrl,
|
toFileUrl,
|
||||||
typeByExtension,
|
typeByExtension,
|
||||||
@@ -171,7 +171,7 @@ export class ServerContext {
|
|||||||
url,
|
url,
|
||||||
name,
|
name,
|
||||||
component,
|
component,
|
||||||
handler: handler ?? ((req) => router.defaultOtherHandler(req)),
|
handler: handler ?? ((req) => rutt.defaultOtherHandler(req)),
|
||||||
csp: Boolean(config?.csp ?? false),
|
csp: Boolean(config?.csp ?? false),
|
||||||
};
|
};
|
||||||
} else if (
|
} else if (
|
||||||
@@ -190,7 +190,7 @@ export class ServerContext {
|
|||||||
name,
|
name,
|
||||||
component,
|
component,
|
||||||
handler: handler ??
|
handler: handler ??
|
||||||
((req, ctx) => router.defaultErrorHandler(req, ctx, ctx.error)),
|
((req, ctx) => rutt.defaultErrorHandler(req, ctx, ctx.error)),
|
||||||
csp: Boolean(config?.csp ?? false),
|
csp: Boolean(config?.csp ?? false),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -279,7 +279,7 @@ export class ServerContext {
|
|||||||
* by fresh, including static files.
|
* by fresh, including static files.
|
||||||
*/
|
*/
|
||||||
handler(): RequestHandler {
|
handler(): RequestHandler {
|
||||||
const inner = router.router<RouterState>(...this.#handlers());
|
const inner = rutt.router<RouterState>(...this.#handlers());
|
||||||
const withMiddlewares = this.#composeMiddlewares(this.#middlewares);
|
const withMiddlewares = this.#composeMiddlewares(this.#middlewares);
|
||||||
return function handler(req: Request, connInfo: ConnInfo) {
|
return function handler(req: Request, connInfo: ConnInfo) {
|
||||||
// Redirect requests that end with a trailing slash
|
// Redirect requests that end with a trailing slash
|
||||||
@@ -302,7 +302,7 @@ export class ServerContext {
|
|||||||
return (
|
return (
|
||||||
req: Request,
|
req: Request,
|
||||||
connInfo: ConnInfo,
|
connInfo: ConnInfo,
|
||||||
inner: router.Handler<RouterState>,
|
inner: rutt.Handler<RouterState>,
|
||||||
) => {
|
) => {
|
||||||
// identify middlewares to apply, if any.
|
// identify middlewares to apply, if any.
|
||||||
// middlewares should be already sorted from deepest to shallow layer
|
// middlewares should be already sorted from deepest to shallow layer
|
||||||
@@ -335,11 +335,11 @@ export class ServerContext {
|
|||||||
* path-to-regex, to handler mapping.
|
* path-to-regex, to handler mapping.
|
||||||
*/
|
*/
|
||||||
#handlers(): [
|
#handlers(): [
|
||||||
router.Routes<RouterState>,
|
rutt.Routes<RouterState>,
|
||||||
router.Handler<RouterState>,
|
rutt.Handler<RouterState>,
|
||||||
router.ErrorHandler<RouterState>,
|
rutt.ErrorHandler<RouterState>,
|
||||||
] {
|
] {
|
||||||
const routes: router.Routes<RouterState> = {};
|
const routes: rutt.Routes<RouterState> = {};
|
||||||
|
|
||||||
routes[`${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/:path*`] = this
|
routes[`${INTERNAL_PREFIX}${JS_PREFIX}/${BUILD_ID}/:path*`] = this
|
||||||
.#bundleAssetRoute();
|
.#bundleAssetRoute();
|
||||||
@@ -484,7 +484,7 @@ export class ServerContext {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const unknownHandler: router.Handler<RouterState> = (
|
const unknownHandler: rutt.Handler<RouterState> = (
|
||||||
req,
|
req,
|
||||||
ctx,
|
ctx,
|
||||||
) =>
|
) =>
|
||||||
@@ -500,7 +500,7 @@ export class ServerContext {
|
|||||||
this.#error,
|
this.#error,
|
||||||
Status.InternalServerError,
|
Status.InternalServerError,
|
||||||
);
|
);
|
||||||
const errorHandler: router.ErrorHandler<RouterState> = (
|
const errorHandler: rutt.ErrorHandler<RouterState> = (
|
||||||
req,
|
req,
|
||||||
ctx,
|
ctx,
|
||||||
error,
|
error,
|
||||||
@@ -528,7 +528,7 @@ export class ServerContext {
|
|||||||
size: number,
|
size: number,
|
||||||
contentType: string,
|
contentType: string,
|
||||||
etag: string,
|
etag: string,
|
||||||
): router.MatchHandler {
|
): rutt.MatchHandler {
|
||||||
return async (req: Request) => {
|
return async (req: Request) => {
|
||||||
const url = new URL(req.url);
|
const url = new URL(req.url);
|
||||||
const key = url.searchParams.get(ASSET_CACHE_BUST_KEY);
|
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
|
* Returns a router that contains all fresh routes. Should be mounted at
|
||||||
* constants.INTERNAL_PREFIX
|
* constants.INTERNAL_PREFIX
|
||||||
*/
|
*/
|
||||||
#bundleAssetRoute = (): router.MatchHandler => {
|
#bundleAssetRoute = (): rutt.MatchHandler => {
|
||||||
return async (_req, _ctx, params) => {
|
return async (_req, _ctx, params) => {
|
||||||
const path = `/${params.path}`;
|
const path = `/${params.path}`;
|
||||||
const file = await this.#bundler.get(path);
|
const file = await this.#bundler.get(path);
|
||||||
@@ -606,7 +606,7 @@ const DEFAULT_NOT_FOUND: UnknownPage = {
|
|||||||
pattern: "",
|
pattern: "",
|
||||||
url: "",
|
url: "",
|
||||||
name: "_404",
|
name: "_404",
|
||||||
handler: (req) => router.defaultOtherHandler(req),
|
handler: (req) => rutt.defaultOtherHandler(req),
|
||||||
csp: false,
|
csp: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -16,8 +16,8 @@ export {
|
|||||||
typeByExtension,
|
typeByExtension,
|
||||||
} from "https://deno.land/std@0.150.0/media_types/mod.ts";
|
} from "https://deno.land/std@0.150.0/media_types/mod.ts";
|
||||||
|
|
||||||
// -- router --
|
// -- rutt --
|
||||||
export * as router from "https://crux.land/router@0.0.11";
|
export * as rutt from "https://deno.land/x/rutt@0.0.13/mod.ts";
|
||||||
|
|
||||||
// -- esbuild --
|
// -- esbuild --
|
||||||
// @deno-types="https://deno.land/x/esbuild@v0.14.51/mod.d.ts"
|
// @deno-types="https://deno.land/x/esbuild@v0.14.51/mod.d.ts"
|
||||||
|
|||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
import { ComponentType } from "preact";
|
import { ComponentType } from "preact";
|
||||||
import { ConnInfo, router, ServeInit } from "./deps.ts";
|
import { ConnInfo, rutt, ServeInit } from "./deps.ts";
|
||||||
import { InnerRenderFunction, RenderContext } from "./render.tsx";
|
import { InnerRenderFunction, RenderContext } from "./render.tsx";
|
||||||
|
|
||||||
// --- APPLICATION CONFIGURATION ---
|
// --- APPLICATION CONFIGURATION ---
|
||||||
@@ -78,7 +78,7 @@ export type Handler<T = any, State = Record<string, unknown>> = (
|
|||||||
|
|
||||||
// deno-lint-ignore no-explicit-any
|
// deno-lint-ignore no-explicit-any
|
||||||
export type Handlers<T = any, State = Record<string, unknown>> = {
|
export type Handlers<T = any, State = Record<string, unknown>> = {
|
||||||
[K in typeof router.METHODS[number]]?: Handler<T, State>;
|
[K in typeof rutt.METHODS[number]]?: Handler<T, State>;
|
||||||
};
|
};
|
||||||
|
|
||||||
export interface RouteModule {
|
export interface RouteModule {
|
||||||
|
|||||||
Reference in New Issue
Block a user