Skip to content

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

Open
feritcemkahraman wants to merge 1 commit into
tinylibs:mainfrom
feritcemkahraman:fix/multibyte-utf8-stream-decoding
Open

feritcemkahraman wants to merge 1 commit into
tinylibs:mainfrom
feritcemkahraman:fix/multibyte-utf8-stream-decoding

Conversation

@feritcemkahraman

Copy link
Copy Markdown

Summary

Fixes #183.

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.

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

1 participant