3 var assert = require('assert');
4 var util = require('util');
5 var stream = require('stream');
9 function PassThrough() {
10 stream.Transform.call(this);
12 util.inherits(PassThrough, stream.Transform);
13 PassThrough.prototype._transform = function(chunk, encoding, done) {
18 function TestStream() {
19 stream.Transform.call(this);
21 util.inherits(TestStream, stream.Transform);
22 TestStream.prototype._transform = function(chunk, encoding, done) {
24 // Char 'a' only exists in the last write
25 passed = chunk.toString().indexOf('a') >= 0;
30 var s1 = new PassThrough();
31 var s2 = new PassThrough();
32 var s3 = new TestStream();
34 // Don't let s2 auto close which may close s3
35 s2.pipe(s3, {end: false});
37 // We must write a buffer larger than highWaterMark
38 var big = new Buffer(s1._writableState.highWaterMark + 1);
41 // Since big is larger than highWaterMark, it will be buffered internally.
42 assert(!s1.write(big));
43 // 'tiny' is small enough to pass through internal buffer.
44 assert(s2.write('tiny'));
46 // Write some small data in next IO loop, which will never be written to s3
47 // Because 'drain' event is not emitted from s1 and s1 is still paused
48 setImmediate(s1.write.bind(s1), 'later');
50 // Assert after two IO loops when all operations have been done.
51 process.on('exit', function() {
52 assert(passed, 'Large buffer is not handled properly by Writable Stream');