question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Don't inspect stream._readableState.ended

See original GitHub issue

The 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:

  1. 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'));
    
  2. 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:open
  • Created 6 years ago
  • Reactions:1
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
isaacscommented, Sep 24, 2019

@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 an end 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().

0reactions
isaacscommented, Sep 24, 2019

Also, Minipass streams have destroy now.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found