Skip to content
forked from nodejs/node

Commit 0b02008

Browse files
authored
doc: define more cases for stream event emissions
PR-URL: nodejs#53317 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 50695e5 commit 0b02008

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

doc/api/stream.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -1282,9 +1282,11 @@ changes:
12821282
-->
12831283

12841284
The `'readable'` event is emitted when there is data available to be read from
1285-
the stream or when the end of the stream has been reached. Effectively, the
1286-
`'readable'` event indicates that the stream has new information. If data is
1287-
available, [`stream.read()`][stream-read] will return that data.
1285+
the stream, up to the configured high water mark (`state.highWaterMark`). Effectively,
1286+
it indicates that the stream has new information within the buffer. If data is available
1287+
within this buffer, [`stream.read()`][stream-read] can be called to retrieve that data.
1288+
Additionally, the `'readable'` event may also be emitted when the end of the stream has been
1289+
reached.
12881290

12891291
```js
12901292
const readable = getReadableStreamSomehow();
@@ -1553,13 +1555,14 @@ readable.on('end', () => {
15531555
});
15541556
```
15551557

1556-
Each call to `readable.read()` returns a chunk of data, or `null`. The chunks
1557-
are not concatenated. A `while` loop is necessary to consume all data
1558-
currently in the buffer. When reading a large file `.read()` may return `null`,
1559-
having consumed all buffered content so far, but there is still more data to
1560-
come not yet buffered. In this case a new `'readable'` event will be emitted
1561-
when there is more data in the buffer. Finally the `'end'` event will be
1562-
emitted when there is no more data to come.
1558+
Each call to `readable.read()` returns a chunk of data or `null`, signifying
1559+
that there's no more data to read at that moment. These chunks aren't automatically
1560+
concatenated. Because a single `read()` call does not return all the data, using
1561+
a while loop may be necessary to continuously read chunks until all data is retrieved.
1562+
When reading a large file, `.read()` might return `null` temporarily, indicating
1563+
that it has consumed all buffered content but there may be more data yet to be
1564+
buffered. In such cases, a new `'readable'` event is emitted once there's more
1565+
data in the buffer, and the `'end'` event signifies the end of data transmission.
15631566

15641567
Therefore to read a file's whole contents from a `readable`, it is necessary
15651568
to collect chunks across multiple `'readable'` events:

0 commit comments

Comments
 (0)