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' ;
1010import type { TypedResponse } from 'hono' ;
1111import type { RedirectStatusCode } from 'hono/utils/http-status' ;
1212import type { RouteDestination , RouteUrlInput } from 'void/routes' ;
@@ -25,6 +25,7 @@ import { convertReturnValue } from './response.ts';
2525import { toUrlString } from './route-url.ts' ;
2626import { ASSET_EXTENSIONS } from './routing-assets.ts' ;
2727import { markRouteHandler } from './routing-pages.ts' ;
28+ import { intentionalNotFoundRequests } from './routing-worker.ts' ;
2829import { isUnchangedStaticAsset , markStaticAsset } from './static-asset-marker.ts' ;
2930export { createErrorRenderer } from './routing-errors.ts' ;
3031
@@ -50,6 +51,8 @@ export type RoutingContext = Context & {
5051export 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