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>
43 lines
1.8 KiB
TypeScript
43 lines
1.8 KiB
TypeScript
// CARD-16 (#112) under the real Deno runtime, importing `src/` the way JSR serves it — so `node:zlib`
|
|
// is reached through `nodeRequire`'s `process.getBuiltinModule` path. Deno runs zlib's async API on
|
|
// the main thread (documented limitation); these checks cover correctness, not throughput.
|
|
// Run with: npm run test:deno
|
|
import 'npm:reflect-metadata';
|
|
import { serveDeno } from '../../src/deno.ts';
|
|
import { compressionApp } from '../fixtures/compression-app.ts';
|
|
import { checks, serverDoesNotCompress, ticksDuringGzip } from '../fixtures/compression-checks.ts';
|
|
|
|
for (const [name, run] of Object.entries(checks)) {
|
|
Deno.test(`compression on Deno.serve: ${name}`, async () => {
|
|
const server = await serveDeno(compressionApp({ compression: true, etag: true }), {
|
|
hostname: '127.0.0.1',
|
|
port: 0,
|
|
onListen: () => {},
|
|
});
|
|
|
|
try {
|
|
await run((server as unknown as { addr: { port: number } }).addr.port);
|
|
} finally {
|
|
await server.close({ timeoutMs: 1000 });
|
|
}
|
|
});
|
|
}
|
|
|
|
// The documented Deno facts (CARD-16; `guides/runtimes.md`). If one of these starts failing, Deno
|
|
// changed and the docs have to change with it — not the assertion.
|
|
Deno.test('documented: Deno.serve does not compress by itself (core compression off → identity)', async () => {
|
|
const server = await serveDeno(compressionApp(), { hostname: '127.0.0.1', port: 0, onListen: () => {} });
|
|
|
|
try {
|
|
await serverDoesNotCompress((server as unknown as { addr: { port: number } }).addr.port);
|
|
} finally {
|
|
await server.close({ timeoutMs: 1000 });
|
|
}
|
|
});
|
|
|
|
Deno.test("documented: node:zlib's async gzip runs on Deno's main thread", async () => {
|
|
const { ms, ticks } = await ticksDuringGzip();
|
|
if (ticks > ms / 10)
|
|
throw new Error(`timers kept firing (${ticks} ticks in ${Math.round(ms)} ms): Deno now offloads zlib — update the docs`);
|
|
});
|