Skip to content

Instantly share code, notes, and snippets.

@max-mapper
Last active August 29, 2015 14:07
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max-mapper/7ce42ec0d841f40488d2 to your computer and use it in GitHub Desktop.
Save max-mapper/7ce42ec0d841f40488d2 to your computer and use it in GitHub Desktop.
Node >= 0.10 Streams2 protips

@mafintosh said most of this, I just wrote it down

how to destroy/end streams in node >= 0.10

  • usually you call .destroy() if it has .destroy
  • if it doesnt have .destroy you are out of luck and the stream should upgrade to use e.g. newer through2
  • in request you call .abort() (this should get fixed to use .destroy())
  • .end() tries to end the stream gracefully

what about close

  • usually no .close method exists except in FD backed streams (usually only fs). usually you would never call .close()

what about events

  • finish event is emitted when a stream ends nicely (e.g. stream.end())
  • close and error events are emitted when a stream ends due to failure (e.g. stream.destroy())
  • end is emitted when a readable stream has no more data
  • end in streams2 behaves more like a flush event, it only gets emitted when the data has been read from the readable
@evansolomon
Copy link

The close event does not indicate failure in any usage I've ever seen.

@max-mapper
Copy link
Author

@evansolomon perhaps the wording is bad, I use failure to mean basically everything that is non-graceful. that being said you could still be correct

update: for example, http in node core basically never emits error and always emits close when errors happen

@stdarg
Copy link

stdarg commented Oct 21, 2014

Great summary. I find close for ungraceful disconnects non-intuitive. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment