• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Copyright Joyent, Inc. and other Node contributors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a
4// copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to permit
8// persons to whom the Software is furnished to do so, subject to the
9// following conditions:
10//
11// The above copyright notice and this permission notice shall be included
12// in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20// USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22'use strict';
23const common = require('../common');
24const assert = require('assert');
25const { Readable, Writable } = require('stream');
26const EE = require('events').EventEmitter;
27
28function runTest(highWaterMark, objectMode, produce) {
29
30  const old = new EE();
31  const r = new Readable({ highWaterMark, objectMode });
32  assert.strictEqual(r, r.wrap(old));
33
34  r.on('end', common.mustCall());
35
36  old.pause = function() {
37    old.emit('pause');
38    flowing = false;
39  };
40
41  old.resume = function() {
42    old.emit('resume');
43    flow();
44  };
45
46  // Make sure pause is only emitted once.
47  let pausing = false;
48  r.on('pause', () => {
49    assert.strictEqual(pausing, false);
50    pausing = true;
51    process.nextTick(() => {
52      pausing = false;
53    });
54  });
55
56  let flowing;
57  let chunks = 10;
58  let oldEnded = false;
59  const expected = [];
60  function flow() {
61    flowing = true;
62    while (flowing && chunks-- > 0) {
63      const item = produce();
64      expected.push(item);
65      old.emit('data', item);
66    }
67    if (chunks <= 0) {
68      oldEnded = true;
69      old.emit('end');
70    }
71  }
72
73  const w = new Writable({ highWaterMark: highWaterMark * 2,
74                           objectMode });
75  const written = [];
76  w._write = function(chunk, encoding, cb) {
77    written.push(chunk);
78    setTimeout(cb, 1);
79  };
80
81  w.on('finish', common.mustCall(function() {
82    performAsserts();
83  }));
84
85  r.pipe(w);
86
87  flow();
88
89  function performAsserts() {
90    assert(oldEnded);
91    assert.deepStrictEqual(written, expected);
92  }
93}
94
95runTest(100, false, function() { return Buffer.allocUnsafe(100); });
96runTest(10, false, function() { return Buffer.from('xxxxxxxxxx'); });
97runTest(1, true, function() { return { foo: 'bar' }; });
98
99const objectChunks = [ 5, 'a', false, 0, '', 'xyz', { x: 4 }, 7, [], 555 ];
100runTest(1, true, function() { return objectChunks.shift(); });
101