Skip to content

fix: stop serializing non-JSON bodies on the hook path - #29

Merged
CodeDredd merged 1 commit into
mainfrom
fix/stream-object-bodies
Nov 24, 2026
Merged

CodeDredd merged 1 commit into
mainfrom
fix/stream-object-bodies

Conversation

@CodeDredd

Copy link
Copy Markdown
Owner

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:

body compress() (buffered) before compressStream() before after
object { a: 1 } ✅ JSON ❌ [object Object] ✅ JSON + Content-Type: application/json on both paths
array [1, 2] ✅ JSON ❌ 1,2 ✅ JSON
node stream (e.g. fs.createReadStream() on h3 v1) ❌ {"_events":{},"_readableState":…} ✅ buffered: left alone · stream: compressed
Blob ❌ {} ✅ buffered: left alone · stream: compressed
Response returned on h3 v1 ❌ {} ❌ [object Response] left alone

The most visible one: the Nuxt module with encoding: 'stream' sent [object Object] for every /server/api route returning an object.

  • JSON is only produced for JSON-like values — objects/arrays that aren't streams, blobs, responses or errors, plus numbers and booleans — matching what h3 itself serializes.
  • On the stream path a node stream is only converted with Readable.toWeb once 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 on main):

  • stream path: objects, arrays and numbers come out as valid JSON with the JSON content type; node streams and blobs are compressed; an uncompressed node stream isn't put into flowing mode; a Response body is untouched;
  • buffered path: node/web streams, blobs, responses and errors are left alone; JSON still works;
  • h3 v1: the Nuxt plugin in stream mode on a JSON API route, and a node stream route through useCompression (untouched) and useCompressionStream (compressed).

Full suite green locally on h3 2.0.8 and 1.15.11.

`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.
@CodeDredd
CodeDredd merged commit bd1ea73 into main Nov 24, 2026
13 checks passed
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