Last active
September 3, 2016 13:13
-
-
Save pabigot/aa7a7f7db0c702dca556c7eac948d330 to your computer and use it in GitHub Desktop.
pump stream continues after callback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lilith[4835]$ node pb.js | |
pump cb 2 0 [Error: second call, carrying on] | |
ws finished 4 49152 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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