Skip to content

Commit ae5930b

Browse files
benglMylesBorins
authored andcommitted
tty,doc: add type-check to isatty
Previously, various inputs other than non-negative integers would produce incorrect results. Added type-checking on input, returning false for anything other than non-negative integers. Also clarified in docs. PR-URL: #15567 Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
1 parent 5095b99 commit ae5930b

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

doc/api/tty.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,5 @@ added: v0.5.8
123123
* `fd` {number} A numeric file descriptor
124124

125125
The `tty.isatty()` method returns `true` if the given `fd` is associated with
126-
a TTY and `false` if is not.
126+
a TTY and `false` if it is not, including whenever `fd` is not a non-negative
127+
integer.

lib/tty.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const readline = require('readline');
1010

1111

1212
exports.isatty = function(fd) {
13-
return isTTY(fd);
13+
return Number.isInteger(fd) && fd >= 0 && isTTY(fd);
1414
};
1515

1616

test/pseudo-tty/test-tty-isatty.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
require('../common');
4+
const { strictEqual } = require('assert');
5+
const { isatty } = require('tty');
6+
7+
strictEqual(isatty(0), true, 'stdin reported to not be a tty, but it is');
8+
strictEqual(isatty(1), true, 'stdout reported to not be a tty, but it is');
9+
strictEqual(isatty(2), true, 'stderr reported to not be a tty, but it is');
10+
11+
strictEqual(isatty(-1), false, '-1 reported to be a tty, but it is not');
12+
strictEqual(isatty(55555), false, '55555 reported to be a tty, but it is not');
13+
strictEqual(isatty(1.1), false, '1.1 reported to be a tty, but it is not');
14+
strictEqual(isatty('1'), false, '\'1\' reported to be a tty, but it is not');
15+
strictEqual(isatty({}), false, '{} reported to be a tty, but it is not');
16+
strictEqual(isatty(() => {}), false,
17+
'() => {} reported to be a tty, but it is not');

test/pseudo-tty/test-tty-isatty.out

Whitespace-only changes.

0 commit comments

Comments
 (0)