From 578a7b90e3afaf6e38a7a5177509504925cc8ec3 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Mon, 26 Feb 2018 09:24:30 +0100 Subject: [PATCH] stream: 'readable' have precedence over flowing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In Streams3 the 'readable' event/.read() method had a lower precedence than the `'data'` event that made them impossible to use them together. This make `.resume()` a no-op if there is a listener for the `'readable'` event, making the stream non-flowing if there is a `'data'`  listener. Fixes: https://github.com/nodejs/node/issues/18058 --- doc/api/stream.md | 22 ++- lib/_stream_readable.js | 57 +++++- .../parallel/test-http-readable-data-event.js | 52 +++++ ...est-stream-readable-reading-readingMore.js | 184 ++++++++++++++---- test/parallel/test-stream3-pause-then-read.js | 3 + 5 files changed, 269 insertions(+), 49 deletions(-) create mode 100644 test/parallel/test-http-readable-data-event.js diff --git a/doc/api/stream.md b/doc/api/stream.md index 9f233cd325132f..86286906b7f297 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -762,6 +762,9 @@ changes: description: > 'readable' is always emitted in the next tick after .push() is called + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/18994 + description: Using 'readable' requires calling .read(). --> The `'readable'` event is emitted when there is data available to be read from @@ -770,10 +773,16 @@ cause some amount of data to be read into an internal buffer. ```javascript const readable = getReadableStreamSomehow(); -readable.on('readable', () => { +readable.on('readable', function() { // there is some data to read now + let data; + + while (data = this.read()) { + console.log(data); + } }); ``` + The `'readable'` event will also be emitted once the end of the stream data has been reached but before the `'end'` event is emitted. @@ -806,6 +815,10 @@ In general, the `readable.pipe()` and `'data'` event mechanisms are easier to understand than the `'readable'` event. However, handling `'readable'` might result in increased throughput. +If both `'readable'` and [`'data'`][] are used at the same time, `'readable'` +takes precedence in controlling the flow, i.e. `'data'` will be emitted +only when [`stream.read()`][stream-read] is called. + ##### readable.destroy([error]) * Returns: {this} @@ -1016,6 +1033,9 @@ getReadableStreamSomehow() }); ``` +The `readable.resume()` method has no effect if there is a `'readable'` +event listener. + ##### readable.setEncoding(encoding)