Skip to content

Commit 4982550

Browse files
committed
tty: set blocking by default, api for unblocking
Builds on nodejs#6816, adds API for explicitly setting non-blocking mode without relying on `_`-prefixed internal property. Adds documentation.
1 parent 78520fa commit 4982550

File tree

2 files changed

+43
-5
lines changed

2 files changed

+43
-5
lines changed

doc/api/tty.md

+26-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
The `tty` module houses the `tty.ReadStream` and `tty.WriteStream` classes. In
66
most cases, you will not need to use this module directly.
77

8-
When Node.js detects that it is being run inside a TTY context, then `process.stdin`
9-
will be a `tty.ReadStream` instance and `process.stdout` will be
8+
When Node.js detects that it is being run inside a TTY context, then
9+
`process.stdin` will be a `tty.ReadStream` instance and `process.stdout` will be
1010
a `tty.WriteStream` instance. The preferred way to check if Node.js is being run
1111
in a TTY context is to check `process.stdout.isTTY`:
1212

@@ -25,8 +25,8 @@ Node.js program (only when `isatty(0)` is true).
2525

2626
### rs.isRaw
2727

28-
A `Boolean` that is initialized to `false`. It represents the current "raw" state
29-
of the `tty.ReadStream` instance.
28+
A `Boolean` that is initialized to `false`. It represents the current "raw"
29+
state of the `tty.ReadStream` instance.
3030

3131
### rs.setRawMode(mode)
3232

@@ -40,6 +40,17 @@ A `net.Socket` subclass that represents the writable portion of a tty. In normal
4040
circumstances, `process.stdout` will be the only `tty.WriteStream` instance
4141
ever created (and only when `isatty(1)` is true).
4242

43+
### Constructor: new fs.WriteStream(fd[, options])
44+
45+
* `fd` {Number} The numeric file descriptor for this TTY instance.
46+
* `options` {Object}
47+
* `blocking` {boolean} `true` if writes to the TTY should be blocking,
48+
`false` otherwise. Defaults to `true`.
49+
50+
Creates a new `tty.WriteStream`. By default, writes to the TTY will be blocking.
51+
Use `new fs.WriteStream(fd, {blocking: false})` to create the `fs.WriteStream`
52+
using non-blocking writes by default.
53+
4354
### Event: 'resize'
4455

4556
`function () {}`
@@ -64,6 +75,17 @@ gets updated on `'resize'` events.
6475
A `Number` that gives the number of rows the TTY currently has. This property
6576
gets updated on `'resize'` events.
6677

78+
### ws.setNonBlocking([bool])
79+
80+
* `bool` {boolean} `true` to set the TTY to non-blocking writes, `false`
81+
otherwise. Defaults to `true`.
82+
83+
Returns a reference to the `tty.WriteStream` so calls can be chained.
84+
85+
```js
86+
const myNonBlockingTTY = new tty.WriteStream(myFd).setNonBlocking();
87+
```
88+
6789
## tty.isatty(fd)
6890

6991
Returns `true` or `false` depending on if the `fd` is associated with a

lib/tty.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,23 @@ ReadStream.prototype.setRawMode = function(flag) {
4040
};
4141

4242

43-
function WriteStream(fd) {
43+
function WriteStream(fd, options) {
4444
if (!(this instanceof WriteStream)) return new WriteStream(fd);
4545
net.Socket.call(this, {
4646
handle: new TTY(fd, false),
4747
readable: false,
4848
writable: true
4949
});
5050

51+
options = options || {};
52+
if (options.blocking === undefined) options.blocking = true;
53+
54+
// Prevents interleaved stdout/stderr output in terminals.
55+
// As noted in the following reference, TTYs tend to be quite fast and this
56+
// behaviour has become expected due to how it has functioned historically.
57+
// Ref: https://github.com/nodejs/node/pull/1771#issuecomment-119351671
58+
this._handle.setBlocking(options.blocking);
59+
5160
var winSize = [];
5261
var err = this._handle.getWindowSize(winSize);
5362
if (!err) {
@@ -58,6 +67,13 @@ function WriteStream(fd) {
5867
inherits(WriteStream, net.Socket);
5968
exports.WriteStream = WriteStream;
6069

70+
// Because the default is for the WriteStream to be blocking,
71+
// this method is intentionally named setNonBlocking instead
72+
// of bubbling up the underlying setBlocking name from the handle.
73+
WriteStream.prototype.setNonBlocking = function(val = true) {
74+
this._handle.setBlocking(!val);
75+
return this;
76+
};
6177

6278
WriteStream.prototype.isTTY = true;
6379

0 commit comments

Comments
 (0)