Skip to content

stream: handle setEncoding after buffered data#63973

Open
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:fix-readable-setencoding-buffered
Open

stream: handle setEncoding after buffered data#63973
mcollina wants to merge 1 commit into
nodejs:mainfrom
mcollina:fix-readable-setencoding-buffered

Conversation

@mcollina

@mcollina mcollina commented Jun 18, 2026

Copy link
Copy Markdown
Member

Fixes Readable.prototype.setEncoding() when it is called after byte data has already been buffered but before that data is consumed.

This addresses the Node.js core stream behavior behind nodejs/undici#5002: if setEncoding('utf8') is applied after bytes are buffered, split multibyte UTF-8 sequences must be handed to a single StringDecoder so consumers do not see U+FFFD corruption at chunk boundaries.

When the buffered data ends with an incomplete multibyte sequence and the stream has ended, the new decoder needs to be finalized so it can emit the replacement character instead of dropping the bytes.

Tests cover:

  • calling setEncoding() after split multibyte data is buffered
  • calling it before the stream starts flowing after _read() buffered the first chunk
  • calling it after EOF with an incomplete buffered UTF-8 sequence

Refs: #63570
Refs: nodejs/undici#5002

Signed-off-by: Matteo Collina <hello@matteocollina.com>
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/streams

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. stream Issues and PRs related to the stream subsystem. labels Jun 18, 2026
@mcollina mcollina added request-ci Add this label to start a Jenkins CI on a PR. baking-for-lts PRs that need to wait before landing in a LTS release. labels Jun 18, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 18, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

baking-for-lts PRs that need to wait before landing in a LTS release. needs-ci PRs that need a full CI run. stream Issues and PRs related to the stream subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants