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:
21 files changed
+1536
-50
No files matched your search
+39
-22
@@ -2,7 +2,7 @@ import type http from 'http';
|
||||
import type https from 'https';
|
||||
import { createRequestGate } from './request-gate';
|
||||
import { renderError, type ErrorRequest } from '../transformers';
|
||||
import type { Bus } from '../bus';
|
||||
import type { Bus, Correlation } from '../bus';
|
||||
import { readBody as readBodyBytes, deriveSecure, deriveIp } from './request';
|
||||
import { pipeStream } from './stream';
|
||||
import { attachWs } from './ws';
|
||||
@@ -22,6 +22,7 @@ import { currentRoutes } from './router';
|
||||
import type { Logger } from '../logger';
|
||||
import { nodeRequire } from '../node-require';
|
||||
import { answerProbe } from './health';
|
||||
import { compressOutcome } from './compression';
|
||||
|
||||
interface HandlerConfig {
|
||||
routes: RouteTable;
|
||||
@@ -183,30 +184,46 @@ function createRequestHandler(cfg: HandlerConfig) {
|
||||
}
|
||||
}
|
||||
|
||||
if ('preflight' in result) {
|
||||
res.writeHead(204, result.preflight);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
if (result.outcome.kind === 'stream') {
|
||||
await pipeStream(
|
||||
res,
|
||||
result.outcome.stream,
|
||||
result.outcome.encoder,
|
||||
bus,
|
||||
result.outcome.route,
|
||||
opts?.streams,
|
||||
correlation,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
res.writeHead(result.outcome.status, result.outcome.headers);
|
||||
res.end(result.outcome.body);
|
||||
await writeResult(req, res, result, { opts, bus, correlation });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes what `handle()` produced: a preflight, or an outcome after `compressOutcome` (CARD-16).
|
||||
* Module scope rather than inside `serve`, whose closure would charge every branch here again.
|
||||
*/
|
||||
async function writeResult(
|
||||
req: http.IncomingMessage,
|
||||
res: http.ServerResponse,
|
||||
result: HandleResult | Preflight,
|
||||
ctx: { opts: HttpOptions | undefined; bus: Bus | undefined; correlation: Correlation },
|
||||
): Promise<void> {
|
||||
const { opts, bus, correlation } = ctx;
|
||||
|
||||
if ('preflight' in result) {
|
||||
res.writeHead(204, result.preflight);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// Not awaited unless compression actually returned a promise: an app without it keeps the
|
||||
// synchronous write it had.
|
||||
const decided = compressOutcome(
|
||||
opts?.compression,
|
||||
{ method: req.method ?? 'GET', headers: req.headers },
|
||||
result.outcome,
|
||||
);
|
||||
const outcome = decided instanceof Promise ? await decided : decided;
|
||||
|
||||
if (outcome.kind === 'stream') {
|
||||
await pipeStream(res, outcome, bus, opts?.streams, correlation);
|
||||
return;
|
||||
}
|
||||
|
||||
res.writeHead(outcome.status, outcome.headers);
|
||||
res.end(outcome.body);
|
||||
}
|
||||
|
||||
/**
|
||||
* Answers a health probe and returns true, or returns false for any other request. Runs before the
|
||||
* gate, so shedding never refuses a probe, and before `handle()`, so no step, plugin or lifecycle
|
||||
|
||||
Reference in new issue
Block a user