Skip to content

Commit f9e6924

Browse files
dvkamcpojer
andauthored
fix(routing): preserve intentional native Worker 404s (#778)
Fixes #775 A route's GET or HEAD 404 could become a 200 when a static file existed at the same path. Keep explicit 404s and let genuine route misses fall back to assets after middleware runs. Miss tracking follows response clones and `pipeThrough()` stream transforms, so middleware such as Hono's `compress()` can transform a genuine miss without disabling asset fallback. Independently created middleware and route 404s still keep their status and body. Workerd regression coverage includes GET and HEAD, rewrites, header edits, both branches of a response clone, chained transforms, compression, and intentional replacement 404s. The compression regression fails on the previous code. Verification: 16,614 tests passed in the full local suite; all 33 package typechecks, package/playground builds, formatting, and lint passed. Direct prototype calls, such as `Response.prototype.clone.call(c.res)` or `ReadableStream.prototype.pipeThrough.call(c.res.body, transform)`, bypass the per-instance tracking. In that case a genuine miss can remain a 404 even when a file exists. --------- Co-authored-by: Christoph Nakazawa <cnakazawa@cloudflare.com>
1 parent caaf7d3 commit f9e6924

7 files changed

Lines changed: 282 additions & 16 deletions

File tree

‎packages/void/src/router/compile.ts‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,14 @@ export function compileRouteTable(
474474
lines.push('');
475475
}
476476

477+
const trackAssetMisses = !options.nodeTarget && !managedDispatch;
478+
const registerNotFound = (handler: string): void => {
479+
const tracked = trackAssetMisses ? `__routingRuntime.trackNotFound(${handler})` : handler;
480+
lines.push(`app.notFound(${tracked});`);
481+
};
482+
if (trackAssetMisses) {
483+
lines.push('app.notFound(__routingRuntime.trackNotFound());');
484+
}
477485
if (
478486
pages &&
479487
(scan.pages.pages.length > 0 ||
@@ -484,8 +492,8 @@ export function compileRouteTable(
484492
const pagePatterns = options.trailingSlash
485493
? `[${scan.pages.pages.map((page) => patternToRegex(page.pattern)).join(', ')}]`
486494
: 'undefined';
487-
lines.push(
488-
`app.notFound(__createPagesFallback({ ${createBoundaryManifest(scan.pages).root ? 'pageNotFound: __voidRootNotFound, ' : ''}routing: __routingRuntime, errors: __errorRenderer, assetsDir: ${JSON.stringify(options.assetsDir)}, fallbackRules: ${fallbackRulesJson}, trailingSlash: ${options.trailingSlash === true}, pagePatterns: ${pagePatterns} }));`,
495+
registerNotFound(
496+
`__createPagesFallback({ ${createBoundaryManifest(scan.pages).root ? 'pageNotFound: __voidRootNotFound, ' : ''}routing: __routingRuntime, errors: __errorRenderer, assetsDir: ${JSON.stringify(options.assetsDir)}, fallbackRules: ${fallbackRulesJson}, trailingSlash: ${options.trailingSlash === true}, pagePatterns: ${pagePatterns} })`,
489497
);
490498
}
491499

@@ -495,8 +503,8 @@ export function compileRouteTable(
495503
lines.push(
496504
`const getClientAssetTags = __createClientAssetTags(${JSON.stringify({ entryId: ssr.clientEntryRelativePath, entryUrl: '/' + ssr.clientEntryRelativePath, kind: 'ssr', dev: options.dev === true, reactRefresh: options.reactRefresh })});`,
497505
);
498-
lines.push(
499-
`app.notFound(__createSsrFallback({ routing: __routingRuntime, nodeTarget: ${JSON.stringify(options.nodeTarget === true)}, assetsDir: ${JSON.stringify(options.assetsDir)}, fallbackRules: ${fallbackRulesJson}, load: () => import(${JSON.stringify(ssr.serverEntryPath)}), assets: getClientAssetTags }));`,
506+
registerNotFound(
507+
`__createSsrFallback({ routing: __routingRuntime, nodeTarget: ${JSON.stringify(options.nodeTarget === true)}, assetsDir: ${JSON.stringify(options.assetsDir)}, fallbackRules: ${fallbackRulesJson}, load: () => import(${JSON.stringify(ssr.serverEntryPath)}), assets: getClientAssetTags })`,
500508
);
501509
} else if (
502510
emitEdgeRules &&
@@ -511,8 +519,8 @@ export function compileRouteTable(
511519
))
512520
)
513521
) {
514-
lines.push(
515-
`app.notFound(__createAssetNotFound({ routing: __routingRuntime, nodeTarget: ${JSON.stringify(options.nodeTarget === true)}, assetsDir: ${JSON.stringify(options.assetsDir)}, fallbackRules: ${fallbackRulesJson} }));`,
522+
registerNotFound(
523+
`__createAssetNotFound({ routing: __routingRuntime, nodeTarget: ${JSON.stringify(options.nodeTarget === true)}, assetsDir: ${JSON.stringify(options.assetsDir)}, fallbackRules: ${fallbackRulesJson} })`,
516524
);
517525
}
518526

‎packages/void/src/runtime/routing-pages.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,9 @@ export function createSsrFallback(options: {
317317
if (typeof render !== 'function') {
318318
throw new Error('ssr: Invalid entry. Export render(c, assetTags).');
319319
}
320-
return render(c, await options.assets(c.req.raw, c.env));
320+
return options.routing.preserveNotFound(
321+
await render(c, await options.assets(c.req.raw, c.env)),
322+
);
321323
};
322324
}
323325
export function createAssetNotFound(options: {

‎packages/void/src/runtime/routing-worker.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { redirectToHttps } from '@void/edge';
22
import type { ExecutionContext } from 'hono';
33
import { createManifestLoader, type ManifestLoader } from './routing-assets.ts';
44

5+
// Native Void routing records intentional 404s before Hono wraps HEAD responses.
6+
export const intentionalNotFoundRequests: WeakSet<Request> = new WeakSet();
7+
58
export type AssetBinding = { fetch: (request: Request) => Promise<Response> };
69
export type RuntimeEnvironment = Record<string, unknown> & { ASSETS?: AssetBinding };
710
export type AssetFallbackOptions = {
@@ -106,6 +109,7 @@ export function createAssetFallback(
106109
return async (request, env, response) => {
107110
if (
108111
response.status !== 404 ||
112+
intentionalNotFoundRequests.has(request) ||
109113
!env.ASSETS ||
110114
(request.method !== 'GET' && request.method !== 'HEAD')
111115
) {

‎packages/void/src/runtime/routing.ts‎

Lines changed: 148 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
mergeQueryIntoLocation,
77
substituteParams,
88
} from '@void/edge';
9-
import type { Context, Hono, MiddlewareHandler } from 'hono';
9+
import type { Context, Hono, MiddlewareHandler, NotFoundHandler } from 'hono';
1010
import type { TypedResponse } from 'hono';
1111
import type { RedirectStatusCode } from 'hono/utils/http-status';
1212
import type { RouteDestination, RouteUrlInput } from 'void/routes';
@@ -25,6 +25,7 @@ import { convertReturnValue } from './response.ts';
2525
import { toUrlString } from './route-url.ts';
2626
import { ASSET_EXTENSIONS } from './routing-assets.ts';
2727
import { markRouteHandler } from './routing-pages.ts';
28+
import { intentionalNotFoundRequests } from './routing-worker.ts';
2829
import { isUnchangedStaticAsset, markStaticAsset } from './static-asset-marker.ts';
2930
export { createErrorRenderer } from './routing-errors.ts';
3031

@@ -50,6 +51,8 @@ export type RoutingContext = Context & {
5051
export type RoutingRuntime = {
5152
requestContext: AsyncLocalStorage<RequestContextStore>;
5253
contextMiddleware: MiddlewareHandler;
54+
trackNotFound: (handler?: NotFoundHandler) => NotFoundHandler;
55+
preserveNotFound: (response: Response) => Response;
5356
requestMiddleware: MiddlewareHandler;
5457
routingMiddleware: (rules: Array<RoutingRule>) => MiddlewareHandler;
5558
headerMiddleware: (rules: Array<RoutingHeaderRule>, phase?: 'auth-error') => MiddlewareHandler;
@@ -75,6 +78,102 @@ export function createRoutingRuntime(
7578
// Contexts whose inner header pass ran after next(). A handler that throws
7679
// past it, such as notFound, leaves the context out.
7780
const headerApplied = new WeakSet<Context>();
81+
const explicitNotFound = new WeakSet<Context>();
82+
let trackAssetMisses = false;
83+
const intentionalResponses = new WeakSet<Response>();
84+
const routeMisses = new WeakMap<Context, Array<Response>>();
85+
const missBodies = new WeakMap<Context, WeakSet<ReadableStream>>();
86+
const trackMissBody = (c: Context, body: ReadableStream | null): void => {
87+
if (!body) {
88+
return;
89+
}
90+
let bodies = missBodies.get(c);
91+
if (!bodies) {
92+
bodies = new WeakSet();
93+
missBodies.set(c, bodies);
94+
}
95+
if (bodies.has(body)) {
96+
return;
97+
}
98+
bodies.add(body);
99+
// Middleware such as Hono's compress() replaces the body with a transform
100+
// of this stream. That still carries a miss; an unrelated replacement does not.
101+
if (Object.isExtensible(body)) {
102+
const pipeThrough = body.pipeThrough;
103+
Object.defineProperty(body, 'pipeThrough', {
104+
configurable: true,
105+
writable: true,
106+
value(this: ReadableStream, ...args: Parameters<ReadableStream['pipeThrough']>) {
107+
const transformed = pipeThrough.apply(this, args);
108+
if (this === body) {
109+
trackMissBody(c, transformed);
110+
}
111+
return transformed;
112+
},
113+
});
114+
}
115+
};
116+
const recordMiss = (c: Context, response: Response): void => {
117+
const misses = routeMisses.get(c) ?? [];
118+
if (misses.includes(response)) {
119+
return;
120+
}
121+
misses.push(response);
122+
routeMisses.set(c, misses);
123+
trackMissBody(c, response.body);
124+
trackResponseCopies(c);
125+
// Fetch's clone tees the body, so neither clone shares its former stream.
126+
if (Object.isExtensible(response)) {
127+
const clone = response.clone;
128+
Object.defineProperty(response, 'clone', {
129+
configurable: true,
130+
writable: true,
131+
value(this: Response) {
132+
const copy = clone.call(this);
133+
if (this === response) {
134+
trackMissBody(c, this.body);
135+
recordMiss(c, copy);
136+
}
137+
return copy;
138+
},
139+
});
140+
}
141+
};
142+
const isRouteMiss = (c: Context, response: Response): boolean =>
143+
routeMisses
144+
.get(c)
145+
?.some((miss) => miss === response || (miss.body !== null && miss.body === response.body)) ===
146+
true ||
147+
(response.body !== null && missBodies.get(c)?.has(response.body) === true);
148+
const tracksResponseCopies = new WeakSet<Context>();
149+
const trackResponseCopies = (c: Context): void => {
150+
if (tracksResponseCopies.has(c)) {
151+
return;
152+
}
153+
tracksResponseCopies.add(c);
154+
// Carry provenance to the response Hono exposes after assignment or a header edit.
155+
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(c), 'res');
156+
if (descriptor?.get && descriptor.set) {
157+
const set = descriptor.set;
158+
Object.defineProperty(c, 'res', {
159+
...descriptor,
160+
set(response: Response | undefined) {
161+
set.call(c, response);
162+
if (response && isRouteMiss(c, response) && c.res !== response) {
163+
recordMiss(c, c.res);
164+
}
165+
},
166+
});
167+
}
168+
const header = c.header.bind(c);
169+
c.header = (...args) => {
170+
const before = c.finalized ? c.res : undefined;
171+
header(...args);
172+
if (before && c.res !== before && isRouteMiss(c, before)) {
173+
recordMiss(c, c.res);
174+
}
175+
};
176+
};
78177
const warnedPatterns = new Set<string>();
79178
const requestContext = new AsyncLocalStorage<RequestContextStore>();
80179
const assetExtensions = options.blockedAssetExtensions ?? ASSET_EXTENSIONS;
@@ -104,6 +203,13 @@ export function createRoutingRuntime(
104203
// The enclosing response may depend on auth even if middleware clones or replaces it.
105204
authDependentRequests.add(c.req.raw);
106205
}
206+
if (trackAssetMisses && response.status === 404) {
207+
if (intentionalNotFoundRequests.has(request)) {
208+
intentionalResponses.add(response);
209+
} else {
210+
recordMiss(c, response);
211+
}
212+
}
107213
return response;
108214
};
109215
const rememberOriginal = (request: Request, c: Context): void => {
@@ -173,6 +279,19 @@ export function createRoutingRuntime(
173279
};
174280
const contextMiddleware: MiddlewareHandler = async (context, next) => {
175281
const c = context as RoutingContext;
282+
if (trackAssetMisses) {
283+
intentionalNotFoundRequests.delete(c.req.raw);
284+
const notFound = c.notFound.bind(c);
285+
// Hono calls its fallback directly when the router runs out of handlers.
286+
c.notFound = () => {
287+
explicitNotFound.add(c);
288+
try {
289+
return notFound();
290+
} finally {
291+
explicitNotFound.delete(c);
292+
}
293+
};
294+
}
176295
if (options.managedDispatch && !originalUrls.has(c.req.raw)) {
177296
const original = c.req.raw.headers.get('X-Void-Original-URL');
178297
if (original != null) {
@@ -222,10 +341,38 @@ export function createRoutingRuntime(
222341
c.originalUrl = () => originalUrls.get(c.req.raw) ?? null;
223342
c.isRewritten = () => originalUrls.has(c.req.raw);
224343
await next();
344+
if (!trackAssetMisses) {
345+
return;
346+
}
347+
if (c.res.status === 404 && !isRouteMiss(c, c.res)) {
348+
intentionalNotFoundRequests.add(c.req.raw);
349+
}
225350
};
226351
return {
227352
requestContext,
228353
contextMiddleware,
354+
trackNotFound: (handler = (c) => c.text('404 Not Found', 404)) => {
355+
trackAssetMisses = true;
356+
return (c) => {
357+
const explicit = explicitNotFound.has(c);
358+
const response = handler(c);
359+
if (explicit) {
360+
return response;
361+
}
362+
return Promise.resolve(response).then((miss) => {
363+
if (miss.status === 404 && !intentionalResponses.has(miss)) {
364+
recordMiss(c, miss);
365+
}
366+
return miss;
367+
});
368+
};
369+
},
370+
preserveNotFound: (response) => {
371+
if (trackAssetMisses && response.status === 404) {
372+
intentionalResponses.add(response);
373+
}
374+
return response;
375+
},
229376
requestMiddleware: async (c, next) => {
230377
await requestContext.run(
231378
options.nodeTarget

0 commit comments

Comments
 (0)