Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7166415

Browse filesBrowse files
anentropicaddaleax
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 <matteo.collina@gmail.com> Reviewed-By: Robert Nagy <ronagy@icloud.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 25939cc commit 7166415
Copy full SHA for 7166415

File tree

Expand file treeCollapse file tree

2 files changed

+37
-19
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+37
-19
lines changed
Open diff view settings
Collapse file

‎doc/api/process.md‎

Copy file name to clipboardExpand all lines: doc/api/process.md
+2-15Lines changed: 2 additions & 15 deletions
  • Display the source diff
  • Display the rich diff
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
Collapse file

‎doc/api/stream.md‎

Copy file name to clipboardExpand all lines: doc/api/stream.md
+35-4Lines changed: 35 additions & 4 deletions
  • Display the source diff
  • Display the rich diff
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.