Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { DENO_DEPLOYMENT_ID } from "./runtime/build_id.ts";
import * as colors from "@std/fmt/colors";
import { type MiddlewareFn, runMiddlewares } from "./middlewares/mod.ts";
import { FreshReqContext } from "./context.ts";
import { type Method, type Router, UrlPatternRouter } from "./router.ts";
import { type Method, Router } from "./router.ts";
import {
type FreshConfig,
normalizeConfig,
Expand Down Expand Up @@ -124,7 +124,7 @@ export let getBuildCache: (app: App<any>) => BuildCache | null;
export let setBuildCache: (app: App<any>, cache: BuildCache | null) => void;

export class App<State> {
#router: Router<MiddlewareFn<State>> = new UrlPatternRouter<
#router: Router<MiddlewareFn<State>> = new Router<
MiddlewareFn<State>
>();
#islandRegistry: ServerIslandRegistry = new Map();
Expand Down Expand Up @@ -201,19 +201,19 @@ export class App<State> {
}

mountApp(path: string, app: App<State>): this {
const routes = app.#router._routes;
const routes = app.#router.routes;
app.#islandRegistry.forEach((value, key) => {
this.#islandRegistry.set(key, value);
});

const middlewares = app.#router._middlewares;
const middlewares = app.#router.middlewares;

// Special case when user calls one of these:
// - `app.mountApp("/", otherApp)`
// - `app.mountApp("/*", otherApp)`
const isSelf = path === "/*" || path === "/";
if (isSelf && middlewares.length > 0) {
this.#router._middlewares.push(...middlewares);
this.#router.middlewares.push(...middlewares);
}

for (let i = 0; i < routes.length; i++) {
Expand Down
32 changes: 10 additions & 22 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,28 @@ export interface RouteResult<T> {
pattern: string | null;
}

export interface Router<T> {
_routes: Route<T>[];
_middlewares: T[];
addMiddleware(fn: T): void;
add(
method: Method | "ALL",
pathname: string | URLPattern,
handlers: T[],
): void;
match(method: Method, url: URL): RouteResult<T>;
}

export const IS_PATTERN = /[*:{}+?()]/;

export class UrlPatternRouter<T> implements Router<T> {
readonly _routes: Route<T>[] = [];
readonly _middlewares: T[] = [];
export class Router<T> {
readonly routes: Route<T>[] = [];
readonly middlewares: T[] = [];

addMiddleware(fn: T): void {
this._middlewares.push(fn);
this.middlewares.push(fn);
}

add(method: Method | "ALL", pathname: string | URLPattern, handlers: T[]) {
if (
typeof pathname === "string" && pathname !== "/*" &&
IS_PATTERN.test(pathname)
) {
this._routes.push({
this.routes.push({
path: new URLPattern({ pathname }),
handlers,
method,
});
} else {
this._routes.push({
this.routes.push({
path: pathname,
handlers,
method,
Expand All @@ -64,12 +52,12 @@ export class UrlPatternRouter<T> implements Router<T> {
pattern: null,
};

if (this._middlewares.length > 0) {
result.handlers.push(this._middlewares);
if (this.middlewares.length > 0) {
result.handlers.push(this.middlewares);
}

for (let i = 0; i < this._routes.length; i++) {
const route = this._routes[i];
for (let i = 0; i < this.routes.length; i++) {
const route = this.routes[i];

// Fast path for string based routes which are expected
// to be either wildcard `*` match or an exact pathname match.
Expand Down
26 changes: 13 additions & 13 deletions src/router_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect } from "@std/expect";
import { IS_PATTERN, pathToPattern, UrlPatternRouter } from "./router.ts";
import { IS_PATTERN, pathToPattern, Router } from "./router.ts";

Deno.test("IS_PATTERN", () => {
expect(IS_PATTERN.test("/foo")).toEqual(false);
Expand All @@ -11,8 +11,8 @@ Deno.test("IS_PATTERN", () => {
expect(IS_PATTERN.test("/foo/(a)")).toEqual(true);
});

Deno.test("UrlPatternRouter - GET get first match", () => {
const router = new UrlPatternRouter();
Deno.test("Router - GET get first match", () => {
const router = new Router();
const A = () => {};
const B = () => {};
const C = () => {};
Expand All @@ -30,8 +30,8 @@ Deno.test("UrlPatternRouter - GET get first match", () => {
});
});

Deno.test("UrlPatternRouter - GET get matches with middlewares", () => {
const router = new UrlPatternRouter();
Deno.test("Router - GET get matches with middlewares", () => {
const router = new Router();
const A = () => {};
const B = () => {};
const C = () => {};
Expand All @@ -49,8 +49,8 @@ Deno.test("UrlPatternRouter - GET get matches with middlewares", () => {
});
});

Deno.test("UrlPatternRouter - GET extract params", () => {
const router = new UrlPatternRouter();
Deno.test("Router - GET extract params", () => {
const router = new Router();
const A = () => {};
router.add("GET", new URLPattern({ pathname: "/:foo/:bar/c" }), [A]);

Expand All @@ -74,8 +74,8 @@ Deno.test("UrlPatternRouter - GET extract params", () => {
});
});

Deno.test("UrlPatternRouter - Wrong method match", () => {
const router = new UrlPatternRouter();
Deno.test("Router - Wrong method match", () => {
const router = new Router();
const A = () => {};
router.add("GET", "/foo", [A]);

Expand All @@ -89,8 +89,8 @@ Deno.test("UrlPatternRouter - Wrong method match", () => {
});
});

Deno.test("UrlPatternRouter - wrong + correct method", () => {
const router = new UrlPatternRouter();
Deno.test("Router - wrong + correct method", () => {
const router = new Router();
const A = () => {};
const B = () => {};
router.add("GET", "/foo", [A]);
Expand All @@ -106,8 +106,8 @@ Deno.test("UrlPatternRouter - wrong + correct method", () => {
});
});

Deno.test("UrlPatternRouter - convert patterns automatically", () => {
const router = new UrlPatternRouter();
Deno.test("Router - convert patterns automatically", () => {
const router = new Router();
const A = () => {};
router.add("GET", "/books/:id", [A]);

Expand Down
Loading