• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const { Readable } = require('stream');
4const assert = require('assert');
5const { strictEqual } = require('assert');
6
7{
8  // Strategy 2
9  const streamData = ['a', 'b', 'c', null];
10
11  // Fulfill a Readable object
12  const readable = new Readable({
13    read: common.mustCall(() => {
14      process.nextTick(() => {
15        readable.push(streamData.shift());
16      });
17    }, streamData.length),
18  });
19
20  // Use helper to convert it to a Web ReadableStream using ByteLength strategy
21  const readableStream = Readable.toWeb(readable, {
22    strategy: new ByteLengthQueuingStrategy({ highWaterMark: 1 }),
23  });
24
25  assert(!readableStream.locked);
26  readableStream.getReader().read().then(common.mustCall());
27}
28
29{
30  // Strategy 2
31  const streamData = ['a', 'b', 'c', null];
32
33  // Fulfill a Readable object
34  const readable = new Readable({
35    read: common.mustCall(() => {
36      process.nextTick(() => {
37        readable.push(streamData.shift());
38      });
39    }, streamData.length),
40  });
41
42  // Use helper to convert it to a Web ReadableStream using Count strategy
43  const readableStream = Readable.toWeb(readable, {
44    strategy: new CountQueuingStrategy({ highWaterMark: 1 }),
45  });
46
47  assert(!readableStream.locked);
48  readableStream.getReader().read().then(common.mustCall());
49}
50
51{
52  const desireSizeExpected = 2;
53
54  const stringStream = new ReadableStream(
55    {
56      start(controller) {
57        // Check if the strategy is being assigned on the init of the ReadableStream
58        strictEqual(controller.desiredSize, desireSizeExpected);
59        controller.enqueue('a');
60        controller.enqueue('b');
61        controller.close();
62      },
63    },
64    new CountQueuingStrategy({ highWaterMark: desireSizeExpected })
65  );
66
67  const reader = stringStream.getReader();
68
69  reader.read().then(common.mustCall());
70  reader.read().then(common.mustCall());
71  reader.read().then(({ value, done }) => {
72    strictEqual(value, undefined);
73    strictEqual(done, true);
74  });
75}
76