ReviewOS

stacks/bun-router

fix(compression): respect identity refusal for unencoded responses

#886
Closed chris opened this 9 days ago ·
9 days ago

Follow-up to #884 and stacksjs/stacks#2462. On 12e3789, the response helper returns an unencoded 200 body for Accept-Encoding: gzip, identity;q=0 when the body is below the compression threshold. The parser's public null result also conflates permitted identity with no acceptable representation.

Reproduction from the repository root:

import { applyResponseCompression } from './packages/bun-router/src/response/compression'
const request = new Request('http://localhost/', {
  headers: { 'accept-encoding': 'gzip, identity;q=0' },
})
const response = await applyResponseCompression(new Response('small', {
  headers: { 'content-type': 'text/plain', 'content-length': '5' },
}), request)
console.log(response.status, response.headers.get('content-encoding'), await response.text())
// 200 null small

RFC 9110 section 12.5.3 excludes identity when explicitly refused or through *;q=0 without a more specific identity entry: https://www.rfc-editor.org/rfc/rfc9110.html#section-12.5.3.

Implement the missing unencoded-response behavior at the existing response compression boundary:

  • Preserve whether identity is acceptable separately from the selected gzip/deflate coding, while keeping the public negotiateEncoding return type compatible.
  • Compress known-small and unknown-small text responses when identity is refused and a supported coding is acceptable. Size is a heuristic, not permission to send refused content.
  • Return an empty 406 when no available representation is acceptable, including types deliberately excluded from compression and range responses. Do not consume or buffer an excluded stream; cancel an abandoned body.
  • Preserve bodyless responses, already-encoded responses, and explicit compression-disable behavior. Those remain owned by their existing caller contracts in this bounded helper change. The broader Stacks pipeline issue remains open.
  • Preserve security headers, cookies and request IDs on a 406, but remove obsolete representation metadata and prevent caching an empty replacement using the original response's cache policy.
  • Cover wildcard precedence, malformed qualities, exact tokens, synchronous common paths, decoded bytes, and real native/generic HTTP dispatch.
  • Measure the normal short-body path against the existing source and keep its no-body-materialization behavior.

This is part of making performance comparisons contract-correct. No benchmark configuration changes or installed-dependency patches.

9 days ago

Fixed in 006c24e, pushed directly to main.

The response negotiator retains identity acceptability separately from the compatible public encoding result. Required gzip/deflate now applies to known-small and unknown-small text bodies, without threshold peeking. If no available representation is acceptable, the helper cancels the abandoned body and returns an empty 406. This preserves security headers, cookies and correlation IDs while removing stale representation metadata and replacing the old cache policy with no-store. Explicit disable options, bodyless responses and caller-owned encoded responses retain their existing contract.

Validation: the new regression suite first failed three cases on 12e3789. Final validation passes all 545 runtime tests / 1,665 assertions, both TypeScript projects, pickier lint and separate Standards/Spec reviews. Tests include actual native and generic HTTP servers, decoded gzip/deflate bytes, exact-token and weighted/wildcard cases, excluded streams, metadata and cancellation failures.

Local CPU comparison: 1.3.14 / darwin / arm64, baseline 12e3789 versus final runtime implementation. Twelve alternating pairs per case, 20,000 warmup calls, 500,000 responses per sample. Response construction and real helper invocation are both in the timed loop; each uses the same fixed private Headers init. GC occurs outside timing, automatic collection inside is included in process user + system CPU. Test/type/lint processes had finished before this final comparison. This shared Mac experiment is not HTTP throughput or a Linux ranking.

Median CPU milliseconds; ratio is the median paired candidate/baseline ratio:

CaseBaselineCandidatePaired ratioLower CPU pairs
common-short350.053322.9800.92812/12
weighted-short354.048334.9920.94511/12
no-offer369.501351.1540.93811/12
binary371.617426.8991.2041/12

Short text paths use about 5-7% less CPU here because absent offers and offers that cannot exclude identity avoid unnecessary content-type checks and full negotiation. Binary responses cost more: they previously escaped before Vary was added, but identity refusal can now change their response to 406, so the cache must account for Accept-Encoding. This correctness cost is retained and reported, not hidden as a blanket performance gain.

The broader work stays open at stacksjs/stacks#2462: Stacks' brotli layer and its own small-body shortcuts still need the same contract, and upstream changes must be adopted through a published dependency. No installed dependencies were modified or published in this change.

Reproducible local comparison

Save compression.ts from baseline 12e3789 next to this harness as baseline.ts, point the candidate import to the desired checkout, and run with Bun from the bun-router root.

import { applyResponseCompression as baseline } from './baseline'
import { applyResponseCompression as candidate } from '/Users/chris/Code/Libraries/bun-router/packages/bun-router/src/response/compression'
const count = 500000
const results = []
let sink = 0
for (const [name, offer, type] of [
  ['common-short', 'gzip, deflate, br', 'text/plain'],
  ['weighted-short', 'gzip;q=0.5, deflate;q=0.8', 'text/plain'],
  ['no-offer', '', 'text/plain'],
  ['binary', 'gzip, deflate, br', 'image/png'],
] as const) {
  const request = new Request('http://localhost/', { headers: { 'accept-encoding': offer } })
  const init = { headers: new Headers({ 'content-type': type, 'content-length': '5' }) }
  const variants = { baseline, candidate }
  for (const fn of Object.values(variants)) {
    for (let i = 0; i < 20000; i++) {
      const response = fn(new Response('small', init), request)
      if (!(response instanceof Response)) throw new Error('short path must stay synchronous')
      sink += response.status
    }
  }
  for (let round = 0; round < 12; round++) {
    const names = Object.keys(variants) as (keyof typeof variants)[]
    if (round % 2) names.reverse()
    for (const variant of names) {
      const fn = variants[variant]
      Bun.gc(true)
      const start = performance.now()
      const cpu = process.cpuUsage()
      for (let i = 0; i < count; i++) sink += (fn(new Response('small', init), request) as Response).status
      const used = process.cpuUsage(cpu)
      results.push({ name, variant, round, cpuMs: (used.user + used.system) / 1000, wallMs: performance.now() - start })
    }
  }
}
console.log(JSON.stringify({ count, bun: Bun.version, platform: process.platform, arch: process.arch, sink, results }, null, 2))
All final raw CPU and wall-time samples
{
  "count": 500000,
  "bun": "1.3.14",
  "platform": "darwin",
  "arch": "arm64",
  "sink": 9632000000,
  "results": [
    {
      "name": "common-short",
      "variant": "baseline",
      "round": 0,
      "cpuMs": 331.433,
      "wallMs": 331.071458
    },
    {
      "name": "common-short",
      "variant": "candidate",
      "round": 0,
      "cpuMs": 330.258,
      "wallMs": 333.114709
    },
    {
      "name": "common-short",
      "variant": "candidate",
      "round": 1,
      "cpuMs": 321.815,
      "wallMs": 324.4084170000001
    },
    {
      "name": "common-short",
      "variant": "baseline",
      "round": 1,
      "cpuMs": 346.565,
      "wallMs": 349.85512500000004
    },
    {
      "name": "common-short",
      "variant": "baseline",
      "round": 2,
      "cpuMs": 361.336,
      "wallMs": 364.6501249999999
    },
    {
      "name": "common-short",
      "variant": "candidate",
      "round": 2,
      "cpuMs": 319.671,
      "wallMs": 320.98145899999986
    },
    {
      "name": "common-short",
      "variant": "candidate",
      "round": 3,
      "cpuMs": 314.958,
      "wallMs": 315.5220410000002
    },
    {
      "name": "common-short",
      "variant": "baseline",
      "round": 3,
      "cpuMs": 346.563,
      "wallMs": 352.1222090000001
    },
    {
      "name": "common-short",
      "variant": "baseline",
      "round": 4,
      "cpuMs": 353.54,
      "wallMs": 355.97120799999993
    },
    {
      "name": "common-short",
      "variant": "candidate",
      "round": 4,
      "cpuMs": 346.963,
      "wallMs": 350.3024999999998
    },
    {
      "name": "common-short",
      "variant": "candidate",
      "round": 5,
      "cpuMs": 332.216,
      "wallMs": 336.3041659999999
    },
    {
      "name": "common-short",
      "variant": "baseline",
      "round": 5,
      "cpuMs": 358.557,
      "wallMs": 357.2630829999998
    },
    {
      "name": "common-short",
      "variant": "baseline",
      "round": 6,
      "cpuMs": 360.955,
      "wallMs": 361.6293340000002
    },
    {
      "name": "common-short",
      "variant": "candidate",
      "round": 6,
      "cpuMs": 355.124,
      "wallMs": 371.62041599999975
    },
    {
      "name": "common-short",
      "variant": "candidate",
      "round": 7,
      "cpuMs": 354.63,
      "wallMs": 382.54233399999976
    },
    {
      "name": "common-short",
      "variant": "baseline",
      "round": 7,
      "cpuMs": 394.362,
      "wallMs": 458.2005829999998
    },
    {
      "name": "common-short",
      "variant": "baseline",
      "round": 8,
      "cpuMs": 355.285,
      "wallMs": 356.85683300000073
    },
    {
      "name": "common-short",
      "variant": "candidate",
      "round": 8,
      "cpuMs": 318.002,
      "wallMs": 321.7868330000001
    },
    {
      "name": "common-short",
      "variant": "candidate",
      "round": 9,
      "cpuMs": 311.528,
      "wallMs": 315.5435000000007
    },
    {
      "name": "common-short",
      "variant": "baseline",
      "round": 9,
      "cpuMs": 341.894,
      "wallMs": 341.52587499999936
    },
    {
      "name": "common-short",
      "variant": "baseline",
      "round": 10,
      "cpuMs": 339.83,
      "wallMs": 340.1721669999997
    },
    {
      "name": "common-short",
      "variant": "candidate",
      "round": 10,
      "cpuMs": 321.655,
      "wallMs": 328.02287499999966
    },
    {
      "name": "common-short",
      "variant": "candidate",
      "round": 11,
      "cpuMs": 324.145,
      "wallMs": 324.3104999999996
    },
    {
      "name": "common-short",
      "variant": "baseline",
      "round": 11,
      "cpuMs": 345.046,
      "wallMs": 343.82716600000094
    },
    {
      "name": "weighted-short",
      "variant": "baseline",
      "round": 0,
      "cpuMs": 359.222,
      "wallMs": 361.4909580000003
    },
    {
      "name": "weighted-short",
      "variant": "candidate",
      "round": 0,
      "cpuMs": 356.32,
      "wallMs": 350.545791999999
    },
    {
      "name": "weighted-short",
      "variant": "candidate",
      "round": 1,
      "cpuMs": 340.257,
      "wallMs": 340.01870899999994
    },
    {
      "name": "weighted-short",
      "variant": "baseline",
      "round": 1,
      "cpuMs": 342.935,
      "wallMs": 340.23616700000093
    },
    {
      "name": "weighted-short",
      "variant": "baseline",
      "round": 2,
      "cpuMs": 345.155,
      "wallMs": 339.21441699999923
    },
    {
      "name": "weighted-short",
      "variant": "candidate",
      "round": 2,
      "cpuMs": 348.233,
      "wallMs": 346.9566670000004
    },
    {
      "name": "weighted-short",
      "variant": "candidate",
      "round": 3,
      "cpuMs": 350.389,
      "wallMs": 353.1140829999986
    },
    {
      "name": "weighted-short",
      "variant": "baseline",
      "round": 3,
      "cpuMs": 376.869,
      "wallMs": 381.5049169999984
    },
    {
      "name": "weighted-short",
      "variant": "baseline",
      "round": 4,
      "cpuMs": 354.703,
      "wallMs": 352.3499580000007
    },
    {
      "name": "weighted-short",
      "variant": "candidate",
      "round": 4,
      "cpuMs": 326.872,
      "wallMs": 324.6263749999998
    },
    {
      "name": "weighted-short",
      "variant": "candidate",
      "round": 5,
      "cpuMs": 327.67,
      "wallMs": 324.12616700000035
    },
    {
      "name": "weighted-short",
      "variant": "baseline",
      "round": 5,
      "cpuMs": 350.416,
      "wallMs": 351.4086670000015
    },
    {
      "name": "weighted-short",
      "variant": "baseline",
      "round": 6,
      "cpuMs": 363.75,
      "wallMs": 367.1202919999996
    },
    {
      "name": "weighted-short",
      "variant": "candidate",
      "round": 6,
      "cpuMs": 334.532,
      "wallMs": 334.00845899999877
    },
    {
      "name": "weighted-short",
      "variant": "candidate",
      "round": 7,
      "cpuMs": 335.452,
      "wallMs": 331.25724999999875
    },
    {
      "name": "weighted-short",
      "variant": "baseline",
      "round": 7,
      "cpuMs": 353.393,
      "wallMs": 358.7801249999993
    },
    {
      "name": "weighted-short",
      "variant": "baseline",
      "round": 8,
      "cpuMs": 358.512,
      "wallMs": 356.3393330000017
    },
    {
      "name": "weighted-short",
      "variant": "candidate",
      "round": 8,
      "cpuMs": 331.721,
      "wallMs": 330.51695799999834
    },
    {
      "name": "weighted-short",
      "variant": "candidate",
      "round": 9,
      "cpuMs": 359.417,
      "wallMs": 366.63616700000057
    },
    {
      "name": "weighted-short",
      "variant": "baseline",
      "round": 9,
      "cpuMs": 360.295,
      "wallMs": 364.3167080000003
    },
    {
      "name": "weighted-short",
      "variant": "baseline",
      "round": 10,
      "cpuMs": 344.621,
      "wallMs": 347.2048340000001
    },
    {
      "name": "weighted-short",
      "variant": "candidate",
      "round": 10,
      "cpuMs": 324.371,
      "wallMs": 319.38624999999956
    },
    {
      "name": "weighted-short",
      "variant": "candidate",
      "round": 11,
      "cpuMs": 332.72,
      "wallMs": 333.4160000000011
    },
    {
      "name": "weighted-short",
      "variant": "baseline",
      "round": 11,
      "cpuMs": 350.387,
      "wallMs": 350.0304169999981
    },
    {
      "name": "no-offer",
      "variant": "baseline",
      "round": 0,
      "cpuMs": 349.808,
      "wallMs": 347.9463340000002
    },
    {
      "name": "no-offer",
      "variant": "candidate",
      "round": 0,
      "cpuMs": 306.016,
      "wallMs": 305.50208399999974
    },
    {
      "name": "no-offer",
      "variant": "candidate",
      "round": 1,
      "cpuMs": 307.807,
      "wallMs": 311.36741600000096
    },
    {
      "name": "no-offer",
      "variant": "baseline",
      "round": 1,
      "cpuMs": 371.285,
      "wallMs": 395.520625000001
    },
    {
      "name": "no-offer",
      "variant": "baseline",
      "round": 2,
      "cpuMs": 400.712,
      "wallMs": 451.12225000000035
    },
    {
      "name": "no-offer",
      "variant": "candidate",
      "round": 2,
      "cpuMs": 337.522,
      "wallMs": 365.8603750000002
    },
    {
      "name": "no-offer",
      "variant": "candidate",
      "round": 3,
      "cpuMs": 337.001,
      "wallMs": 342.6889999999985
    },
    {
      "name": "no-offer",
      "variant": "baseline",
      "round": 3,
      "cpuMs": 344.543,
      "wallMs": 344.75241599999936
    },
    {
      "name": "no-offer",
      "variant": "baseline",
      "round": 4,
      "cpuMs": 350.873,
      "wallMs": 358.1555840000001
    },
    {
      "name": "no-offer",
      "variant": "candidate",
      "round": 4,
      "cpuMs": 325.788,
      "wallMs": 327.60783399999855
    },
    {
      "name": "no-offer",
      "variant": "candidate",
      "round": 5,
      "cpuMs": 353.003,
      "wallMs": 370.2268750000003
    },
    {
      "name": "no-offer",
      "variant": "baseline",
      "round": 5,
      "cpuMs": 365.105,
      "wallMs": 389.4091249999983
    },
    {
      "name": "no-offer",
      "variant": "baseline",
      "round": 6,
      "cpuMs": 360.998,
      "wallMs": 383.5465829999994
    },
    {
      "name": "no-offer",
      "variant": "candidate",
      "round": 6,
      "cpuMs": 367.757,
      "wallMs": 397.07750000000306
    },
    {
      "name": "no-offer",
      "variant": "candidate",
      "round": 7,
      "cpuMs": 349.305,
      "wallMs": 370.67095899999913
    },
    {
      "name": "no-offer",
      "variant": "baseline",
      "round": 7,
      "cpuMs": 368.327,
      "wallMs": 390.0094169999975
    },
    {
      "name": "no-offer",
      "variant": "baseline",
      "round": 8,
      "cpuMs": 425.217,
      "wallMs": 498.94945799999914
    },
    {
      "name": "no-offer",
      "variant": "candidate",
      "round": 8,
      "cpuMs": 422.386,
      "wallMs": 497.7685420000016
    },
    {
      "name": "no-offer",
      "variant": "candidate",
      "round": 9,
      "cpuMs": 367.854,
      "wallMs": 406.3100000000013
    },
    {
      "name": "no-offer",
      "variant": "baseline",
      "round": 9,
      "cpuMs": 455.192,
      "wallMs": 534.0952910000015
    },
    {
      "name": "no-offer",
      "variant": "baseline",
      "round": 10,
      "cpuMs": 453.176,
      "wallMs": 530.6375829999997
    },
    {
      "name": "no-offer",
      "variant": "candidate",
      "round": 10,
      "cpuMs": 383.396,
      "wallMs": 426.25187500000175
    },
    {
      "name": "no-offer",
      "variant": "candidate",
      "round": 11,
      "cpuMs": 366.162,
      "wallMs": 395.41733300000124
    },
    {
      "name": "no-offer",
      "variant": "baseline",
      "round": 11,
      "cpuMs": 370.676,
      "wallMs": 409.591124999999
    },
    {
      "name": "binary",
      "variant": "baseline",
      "round": 0,
      "cpuMs": 336.397,
      "wallMs": 421.61933399999907
    },
    {
      "name": "binary",
      "variant": "candidate",
      "round": 0,
      "cpuMs": 438.894,
      "wallMs": 514.0989579999987
    },
    {
      "name": "binary",
      "variant": "candidate",
      "round": 1,
      "cpuMs": 416.121,
      "wallMs": 494.33420900000056
    },
    {
      "name": "binary",
      "variant": "baseline",
      "round": 1,
      "cpuMs": 389.533,
      "wallMs": 473.17083400000047
    },
    {
      "name": "binary",
      "variant": "baseline",
      "round": 2,
      "cpuMs": 393.171,
      "wallMs": 480.1976250000007
    },
    {
      "name": "binary",
      "variant": "candidate",
      "round": 2,
      "cpuMs": 385.239,
      "wallMs": 429.1703750000015
    },
    {
      "name": "binary",
      "variant": "candidate",
      "round": 3,
      "cpuMs": 389.368,
      "wallMs": 435.86041599999953
    },
    {
      "name": "binary",
      "variant": "baseline",
      "round": 3,
      "cpuMs": 323.088,
      "wallMs": 358.057249999998
    },
    {
      "name": "binary",
      "variant": "baseline",
      "round": 4,
      "cpuMs": 328.44,
      "wallMs": 379.1245000000017
    },
    {
      "name": "binary",
      "variant": "candidate",
      "round": 4,
      "cpuMs": 424.309,
      "wallMs": 531.422208
    },
    {
      "name": "binary",
      "variant": "candidate",
      "round": 5,
      "cpuMs": 482.311,
      "wallMs": 574.9774999999972
    },
    {
      "name": "binary",
      "variant": "baseline",
      "round": 5,
      "cpuMs": 400.798,
      "wallMs": 475.5156670000033
    },
    {
      "name": "binary",
      "variant": "baseline",
      "round": 6,
      "cpuMs": 386.419,
      "wallMs": 482.1393750000025
    },
    {
      "name": "binary",
      "variant": "candidate",
      "round": 6,
      "cpuMs": 480.704,
      "wallMs": 556.7410419999942
    },
    {
      "name": "binary",
      "variant": "candidate",
      "round": 7,
      "cpuMs": 447.847,
      "wallMs": 537.6548340000008
    },
    {
      "name": "binary",
      "variant": "baseline",
      "round": 7,
      "cpuMs": 379.009,
      "wallMs": 453.5047080000004
    },
    {
      "name": "binary",
      "variant": "baseline",
      "round": 8,
      "cpuMs": 341.203,
      "wallMs": 403.3957079999964
    },
    {
      "name": "binary",
      "variant": "candidate",
      "round": 8,
      "cpuMs": 421.36,
      "wallMs": 477.2936250000057
    },
    {
      "name": "binary",
      "variant": "candidate",
      "round": 9,
      "cpuMs": 429.488,
      "wallMs": 500.13408300000447
    },
    {
      "name": "binary",
      "variant": "baseline",
      "round": 9,
      "cpuMs": 378.542,
      "wallMs": 444.8704579999976
    },
    {
      "name": "binary",
      "variant": "baseline",
      "round": 10,
      "cpuMs": 328.752,
      "wallMs": 380.54274999999325
    },
    {
      "name": "binary",
      "variant": "candidate",
      "round": 10,
      "cpuMs": 431.039,
      "wallMs": 472.3733330000032
    },
    {
      "name": "binary",
      "variant": "candidate",
      "round": 11,
      "cpuMs": 419.474,
      "wallMs": 479.21845799999574
    },
    {
      "name": "binary",
      "variant": "baseline",
      "round": 11,
      "cpuMs": 364.693,
      "wallMs": 430.7404170000009
    }
  ]
}

Sign in to comment on this issue.