summaryrefslogtreecommitdiff
path: root/test/parallel/test-stream-readable-strategy-option.js
blob: a32e70ef2155ea503303b09f27c52caae386e96f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'use strict';
const common = require('../common');
const { Readable } = require('stream');
const assert = require('assert');
const { strictEqual } = require('assert');

{
  // Strategy 2
  const streamData = ['a', 'b', 'c', null];

  // Fulfill a Readable object
  const readable = new Readable({
    read: common.mustCall(() => {
      process.nextTick(() => {
        readable.push(streamData.shift());
      });
    }, streamData.length),
  });

  // Use helper to convert it to a Web ReadableStream using ByteLength strategy
  const readableStream = Readable.toWeb(readable, {
    strategy: new ByteLengthQueuingStrategy({ highWaterMark: 1 }),
  });

  assert(!readableStream.locked);
  readableStream.getReader().read().then(common.mustCall());
}

{
  // Strategy 2
  const streamData = ['a', 'b', 'c', null];

  // Fulfill a Readable object
  const readable = new Readable({
    read: common.mustCall(() => {
      process.nextTick(() => {
        readable.push(streamData.shift());
      });
    }, streamData.length),
  });

  // Use helper to convert it to a Web ReadableStream using Count strategy
  const readableStream = Readable.toWeb(readable, {
    strategy: new CountQueuingStrategy({ highWaterMark: 1 }),
  });

  assert(!readableStream.locked);
  readableStream.getReader().read().then(common.mustCall());
}

{
  const desireSizeExpected = 2;

  const stringStream = new ReadableStream(
    {
      start(controller) {
        // Check if the strategy is being assigned on the init of the ReadableStream
        strictEqual(controller.desiredSize, desireSizeExpected);
        controller.enqueue('a');
        controller.enqueue('b');
        controller.close();
      },
    },
    new CountQueuingStrategy({ highWaterMark: desireSizeExpected })
  );

  const reader = stringStream.getReader();

  reader.read().then(common.mustCall());
  reader.read().then(common.mustCall());
  reader.read().then(({ value, done }) => {
    strictEqual(value, undefined);
    strictEqual(done, true);
  });
}