From cd9b0bf68c2c278c703c8993beed3029cddb41a8 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 11 Aug 2021 08:58:27 -0700 Subject: [PATCH] stream: ensure text() stream consumer flushes correctly Signed-off-by: James M Snell PR-URL: https://github.com/nodejs/node/pull/39737 Reviewed-By: Robert Nagy Reviewed-By: Matteo Collina --- lib/stream/consumers.js | 3 +++ test/parallel/test-stream-consumers.js | 14 ++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/lib/stream/consumers.js b/lib/stream/consumers.js index ffe6e531205e7f..4566eff6a30f7f 100644 --- a/lib/stream/consumers.js +++ b/lib/stream/consumers.js @@ -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; } diff --git a/test/parallel/test-stream-consumers.js b/test/parallel/test-stream-consumers.js index 8f6a9deb1c27dc..1a3b6ce00200d4 100644 --- a/test/parallel/test-stream-consumers.js +++ b/test/parallel/test-stream-consumers.js @@ -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()); +}