feat(http): ETag and 304 for static files and buffered responses (CARD-15, #114) #144

Merged
chrnx merged 1 commits from feature/fprd-001c-etag into develop 2026-10-23 06:11:43 +00:00
Owner

Depends on #140 (CARD-14). Review and merge after it. This branch stacks on feature/fprd-001c-cookies, so until #140 merges the diff against develop includes CARD-14's commit. Review only the top commit (feat(http): ETag and 304 …).

Implements CARD-15 (core #114) under decision D12 (on for static, opt-in for handlers) and rule 1a (static Cache-Control).

What changed and why

  • Static (always on):
    • StaticHit now holds { contentType, size, mtimeMs, cacheControl, read() } instead of the bytes. It is internal and not exported.
    • unmatchedOutcome sets ETag: W/"<size>-<mtimeMs base36>" and Last-Modified from the resolver's stat, plus Cache-Control. It decides the conditional request before calling read(), so a 304 never reads the file. HEAD no longer reads it either.
    • If-None-Match takes precedence; If-Modified-Since is used only when If-None-Match is absent, compared at one-second resolution.
  • static: { dir?, cacheControl? }:
    • Cache-Control: no-cache by default on 200 and 304.
    • normalizeStatic validates at boot. It throws on an unknown key, a non-string dir/cacheControl, or a cacheControl containing CR/LF.
    • static: true | '<dir>' is unchanged.
  • createApp({ etag: true }):
    • Off by default; anything but a boolean throws.
    • In dispatch, a buffered 200 to GET/HEAD gets a weak tag from 32-bit FNV-1a over the UTF-8 bytes plus the byte length (W/"<len36>-<hash36>"). It is computed before HEAD blanks the body.
    • A handler-set etag (any case) is kept and only compared.
    • Comparison is weak. Lists are split on commas outside quotes, and * is supported.
  • 304 shape:
    • Every buffered 304 leaving dispatch passes through bodyless(): empty body, content-* headers dropped (except content-location), transfer-encoding dropped.
    • ETag, Vary, Cache-Control, Last-Modified and the injected security headers stay.
    • throw new NotModified() now goes through the same function. Before, it sent the JSON envelope's content-type; now it sends no body headers.
  • Mesh: a remote route is buffered on the teacup, so etag: true there tags it like a local route. No protocol change.

Unsure / conservative choices

  • Handler-set tags without etag: true. The card's rule 3 doesn't say whether a handler-set etag is compared when etag is off. I compare only under etag: true, following the AC "absent or false adds none (no silent partial behaviour)". Tested both ways.
  • Hash width. 32-bit FNV plus length; the card allows 32 or 64. Collisions can only happen between bodies of equal length.
  • Size format. <size> in the static tag is decimal and mtimeMs is floored, then base36. The card leaves the size radix open.
  • 304s keep non-body headers such as set-cookie. Rule 6 lists what must be present, and I strip only the body-describing headers.
  • views-static.test.ts changed one line, from hit.body to hit.read(), because StaticHit changed.

Test evidence

New test/etag.test.ts covers T1–T10, the NotModified 304, static object form and validation, etag validation, and Node over a raw socket (T11). The readFile spy checks T1 on Node, Deno and Bun. New test/deno/etag.test.ts and test/bun/etag.test.ts run T1 + T5 on real Deno.serve/Bun.serve. Deno's own readFile re-enters itself with a file descriptor, so that suite counts only calls that name a path. test/edge/run.ts runs T5 on workerd. Static on the edge is N/A: static is a boot error there.

== npm run lint
exit 0
✖ 50 problems (0 errors, 50 warnings)
== npm run format:check
exit 0
All matched files use Prettier code style!
== npm run typecheck
exit 0
== npm run test
exit 0
 Test Files  94 passed (94)
      Tests  960 passed (960)
== npm run build
exit 0
ESM ⚡️ Build success in 26ms
CJS ⚡️ Build success in 26ms
DTS ⚡️ Build success in 927ms
== npm run complexity:check
exit 0
✓ cognitive complexity: every function ≤ 25
== npm run test:deno
exit 0
T3 serveDeno: an IPv6 peer is passed through as Deno reports it ... ok (702µs)
ok | 38 passed | 0 failed (4s)
== npm run test:bun
exit 0
 28 pass
 0 fail
== npm run test:edge
exit 0
edge (workerd via miniflare): HTTP 200 + cookies + etag/304 + body limits 413/413/200/413 + WS echo:hi + ws limits — PASS
edge (workerd): router parity — 1158 matcher cases + 55 app.fetch cases — PASS

Frozen router-parity captures are untouched.

Docs follow-up (CARD-12)

  • reference/createapp.md: the etag option, the static object form (dir, cacheControl), and what static responses now carry (ETag, Last-Modified, Cache-Control: no-cache, 304).
  • guides/html.md: revalidation for static assets, and the immutable recipe for hashed assets.
**Depends on #140 (CARD-14). Review and merge after it.** This branch stacks on `feature/fprd-001c-cookies`, so until #140 merges the diff against `develop` includes CARD-14's commit. Review only the top commit (`feat(http): ETag and 304 …`). Implements **CARD-15** (core #114) under decision **D12** (on for static, opt-in for handlers) and rule 1a (static `Cache-Control`). ## What changed and why - **Static (always on):** - `StaticHit` now holds `{ contentType, size, mtimeMs, cacheControl, read() }` instead of the bytes. It is internal and not exported. - `unmatchedOutcome` sets `ETag: W/"<size>-<mtimeMs base36>"` and `Last-Modified` from the resolver's `stat`, plus `Cache-Control`. It decides the conditional request **before** calling `read()`, so a `304` never reads the file. `HEAD` no longer reads it either. - `If-None-Match` takes precedence; `If-Modified-Since` is used only when `If-None-Match` is absent, compared at one-second resolution. - **`static: { dir?, cacheControl? }`:** - `Cache-Control: no-cache` by default on 200 and 304. - `normalizeStatic` validates at boot. It throws on an unknown key, a non-string `dir`/`cacheControl`, or a `cacheControl` containing CR/LF. - `static: true | '<dir>'` is unchanged. - **`createApp({ etag: true })`:** - Off by default; anything but a boolean throws. - In `dispatch`, a buffered `200` to GET/HEAD gets a weak tag from 32-bit FNV-1a over the UTF-8 bytes plus the byte length (`W/"<len36>-<hash36>"`). It is computed before HEAD blanks the body. - A handler-set `etag` (any case) is kept and only compared. - Comparison is weak. Lists are split on commas outside quotes, and `*` is supported. - **304 shape:** - Every buffered 304 leaving `dispatch` passes through `bodyless()`: empty body, `content-*` headers dropped (except `content-location`), `transfer-encoding` dropped. - `ETag`, `Vary`, `Cache-Control`, `Last-Modified` and the injected security headers stay. - `throw new NotModified()` now goes through the same function. Before, it sent the JSON envelope's `content-type`; now it sends no body headers. - **Mesh:** a remote route is buffered on the teacup, so `etag: true` there tags it like a local route. No protocol change. ## Unsure / conservative choices - **Handler-set tags without `etag: true`.** The card's rule 3 doesn't say whether a handler-set `etag` is compared when `etag` is off. I compare **only under `etag: true`**, following the AC "absent or false adds none (no silent partial behaviour)". Tested both ways. - **Hash width.** 32-bit FNV plus length; the card allows 32 or 64. Collisions can only happen between bodies of equal length. - **Size format.** `<size>` in the static tag is decimal and `mtimeMs` is floored, then base36. The card leaves the size radix open. - `304`s keep non-body headers such as `set-cookie`. Rule 6 lists what *must* be present, and I strip only the body-describing headers. - `views-static.test.ts` changed one line, from `hit.body` to `hit.read()`, because `StaticHit` changed. ## Test evidence New `test/etag.test.ts` covers T1–T10, the `NotModified` 304, `static` object form and validation, `etag` validation, and Node over a raw socket (T11). The `readFile` spy checks T1 on Node, Deno and Bun. New `test/deno/etag.test.ts` and `test/bun/etag.test.ts` run T1 + T5 on real `Deno.serve`/`Bun.serve`. Deno's own `readFile` re-enters itself with a file descriptor, so that suite counts only calls that name a path. `test/edge/run.ts` runs T5 on workerd. Static on the edge is N/A: `static` is a boot error there. ``` == npm run lint exit 0 ✖ 50 problems (0 errors, 50 warnings) == npm run format:check exit 0 All matched files use Prettier code style! == npm run typecheck exit 0 == npm run test exit 0 Test Files 94 passed (94) Tests 960 passed (960) == npm run build exit 0 ESM ⚡️ Build success in 26ms CJS ⚡️ Build success in 26ms DTS ⚡️ Build success in 927ms == npm run complexity:check exit 0 ✓ cognitive complexity: every function ≤ 25 == npm run test:deno exit 0 T3 serveDeno: an IPv6 peer is passed through as Deno reports it ... ok (702µs) ok | 38 passed | 0 failed (4s) == npm run test:bun exit 0 28 pass 0 fail == npm run test:edge exit 0 edge (workerd via miniflare): HTTP 200 + cookies + etag/304 + body limits 413/413/200/413 + WS echo:hi + ws limits — PASS edge (workerd): router parity — 1158 matcher cases + 55 app.fetch cases — PASS ``` Frozen router-parity captures are untouched. ## Docs follow-up (CARD-12) - `reference/createapp.md`: the `etag` option, the `static` object form (`dir`, `cacheControl`), and what static responses now carry (`ETag`, `Last-Modified`, `Cache-Control: no-cache`, `304`). - `guides/html.md`: revalidation for static assets, and the `immutable` recipe for hashed assets.
chrnx force-pushed feature/fprd-001c-etag from c86d298fd4 to fc1db2dbab 2026-10-23 05:03:09 +00:00 Compare
Author
Owner

Owner decision (2026-10-23): with etag off, a handler-set etag header does not trigger a 304. Accepted as implemented. Not merging yet: implementation review first.

Owner decision (2026-10-23): with `etag` off, a handler-set `etag` header does not trigger a 304. Accepted as implemented. Not merging yet: implementation review first.
Author
Owner

Implementation review done on 2026-10-23: a Codex review of all 12 sprint PRs, with each finding verified. The P1s in #142 and the P3 in #149 are fixed. The owner (Chrnx) authorized merging in order, each one rebased on develop with CI green. This supersedes the earlier "not merging yet" note.

Implementation review done on 2026-10-23: a Codex review of all 12 sprint PRs, with each finding verified. The P1s in #142 and the P3 in #149 are fixed. The owner (Chrnx) authorized merging in order, each one rebased on develop with CI green. This supersedes the earlier "not merging yet" note.
chrnx force-pushed feature/fprd-001c-etag from fc1db2dbab to ea19ee21b7 2026-10-23 06:04:15 +00:00 Compare
chrnx added 1 commit 2026-10-23 06:09:57 +00:00
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 / runtimes (deno) (pull_request) Successful in 21s
CI / runtimes (edge) (pull_request) Successful in 13s
CI / build (pull_request) Successful in 27s
d32fc9f8d7
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>
chrnx force-pushed feature/fprd-001c-etag from ea19ee21b7 to d32fc9f8d7 2026-10-23 06:09:57 +00:00 Compare
chrnx merged commit c22c37e0b3 into develop 2026-10-23 06:11:43 +00:00
chrnx deleted branch feature/fprd-001c-etag 2026-10-23 06:11:44 +00:00
Sign in to join this conversation.