stream: add ReadableByteStream.tee()

This supports teeing readable byte streams to meet
the latest web streams standards.

Signed-off-by: Daeyeon Jeong daeyeon.dev@gmail.com
PR-URL: https://github.com/nodejs/node/pull/44505
Refs: https://streams.spec.whatwg.org/#readable-stream-tee
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
Daeyeon Jeong authored and GitHub committed 2022-09-08 22:06:06 +00:00
1 parent 7f9cd60eef
commit e06384cb48
6 files changed
+343 -53

No files matched your search

+1 -1
View File
@@ -1561,7 +1561,7 @@ class Source {
assert(!readableStreamDefaultControllerCanCloseOrEnqueue(controller));
readableStreamDefaultControllerEnqueue(controller);
readableByteStreamControllerClose(controller);
readableByteStreamControllerEnqueue(controller);
readableByteStreamControllerEnqueue(controller, new Uint8Array(1));
}
{
@@ -34,3 +34,37 @@ import assert from 'assert';
assert.strictEqual(dataReader2, 'foobar');
})().then(mustCall());
}
{
// Test ReadableByteStream.tee() with close in the nextTick after enqueue
async function read(stream) {
const chunks = [];
for await (const chunk of stream)
chunks.push(chunk);
return Buffer.concat(chunks).toString();
}
const [r1, r2] = new ReadableStream({
type: 'bytes',
start(controller) {
process.nextTick(() => {
controller.enqueue(new Uint8Array([102, 111, 111, 98, 97, 114]));
process.nextTick(() => {
controller.close();
});
});
}
}).tee();
(async () => {
const [dataReader1, dataReader2] = await Promise.all([
read(r1),
read(r2),
]);
assert.strictEqual(dataReader1, dataReader2);
assert.strictEqual(dataReader1, 'foobar');
assert.strictEqual(dataReader2, 'foobar');
})().then(mustCall());
}