Skip to content

Commit

Permalink
stream: ensure text() stream consumer flushes correctly
Browse files Browse the repository at this point in the history
Signed-off-by: James M Snell <[email protected]>

PR-URL: #39737
Reviewed-By: Robert Nagy <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
  • Loading branch information
jasnell committed Aug 13, 2021
1 parent 394c991 commit ca19775
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/stream/consumers.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ async function text(stream) {
else
str += dec.decode(chunk, { stream: true });
}
// Flush the streaming TextDecoder so that any pending
// incomplete multibyte characters are handled.
str += dec.decode(undefined, { stream: false });
return str;
}

Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-stream-consumers.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,17 @@ const kArrayBuffer =
stream.write({});
stream.end({});
}

{
const stream = new TransformStream();
text(stream.readable).then(common.mustCall((str) => {
// Incomplete utf8 character is flushed as a replacement char
assert.strictEqual(str.charCodeAt(0), 0xfffd);
}));
const writer = stream.writable.getWriter();
Promise.all([
writer.write(new Uint8Array([0xe2])),
writer.write(new Uint8Array([0x82])),
writer.close(),
]).then(common.mustCall());
}

0 comments on commit ca19775

Please sign in to comment.