From 861124a0db1f23adc9343591f2acfbb5e28a6c6b Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Thu, 9 Aug 2018 14:02:33 +0200 Subject: [PATCH] stream: restore flow if there are 'data' handlers after once('readable') Fixes: https://github.com/nodejs/node/issues/21398 See: https://github.com/nodejs/node/pull/21696 --- doc/api/stream.md | 22 ++++++- lib/_stream_readable.js | 9 ++- .../test-stream-once-readable-pipe.js | 61 +++++++++++++++++++ ...est-stream-readable-reading-readingMore.js | 7 ++- 4 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 test/parallel/test-stream-once-readable-pipe.js diff --git a/doc/api/stream.md b/doc/api/stream.md index c42df4ee7c757d..e38e73f3ab863e 100644 --- a/doc/api/stream.md +++ b/doc/api/stream.md @@ -618,6 +618,12 @@ instance, when the `readable.resume()` method is called without a listener attached to the `'data'` event, or when a `'data'` event handler is removed from the stream. +Adding a [`'readable'`][] event handler automatically make the stream to +stop flowing, and the data to be consumed via +[`readable.read()`][stream-read]. If the [`'readable'`] event handler is +removed, then the stream will start flowing again if there is a +[`'data'`][] event handler. + #### Three States The "two modes" of operation for a `Readable` stream are a simplified @@ -666,12 +672,15 @@ within the streams internal buffer. The `Readable` stream API evolved across multiple Node.js versions and provides multiple methods of consuming stream data. In general, developers should choose *one* of the methods of consuming data and *should never* use multiple methods -to consume data from a single stream. +to consume data from a single stream. Specifically, using a combination +of `on('data')`, `on('readable')`, `pipe()` or async iterators could +lead to unintuitive behavior. Use of the `readable.pipe()` method is recommended for most users as it has been implemented to provide the easiest way of consuming stream data. Developers that require more fine-grained control over the transfer and generation of data can -use the [`EventEmitter`][] and `readable.pause()`/`readable.resume()` APIs. +use the [`EventEmitter`][] and `readable.on('readable')`/`readable.read()` +or the `readable.pause()`/`readable.resume()` APIs. #### Class: stream.Readable