• 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';
23require('../common');
24const assert = require('assert');
25
26// This test verifies that stream.unshift(Buffer.alloc(0)) or
27// stream.unshift('') does not set state.reading=false.
28const Readable = require('stream').Readable;
29
30const r = new Readable();
31let nChunks = 10;
32const chunk = Buffer.alloc(10, 'x');
33
34r._read = function(n) {
35  setImmediate(() => {
36    r.push(--nChunks === 0 ? null : chunk);
37  });
38};
39
40let readAll = false;
41const seen = [];
42r.on('readable', () => {
43  let chunk;
44  while (chunk = r.read()) {
45    seen.push(chunk.toString());
46    // Simulate only reading a certain amount of the data,
47    // and then putting the rest of the chunk back into the
48    // stream, like a parser might do.  We just fill it with
49    // 'y' so that it's easy to see which bits were touched,
50    // and which were not.
51    const putBack = Buffer.alloc(readAll ? 0 : 5, 'y');
52    readAll = !readAll;
53    r.unshift(putBack);
54  }
55});
56
57const expect =
58  [ 'xxxxxxxxxx',
59    'yyyyy',
60    'xxxxxxxxxx',
61    'yyyyy',
62    'xxxxxxxxxx',
63    'yyyyy',
64    'xxxxxxxxxx',
65    'yyyyy',
66    'xxxxxxxxxx',
67    'yyyyy',
68    'xxxxxxxxxx',
69    'yyyyy',
70    'xxxxxxxxxx',
71    'yyyyy',
72    'xxxxxxxxxx',
73    'yyyyy',
74    'xxxxxxxxxx',
75    'yyyyy' ];
76
77r.on('end', () => {
78  assert.deepStrictEqual(seen, expect);
79  console.log('ok');
80});
81