• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import { mustCall } from '../common/index.mjs';
2import { ReadableStream } from 'stream/web';
3import assert from 'assert';
4
5{
6  // Test tee() with close in the nextTick after enqueue
7  async function read(stream) {
8    const chunks = [];
9    for await (const chunk of stream)
10      chunks.push(chunk);
11    return Buffer.concat(chunks).toString();
12  }
13
14  const [r1, r2] = new ReadableStream({
15    start(controller) {
16      process.nextTick(() => {
17        controller.enqueue(new Uint8Array([102, 111, 111, 98, 97, 114]));
18
19        process.nextTick(() => {
20          controller.close();
21        });
22      });
23    }
24  }).tee();
25
26  (async () => {
27    const [dataReader1, dataReader2] = await Promise.all([
28      read(r1),
29      read(r2),
30    ]);
31
32    assert.strictEqual(dataReader1, dataReader2);
33    assert.strictEqual(dataReader1, 'foobar');
34    assert.strictEqual(dataReader2, 'foobar');
35  })().then(mustCall());
36}
37
38{
39  // Test ReadableByteStream.tee() with close in the nextTick after enqueue
40  async function read(stream) {
41    const chunks = [];
42    for await (const chunk of stream)
43      chunks.push(chunk);
44    return Buffer.concat(chunks).toString();
45  }
46
47  const [r1, r2] = new ReadableStream({
48    type: 'bytes',
49    start(controller) {
50      process.nextTick(() => {
51        controller.enqueue(new Uint8Array([102, 111, 111, 98, 97, 114]));
52
53        process.nextTick(() => {
54          controller.close();
55        });
56      });
57    }
58  }).tee();
59
60  (async () => {
61    const [dataReader1, dataReader2] = await Promise.all([
62      read(r1),
63      read(r2),
64    ]);
65
66    assert.strictEqual(dataReader1, dataReader2);
67    assert.strictEqual(dataReader1, 'foobar');
68    assert.strictEqual(dataReader2, 'foobar');
69  })().then(mustCall());
70}
71