perf(cors): resolve the request's headers once, not two or three times
`security` and `cors` were computed twice for every request and three times for a
preflight: once in the adapter, to seed the headers a response written *above*
`handle()` still has to carry — a shed 503, a 413 from the body reader — and again in
`dispatch`, plus a third time inside `corsPreflightHeaders`.
Nothing was incorrect, because the predicate is expected to be pure with respect to one
request. The cost is that `cors.origins` is a predicate precisely so it can be a lookup:
an allowlist in Redis, a tenant query. The framework charged the caller's latency budget
and their backend two to three times for an answer whose inputs did not change in
between, and made a predicate with a counter in it count double.
`handle()` now takes the headers the adapter already computed and hands them down, with
`computeInjected` kept as the fallback for a caller that has none.
`corsPreflightHeaders` extends that result instead of re-deriving it. The Node adapter
was carrying its own copy of `computeInjected`, inlined; it uses the real one now, which
is what removes the duplicate rather than papering over it.
Measured against the built `dist/`, same script that produced the numbers in the issue:
GET with Origin 2x -> 1x
OPTIONS preflight 3x -> 1x
buildFetch, Origin 2x -> 1x
GET without Origin 0x -> 0x (never reached the predicate; unchanged)
The risk here is not the call count, it is that `injected` carries the security headers
onto every response shape — so the tests that matter are the existing ones across
`transport-security`, `http-headers`, `fetch`, `parity` and `web-stream`, all green.
The two new tests assert the count directly, on both adapters, which is the assertion
that keeps it from drifting back.
Closes #77.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
6 files changed
+120
-41
No files matched your search
@@ -166,6 +166,28 @@ describe('buildFetch request gate', () => {
|
||||
const afterRelease = await fetchHandler(new Request('http://localhost/slow'));
|
||||
expect(afterRelease.status).toBe(200);
|
||||
});
|
||||
it('consults a cors.origins predicate once per request on the fetch path too', async () => {
|
||||
let calls = 0;
|
||||
const fetchHandler = buildFetch(
|
||||
[{ method: 'GET', pattern: '/ok', transport: 'buffer',
|
||||
handler: async () => ({ status: 200, headers: {}, body: 'ok' }) }],
|
||||
{ cors: { origins: () => { calls++; return true; } } },
|
||||
);
|
||||
|
||||
const res = await fetchHandler(new Request('http://localhost/ok', { headers: { origin: 'https://a.com' } }));
|
||||
expect(res.headers.get('access-control-allow-origin')).toBe('https://a.com');
|
||||
expect(calls).toBe(1);
|
||||
|
||||
calls = 0;
|
||||
const preflight = await fetchHandler(new Request('http://localhost/ok', {
|
||||
method: 'OPTIONS',
|
||||
headers: { origin: 'https://a.com', 'access-control-request-method': 'GET' },
|
||||
}));
|
||||
expect(preflight.status).toBe(204);
|
||||
expect(preflight.headers.get('access-control-allow-methods')).toContain('GET');
|
||||
expect(calls).toBe(1);
|
||||
});
|
||||
|
||||
it('releases the slot when a streaming handler returns', async () => {
|
||||
async function* stream() {
|
||||
yield { n: 1 };
|
||||
|
||||
Reference in new issue
Block a user