I ran into this today:
const{ Readable }=require('stream');constsomeOtherResource={doStuff(){if(this.destroyed){thrownewError('destroyed!')}},destroy(){this.destroyed=true}}constr=newReadable({})r.on('data',()=>{someOtherResource.doStuff()})someOtherResource.destroy()r.push('asd')r.destroy()// or r.destroy(new Error('kaboom'))r.read()This will crash with destroyed! since read() will emit already buffered 'data' even though the stream is destroyed.
I can fix my above example by replacing r.destroy() with:
functiondestroy(r){r.removeAllListeners('data')r.destroy()}I find this a bit unfortunate.
Questions:
- Is this expected behaviour?
- Can we fix it without breaking the ecosystem?
- If no, can we provide another method to
Readable that makes it possible to fully destroy and silence a readable?
In order to fix this in readable we would have to check for destroyed and return early inside Readable.read().
I ran into this today:
This will crash with
destroyed!sinceread()will emit already buffered'data'even though the stream is destroyed.I can fix my above example by replacing
r.destroy()with:I find this a bit unfortunate.
Questions:
Readablethat makes it possible to fully destroy and silence a readable?In order to fix this in readable we would have to check for
destroyedand return early insideReadable.read().