Repository navigation
fix: stop serializing non-JSON bodies on the hook path - #29
Merged
Merged
Conversation
`compressStream()` wrapped every body in `new Response(body)`, which turns
objects into `[object Object]` and arrays into `1,2` — so the Nuxt module
with `encoding: 'stream'` sent broken JSON for `/server/api` routes. It now
serializes JSON bodies (and sets the content type, like the buffered path),
streams web / node streams, blobs, strings and bytes as they are, and leaves
anything else alone. A node stream is only converted once it is actually
compressed, so the uncompressed path never starts consuming it.
The buffered path ran `JSON.stringify` on anything that wasn't a string or
bytes, so a node stream returned from an h3 v1 handler was replaced by its
internal state (`{"_events":…}`) and blobs / responses by `{}`. Only
JSON-like values are serialized now; other bodies are skipped.
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.
Found while working on #24 / #25 — no separate issue.
What
The hook path (
useCompression*, the h3 v1 app hook, the Nuxt module) receives whatever the handler returned, and both helpers mishandled some body types:compress()(buffered) beforecompressStream()before{ a: 1 }[object Object]Content-Type: application/jsonon both paths[1, 2]1,2fs.createReadStream()on h3 v1){"_events":{},"_readableState":…}Blob{}Responsereturned on h3 v1{}[object Response]The most visible one: the Nuxt module with
encoding: 'stream'sent[object Object]for every/server/apiroute returning an object.Readable.toWebonce it is actually being compressed: the conversion starts consuming the stream, so the "client doesn't accept it" path must not touch it.Tests
New
test/body-types.test.ts(8 of its h3 v2 tests fail onmain):Responsebody is untouched;useCompression(untouched) anduseCompressionStream(compressed).Full suite green locally on h3 2.0.8 and 1.15.11.