CorsOptions.origins accepts a predicate, and the framework consults it more than once for a single request: twice for a normal request, three times for a preflight, on both the Node and the Fetch adapter.
What happens
CORS headers are computed at two independent points, and the preflight path computes them a third time.
Node, one GET carrying an Origin:
#
Call site
Path
1
src/http/server.ts:104
resolveCors directly, to seed injected before routing
The Fetch adapter has the same shape: src/http/web.ts:142 calls computeInjected before the gate, then handle() calls it again at core.ts:280.
Every one of those reaches originAllowed, and originAllowed calls the predicate.
Measured
Against the built dist/, with a predicate that logs each call, one request each:
GET with Origin -> predicate called 2x
OPTIONS preflight -> predicate called 3x
buildFetch, Origin -> predicate called 2x
GET without Origin -> predicate called 0x (originAllowed is not reached)
Why it matters
A predicate is the whole reason the option takes a function. The shapes it exists for are an allowlist in Redis, a tenant lookup, a regex over a parsed URL — the first two are I/O. The framework runs that I/O two to three times to answer one question whose inputs did not change between the calls, and it charges the user's latency budget and their Redis for the duplicates.
It is also observable in ways a user will read as a bug in their own code: a predicate with a counter in it counts double, and one that logs prints twice. That surfaced immediately once #72 gave the failure path a log line — one broken request produced two identical warnings.
Nothing is incorrect today: the predicate is expected to be pure with respect to one request, so the second call returns what the first did. The cost is duplicated work and a surprising contract, not a wrong answer.
Scope
The two computations exist for a real reason — injected has to be seeded before routing so that every response, including ones written above handle(), carries the headers. The fix is to compute once per request and share the result, rather than to remove either call site:
resolve the CORS headers once at the top of the request and hand the result down, instead of recomputing inside handle();
corsPreflightHeaders then extends that result rather than re-deriving it.
Worth checking at the same time whether buildSecurityHeaders is duplicated the same way — it sits in the same computeInjected and is pure computation, so it is cheap, but it is the same double call.
If sharing turns out to be structurally awkward, the honest fallback is to document that the predicate may be consulted more than once per request and must be cheap and side-effect free — but that is the worse outcome, since the option exists precisely for lookups that are neither.
Related
Found while fixing #72, which put a log line on the predicate's failure path and made the duplication visible. Deliberately not fixed there: a crash fix is the wrong place to restructure how headers are computed.
`CorsOptions.origins` accepts a predicate, and the framework consults it more than once for a single request: twice for a normal request, three times for a preflight, on both the Node and the Fetch adapter.
## What happens
CORS headers are computed at two independent points, and the preflight path computes them a third time.
Node, one `GET` carrying an `Origin`:
| # | Call site | Path |
|---|---|---|
| 1 | `src/http/server.ts:104` | `resolveCors` directly, to seed `injected` before routing |
| 2 | `src/http/core.ts:280` | `handle()` → `computeInjected` → `resolveCors` |
Node, one `OPTIONS` preflight — the same two, plus:
| 3 | `src/http/core.ts:290` | `corsPreflightHeaders` → `resolveCors` |
The Fetch adapter has the same shape: `src/http/web.ts:142` calls `computeInjected` before the gate, then `handle()` calls it again at `core.ts:280`.
Every one of those reaches `originAllowed`, and `originAllowed` calls the predicate.
## Measured
Against the built `dist/`, with a predicate that logs each call, one request each:
GET with Origin -> predicate called 2x
OPTIONS preflight -> predicate called 3x
buildFetch, Origin -> predicate called 2x
GET without Origin -> predicate called 0x (originAllowed is not reached)
## Why it matters
A predicate is the whole reason the option takes a function. The shapes it exists for are an allowlist in Redis, a tenant lookup, a regex over a parsed URL — the first two are I/O. The framework runs that I/O two to three times to answer one question whose inputs did not change between the calls, and it charges the user's latency budget and their Redis for the duplicates.
It is also observable in ways a user will read as a bug in their own code: a predicate with a counter in it counts double, and one that logs prints twice. That surfaced immediately once #72 gave the failure path a log line — one broken request produced two identical warnings.
Nothing is *incorrect* today: the predicate is expected to be pure with respect to one request, so the second call returns what the first did. The cost is duplicated work and a surprising contract, not a wrong answer.
## Scope
The two computations exist for a real reason — `injected` has to be seeded before routing so that every response, including ones written above `handle()`, carries the headers. The fix is to compute once per request and share the result, rather than to remove either call site:
- resolve the CORS headers once at the top of the request and hand the result down, instead of recomputing inside `handle()`;
- `corsPreflightHeaders` then extends that result rather than re-deriving it.
Worth checking at the same time whether `buildSecurityHeaders` is duplicated the same way — it sits in the same `computeInjected` and is pure computation, so it is cheap, but it is the same double call.
If sharing turns out to be structurally awkward, the honest fallback is to document that the predicate may be consulted more than once per request and must be cheap and side-effect free — but that is the worse outcome, since the option exists precisely for lookups that are neither.
## Related
Found while fixing #72, which put a log line on the predicate's failure path and made the duplication visible. Deliberately not fixed there: a crash fix is the wrong place to restructure how headers are computed.
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
CorsOptions.originsaccepts a predicate, and the framework consults it more than once for a single request: twice for a normal request, three times for a preflight, on both the Node and the Fetch adapter.What happens
CORS headers are computed at two independent points, and the preflight path computes them a third time.
Node, one
GETcarrying anOrigin:src/http/server.ts:104resolveCorsdirectly, to seedinjectedbefore routingsrc/http/core.ts:280handle()→computeInjected→resolveCorsNode, one
OPTIONSpreflight — the same two, plus:| 3 |
src/http/core.ts:290|corsPreflightHeaders→resolveCors|The Fetch adapter has the same shape:
src/http/web.ts:142callscomputeInjectedbefore the gate, thenhandle()calls it again atcore.ts:280.Every one of those reaches
originAllowed, andoriginAllowedcalls the predicate.Measured
Against the built
dist/, with a predicate that logs each call, one request each:Why it matters
A predicate is the whole reason the option takes a function. The shapes it exists for are an allowlist in Redis, a tenant lookup, a regex over a parsed URL — the first two are I/O. The framework runs that I/O two to three times to answer one question whose inputs did not change between the calls, and it charges the user's latency budget and their Redis for the duplicates.
It is also observable in ways a user will read as a bug in their own code: a predicate with a counter in it counts double, and one that logs prints twice. That surfaced immediately once #72 gave the failure path a log line — one broken request produced two identical warnings.
Nothing is incorrect today: the predicate is expected to be pure with respect to one request, so the second call returns what the first did. The cost is duplicated work and a surprising contract, not a wrong answer.
Scope
The two computations exist for a real reason —
injectedhas to be seeded before routing so that every response, including ones written abovehandle(), carries the headers. The fix is to compute once per request and share the result, rather than to remove either call site:handle();corsPreflightHeadersthen extends that result rather than re-deriving it.Worth checking at the same time whether
buildSecurityHeadersis duplicated the same way — it sits in the samecomputeInjectedand is pure computation, so it is cheap, but it is the same double call.If sharing turns out to be structurally awkward, the honest fallback is to document that the predicate may be consulted more than once per request and must be cheap and side-effect free — but that is the worse outcome, since the option exists precisely for lookups that are neither.
Related
Found while fixing #72, which put a log line on the predicate's failure path and made the duplication visible. Deliberately not fixed there: a crash fix is the wrong place to restructure how headers are computed.