stream: ensure text() stream consumer flushes correctly

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: https://github.com/nodejs/node/pull/39737
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
James M Snell 2021-08-11 08:58:27 -07:00
parent 394c99118d
commit ca19775d0e
No known key found for this signature in database
GPG Key ID: 7341B15C070877AC
2 changed files with 17 additions and 0 deletions

View File

@ -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;
}

View File

@ -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());
}