Skip to content

fix: decode stream chunks with StringDecoder to prevent multibyte corruption - #186

Merged
43081j merged 2 commits into
tinylibs:mainfrom
feritcemkahraman:fix/multibyte-utf8-stream-decoding
Nov 24, 2026
Merged

43081j merged 2 commits into
tinylibs:mainfrom
feritcemkahraman:fix/multibyte-utf8-stream-decoding

Conversation

@feritcemkahraman

@feritcemkahraman feritcemkahraman commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #183.
Closes #185.

In readStream, buffer chunks were accumulated using output += chunk.toString(). When a multibyte UTF-8 character (e.g. Cyrillic, CJK, emoji) is split across stream chunk boundaries, decoding each chunk individually causes incomplete byte sequences to be replaced by the Unicode replacement character (\uFFFD).

This PR uses Node.js's standard StringDecoder from node:string_decoder to buffer partial multibyte sequences across stream chunks and flush any remaining bytes on stream end.

Changes

  • Use StringDecoder in readStream() to safely decode chunks.
  • Add test in src/test/main_test.ts verifying that multibyte UTF-8 characters split across stream writes are decoded without corruption.

@43081j
43081j merged commit 254e7b1 into tinylibs: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.

readStream decodes each chunk separately — multibyte UTF-8 characters split across chunks become U+FFFD

2 participants