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 <jasnell@gmail.com>

PR-URL: #39737
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
  • Loading branch information
jasnell authored and danielleadams committed Aug 16, 2021
1 parent 03be967 commit cd9b0bf
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 cd9b0bf

Please sign in to comment.