• 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');
25const stream = require('stream');
26
27let passed = false;
28
29class TestStream extends stream.Transform {
30  _transform(chunk, encoding, done) {
31    if (!passed) {
32      // Char 'a' only exists in the last write
33      passed = chunk.toString().includes('a');
34    }
35    done();
36  }
37}
38
39const s1 = new stream.Transform({
40  transform(chunk, encoding, cb) {
41    process.nextTick(cb, null, chunk);
42  }
43});
44const s2 = new stream.PassThrough();
45const s3 = new TestStream();
46s1.pipe(s3);
47// Don't let s2 auto close which may close s3
48s2.pipe(s3, { end: false });
49
50// We must write a buffer larger than highWaterMark
51const big = Buffer.alloc(s1.writableHighWaterMark + 1, 'x');
52
53// Since big is larger than highWaterMark, it will be buffered internally.
54assert(!s1.write(big));
55// 'tiny' is small enough to pass through internal buffer.
56assert(s2.write('tiny'));
57
58// Write some small data in next IO loop, which will never be written to s3
59// Because 'drain' event is not emitted from s1 and s1 is still paused
60setImmediate(s1.write.bind(s1), 'later');
61
62// Assert after two IO loops when all operations have been done.
63process.on('exit', function() {
64  assert(passed, 'Large buffer is not handled properly by Writable Stream');
65});
66