Files
core/test/edge/compression.ts
T
chrnx 5a494d44a2
CI / dco (pull_request) Successful in 5s
CI / runtimes (bun) (pull_request) Successful in 17s
CI / build (pull_request) Successful in 29s
CI / runtimes (deno) (pull_request) Successful in 22s
CI / runtimes (edge) (pull_request) Successful in 19s
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>
2026-10-23 00:27:43 -05:00

156 lines
5.2 KiB
TypeScript

// CARD-16 (#112) on real workerd via miniflare: T1 (edge), T12, T13, T16 and SSE. Reads raw bytes
// off a real socket with node:http — `dispatchFetch` would decode and hide a double encoding — and
// peels encodings by magic bytes, requiring at most one layer and the original bytes. Run twice:
// with the default compat flags (workerd detected through `navigator.userAgent`) and with
// `no_global_navigator` (no `navigator` at all), so neither signal alone carries the result.
//
// Run with: tsx test/edge/compression.ts (chained from `npm run test:edge`)
import 'reflect-metadata';
import { strict as assert } from 'node:assert';
import * as http from 'node:http';
import * as zlib from 'node:zlib';
import { build } from 'esbuild';
import { Miniflare } from 'miniflare';
import { JSON_BODY, HTML_BODY, SSE_EVENTS, SSE_GAP_MS } from '../fixtures/compression-app';
const BUNDLE = 'test/edge/_compression-worker.mjs';
interface Raw {
status: number;
headers: http.IncomingHttpHeaders;
bytes: Buffer;
}
function get(url: URL, path: string, host: string, ae?: string): Promise<Raw> {
return new Promise((resolve, reject) => {
http
.get(new URL(path, url), { headers: { host, ...(ae ? { 'accept-encoding': ae } : {}) } }, (res) => {
const chunks: Buffer[] = [];
res.on('data', (c) => chunks.push(c));
res.on('end', () => resolve({ status: res.statusCode!, headers: res.headers, bytes: Buffer.concat(chunks) }));
})
.on('error', reject);
});
}
/** Decodes by magic bytes, not by header, so a second layer is counted even if the header lies. */
function peel(bytes: Buffer): { layers: string[]; text: string } {
const layers: string[] = [];
let b = bytes;
for (let k = 0; k < 4; k++) {
if (b[0] === 0x1f && b[1] === 0x8b) {
b = zlib.gunzipSync(b);
layers.push('gzip');
continue;
}
const first = b.toString('utf8', 0, 1);
if (b.length === 0 || first === '{' || first === '<') break;
try {
b = zlib.brotliDecompressSync(b);
layers.push('br');
} catch {
layers.push('??');
break;
}
}
return { layers, text: b.toString('utf8') };
}
function sseLags(url: URL, host: string): Promise<number[]> {
return new Promise((resolve, reject) => {
http
.get(new URL('/c/sse', url), { headers: { host, 'accept-encoding': 'gzip, br', accept: 'text/event-stream' } }, (res) => {
assert.equal(res.headers['content-encoding'], undefined, 'edge SSE: encoded');
const out: number[] = [];
let buf = '';
res.on('data', (d: Buffer) => {
buf += d.toString();
let i;
while ((i = buf.indexOf('\n\n')) >= 0) {
const m = buf.slice(0, i).match(/data: (\{.*\})/);
buf = buf.slice(i + 2);
if (m) out.push(Date.now() - JSON.parse(m[1]).t);
}
});
res.on('end', () => resolve(out));
})
.on('error', reject);
});
}
async function run(flags: string[]): Promise<void> {
const mf = new Miniflare({
modules: true,
scriptPath: BUNDLE,
compatibilityDate: '2024-11-01',
compatibilityFlags: flags,
host: '127.0.0.1',
port: 0,
});
const url = await mf.ready;
try {
for (const host of ['handler', 'bare']) {
for (const [path, original] of [
['/c/json', JSON_BODY],
['/c/html', HTML_BODY],
] as const) {
for (const ae of ['gzip, br', 'gzip', 'br', 'br;q=0, gzip', 'identity', undefined]) {
const label = `[${flags.join(',')}] ${host} ${path} ${ae ?? '(none)'}`;
const r = await get(url, path, host, ae);
const { layers, text } = peel(r.bytes);
assert.equal(r.status, 200, `${label}: status ${r.status}`);
assert.ok(layers.length <= 1, `${label}: ${layers.join('+')} — doubly encoded`);
assert.equal(text, original, `${label}: body mismatch`);
assert.equal(r.headers['content-encoding'], layers[0], `${label}: header does not match the payload`);
assert.match(String(r.headers.vary), /accept-encoding/i, `${label}: no Vary`);
if (!ae || ae === 'identity') assert.equal(layers.length, 0, `${label}: identity client got an encoding`);
}
}
const lags = await sseLags(url, host);
assert.equal(lags.length, SSE_EVENTS, `${host}: SSE events`);
for (const lag of lags) assert.ok(lag < SSE_GAP_MS / 2, `${host}: SSE lag ${lag} ms`);
}
} finally {
await mf.dispose();
}
}
async function main() {
await build({
entryPoints: ['test/edge/compression-worker.ts'],
bundle: true,
format: 'esm',
outfile: BUNDLE,
platform: 'browser',
target: 'esnext',
external: ['node:*', 'http', 'https'],
alias: {
crypto: 'node:crypto',
stream: 'node:stream',
zlib: 'node:zlib',
buffer: 'node:buffer',
events: 'node:events',
net: 'node:net',
tls: 'node:tls',
util: 'node:util',
},
});
await run(['nodejs_compat']);
await run(['nodejs_compat', 'no_global_navigator']);
console.log(
'edge (workerd): compression — edgeHandler and bare app.fetch, with and without navigator: identity from core, ≤ 1 layer on the wire, Vary set, SSE on time — PASS',
);
}
main().catch((err) => {
console.error(err);
process.exit(1);
});