Skip to content
This repository has been archived by the owner on Aug 31, 2024. It is now read-only.

Callback not called / promise not resolved for non-flowing streams with data. #25

Open
BryceCicada opened this issue Apr 27, 2018 · 1 comment

Comments

@BryceCicada
Copy link

Thanks for this library. I've found the following issue after using stream-to-promise that depends on this library.

Consider the following code that has a paused readable stream containing data at the time of calling toArray():

const MiniPass = require('minipass');
const toArray = require('stream-to-array');

const mp = new MiniPass();
mp.end('foo');
toArray(mp).then(parts => console.log(parts));

The promise never resolves.

On digging, it appears that, within stream-to-array, event listeners are registered in the following order:

stream.on('data', onData)
stream.on('end', onEnd)

However, if I change the order of listener registration ('end' before 'data') then the promise will resolve.

From the docs for Streams at https://nodejs.org/api/stream.html:

The 'data' event is emitted whenever the stream is relinquishing ownership of a chunk of data to a consumer. This may occur whenever the stream is switched in flowing mode by calling readable.pipe(), readable.resume(), or by attaching a listener callback to the 'data' event.

The 'end' event is emitted when there is no more data to be consumed from the stream. The 'end' event will not be emitted unless the data is completely consumed. This can be accomplished by switching the stream into flowing mode, or by calling stream.read() repeatedly until all data has been consumed.

I think it follows that, if there is data available on a non-flowing readable stream, then registering the data handler will cause it to start flowing and potentially result in the 'end' event firing immediately.

@BryceCicada BryceCicada changed the title Should register 'end' event listener before 'data' listener. Callback not called / promise not resolved for non-flowing streams with data. Apr 27, 2018
@jonathanong
Copy link
Contributor

@BryceCicada it seems like the module is not emitting the data chunks after the initial event loop, which is incorrect. however, this module is no longer necessary so we won't fix this. thank you for the report!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants