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.