Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
document more clearly that stdin will emit multiple readable events
  • Loading branch information
anentropic committed Apr 9, 2020
commit 990e4a6c7d47ce6c0e9c8f984cb3157c4b75a5a7
42 changes: 35 additions & 7 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -2254,29 +2254,57 @@ The `process.stdin` property returns a stream connected to
stream) unless fd `0` refers to a file, in which case it is
a [Readable][] stream.

As a [Duplex][] stream, `process.stdin` can also be used in "old" mode that
is compatible with scripts written for Node.js prior to v0.10.
For more information see [Stream compatibility][].

In "old" streams mode the `stdin` stream is paused by default, so one
must call `process.stdin.resume()` to read from it. Note also that calling
`process.stdin.resume()` itself would switch stream to "old" mode.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer we did not mention "old" stream mode in this docs. A user can add a 'data' event to start reading as well, it's part of the documented API.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This text is not something I added btw, I just moved it around. I'm happy to remove it though. Please be very specific about which part to remove or any alternative wording (I am just a casual node.js user so I don't have a good idea what is appropriate)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mcollina PTAL

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok! let's keep the text for now.


```js
process.stdin.setEncoding('utf8');

// 'readable' may be triggered multiple times as data is buffered in
process.stdin.on('readable', () => {
let chunk;
// Use a loop to make sure we read all available data.
// Use a loop to make sure we read all currently available data
while ((chunk = process.stdin.read()) !== null) {
process.stdout.write(`data: ${chunk}`);
}
});

// 'end' will be triggered once when there is no more data available
process.stdin.on('end', () => {
process.stdout.write('end');
});
```

As a [Duplex][] stream, `process.stdin` can also be used in "old" mode that
is compatible with scripts written for Node.js prior to v0.10.
For more information see [Stream compatibility][].
Each call to `stdin.read()` returns a chunk of data. The chunks are not
concatenated. A `while` loop is necessary to consume all data currently in the
buffer. When reading a large file `.read()` may return `null`, having
consumed all buffered content so far, but there is still more data to come not
yet buffered. In this case a new `'readable'` event will be emitted when there
is more data in the buffer. Finally the `'end'` event will be emitted when
there is no more data to come.

In "old" streams mode the `stdin` stream is paused by default, so one
must call `process.stdin.resume()` to read from it. Note also that calling
`process.stdin.resume()` itself would switch stream to "old" mode.
Therefore to read a file's whole contents from `stdin` you need to collect
chunks across multiple `'readable'` events, something like:
Comment thread
anentropic marked this conversation as resolved.
Outdated

```js
var chunks = [];

process.stdin.on('readable', () => {
let chunk;
while ((chunk = process.stdin.read()) !== null) {
chunks.push(chunk);
}
});

process.stdin.on('end', () => {
let content = chunks.join('');
});
```

### `process.stdin.fd`

Expand Down