Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit 'done' event on writable streams Needle's response is being piped to #373

Merged
merged 2 commits into from
Aug 26, 2021

Conversation

tomas
Copy link
Owner

@tomas tomas commented Aug 26, 2021

This lets us use a consistent API regardless listening to events on the response stream itself or to destination/writable streams.

This is one of the examples in the readme:

needle
  .post('https://my.server.com/foo', data, { multipart: true })
  .on('readable', function() { /* eat your chunks */ })
  .on('done', function(err) {
    console.log('Ready-o!');
  })

However if instead of processing the chunks manually you wanted to pipe to another stream, then you'd be attaching the on('done') listener to the target stream:

needle
  .post('https://my.server.com/foo', data, { multipart: true })
  .pipe(someWritableStream)
  .on('done', function(err) {
    // this would never fire, because it's Needle's response 
    // that emits the 'done' event, not the writableStream returned by the pipe() function
  })

This PR ensures that both Needle's response and its target streams, when piped, all emit the 'done' event, providing a more consistent way of piping streams.

On a related note, please take a look at #372 for more info regarding error handling on streams. The gist of it is that you should use stream.pipeline instead of pipe, when possible.

@tomas tomas merged commit acd6165 into master Aug 26, 2021
@tomas tomas deleted the done-event branch August 26, 2021 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant