fix: keep the default response encoding stable across Node.js versions#403
Open
rxits wants to merge 1 commit into
Open
fix: keep the default response encoding stable across Node.js versions#403rxits wants to merge 1 commit into
rxits wants to merge 1 commit into
Conversation
zstd was unshifted to the front of the supported-encodings list whenever the runtime exposed zlib.createZstdCompress (Node.js >= 22.15), which made it the negotiated default. The same app therefore switched its default compression from br to zstd purely by upgrading Node, breaking consumers that don't expect zstd. Insert zstd before identity instead of at the front, so br stays the negotiated default while zstd remains available when a client explicitly prefers it. Applied to both the compress and decompress encoding lists. Fixes fastify#369
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.
Fixes #369
Regression
Since v8.1.0, the default response compression changes based on the Node.js version: under Node.js < 22.15 the default is
br, but under Node.js ≥ 22.15 (wherezlib.createZstdCompressexists) the same app defaults tozstd. Upgrading Node silently changes the wire format, which breaks consumers that don't expect/recognizezstd.Cause
processCompressParams/processDecompressParamsbuild the supported-encodings list and thenunshift('zstd')to the front whenever the runtime supports it:@fastify/accept-negotiatoruses this order to break ties, so a normal client sendingAccept-Encoding: gzip, deflate, br, zstdnow negotiateszstdinstead ofbr.Fix
Insert
zstdbeforeidentityrather than at the front, sobrstays the negotiated default (matching pre-8.1.0 behavior) whilezstdis still offered when a client explicitly prefers it.identityremains last. Applied to both the compress and decompress encoding lists.Tests
Added a test asserting that a client accepting
gzip, deflate, br, zstdgetsbr(notzstd) on a zstd-capable runtime. Verified failing before the fix (expected 'br', actual 'zstd') and passing after. Full unit suite: 174/174 pass, lint clean. The existing explicit-zstdand wildcard (*→ gzip) tests are unaffected.