feat(http)!: cookie decorators, setCookie, and string | string[] response headers (CARD-14, #113)
Reading a cookie meant parsing the Cookie header by hand, and a response
could not carry more than one Set-Cookie: response header values were
Record<string, string>, and the fetch adapters would have stringified an
array into one broken "a,b" cookie anyway.
- @cookie('name') / @cookies() sit next to @header/@headers as a new
'cookies' arg source. The header is parsed on first use per request and
cached on the context, so a route without a cookie argument never parses
it. RFC 6265: first value wins, %-sequences decoded, malformed pairs
skipped (never a 400), undecodable values kept raw. Several cookie lines
are joined with "; ". The map has no prototype. @needs('cookies') reads
the same map, and 'cookies' joins the seed-key allow-list.
- setCookie(name, value, options) serialises one Set-Cookie value with
HttpOnly, SameSite=Lax, Path=/ and Secure by default (D10), and throws,
naming the field, on anything that could inject a header or that browsers
reject (sameSite none / partitioned without secure).
- Response header values widen to string | string[] (D11) across
TransformerFn, ErrorResponse, HttpError(Like).headers, ResponseShape and
the neutral outcome. Node writes a set-cookie array as one line per
element and joins every other array with ", " before writeHead; the
fetch adapters build a Headers and append each set-cookie element.
mergeInjectedHeaders keeps arrays by reference and joins a vary array
before merging it.
- OpenAPI maps a cookie argument to an in: cookie parameter.
BREAKING CHANGE: code that reads a header value from TransformerFn,
ErrorResponse, HttpError or ResponseShape as a string must handle string[].
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
24 files changed
+781
-34
No files matched your search
+13
-1
@@ -66,6 +66,18 @@ async function main() {
|
||||
assert.equal(httpRes.status, 200);
|
||||
assert.deepEqual(await httpRes.json(), { ok: true });
|
||||
|
||||
// CARD-14 (#113): a Set-Cookie array leaves workerd as one line per cookie; other arrays join.
|
||||
const cookieRes = await mf.dispatchFetch('http://localhost/rt/cookies');
|
||||
await cookieRes.arrayBuffer();
|
||||
assert.deepEqual(cookieRes.headers.getSetCookie(), ['a=1; Path=/', 'b=2; Path=/']);
|
||||
assert.equal(cookieRes.headers.get('x-custom'), 'a, b');
|
||||
const cookieErr = await mf.dispatchFetch('http://localhost/rt/cookie-error');
|
||||
await cookieErr.arrayBuffer();
|
||||
assert.equal(cookieErr.status, 401);
|
||||
assert.deepEqual(cookieErr.headers.getSetCookie(), ['a=1; Path=/', 'b=2; Path=/']);
|
||||
const readCookie = await mf.dispatchFetch('http://localhost/rt/cookie', { headers: { cookie: 'a=1; b=%41' } });
|
||||
assert.deepEqual(await readCookie.json(), { b: 'A' });
|
||||
|
||||
// Body limits (#105): the same statuses as app.fetch, Deno and Bun. workerd may drop a
|
||||
// chunked upload's content-length, so the streamed case is enforced by the read count alone.
|
||||
const LIMIT = 1000; // the route's maxBodyBytes, see worker.ts
|
||||
@@ -138,7 +150,7 @@ async function main() {
|
||||
assert.equal((await exchange(mf, 'http://localhost/rt/echo', ['a'.repeat(1_048_576)])).replies.length, 1);
|
||||
assert.equal((await exchange(mf, 'http://localhost/rt/echo', ['a'.repeat(1_048_577)])).close?.code, 1009);
|
||||
|
||||
console.log('edge (workerd via miniflare): HTTP 200 + body limits 413/413/200/413 + WS echo:hi + ws limits — PASS');
|
||||
console.log('edge (workerd via miniflare): HTTP 200 + cookies + body limits 413/413/200/413 + WS echo:hi + ws limits — PASS');
|
||||
} finally {
|
||||
await mf.dispose();
|
||||
}
|
||||
|
||||
+18
-1
@@ -1,7 +1,7 @@
|
||||
// Worker entry for the real workerd (Miniflare) integration test.
|
||||
// Mirrors test/deno/ws.test.ts's graph so HTTP + WebSocket behavior is proven
|
||||
// identically across runtimes. Run via `npm run test:edge` (see test/edge/run.ts).
|
||||
import { createApp, Route, Get, Post, Ws, body, ctx, Module } from '../../src/index';
|
||||
import { createApp, Route, Get, Post, Ws, body, ctx, cookie, Module, Transformer, HttpError } from '../../src/index';
|
||||
import { edgeHandler } from '../../src/edge';
|
||||
|
||||
@Route('/rt')
|
||||
@@ -11,6 +11,23 @@ class Rt {
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
// CARD-14 (#113): two Set-Cookie lines from workerd, and @cookie on the request side.
|
||||
@Get('/cookies')
|
||||
@Transformer(() => ({ headers: { 'set-cookie': ['a=1; Path=/', 'b=2; Path=/'], 'x-custom': ['a', 'b'] }, body: '{}' }))
|
||||
cookies() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Get('/cookie-error')
|
||||
cookieError() {
|
||||
throw new HttpError(401, 'nope', undefined, { 'set-cookie': ['a=1; Path=/', 'b=2; Path=/'] });
|
||||
}
|
||||
|
||||
@Get('/cookie')
|
||||
readCookie(@cookie('b') b: string | undefined) {
|
||||
return { b: b ?? null };
|
||||
}
|
||||
|
||||
// #105: the body limit, read from workerd's own request stream. Small, so every refused body still
|
||||
// fits in the socket buffer: a client that is mid-upload when the 413 lands can see ECONNRESET
|
||||
// instead, which is the transport racing the answer rather than a different answer.
|
||||
|
||||
Reference in new issue
Block a user