Skip to content

Commit ca19775

Browse files
committed
stream: ensure text() stream consumer flushes correctly
Signed-off-by: James M Snell <[email protected]> PR-URL: #39737 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 394c991 commit ca19775

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

lib/stream/consumers.js

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ async function text(stream) {
6363
else
6464
str += dec.decode(chunk, { stream: true });
6565
}
66+
// Flush the streaming TextDecoder so that any pending
67+
// incomplete multibyte characters are handled.
68+
str += dec.decode(undefined, { stream: false });
6669
return str;
6770
}
6871

test/parallel/test-stream-consumers.js

+14
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,17 @@ const kArrayBuffer =
232232
stream.write({});
233233
stream.end({});
234234
}
235+
236+
{
237+
const stream = new TransformStream();
238+
text(stream.readable).then(common.mustCall((str) => {
239+
// Incomplete utf8 character is flushed as a replacement char
240+
assert.strictEqual(str.charCodeAt(0), 0xfffd);
241+
}));
242+
const writer = stream.writable.getWriter();
243+
Promise.all([
244+
writer.write(new Uint8Array([0xe2])),
245+
writer.write(new Uint8Array([0x82])),
246+
writer.close(),
247+
]).then(common.mustCall());
248+
}

0 commit comments

Comments
 (0)