Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

streams: data/pipe and readable can't be used together #8568

Closed
iliakan opened this issue Oct 17, 2014 · 5 comments
Closed

streams: data/pipe and readable can't be used together #8568

iliakan opened this issue Oct 17, 2014 · 5 comments
Labels

Comments

@iliakan
Copy link

iliakan commented Oct 17, 2014

data/pipe events and readable can't be used together, the latter is ignored.

E.g. in this example, there are many data events, but no readable events (except the last one), so only 1 dot is printed:

fileStream.on('data', function(data) { console.log(data); });
fileStream.on('readable', function() { console.log('.');  });

The same with pipe - only 1 dot even if there are many data chunks:

fileStream.pipe(outputStream);
fileStream.on('readable', function() { console.log('.');  });

It is quite visible in the node code why that happens, but I couldn't find any mentions in the docs that "data/pipe" and "readable" are antagonists.

Also, it is not obvious from the common sense standpoint why the examples above don't work "as intended".

Is there a reason for such decision? Anything in the docs about that?

P.S. Node 0.11.14, the stream is fs.ReadStream.

@iliakan iliakan changed the title streams: data/pipe VS readable streams: data/pipe VS readable can't be used together Oct 17, 2014
@iliakan iliakan changed the title streams: data/pipe VS readable can't be used together streams: data/pipe and readable can't be used together Oct 17, 2014
@iliakan
Copy link
Author

iliakan commented Oct 19, 2014

Maybe it should be added to the docs that "readable" almost (except the EOF) never happens in "flowing" mode?

@indutny
Copy link
Member

indutny commented Oct 20, 2014

@iliakan I tend to agree with you on this. I'd say something like:

However, if the stream is in "flowing" mode the "readable" may not be emitted for every available data chunk.

How does it sound to you? cc @trevnorris @bnoordhuis

@iliakan
Copy link
Author

iliakan commented Oct 20, 2014

To be precise:

The "readable" event is not emitted in the "flowing" mode. It is overshadowed by the `"data"` event which delivers the data ASAP. 

The sole exception is the end-of-stream: "readable" is emitted, but "stream.read()" returns "null", because there's no data left at this point.

@trevnorris
Copy link

@iliakan Sounds good. Want to open a PR?

@jasnell
Copy link
Member

jasnell commented Jun 26, 2015

Landed in b436e59

@jasnell jasnell closed this as completed Jun 26, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants