Repository navigation
fix: skip already-encoded responses and drop stale Content-Length - #26
Merged
Merged
Conversation
`compress()` and `compressStream()` compressed responses that already carry a `Content-Encoding` (e.g. pre-built `.br` assets served by Nitro's static handler with `compressPublicAssets`) a second time, and kept the `Content-Length` of the uncompressed body. Browsers then decoded only one layer and the announced length no longer matched the bytes on the wire. Both now bail out on an existing (non-identity) `Content-Encoding` — checked on the event and on the `render:response` header record — and remove the stale `Content-Length` whenever they set one, mirroring what the h3 v2 middleware path already did. `removeResponseHeader` only exists since h3 1.8, so it is read through the runtime key like `send` / `toResponse`, with a `node.res` fallback. Closes #24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #24
What
compress()andcompressStream()— the functions behinduseCompression*, the h3 v1 app hook and the Nuxt module — now behave like the h3 v2 middleware path already did:Content-EncodingContent-LengthcompressResponse()/compressResponseStream()compress()compressStream()Content-Encodingother thanidentity— e.g. a pre-built.brasset served by Nitro's static handler withcompressPublicAssets: true— nothing is compressed, re-wrapped or re-headered. The check looks at the event's response headers and at the header record arender:responsehook hands over (Nitro only applies that record after the hook ran).Content-Lengthis dropped whenever we setContent-Encoding, again on both the event and the hook record. The runtime recomputes it (buffered path) or switches to chunked framing (stream path). This is a framing fix on its own, independent of (1).h3 1.6 / 1.7 compatibility
removeResponseHeaderonly exists since h3 1.8, but the peer range starts at^1.6.0. It is therefore read through the same runtime-key indirection assend/toResponse(so re-bundlers don't emit a static named import), with anode.res.removeHeaderfallback. Verified manually against h3 1.7.1; the downstream-bundling test stub now also omitsremoveResponseHeaderand asserts no missing-export warning for it.Tests
New
test/already-encoded.test.ts:mockEvent):compress,compressStream,useCompression,useCompressionStreamand the Nuxt plugin leave a pre-compressed asset untouched;Content-Lengthis removed from the event and from therender:responserecord when compressing and kept when nothing is compressed;Content-Encoding: identitystill compresses; both middlewares pass an encodedResponsethrough.fetch/superagent decode one layer and would hide the bug. Asserts the announcedContent-Lengthequals the bytes on the wire and that a single decode yields the original.All 15 new assertions-bearing tests fail on
mainand pass here, on both h3 v1 (1.8.1) and v2.