Skip to content

fix(compress): do not compress 206 Partial Content responses#5020

Open
dr4gan0x wants to merge 1 commit into
honojs:mainfrom
dr4gan0x:fix/compress-skip-206
Open

fix(compress): do not compress 206 Partial Content responses#5020
dr4gan0x wants to merge 1 commit into
honojs:mainfrom
dr4gan0x:fix/compress-skip-206

Conversation

@dr4gan0x

@dr4gan0x dr4gan0x commented Jun 11, 2026

Copy link
Copy Markdown

This addresses the second of the three problems reported in #5010 (compress encodes 206 Partial Content).

The middleware bails out for already-encoded, chunked, HEAD, small, non-compressible and no-transform responses, but it never checks the status code. A 206 ends up gzipped while its Content-Range header still refers to the uncompressed byte range, and the Content-Length that matched the partial body gets dropped. A range client stitching parts together from such responses gets corrupted data. This is easy to hit with a handler that does its own range handling, or when proxying an upstream that serves ranges, since the proxy helper passes the upstream status through.

The fix skips compression when the status is 206.

The new test serves a 1024 byte text/plain part with Content-Range and asserts it comes back uncompressed with status, Content-Length and Content-Range intact. It fails without the guard.

The author should do the following, if applicable

  • Add tests
  • Run tests
  • bun run format:fix && bun run lint:fix to format the code
  • Add TSDoc/JSDoc to document the code

compress() never looked at the response status, so a 206 was gzipped
while its Content-Range header still described the uncompressed byte
range, and the Content-Length matching the partial body was dropped.
Range clients reassembling parts from such responses end up with
corrupted data.

Skip compression when the status is 206. Reported as the second issue
in honojs#5010.
@codecov

codecov Bot commented Jun 11, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.01%. Comparing base (e50df01) to head (a148c1d).
⚠️ Report is 11 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5020   +/-   ##
=======================================
  Coverage   79.01%   79.01%           
=======================================
  Files         154      154           
  Lines       10578    10579    +1     
  Branches     2215     2216    +1     
=======================================
+ Hits         8358     8359    +1     
  Misses       2220     2220           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant