Skip to content

Commit 7c9df66

Browse files
anentropicjasnell
authored andcommitted
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 cfa3d8f commit 7c9df66

File tree

2 files changed

+37
-19
lines changed

2 files changed

+37
-19
lines changed

doc/api/process.md

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,21 +2281,7 @@ The `process.stdin` property returns a stream connected to
22812281
stream) unless fd `0` refers to a file, in which case it is
22822282
a [Readable][] stream.
22832283

2284-
```js
2285-
process.stdin.setEncoding('utf8');
2286-
2287-
process.stdin.on('readable', () => {
2288-
let chunk;
2289-
// Use a loop to make sure we read all available data.
2290-
while ((chunk = process.stdin.read()) !== null) {
2291-
process.stdout.write(`data: ${chunk}`);
2292-
}
2293-
});
2294-
2295-
process.stdin.on('end', () => {
2296-
process.stdout.write('end');
2297-
});
2298-
```
2284+
For details of how to read from `stdin` see [`readable.read()`][].
22992285

23002286
As a [Duplex][] stream, `process.stdin` can also be used in "old" mode that
23012287
is compatible with scripts written for Node.js prior to v0.10.
@@ -2646,6 +2632,7 @@ cases:
26462632
[Event Loop]: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#process-nexttick
26472633
[LTS]: https://github.com/nodejs/Release
26482634
[Readable]: stream.html#stream_readable_streams
2635+
[`readable.read()`]: stream.html#stream_readable_read_size
26492636
[Signal Events]: #process_signal_events
26502637
[Stream compatibility]: stream.html#stream_compatibility_with_older_node_js_versions
26512638
[TTY]: tty.html#tty_tty

doc/api/stream.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,17 +1127,48 @@ automatically until the internal buffer is fully drained.
11271127

11281128
```js
11291129
const readable = getReadableStreamSomehow();
1130+
1131+
// 'readable' may be triggered multiple times as data is buffered in
11301132
readable.on('readable', () => {
11311133
let chunk;
1134+
console.log('Stream is readable (new data received in buffer)');
1135+
// Use a loop to make sure we read all currently available data
11321136
while (null !== (chunk = readable.read())) {
1133-
console.log(`Received ${chunk.length} bytes of data.`);
1137+
console.log(`Read ${chunk.length} bytes of data...`);
11341138
}
11351139
});
1140+
1141+
// 'end' will be triggered once when there is no more data available
1142+
readable.on('end', () => {
1143+
console.log('Reached end of stream.');
1144+
});
11361145
```
11371146

1138-
The `while` loop is necessary when processing data with
1139-
`readable.read()`. Only after `readable.read()` returns `null`,
1140-
[`'readable'`][] will be emitted.
1147+
Each call to `readable.read()` returns a chunk of data, or `null`. The chunks
1148+
are not concatenated. A `while` loop is necessary to consume all data
1149+
currently in the buffer. When reading a large file `.read()` may return `null`,
1150+
having consumed all buffered content so far, but there is still more data to
1151+
come not yet buffered. In this case a new `'readable'` event will be emitted
1152+
when there is more data in the buffer. Finally the `'end'` event will be
1153+
emitted when there is no more data to come.
1154+
1155+
Therefore to read a file's whole contents from a `readable`, it is necessary
1156+
to collect chunks across multiple `'readable'` events:
1157+
1158+
```js
1159+
const chunks = [];
1160+
1161+
readable.on('readable', () => {
1162+
let chunk;
1163+
while (null !== (chunk = readable.read())) {
1164+
chunks.push(chunk);
1165+
}
1166+
});
1167+
1168+
readable.on('end', () => {
1169+
const content = chunks.join('');
1170+
});
1171+
```
11411172

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

0 commit comments

Comments
 (0)