Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7166415

Browse files
anentropicaddaleax
authored andcommittedSep 22, 2020
doc: clarify how to read process.stdin
document more clearly that stdin will emit multiple readable events PR-URL: #27350 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 25939cc commit 7166415

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed
 

‎doc/api/process.md

+2-15
Original file line numberDiff line numberDiff line change
@@ -2226,21 +2226,7 @@ The `process.stdin` property returns a stream connected to
22262226
stream) unless fd `0` refers to a file, in which case it is
22272227
a [Readable][] stream.
22282228

2229-
```js
2230-
process.stdin.setEncoding('utf8');
2231-
2232-
process.stdin.on('readable', () => {
2233-
let chunk;
2234-
// Use a loop to make sure we read all available data.
2235-
while ((chunk = process.stdin.read()) !== null) {
2236-
process.stdout.write(`data: ${chunk}`);
2237-
}
2238-
});
2239-
2240-
process.stdin.on('end', () => {
2241-
process.stdout.write('end');
2242-
});
2243-
```
2229+
For details of how to read from `stdin` see [`readable.read()`][].
22442230

22452231
As a [Duplex][] stream, `process.stdin` can also be used in "old" mode that
22462232
is compatible with scripts written for Node.js prior to v0.10.
@@ -2594,6 +2580,7 @@ cases:
25942580
[Event Loop]: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#process-nexttick
25952581
[LTS]: https://github.com/nodejs/Release
25962582
[Readable]: stream.html#stream_readable_streams
2583+
[`readable.read()`]: stream.html#stream_readable_read_size
25972584
[Signal Events]: #process_signal_events
25982585
[Stream compatibility]: stream.html#stream_compatibility_with_older_node_js_versions
25992586
[TTY]: tty.html#tty_tty

‎doc/api/stream.md

+35-4
Original file line numberDiff line numberDiff line change
@@ -1108,17 +1108,48 @@ automatically until the internal buffer is fully drained.
11081108

11091109
```js
11101110
const readable = getReadableStreamSomehow();
1111+
1112+
// 'readable' may be triggered multiple times as data is buffered in
11111113
readable.on('readable', () => {
11121114
let chunk;
1115+
console.log('Stream is readable (new data received in buffer)');
1116+
// Use a loop to make sure we read all currently available data
11131117
while (null !== (chunk = readable.read())) {
1114-
console.log(`Received ${chunk.length} bytes of data.`);
1118+
console.log(`Read ${chunk.length} bytes of data...`);
11151119
}
11161120
});
1121+
1122+
// 'end' will be triggered once when there is no more data available
1123+
readable.on('end', () => {
1124+
console.log('Reached end of stream.');
1125+
});
11171126
```
11181127

1119-
The `while` loop is necessary when processing data with
1120-
`readable.read()`. Only after `readable.read()` returns `null`,
1121-
[`'readable'`][] will be emitted.
1128+
Each call to `readable.read()` returns a chunk of data, or `null`. The chunks
1129+
are not concatenated. A `while` loop is necessary to consume all data
1130+
currently in the buffer. When reading a large file `.read()` may return `null`,
1131+
having consumed all buffered content so far, but there is still more data to
1132+
come not yet buffered. In this case a new `'readable'` event will be emitted
1133+
when there is more data in the buffer. Finally the `'end'` event will be
1134+
emitted when there is no more data to come.
1135+
1136+
Therefore to read a file's whole contents from a `readable`, it is necessary
1137+
to collect chunks across multiple `'readable'` events:
1138+
1139+
```js
1140+
const chunks = [];
1141+
1142+
readable.on('readable', () => {
1143+
let chunk;
1144+
while (null !== (chunk = readable.read())) {
1145+
chunks.push(chunk);
1146+
}
1147+
});
1148+
1149+
readable.on('end', () => {
1150+
const content = chunks.join('');
1151+
});
1152+
```
11221153

11231154
A `Readable` stream in object mode will always return a single item from
11241155
a call to [`readable.read(size)`][stream-read], regardless of the value of the

0 commit comments

Comments
 (0)
Please sign in to comment.