Skip to content

Commit a0cfa62

Browse files
anentropicMylesBorins
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 e818455 commit a0cfa62

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
@@ -2259,21 +2259,7 @@ The `process.stdin` property returns a stream connected to
22592259
stream) unless fd `0` refers to a file, in which case it is
22602260
a [Readable][] stream.
22612261

2262-
```js
2263-
process.stdin.setEncoding('utf8');
2264-
2265-
process.stdin.on('readable', () => {
2266-
let chunk;
2267-
// Use a loop to make sure we read all available data.
2268-
while ((chunk = process.stdin.read()) !== null) {
2269-
process.stdout.write(`data: ${chunk}`);
2270-
}
2271-
});
2272-
2273-
process.stdin.on('end', () => {
2274-
process.stdout.write('end');
2275-
});
2276-
```
2262+
For details of how to read from `stdin` see [`readable.read()`][].
22772263

22782264
As a [Duplex][] stream, `process.stdin` can also be used in "old" mode that
22792265
is compatible with scripts written for Node.js prior to v0.10.
@@ -2624,6 +2610,7 @@ cases:
26242610
[Event Loop]: https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#process-nexttick
26252611
[LTS]: https://github.com/nodejs/Release
26262612
[Readable]: stream.html#stream_readable_streams
2613+
[`readable.read()`]: stream.html#stream_readable_read_size
26272614
[Signal Events]: #process_signal_events
26282615
[Stream compatibility]: stream.html#stream_compatibility_with_older_node_js_versions
26292616
[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)