Skip to content

Commit 82d6f5d

Browse files
vladfranguaduh95
authored andcommitted
os: expose guessFileDescriptorType
Exposes the internal `guessHandleType` function as `guessFileDescriptorType`, which can be used to see if a handle has a specific type, regardless of the OS it is on. This helps out with detecting, for example, if standard input is piped into the process, instead of relying on file system calls. Refs: #57603 Signed-off-by: Vlad Frangu <me@vladfrangu.dev> PR-URL: #58060 Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Aviv Keller <me@aviv.sh>
1 parent 6bde4f8 commit 82d6f5d

6 files changed

Lines changed: 67 additions & 2 deletions

File tree

‎doc/api/os.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,33 @@ On POSIX systems, the operating system release is determined by calling
504504
available, `GetVersionExW()` will be used. See
505505
<https://en.wikipedia.org/wiki/Uname#Examples> for more information.
506506

507+
## `os.guessFileDescriptorType(fd)`
508+
509+
<!-- YAML
510+
added: REPLACEME
511+
-->
512+
513+
* `fd` {integer} The file descriptor number to try and guess the type of.
514+
515+
* Returns: {string}
516+
517+
Returns the type of the file descriptor passed in. If `fd` is not a non-negative
518+
32-bit integer, an [`ERR_INVALID_FD`][] error is thrown. If `fd` does not refer
519+
to an open file descriptor, `'UNKNOWN'` is returned.
520+
A common use case for this function is checking whether standard input is passed into your process,
521+
and if it is, if it can be consumed by the process. For example, on Unix systems, if the type is `TTY`, it means
522+
you can prompt the user for new data while the process is running, and if it's `FILE` or `PIPE`, it means there is data
523+
available, but you shouldn't try to prompt for more.
524+
525+
Currently, the following types for a file descriptor can be returned:
526+
527+
* `'TCP'`
528+
* `'TTY'`
529+
* `'UDP'`
530+
* `'FILE'`
531+
* `'PIPE'`
532+
* `'UNKNOWN'`
533+
507534
## OS constants
508535

509536
The following constants are exported by `os.constants`.
@@ -1370,6 +1397,7 @@ The following process scheduling constants are exported by
13701397

13711398
[Android building]: https://github.com/nodejs/node/blob/HEAD/BUILDING.md#android
13721399
[EUID]: https://en.wikipedia.org/wiki/User_identifier#Effective_user_ID
1400+
[`ERR_INVALID_FD`]: errors.md#err_invalid_fd
13731401
[`SystemError`]: errors.md#class-systemerror
13741402
[`process.arch`]: process.md#processarch
13751403
[`process.platform`]: process.md#processplatform

‎lib/internal/util.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ const {
5050

5151
const {
5252
codes: {
53+
ERR_INVALID_FD,
5354
ERR_NO_CRYPTO,
5455
ERR_NO_TYPESCRIPT,
5556
ERR_UNKNOWN_SIGNAL,
@@ -861,7 +862,12 @@ function getCIDR(address, netmask, family) {
861862
}
862863

863864
const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN'];
865+
864866
function guessHandleType(fd) {
867+
if (typeof fd !== 'number' || fd >> 0 !== fd || fd < 0) {
868+
throw new ERR_INVALID_FD(fd);
869+
}
870+
865871
const type = _guessHandleType(fd);
866872
return handleTypes[type];
867873
}

‎lib/os.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const {
4040
},
4141
hideStackFrames,
4242
} = require('internal/errors');
43-
const { getCIDR } = require('internal/util');
43+
const { getCIDR, guessHandleType: guessFileDescriptorType } = require('internal/util');
4444
const { validateInt32 } = require('internal/validators');
4545

4646
const {
@@ -329,6 +329,7 @@ module.exports = {
329329
uptime: getUptime,
330330
version: getOSVersion,
331331
machine: getMachine,
332+
guessFileDescriptorType,
332333
};
333334

334335
ObjectFreeze(constants.signals);

‎src/node_util.cc‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,9 @@ static uint32_t GetUVHandleTypeCode(const uv_handle_type type) {
209209
case UV_UNKNOWN_HANDLE:
210210
return 5;
211211
default:
212-
ABORT();
212+
// The type is known by libuv, just not exposed to JS land, so report it
213+
// as `UNKNOWN`.
214+
return 5;
213215
}
214216
}
215217

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
3+
require('../common');
4+
const assert = require('node:assert');
5+
const { guessFileDescriptorType } = require('os');
6+
7+
assert.strictEqual(guessFileDescriptorType(0), 'TTY');
8+
assert.strictEqual(guessFileDescriptorType(1), 'TTY');
9+
assert.strictEqual(guessFileDescriptorType(2), 'TTY');
10+
11+
assert.strictEqual(guessFileDescriptorType(55555), 'UNKNOWN');
12+
assert.strictEqual(guessFileDescriptorType(2 ** 31 - 1), 'UNKNOWN');
13+
14+
[
15+
-1,
16+
1.1,
17+
'1',
18+
[],
19+
{},
20+
() => {},
21+
2 ** 31,
22+
true,
23+
false,
24+
1n,
25+
Symbol(),
26+
undefined,
27+
null,
28+
].forEach((val) => assert.throws(() => guessFileDescriptorType(val), { code: 'ERR_INVALID_FD' }));

‎test/pseudo-tty/test-os-guessFileDescriptorType.out‎

Whitespace-only changes.

0 commit comments

Comments
 (0)