diff --git a/CHANGELOG.md b/CHANGELOG.md index 7ee823b..448564c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -163,7 +163,10 @@ npm treats versions as semver and semver forbids leading zeros. 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`. + decided the outcome: identity clients, `HEAD` and every `304`. A body under the threshold gets + none: it is sent identity whatever the client accepts, and Deno pays about 10% of a small request + for the header (about 3% on Bun). On the edge, where Cloudflare encodes, every compressible + response keeps `Vary`. `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: diff --git a/src/http/compression.ts b/src/http/compression.ts index b9d4bdb..692e1ce 100644 --- a/src/http/compression.ts +++ b/src/http/compression.ts @@ -195,7 +195,8 @@ type Headers = Record; /** * Whether the response's representation could vary by `Accept-Encoding` at all. When it cannot — * already encoded, a range, no or an incompressible type, `no-transform` — core neither encodes - * nor adds `Vary`. Request-side reasons (HEAD, the client's header) do not appear here: those + * nor adds `Vary`. A buffered body under the threshold is the same case, decided in + * {@link compressBuffered} once the size is known. Request-side reasons (HEAD, the client's header) do not appear here: those * responses still get `Vary`, so a shared cache keys them correctly. */ function eligible(headers: Headers): boolean { @@ -312,15 +313,20 @@ function compressBuffered( if (status === 304) return { ...outcome, headers: withVary(headers) }; if (status === 204 || !eligible(headers)) return outcome; - const vary = { ...outcome, headers: withVary(headers) }; - if (status < 200 || req.method === 'HEAD' || !cfg.encode) return vary; + const vary = (): Buffered => ({ ...outcome, headers: withVary(headers) }); + if (status < 200 || req.method === 'HEAD' || !cfg.encode) return vary(); - const bytes = typeof body === 'string' ? Buffer.from(body) : body; - if (bytes.byteLength < cfg.threshold) return vary; + // Under the threshold the body is sent identity whatever the client accepts, so `Accept-Encoding` + // played no part and there is no `Vary` to send (RFC 9110 §12.5.5). A cache that stores this + // response for everyone is right to: every client accepts identity. Skipping it matters on Deno, + // which pays ~10% of a small request for a `Vary` header. Measured without allocating the bytes. + const size = typeof body === 'string' ? Buffer.byteLength(body) : body.byteLength; + if (size < cfg.threshold) return outcome; const encoding = negotiate(acceptOf(req), cfg.encodings); - if (!encoding) return vary; + if (!encoding) return vary(); + const bytes = typeof body === 'string' ? Buffer.from(body) : body; return encodeBody(bytes, encoding).then((encoded) => ({ ...outcome, headers: encodedHeaders(headers, encoding), diff --git a/test/compression.test.ts b/test/compression.test.ts index 8dd59ce..581909e 100644 --- a/test/compression.test.ts +++ b/test/compression.test.ts @@ -135,7 +135,7 @@ for (const adapter of ['node', 'fetch'] as const) { }); it.each([ - ['/c/small', 'T7 under threshold', true], + ['/c/small', 'T7 under threshold', false], ['/c/png', 'T8 image/png', false], ['/c/notype', 'T8 missing content-type', false], ['/c/range', 'T8 content-range', false], diff --git a/test/fixtures/compression-checks.ts b/test/fixtures/compression-checks.ts index fb89e1a..d65206a 100644 --- a/test/fixtures/compression-checks.ts +++ b/test/fixtures/compression-checks.ts @@ -60,7 +60,7 @@ async function negotiation(port: number): Promise { /** T7, T8, T11, T15: skips, and the 304 shape. */ async function skips(port: number): Promise { for (const [path, vary] of [ - ['/c/small', true], + ['/c/small', false], ['/c/png', false], ['/c/notype', false], ['/c/range', false],