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:
5 files changed
+262
-201
No files matched your search
+50
-11
@@ -5,6 +5,7 @@ import { isStreamResult, type PipelineResult } from '../pipeline';
|
||||
import { buildSecurityHeaders, resolveCors, corsPreflightHeaders } from '../security';
|
||||
import { resolveRoute, allowedMethods, normalizeRequestPath, parseQuery } from './router';
|
||||
import { pickEncoder } from './stream';
|
||||
import { acquireBody, type BodyReader } from './body';
|
||||
import type { RouteDef, HttpOptions } from './types';
|
||||
import type { StreamEncoder } from '../encoders';
|
||||
import type { Bus } from '../bus';
|
||||
@@ -16,12 +17,24 @@ import type { Bus } from '../bus';
|
||||
// have without a compatibility flag.
|
||||
declare const crypto: { randomUUID(): string };
|
||||
|
||||
/** A runtime-neutral inbound request: body already parsed by the caller's ingress (Node stream, fetch Request, …). */
|
||||
/**
|
||||
* A runtime-neutral inbound request.
|
||||
*
|
||||
* The body arrives as a {@link BodyReader} rather than a value, and that is the whole reason this
|
||||
* core resolves each request exactly once. When the adapter had to hand over an already-parsed
|
||||
* body, it first had to route the request itself — a body is only read for a request that reaches
|
||||
* a handler — so both the adapter and `dispatch` resolved the same route from the same inputs, and
|
||||
* a four-route app walked its table two and a half times per request. Handing over the *ability*
|
||||
* to read instead of the result moves that decision here, where the route is known anyway.
|
||||
*/
|
||||
export interface NeutralRequest {
|
||||
method: string;
|
||||
url: string; // path + query, e.g. "/users/9?x=1"
|
||||
headers: Record<string, string | string[] | undefined>;
|
||||
body: unknown; // already-parsed body
|
||||
/** Called only once a route matches, so an unmatched request still never reads a body. */
|
||||
readBody: BodyReader;
|
||||
/** The runtime's own request object, passed back to {@link readBody} untouched and never inspected here. */
|
||||
source: unknown;
|
||||
secure: boolean;
|
||||
ip: string;
|
||||
requestId: string;
|
||||
@@ -36,10 +49,10 @@ const first = (value: string | string[] | undefined): string | undefined => (Arr
|
||||
/**
|
||||
* Derives a request's identity from its headers, for the adapters to stamp on a `NeutralRequest`.
|
||||
*
|
||||
* **Called by the adapters, not by {@link handle}**, and the placement is the point. Both adapters
|
||||
* read and size-check the body before they reach `handle()` (`server.ts`, `web.ts`), so a `413` is
|
||||
* answered without `handle()` ever seeing the request. Generating identity in here would leave
|
||||
* precisely the failures an operator most wants to correlate with nothing to correlate them by.
|
||||
* **Called by the adapters, not by {@link handle}**, and the placement is the point. An adapter's
|
||||
* `BodyReader` renders its own `413` (`server.ts`, `web.ts`) without `handle()` ever producing it,
|
||||
* so identity has to exist before the read starts. Generating it in here would leave precisely the
|
||||
* failures an operator most wants to correlate with nothing to correlate them by.
|
||||
*
|
||||
* An `x-request-id` from a gateway is adopted rather than replaced — a service behind a proxy must
|
||||
* not open a second identity for a request that already has one. `traceparent` is carried through
|
||||
@@ -71,7 +84,14 @@ export function correlateRequest(headers: Record<string, string | string[] | und
|
||||
/** The neutral shape of a response: a fully buffered body, or a stream to pipe frame-by-frame. */
|
||||
export type NeutralOutcome =
|
||||
| { kind: 'buffered'; status: number; headers: Record<string, string>; body: string | Buffer }
|
||||
| { kind: 'stream'; headers: Record<string, string>; stream: AsyncIterable<unknown>; encoder: StreamEncoder };
|
||||
| {
|
||||
kind: 'stream';
|
||||
headers: Record<string, string>;
|
||||
stream: AsyncIterable<unknown>;
|
||||
encoder: StreamEncoder;
|
||||
/** The matched route's pattern, for naming the stream lifecycle events. */
|
||||
route: string;
|
||||
};
|
||||
|
||||
/** {@link handle}'s result for a normal (non-preflight) request. */
|
||||
export interface HandleResult {
|
||||
@@ -106,7 +126,7 @@ function errorOutcome(
|
||||
opts: HttpOptions | undefined,
|
||||
headers: Record<string, string> = {},
|
||||
): HandleResult {
|
||||
const rendered = renderError(error, { method: req.method, url: req.url, headers: req.headers }, opts?.onError);
|
||||
const rendered = renderError(error, req, opts?.onError);
|
||||
return {
|
||||
injected,
|
||||
outcome: {
|
||||
@@ -279,6 +299,16 @@ async function dispatch(
|
||||
traceId: req.traceId,
|
||||
});
|
||||
|
||||
// `req` is passed straight through as the error context: a `NeutralRequest` already has the
|
||||
// `method`/`url`/`headers` an `ErrorRequest` is, so copying those three fields into a fresh
|
||||
// object per request bought nothing.
|
||||
const acquired = await acquireBody(req.readBody, req.source, matched, opts, req);
|
||||
|
||||
// The adapter rendered this one itself (a 413 it had to answer while reading), or `acquireBody`
|
||||
// did (a malformed or unsupported body). Either way it is already the standard envelope, and the
|
||||
// caller merges `injected` into it exactly as it does for every other outcome.
|
||||
if ('fail' in acquired) return { injected, outcome: { kind: 'buffered', ...acquired.fail } };
|
||||
|
||||
let result: PipelineResult;
|
||||
|
||||
try {
|
||||
@@ -288,19 +318,28 @@ async function dispatch(
|
||||
headers: req.headers,
|
||||
params: matched.params,
|
||||
query: parseQuery(req.url),
|
||||
body: req.body,
|
||||
body: acquired.body,
|
||||
protocol: req.secure ? 'https' : 'http',
|
||||
ip: req.ip,
|
||||
requestId: req.requestId,
|
||||
traceId: req.traceId,
|
||||
});
|
||||
} catch (error) {
|
||||
result = renderError(error, { method: req.method, url: req.url, headers: req.headers }, opts?.onError);
|
||||
result = renderError(error, req, opts?.onError);
|
||||
}
|
||||
|
||||
if (isStreamResult(result)) {
|
||||
const encoder = pickEncoder(matched.def.transport, String(req.headers.accept ?? ''));
|
||||
return { injected, outcome: { kind: 'stream', headers: encoder.headers, stream: result.stream, encoder } };
|
||||
return {
|
||||
injected,
|
||||
outcome: {
|
||||
kind: 'stream',
|
||||
headers: encoder.headers,
|
||||
stream: result.stream,
|
||||
encoder,
|
||||
route: matched.def.pattern,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
|
||||
Reference in new issue
Block a user