fix(fetch): security headers on early failures, live mesh boot gate

app.fetch's 413/400/501 body-acquisition failures were returned without
the injected security/CORS headers Node's writeHead patch always
applies, breaking byte-identical parity with the listen() path. Extract
computeInjected() (shared by handle() and buildFetch()) and merge it
into every error response, not just handle()'s outcomes.

Also, buildAppFetch captured `booted` by value at createApp() time, so
mesh apps (whose finalize() is deferred into listen()) had app.fetch
permanently throw even after a successful listen(). Read booted live
via a getter, mirroring explain()/graph()/inspect(), and lazily
build/cache the fetch handler on first call after boot.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-07-09 14:26:45 -05:00
1 parent 93a4246de5
commit 08cb8554e6
4 files changed
+108 -18

No files matched your search

+16 -2
View File
@@ -34,6 +34,21 @@ export interface Preflight {
preflight: Record<string, string>;
}
/**
* Computes the security + CORS headers that get injected into every response for a request — the same
* authoritative set {@link handle} applies to its outcomes. Shared with `buildFetch` (src/http/web.ts)
* so early-failure responses (413/400/501, before a route even matches) carry the same headers Node's
* `patchResponseHeaders` would have installed up-front.
*/
export function computeInjected(
opts: HttpOptions | undefined,
req: { secure: boolean; headers: Record<string, string | string[] | undefined> },
): Record<string, string> {
const injected: Record<string, string> = { ...buildSecurityHeaders(opts?.security ?? true, req.secure) };
if (opts?.cors) Object.assign(injected, resolveCors(opts.cors, req));
return injected;
}
/**
* Runtime-neutral request handler: security/CORS headers, preflight short-circuit, route match / 404 / 405,
* route-handler invocation with error rendering, and the buffered-vs-stream decision. Does no I/O — the
@@ -45,8 +60,7 @@ export async function handle(
req: NeutralRequest,
): Promise<HandleResult | Preflight> {
const path = req.url.split('?')[0];
const injected: Record<string, string> = { ...buildSecurityHeaders(opts?.security ?? true, req.secure) };
if (opts?.cors) Object.assign(injected, resolveCors(opts.cors, req));
const injected = computeInjected(opts, req);
if (opts?.cors && req.method === 'OPTIONS' && req.headers['access-control-request-method']) {
return { preflight: corsPreflightHeaders(opts.cors, req) };