perf(http): resolve ctx.ip on first read instead of on every request
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 20s
CI / build (pull_request) Successful in 30s
CI / runtimes (edge) (pull_request) Successful in 23s
CI / runtimes (deno) (pull_request) Successful in 34s

Every Bun request called server.requestIP() for ctx.ip (#116), a native
call that builds an object, whether anything read the address or not. On
Mac that measured 7-11% of a small request; recording the server in the
peer map instead costs nothing.

The neutral request now carries a resolver rather than an address. The
handler request and the pipeline seed expose ctx.ip as an accessor that
resolves once on first read, and a step may still assign it. runPipeline
uses the seed it is given instead of spreading it, which would have
evaluated the accessor; both callers build the seed fresh per request.

Inside a stream body ctx.ip is '' unless a step or the handler read it
before the stream was returned: the body runs after the response has
started, when the socket may be gone.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-28 14:31:20 -05:00
1 parent 109297ca4c
commit b86cd605c4
9 files changed
+283 -57

No files matched your search

+17 -12
View File
@@ -245,12 +245,24 @@ function outcomeToResponse(
}
/** The first `x-forwarded-for` hop when the proxy is trusted, else `''`. */
function forwardedFor(headers: Record<string, string>, opts: HttpOptions | undefined): string {
function forwardedFor(request: Request, opts: HttpOptions | undefined): string {
if (!opts?.trustProxy) return '';
return String(headers['x-forwarded-for'] ?? '')
.split(',')[0]
.trim();
return (request.headers.get('x-forwarded-for') ?? '').split(',')[0].trim();
}
/**
* `ctx.ip` on the fetch path, for `NeutralRequest.resolveIp`. Mirrors Node's `deriveIp`
* (src/http/request.ts): only trust `x-forwarded-for` when the proxy is trusted, and take its first
* hop, because an untrusted XFF is client-spoofable. Otherwise the socket peer, which only the Deno
* and Bun adapters can supply; a bare `app.fetch` has none, so there the answer is still ''. Ahead of
* both, under `trustProxy` only: the address the edge adapter read from `cf-connecting-ip`.
* Cloudflare sets that header itself, while XFF can carry hops the client supplied, so on the edge
* it wins (CARD-23).
*/
function fetchIp(source: unknown, opts: HttpOptions | undefined): string {
const request = source as Request;
return trustedProxyClient(request, opts?.trustProxy) || forwardedFor(request, opts) || peerOf(request);
}
/**
@@ -324,14 +336,7 @@ export function buildFetch(routes: RouteTable, opts: HttpOptions | undefined) {
readBody,
source: request,
secure,
// Mirrors Node's `deriveIp` (src/http/request.ts): only trust `x-forwarded-for` when the
// proxy is trusted, and take its first hop — an untrusted XFF is client-spoofable.
// Otherwise the socket peer, which only the Deno and Bun adapters can supply; a bare
// `app.fetch` has none, so there the answer is still ''.
// Ahead of both, under `trustProxy` only: the address the edge adapter read from
// `cf-connecting-ip`. Cloudflare sets that header itself, while XFF can carry hops the
// client supplied, so on the edge it wins (CARD-23).
ip: trustedProxyClient(request, opts?.trustProxy) || forwardedFor(headers, opts) || peerOf(request),
resolveIp: fetchIp,
},
// Already computed above for the shed-503 path; handing it down keeps `cors.origins` to
// one call per request.