I feel like there is something missing in the streams _read API for implementors.
Basically the current state of things is that stream implementors need to implement async error handling something like this:
_read (n) {
doAsyncRead(n, (err, buf) => {
if (err) {
if (this._readableState.autoDestroy) {
this.destroy(err)
} else {
this.emit('error', err)
}
} else {
this.push(buf)
}
})
}
There are some problems with this:
- Most stream implementors don't do this and simply don't bother to conform to the
autoDestroy option and either destroy() or emit('error', err), which one they choose is mostly unclear and undocumented.
- Even when conforming you either have to access the
_readableState object or add a flag based on the options sent to the Readable super.
I'm not quite sure how to resolve this. I can think of a few ways:
- Add a
cb to _read that can be invoked with an error and would internally call the errorOrDestroy method.
- Make the
errorOrDestroy util directly accessible for stream implementors and document it somehow.
- Leave as is and clarify in documentation how to handle this scenario (preferable without
_readableState).
- Deprecate
autoDestroy and make it true by default.
- Document that
autoDestroy is for stream implementors only and not for stream consumers.
I feel like there is something missing in the streams
_readAPI for implementors.Basically the current state of things is that stream implementors need to implement async error handling something like this:
There are some problems with this:
autoDestroyoption and eitherdestroy()oremit('error', err), which one they choose is mostly unclear and undocumented._readableStateobject or add a flag based on the options sent to theReadablesuper.I'm not quite sure how to resolve this. I can think of a few ways:
cbto_readthat can be invoked with an error and would internally call theerrorOrDestroymethod.errorOrDestroyutil directly accessible for stream implementors and document it somehow._readableState).autoDestroyand make ittrueby default.autoDestroyis for stream implementors only and not for stream consumers.