feat(http): ETag and 304 for static files and buffered responses (CARD-15, #114)
CI / dco (pull_request) Successful in 4s
CI / runtimes (bun) (pull_request) Successful in 16s
CI / build (pull_request) Successful in 27s
CI / runtimes (deno) (pull_request) Successful in 21s
CI / runtimes (edge) (pull_request) Successful in 13s

Conditional GETs were never answered: every revalidation re-downloaded
the file or response.

- Static (D12, always on): a hit carries W/"<size>-<mtimeMs base36>",
  Last-Modified and Cache-Control from the stat the resolver already did.
  A matching If-None-Match, or If-Modified-Since when If-None-Match is
  absent, answers 304 before the file is read. StaticHit now carries the
  stat and a read() for the bytes, and HEAD no longer reads either.
- static: { dir?, cacheControl? } (rule 1a): Cache-Control defaults to
  no-cache so browsers revalidate rather than reuse a file for a lifetime
  guessed from Last-Modified. An unknown key or CR/LF throws at boot.
- createApp({ etag: true }) (off by default, boolean only): a buffered 200
  to GET/HEAD gets a weak tag from a 32-bit FNV-1a hash of its UTF-8 bytes
  plus its length, computed before HEAD blanks the body. A handler-set
  etag is kept and only compared. Weak comparison, lists split outside
  quotes, and * are supported.
- Every buffered 304 leaves dispatch bodiless, with its content-* headers
  dropped, so NotModified matches the ETag path on every adapter.

Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
chrnx committed 2026-10-23 01:09:54 -05:00
1 parent 50010da88d
commit d32fc9f8d7
14 files changed
+706 -37

No files matched your search

+15 -1
View File
@@ -78,6 +78,20 @@ async function main() {
const readCookie = await mf.dispatchFetch('http://localhost/rt/cookie', { headers: { cookie: 'a=1; b=%41' } });
assert.deepEqual(await readCookie.json(), { b: 'A' });
// CARD-15 (#114), T5/T11: handler ETag round-trip on workerd. Static is a boot error there (N/A).
const tag1 = await mf.dispatchFetch('http://etag/rt/hello');
await tag1.arrayBuffer();
const etag = tag1.headers.get('etag');
assert.match(String(etag), /^W\/"\w+-\w+"$/);
const tag2 = await mf.dispatchFetch('http://etag/rt/hello', { headers: { 'if-none-match': String(etag) } });
assert.equal(tag2.status, 304);
assert.equal(await tag2.text(), '');
assert.equal(tag2.headers.get('etag'), etag);
assert.equal(tag2.headers.get('content-type'), null);
const untagged = await mf.dispatchFetch('http://localhost/rt/hello');
await untagged.arrayBuffer();
assert.equal(untagged.headers.get('etag'), null, 'etag off: no ETag');
// 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
@@ -203,7 +217,7 @@ async function main() {
assert.equal(ready.headers.get('cache-control'), 'no-store');
console.log(
'edge (workerd via miniflare): HTTP 200 + cookies + body limits 413/413/200/413 + WS echo:hi + ws limits + cf-connecting-ip under trustProxy + reserved path + health — PASS',
'edge (workerd via miniflare): HTTP 200 + cookies + etag/304 + body limits 413/413/200/413 + WS echo:hi + ws limits + cf-connecting-ip under trustProxy + reserved path + health — PASS',
);
} finally {
await mf.dispose();
+2
View File
@@ -87,6 +87,8 @@ const handlers: Record<string, ReturnType<typeof edgeHandler>> = {
trusted: edgeHandler(trusted),
// CARD-17 T10: health probes, reached through the `health` host. Never booted by anything but probes.
health: edgeHandler(createApp({ modules: [M], health: true })),
// CARD-15 (#114): the same graph with handler ETags on, reached through the `etag` host.
etag: edgeHandler(createApp({ modules: [M], etag: true })),
};
// CARD-21 (D17): a route on the reserved mesh path must fail createApp inside workerd too. Built per