refactor(http): resolve each request once, in the core that already routes it

Both adapters routed every request themselves, and then handle() routed it
again from the same inputs — a four-route app walked its table 2.5 times
per request. Neither side was wrong: an adapter must know the route to
decide whether to read a body, and the core must know it to dispatch.

The cause was the shape of the handoff. NeutralRequest carried an
already-parsed body, so producing one was the adapter's problem, and every
policy that depends on the matched route came with it — these four chains
were written out in server.ts and web.ts character for character:

    matched.def.maxBodyBytes   ?? opts?.limits?.maxBodyBytes ?? 1_000_000
    matched.def.bodyDuplicates ?? opts?.bodyDuplicates       ?? 'last'
    matched.def.maxParts       ?? opts?.limits?.maxParts     ?? 1000
    opts?.limits?.maxBodyBytes ?? 1_000_000

A fifth knob, or one changed default, meant editing both or letting the
runtimes quietly disagree about limits.

Handing over the ability to read rather than the result moves the decision
to where the route is already known. What stays in an adapter is what
genuinely differs between runtimes: getting bytes in, putting bytes out.
Enforcement stays with the reader too, because Node aborts an oversized
upload as chunks arrive while a fetch runtime can only measure what
arrayBuffer() already resolved — passing the limit in preserves that.
413/400/501 keep their exact current shape, Node's connection: close
included; it now has the test it never had.

Measured through app.fetch, both versions in one process, interleaved:
40 routes with the match declared last, 3.89 -> 3.58 us (-8.2%). One
route, where there is no second scan to save, is unchanged.

The first cut of this was 13% SLOWER, and the reason is worth recording:
a per-request closure for the reader plus the error-context object it
captured cost 250 ns, and the async frames around a body that did not
exist cost another 215 ns — together more than the duplicate scan the
whole change removes. The reader is now built once per server and takes
the runtime's request as an argument, and a bodiless request touches no
promise on this path at all.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-08-16 23:46:57 -05:00
1 parent 0c9f8a545e
commit a4bb3c02d6
5 files changed
+262 -201

No files matched your search

+135
View File
@@ -0,0 +1,135 @@
// src/http/body.ts — runtime-neutral body acquisition: limit resolution, parsing, and the failure
// envelope. The one part that is genuinely runtime-specific — getting the bytes — is supplied by
// the caller as a {@link BodyReader}.
import { HttpError, isHttpError } from '../signals';
import { renderError, type ErrorRequest } from '../transformers';
import { parseMultipart, extractBoundary, collapseDuplicates } from '../multipart';
import type { MatchedRoute, HttpOptions } from './types';
/** A ready-to-send error response produced while acquiring the request body. */
export interface BodyFailure {
fail: { status: number; headers: Record<string, string>; body: string };
}
/** A value, or a promise of one. Readers that have nothing to await return the value directly. */
export type MaybePromise<T> = T | Promise<T>;
/** True for the native promises both adapters' readers return; no cross-realm thenables reach here. */
function isPromise<T>(value: MaybePromise<T>): value is Promise<T> {
return value instanceof Promise;
}
/**
* Reads the raw request body, enforcing `limit`.
*
* The adapter owns this and nothing else about the body, because enforcement is the one place the
* runtimes legitimately differ: Node checks the running total as chunks arrive and can abort a
* 10 GB upload after 1 MB, while a fetch runtime has only `request.arrayBuffer()` and can check
* `byteLength` after the fact. Passing the limit *in*, rather than measuring the returned buffer
* here, is what keeps Node's incremental abort — measuring afterwards would force it to buffer
* without bound first.
*
* The failure branch is opaque on purpose: Node answers an over-limit request with
* `connection: close`, so the rest of the upload does not keep arriving on a kept-alive socket.
* That header means nothing on the fetch side, and is a forbidden response header on some
* runtimes, so the adapter that needs it is the one that attaches it.
*
* `source` is the runtime's own request object, handed straight back untouched. It exists so the
* reader can be built **once per server** instead of once per request: a closure capturing the
* request measured at 250 ns per request together with the error-context object it captured —
* more than the duplicate route resolution this whole indirection removes.
*/
export type BodyReader = (source: unknown, limit: number) => MaybePromise<{ bytes: Buffer | undefined } | BodyFailure>;
/** Parse outcome: the decoded body, or an error message plus the status to send (400 unless set). */
type ParseResult = { body: unknown } | { error: string; status?: number };
/**
* Parses a raw request body by content type (JSON, urlencoded, multipart, or plain text).
* @returns `{ body }` on success, or `{ error, status? }` on malformed input / missing multipart support.
*/
export async function parseRequestBody(
buf: Buffer | undefined,
contentType: string,
duplicates: 'array' | 'last',
maxParts: number,
): Promise<ParseResult> {
if (buf !== undefined && contentType.includes('application/json')) {
try {
return { body: JSON.parse(buf.toString('utf8')) };
} catch {
return { error: 'Invalid JSON body' };
}
}
if (buf !== undefined && contentType.includes('application/x-www-form-urlencoded')) {
return { body: collapseDuplicates(new URLSearchParams(buf.toString('utf8')), duplicates) };
}
if (buf !== undefined && contentType.includes('multipart/form-data')) {
const boundary = extractBoundary(contentType);
if (!boundary) return { error: 'Invalid multipart body' };
try {
return { body: await parseMultipart(buf, boundary, { maxParts, duplicates }) };
} catch (error) {
// HttpError = the busboy peer dep is missing (501); anything else = malformed input (400).
if (isHttpError(error)) return { error: error.message, status: error.status };
return { error: 'Invalid multipart body' };
}
}
const text = buf?.toString('utf8');
return { body: text === '' ? undefined : text };
}
/**
* Resolves the body limits for a matched route, reads it through `read`, and parses it.
*
* The four `route ?? server ?? default` chains below used to be written out in both adapters,
* character for character, because each adapter had to route the request itself in order to know
* whether to read a body — and so ended up owning every policy that depends on the matched route.
* Adding a fifth knob, or changing one default, meant changing it in two places or letting the
* runtimes quietly disagree about limits.
*/
export function acquireBody(
read: BodyReader,
source: unknown,
matched: MatchedRoute,
opts: HttpOptions | undefined,
req: ErrorRequest,
): MaybePromise<{ body: unknown } | BodyFailure> {
const limit = matched.def.maxBodyBytes ?? opts?.limits?.maxBodyBytes ?? 1_000_000;
const acquired = read(source, limit);
// Deliberately not `async`. A request with no body — every GET, which is most of them — resolves
// this whole path without a single promise, and an `async` wrapper here would allocate one
// regardless. Same reason `handle()` is not async.
return isPromise(acquired)
? acquired.then((settled) => parseAcquired(settled, matched, opts, req))
: parseAcquired(acquired, matched, opts, req);
}
function parseAcquired(
acquired: { bytes: Buffer | undefined } | BodyFailure,
matched: MatchedRoute,
opts: HttpOptions | undefined,
req: ErrorRequest,
): MaybePromise<{ body: unknown } | BodyFailure> {
if ('fail' in acquired) return acquired;
// `parseRequestBody(undefined, …)` answers `{ body: undefined }` for every content type, so
// there is nothing to parse and nothing to await. Skipping it is what keeps a bodiless request
// off the promise path entirely.
if (acquired.bytes === undefined) return { body: undefined };
const contentType = String(req.headers['content-type'] ?? '');
const duplicates = matched.def.bodyDuplicates ?? opts?.bodyDuplicates ?? 'last';
const maxParts = matched.def.maxParts ?? opts?.limits?.maxParts ?? 1000;
return parseRequestBody(acquired.bytes, contentType, duplicates, maxParts).then((parsed) =>
'error' in parsed
? { fail: renderError(new HttpError(parsed.status ?? 400, parsed.error), req, opts?.onError) }
: { body: parsed.body },
);
}