@@ -2254,6 +2254,8 @@ The `process.stdin` property returns a stream connected to
2254
2254
stream) unless fd ` 0 ` refers to a file, in which case it is
2255
2255
a [ Readable] [ ] stream.
2256
2256
2257
+ For details of how to read from ` stdin ` see [ ` readable.read() ` ] [ ] .
2258
+
2257
2259
As a [ Duplex] [ ] stream, ` process.stdin ` can also be used in "old" mode that
2258
2260
is compatible with scripts written for Node.js prior to v0.10.
2259
2261
For more information see [ Stream compatibility] [ ] .
@@ -2262,50 +2264,6 @@ In "old" streams mode the `stdin` stream is paused by default, so one
2262
2264
must call ` process.stdin.resume() ` to read from it. Note also that calling
2263
2265
` process.stdin.resume() ` itself would switch stream to "old" mode.
2264
2266
2265
- ``` js
2266
- process .stdin .setEncoding (' utf8' );
2267
-
2268
- // 'readable' may be triggered multiple times as data is buffered in
2269
- process .stdin .on (' readable' , () => {
2270
- let chunk;
2271
- // Use a loop to make sure we read all currently available data
2272
- while ((chunk = process .stdin .read ()) !== null ) {
2273
- process .stdout .write (` data: ${ chunk} ` );
2274
- }
2275
- });
2276
-
2277
- // 'end' will be triggered once when there is no more data available
2278
- process .stdin .on (' end' , () => {
2279
- process .stdout .write (' end' );
2280
- });
2281
- ```
2282
-
2283
- Each call to ` stdin.read() ` returns a chunk of data. The chunks are not
2284
- concatenated. A ` while ` loop is necessary to consume all data currently in the
2285
- buffer. When reading a large file ` .read() ` may return ` null ` , having
2286
- consumed all buffered content so far, but there is still more data to come not
2287
- yet buffered. In this case a new ` 'readable' ` event will be emitted when there
2288
- is more data in the buffer. Finally the ` 'end' ` event will be emitted when
2289
- there is no more data to come.
2290
-
2291
- Therefore to read a file's whole contents from ` stdin ` you need to collect
2292
- chunks across multiple ` 'readable' ` events, something like:
2293
-
2294
- ``` js
2295
- var chunks = [];
2296
-
2297
- process .stdin .on (' readable' , () => {
2298
- let chunk;
2299
- while ((chunk = process .stdin .read ()) !== null ) {
2300
- chunks .push (chunk);
2301
- }
2302
- });
2303
-
2304
- process .stdin .on (' end' , () => {
2305
- let content = chunks .join (' ' );
2306
- });
2307
- ```
2308
-
2309
2267
### ` process.stdin.fd `
2310
2268
2311
2269
* {number}
@@ -2639,6 +2597,7 @@ cases:
2639
2597
[ Event Loop ] : https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/#process-nexttick
2640
2598
[ LTS ] : https://github.com/nodejs/Release
2641
2599
[ Readable ] : stream.html#stream_readable_streams
2600
+ [ `readable.read()` ] : stream.html#stream_readable_read_size
2642
2601
[ Signal Events ] : #process_signal_events
2643
2602
[ Stream compatibility ] : stream.html#stream_compatibility_with_older_node_js_versions
2644
2603
[ TTY ] : tty.html#tty_tty
0 commit comments