Don't inspect stream._readableState.ended
See original GitHub issueThe onclose
handler is coupled to node core’s stream implementation details, which creates some unfortunate problems for userland streams.
var onclose = function() {
if (readable && !(rs && rs.ended)) return callback.call(stream, new Error('premature close'));
if (writable && !(ws && ws.ended)) return callback.call(stream, new Error('premature close'));
};
If you have a readable stream that doesn’t have a _readableState
member (or writable/_writableState), then this always raises a premature close error.
While it was straightforward to work around in my case, it was surprising and unnecessarily tricky to debug.
I suggest either of the following approaches:
- If a stream doesn’t have a readable/writable state, don’t add the “premature close” check at all. Then the line becomes:
if (readable && rs && !rs.ended) return callback.call(stream, new Error('premature close')); if (writable && ws && !ws.ended) return callback.call(stream, new Error('premature close'));
- If a stream doesn’t hae a readable/writable state, throw a TypeError at the outset so that people don’t attempt to use eos with Streams that don’t derive from the core stream classes.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:11 (4 by maintainers)
Top Results From Across the Web
Stream | Node.js v19.3.0 Documentation
The 'close' event is emitted when the stream and any of its underlying resources (a file descriptor, for example) have been closed. The...
Read more >Know if a Readable stream can be read from - Stack Overflow
I want to check that it is readable before continuing to interact with it: function do_some_stuff_with_stream(stream) { if (stream.canBeReadFrom ...
Read more >readable-stream - NPM Package Compare versions - Socket
Start using Socket to analyze readable-stream and its 3 dependencies to secure your app from ... debug('read: emitReadable', state.length, state.ended);.
Read more >ReadableStream.cancel() - Web APIs - MDN Web Docs
Cancel is used when you've completely finished with the stream and don't need any more data from it, even if there are chunks...
Read more >Streams Standard
This will lock the stream, making it no longer directly usable; however, ... with an error indicating piping to a closed stream failed, ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@lovell Yeah, that broke a bunch of stuff that was already using
ended
to mean something different. Sometimes a semver minor change turns out to be breaking, unfortunately.Minipass streams have an
emittedEnd
getter that means the same thing, anyway. Just less pretty than “ended”, imo. But also, if you add anend
event handler on a Minipass stream, and it’s already ended, it’ll re-emit immediately, so it’ll be like it just ended. Kind of like how a resolved promise automatically calls the cb you pass to.then()
.Also, Minipass streams have
destroy
now.