feat(http): response compression with gzip and br (CARD-16, #112)
Text responses went out uncompressed unless a proxy sat in front of every
runtime. createApp({ compression: true | { threshold, encodings } }), off
by default, implements D13 strategy B as accepted on 2026-10-22.
- One function, compressOutcome, runs after handle() and before either
adapter writes, so Node and the fetch runtimes cannot diverge.
- Accept-Encoding negotiation with q-values, identity, * and q=0; ties go
to server preference (br before gzip). Vary: Accept-Encoding is merged
wherever the request decided the outcome, including every 304. Skips:
existing Content-Encoding or Content-Range, no or an incompressible
Content-Type (ndjson is compressible), Cache-Control: no-transform.
HEAD and bodies under threshold keep Vary; 204 gets none.
- Node, Deno, Bun: node:zlib only, loaded lazily through nodeRequire.
Buffered bodies use the async API, never the sync one; br at quality 4.
Content-Length is dropped from an encoded body.
- Streams (SSE, ndjson): a zlib stream flushed after every frame, error
frame and ping, piped into res on Node and wrapped around the
ReadableStream on fetch runtimes. Backpressure follows the compressor;
a client leaving destroys it.
- Edge: core never encodes and sets Vary only. Edge is recognised by
edgeHandler, which marks the app through an internal symbol, and by
createApp detecting workerd (navigator.userAgent, or node:zlib failing
to load), which covers a bare app.fetch export.
- ETags from CARD-15 are taken on the identity body and stay weak, so the
tag does not change with the coding.
Deno runs zlib's async API on the main thread; that is documented and
pinned by a test, not worked around.
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
20 files changed
+1437
-40
No files matched your search
@@ -69,6 +69,25 @@ npm treats versions as semver and semver forbids leading zeros.
|
||||
|
||||
### Added
|
||||
|
||||
- **`createApp({ compression })`** (#112, D13): response compression, off by default. `true`, or
|
||||
`{ threshold?, encodings? }` (default `1024` bytes and `['br', 'gzip']`, in preference order).
|
||||
`Accept-Encoding` is negotiated with q-values, `identity`, `*` and `q=0`, and ties go to server
|
||||
preference. Text, JSON, JavaScript, ndjson, SVG, XML and wasm are compressed. A response that
|
||||
already has `Content-Encoding` or `Content-Range`, has no `Content-Type`, or says
|
||||
`Cache-Control: no-transform` is left alone. `Vary: Accept-Encoding` is added wherever the request
|
||||
decided the outcome: identity clients, `HEAD`, bodies under the threshold and every `304`.
|
||||
`Content-Length` is dropped from an encoded body. SSE and ndjson streams are compressed too,
|
||||
flushed after every event, error frame and ping, so each event decodes when it arrives.
|
||||
Per runtime:
|
||||
- **Node and Bun:** async `node:zlib`, br at quality 4.
|
||||
- **Deno:** the same code, but Deno 2.9 runs zlib's async API on the main thread, so compression
|
||||
costs event-loop time there (about 4× slower than Node for a 51 KB gzip under load).
|
||||
- **Edge:** core never encodes and sets only `Vary`; Cloudflare applies the encoding. Edge is
|
||||
recognised through `edgeHandler` or by detecting workerd at `createApp`.
|
||||
|
||||
ETags (`etag: true`) are taken before encoding and stay the same across codings. No dependency;
|
||||
invalid options throw.
|
||||
|
||||
- **`createApp({ etag: true })`** (#114, D12): weak ETags for buffered `200` responses to `GET` and
|
||||
`HEAD`, from a 32-bit FNV-1a hash of the body plus its length. A matching `If-None-Match` (weak
|
||||
comparison, lists, `*`) gets a `304` with no body. A handler that sets its own `etag` keeps it, and
|
||||
|
||||
Reference in new issue
Block a user