• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3
4const assert = require('assert');
5const stream = require('stream');
6const { inspect } = require('util');
7
8{
9  // This test ensures that the stream implementation correctly handles values
10  // for highWaterMark which exceed the range of signed 32 bit integers and
11  // rejects invalid values.
12
13  // This number exceeds the range of 32 bit integer arithmetic but should still
14  // be handled correctly.
15  const ovfl = Number.MAX_SAFE_INTEGER;
16
17  const readable = stream.Readable({ highWaterMark: ovfl });
18  assert.strictEqual(readable._readableState.highWaterMark, ovfl);
19
20  const writable = stream.Writable({ highWaterMark: ovfl });
21  assert.strictEqual(writable._writableState.highWaterMark, ovfl);
22
23  for (const invalidHwm of [true, false, '5', {}, -5, NaN]) {
24    for (const type of [stream.Readable, stream.Writable]) {
25      assert.throws(() => {
26        type({ highWaterMark: invalidHwm });
27      }, {
28        name: 'TypeError',
29        code: 'ERR_INVALID_ARG_VALUE',
30        message: "The property 'options.highWaterMark' is invalid. " +
31          `Received ${inspect(invalidHwm)}`
32      });
33    }
34  }
35}
36
37{
38  // This test ensures that the push method's implementation
39  // correctly handles the edge case where the highWaterMark and
40  // the state.length are both zero
41
42  const readable = stream.Readable({ highWaterMark: 0 });
43
44  for (let i = 0; i < 3; i++) {
45    const needMoreData = readable.push();
46    assert.strictEqual(needMoreData, true);
47  }
48}
49
50{
51  // This test ensures that the read(n) method's implementation
52  // correctly handles the edge case where the highWaterMark, state.length
53  // and n are all zero
54
55  const readable = stream.Readable({ highWaterMark: 0 });
56
57  readable._read = common.mustCall();
58  readable.read(0);
59}
60
61{
62  // Parse size as decimal integer
63  ['1', '1.0', 1].forEach((size) => {
64    const readable = new stream.Readable({
65      read: common.mustCall(),
66      highWaterMark: 0,
67    });
68    readable.read(size);
69
70    assert.strictEqual(readable._readableState.highWaterMark, Number(size));
71  });
72}
73
74{
75  // Test highwatermark limit
76  const hwm = 0x40000000 + 1;
77  const readable = stream.Readable({
78    read() {},
79  });
80
81  assert.throws(() => readable.read(hwm), common.expectsError({
82    code: 'ERR_OUT_OF_RANGE',
83    message: 'The value of "size" is out of range.' +
84             ' It must be <= 1GiB. Received ' +
85             hwm,
86  }));
87}
88