1 Commits
Author SHA1 Message Date
chrnx d5643da03a feat(http): response compression with gzip and br (CARD-16, #112)
CI / dco (pull_request) Successful in 5s
CI / build (pull_request) Failing after 23s
CI / runtimes (bun) (pull_request) Successful in 17s
CI / runtimes (deno) (pull_request) Successful in 22s
CI / runtimes (edge) (pull_request) Successful in 18s
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>
2026-10-23 00:24:59 -05:00
4 changed files with 12 additions and 8 deletions

No files matched your search

+1
View File
@@ -31,5 +31,6 @@ test('documented: Bun.serve does not compress by itself (core compression off
test("documented: node:zlib's async gzip is off the main thread on Bun", async () => { test("documented: node:zlib's async gzip is off the main thread on Bun", async () => {
const { ms, ticks } = await ticksDuringGzip(); const { ms, ticks } = await ticksDuringGzip();
if (ms < 50) throw new Error(`gzip took ${Math.round(ms)} ms — too fast to tell; enlarge the input`);
if (ticks < ms / 4) throw new Error(`only ${ticks} ticks in ${Math.round(ms)} ms: Bun blocked the loop — update the docs`); if (ticks < ms / 4) throw new Error(`only ${ticks} ticks in ${Math.round(ms)} ms: Bun blocked the loop — update the docs`);
}); });
+1
View File
@@ -355,6 +355,7 @@ describe('slow client backpressure (Node)', () => {
describe('documented runtime behaviour (Node)', () => { describe('documented runtime behaviour (Node)', () => {
it("node:zlib's async gzip runs off the main thread, so the loop keeps ticking", async () => { it("node:zlib's async gzip runs off the main thread, so the loop keeps ticking", async () => {
const { ms, ticks } = await ticksDuringGzip(); const { ms, ticks } = await ticksDuringGzip();
expect(ms).toBeGreaterThan(50);
expect(ticks).toBeGreaterThan(ms / 4); expect(ticks).toBeGreaterThan(ms / 4);
}); });
}); });
+2 -1
View File
@@ -37,6 +37,7 @@ Deno.test('documented: Deno.serve does not compress by itself (core compression
Deno.test("documented: node:zlib's async gzip runs on Deno's main thread", async () => { Deno.test("documented: node:zlib's async gzip runs on Deno's main thread", async () => {
const { ms, ticks } = await ticksDuringGzip(); const { ms, ticks } = await ticksDuringGzip();
if (ticks > ms / 10) if (ms < 50) throw new Error(`gzip took ${Math.round(ms)} ms — too fast to tell; enlarge the input`);
if (ticks > Math.max(5, ms / 10))
throw new Error(`timers kept firing (${ticks} ticks in ${Math.round(ms)} ms): Deno now offloads zlib — update the docs`); throw new Error(`timers kept firing (${ticks} ticks in ${Math.round(ms)} ms): Deno now offloads zlib — update the docs`);
}); });
+8 -7
View File
@@ -133,15 +133,16 @@ async function sse(port: number): Promise<Record<string, number[]>> {
export const checks = { negotiation, skips, etag, ndjson, sse }; export const checks = { negotiation, skips, etag, ndjson, sse };
/** /**
* How many 1 ms timer ticks fire while `zlib.gzip` (the async API core uses) compresses ~8 MB. When * How many 1 ms timer ticks fire while `zlib.gzip` (the async API core uses) compresses 16 MB of
* the runtime offloads zlib to a worker thread the loop keeps ticking, roughly one tick per ms; * random bytes — incompressible, so the work takes a few hundred ms on any machine (repetitive
* when it runs it on the main thread (Deno 2.9.7, SPIKE-COMPRESSION-B §7) almost none fire. The * input finished in 4 ms on a CI runner and proved nothing). When the runtime offloads zlib to a
* runtime docs state which one each runtime does, and the suites pin it with this. * worker thread the loop keeps ticking, roughly one tick per ms; when it runs it on the main thread
* (Deno 2.9.7, SPIKE-COMPRESSION-B §7) almost none fire. The runtime docs state which one each
* runtime does, and the suites pin it with this.
*/ */
export async function ticksDuringGzip(): Promise<{ ms: number; ticks: number }> { export async function ticksDuringGzip(): Promise<{ ms: number; ticks: number }> {
const bytes = Buffer.alloc(8_000_000); const bytes = new Uint8Array(16 * 1024 * 1024);
let seed = 1; for (let i = 0; i < bytes.length; i += 65536) crypto.getRandomValues(bytes.subarray(i, i + 65536));
for (let i = 0; i < bytes.length; i++) bytes[i] = 97 + ((seed = (seed * 1103515245 + 12345) >>> 0) % 26);
let ticks = 0; let ticks = 0;
const timer = setInterval(() => ticks++, 1); const timer = setInterval(() => ticks++, 1);
const started = performance.now(); const started = performance.now();