• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../common');
3const { Readable, PassThrough } = require('stream');
4
5function test(r) {
6  const wrapper = new Readable({
7    read: () => {
8      let data = r.read();
9
10      if (data) {
11        wrapper.push(data);
12        return;
13      }
14
15      r.once('readable', function() {
16        data = r.read();
17        if (data) {
18          wrapper.push(data);
19        }
20        // else: the end event should fire
21      });
22    },
23  });
24
25  r.once('end', function() {
26    wrapper.push(null);
27  });
28
29  wrapper.resume();
30  wrapper.once('end', common.mustCall());
31}
32
33{
34  const source = new Readable({
35    read: () => {}
36  });
37  source.push('foo');
38  source.push('bar');
39  source.push(null);
40
41  const pt = source.pipe(new PassThrough());
42  test(pt);
43}
44
45{
46  // This is the underlying cause of the above test case.
47  const pushChunks = ['foo', 'bar'];
48  const r = new Readable({
49    read: () => {
50      const chunk = pushChunks.shift();
51      if (chunk) {
52        // synchronous call
53        r.push(chunk);
54      } else {
55        // asynchronous call
56        process.nextTick(() => r.push(null));
57      }
58    },
59  });
60
61  test(r);
62}
63