ReviewOS

stacks/bun-router

fix(compression): select the highest-quality supported encoding

#884
Closed chris opened this 8 days ago ·
8 days ago

The response-compression negotiator always selects gzip when it has a positive quality, even if deflate has a higher quality. For example, gzip;q=0.2, deflate;q=0.8 currently produces gzip. Invalid weights such as gzip;q=bogus also become an implicit quality of 1.

This is the upstream selection portion of stacksjs/stacks#2462. RFC 9110 sections 12.4.2 and 12.5.3 specify quality values and preference ordering: https://www.rfc-editor.org/rfc/rfc9110.html#section-12.5.3.

Acceptance for this small chunk:

  • Choose the highest supported nonzero quality; retain gzip before deflate for ties.
  • Respect explicit identity preference and explicit coding weights over wildcard weights.
  • Match complete tokens and parse HTTP quality syntax. Treat malformed named offers as unavailable rather than silently promoting them or falling back to wildcard for that name.
  • Verify known and unknown-length response bytes, and real HTTP generic/native dispatch.
  • Preserve the synchronous response path and measure common-offer CPU against the original parser.

Baseline at 272f696: the added regression cases fail in four groups (32 existing tests pass). Full identity-exclusion enforcement, skipped/small representations, and the Stacks brotli layer remain tracked in stacksjs/stacks#2462 and are outside this selection-only chunk.

8 days ago

Implemented in 12e3789 on main, following the separate public server-options type fix ddfda2c for #885.

The parser selects the highest supported nonzero quality, prefers gzip on ties, honors explicit identity preference and wildcard overrides, and treats malformed named weights as unavailable. It avoids the old per-call Map and parameter arrays.

Validation: four added regression groups failed on 272f696. The candidate passes all 537 runtime tests, including decoded response bytes for known/unknown lengths and native/generic real HTTP dispatch. Both TypeScript projects, full pickier lint, diff checks, and independent Standards/Spec reviews pass.

Local process CPU comparison on Bun 1.3.14/macOS arm64, 8 alternating before/after pairs of 500,000 calls, median milliseconds:

OfferBeforeAfterPairs lower
gzip145.77854.7048/8
gzip, deflate, br408.639160.4798/8
gzip;q=0.2, deflate;q=0.8393.764292.0288/8
gzip;q=0, *;q=0.8370.097253.8768/8

This measures isolated parser CPU, not HTTP throughput or a dedicated-host benchmark. The unchanged absent-header branch also varied (9.392 to 5.157 ms), so it provides no optimization claim. Raw samples and reproduction script are retained locally under /tmp/bun-router-negotiation-20260908/.

Full identity-exclusion handling for small/skipped representations, the Stacks brotli layer, and adoption through a published dependency remain tracked in stacksjs/stacks#2462. No installed dependency was patched and no release was published.

Sign in to comment on this issue.