Skip to content

Instantly share code, notes, and snippets.

@pabigot
Last active September 3, 2016 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pabigot/aa7a7f7db0c702dca556c7eac948d330 to your computer and use it in GitHub Desktop.
Save pabigot/aa7a7f7db0c702dca556c7eac948d330 to your computer and use it in GitHub Desktop.
pump stream continues after callback
lilith[4835]$ node pb.js
pump cb 2 0 [Error: second call, carrying on]
ws finished 4 49152
'use strict';
const pump = require('pump');
const stream = require('stream');
class Readable extends stream.Readable {
constructor() {
super();
this.called = 0;
}
_read(size) {
let chunk = new Buffer(size);
chunk.fill(0);
this.called += 1;
if (2 === this.called) {
this.emit('error', new Error('second call, carrying on'));
} else if (4 <= this.called) {
chunk = null;
}
return this.push(chunk);
}
}
class Writable extends stream.Writable {
constructor() {
super();
this.received = 0;
}
_write(chunk, encoding, callback) {
this.received += chunk.length;
return callback();
}
}
const rs = new Readable();
const ws = new Writable();
ws.on('finish', () => {
console.log('ws finished', rs.called, ws.received);
});
pump(rs, ws, err => {
console.log('pump cb', rs.called, ws.received, err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment