fix(stream): preserve byte order when string chunks interrupt decoding

XI
20d ago
+95-9

Both writable stream adapters accept decoded string chunks alongside binary chunks. If a UTF-8 character is incomplete when a nonempty string arrives, the decoder retains the bytes until a later binary chunk. For [0xe2,0x82], "text", [0xac], callbacks therefore emit text€, moving earlier bytes after the string.

Flush pending decoder bytes before nonempty string chunks in the Node and Web adapters. Empty strings retain the decoder state so they do not split a valid fragmented character. Tests pin ordering and the empty-string case for both APIs.

Validation: both ordering regressions fail before the change. npm test passes all 204 tests plus ESLint, TypeScript and Biome; npm run build and git diff --check pass.

Prepared and tested with OpenAI Codex assistance.

Summary by CodeRabbit

  • Bug Fixes
    • Improved UTF-8 handling when string chunks arrive between byte chunks. Incomplete characters are now resolved correctly before non-empty strings, while empty strings preserve buffered bytes so later chunks can complete the character.
    • Corrected byte order mark handling across mixed byte and string chunks: a leading mark remains omitted, while marks appearing later in the stream are preserved. These updates ensure text is decoded consistently regardless of how the stream divides its content into chunks.