fix: middleware regressions (#3106)

Fixes some regressions found after merging
https://github.com/denoland/fresh/pull/3086

- Missing 404 middlewares when mounting sub-apps
- revert more staticFiles logic to always require the middleware like
before
This commit is contained in:
Marvin Hagemeister
2025-07-17 15:23:27 +02:00
committed by GitHub
parent c279efa345
commit 226e250386
7 changed files with 69 additions and 71 deletions
+18 -44
View File
@@ -21,6 +21,18 @@ export interface DynamicRouteDef<T> {
byMethod: RouteByMethod<T>;
}
function newByMethod<T>(): RouteByMethod<T> {
return {
GET: [],
POST: [],
PATCH: [],
DELETE: [],
PUT: [],
HEAD: [],
OPTIONS: [],
};
}
export interface RouteResult<T> {
params: Record<string, string>;
handlers: T[];
@@ -33,7 +45,6 @@ export interface Router<T> {
method: Method | "OPTIONS" | "ALL",
pathname: string,
handlers: T[],
unshift?: boolean,
): void;
match(method: Method, url: URL, init?: T[]): RouteResult<T>;
getAllowedMethods(pattern: string): string[];
@@ -56,27 +67,17 @@ export class UrlPatternRouter<T> implements Router<T> {
}
add(
method: Method | "ALL",
method: Method,
pathname: string,
handlers: T[],
unshift = false,
) {
let allowed = this.#allowed.get(pathname);
if (allowed === undefined) {
allowed = new Set();
this.#allowed.set(pathname, allowed);
}
if (method === "ALL") {
allowed.add("GET");
allowed.add("POST");
allowed.add("PATCH");
allowed.add("PUT");
allowed.add("DELETE");
allowed.add("HEAD");
allowed.add("OPTIONS");
} else {
allowed.add(method);
}
allowed.add(method);
let byMethod: RouteByMethod<T>;
if (IS_PATTERN.test(pathname)) {
@@ -84,15 +85,7 @@ export class UrlPatternRouter<T> implements Router<T> {
if (def === undefined) {
def = {
pattern: new URLPattern({ pathname }),
byMethod: {
DELETE: [],
GET: [],
HEAD: [],
OPTIONS: [],
PATCH: [],
POST: [],
PUT: [],
},
byMethod: newByMethod(),
};
this.#dynamics.set(pathname, def);
this.#dynamicArr.push(def);
@@ -104,15 +97,7 @@ export class UrlPatternRouter<T> implements Router<T> {
if (def === undefined) {
def = {
pattern: pathname,
byMethod: {
DELETE: [],
GET: [],
HEAD: [],
OPTIONS: [],
PATCH: [],
POST: [],
PUT: [],
},
byMethod: newByMethod(),
};
this.#statics.set(pathname, def);
}
@@ -120,18 +105,7 @@ export class UrlPatternRouter<T> implements Router<T> {
byMethod = def.byMethod;
}
const fn = unshift ? "unshift" : "push";
if (method === "ALL") {
byMethod.DELETE[fn](...handlers);
byMethod.GET[fn](...handlers);
byMethod.HEAD[fn](...handlers);
byMethod.OPTIONS[fn](...handlers);
byMethod.PATCH[fn](...handlers);
byMethod.POST[fn](...handlers);
byMethod.PUT[fn](...handlers);
} else {
byMethod[method][fn](...handlers);
}
byMethod[method].push(...handlers);
}
match(method: Method, url: URL, init: T[] = []): RouteResult<T> {