Skip to content

os: expose guessFileDescriptorType #58060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 26 additions & 0 deletions doc/api/os.md
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,32 @@ On POSIX systems, the operating system release is determined by calling
available, `GetVersionExW()` will be used. See
<https://en.wikipedia.org/wiki/Uname#Examples> for more information.

## `os.guessFileDescriptorType(fd)`

<!-- YAML
added: REPLACEME
-->

* `fd` {integer} The file descriptor number to try and guess the type of.

* Returns: {string|null}

Returns the type of the file descriptor passed in, or `null` if the provided file descriptor
is invalid.
Copy link
Member

Choose a reason for hiding this comment

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

Nit: some expanded explanation about why this is useful would probably be helpful. It's rather obscure.

Copy link
Author

Choose a reason for hiding this comment

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

I tried to improve the documentation, but I'm not sure if this is good 😅

A common use case for this function is checking whether standard input is passed into your process,
and if it is, if it can be consumed by the process. For example, on Unix systems, if the type is `TTY`, it means
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
available, but you shouldn't try to prompt for more.

Currently, the following types for a file descriptor can be returned:

* `'TCP'`
* `'TTY'`
* `'UDP'`
* `'FILE'`
* `'PIPE'`
* `'UNKNOWN'`

## OS constants

The following constants are exported by `os.constants`.
Expand Down
8 changes: 7 additions & 1 deletion lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const {

const {
codes: {
ERR_INVALID_FD,
ERR_NO_CRYPTO,
ERR_NO_TYPESCRIPT,
ERR_UNKNOWN_SIGNAL,
Expand Down Expand Up @@ -876,9 +877,14 @@ function getCIDR(address, netmask, family) {
}

const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN'];

function guessHandleType(fd) {
if (typeof fd !== 'number' || fd >> 0 !== fd || fd < 0) {
throw new ERR_INVALID_FD(fd);
}

const type = _guessHandleType(fd);
return handleTypes[type];
return handleTypes[type] || type;
}

class WeakReference {
Expand Down
3 changes: 2 additions & 1 deletion lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const {
},
hideStackFrames,
} = require('internal/errors');
const { getCIDR } = require('internal/util');
const { getCIDR, guessHandleType: guessFileDescriptorType } = require('internal/util');
const { validateInt32 } = require('internal/validators');

const {
Expand Down Expand Up @@ -328,6 +328,7 @@ module.exports = {
uptime: getUptime,
version: getOSVersion,
machine: getMachine,
guessFileDescriptorType,
};

ObjectDefineProperties(module.exports, {
Expand Down
12 changes: 10 additions & 2 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,23 @@ static uint32_t GetUVHandleTypeCode(const uv_handle_type type) {
case UV_UNKNOWN_HANDLE:
return 5;
default:
ABORT();
// For an unhandled handle type, we want to return `UNKNOWN` instead of
// `null` since the type is "known" by UV, just not exposed further to
// JS land
return 5;
Copy link
Member

Choose a reason for hiding this comment

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

Nit... for later... not something to do in this PR... these really ought to be defined in an enum

Copy link
Author

Choose a reason for hiding this comment

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

I would do it if I knew how 😅, and there is a TODO for that already in the function

}
}

static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
int fd;
if (!args[0]->Int32Value(env->context()).To(&fd)) return;
CHECK_GE(fd, 0);

// If the provided file descriptor is not valid, we return null
if (fd < 0) [[unlikely]] {
args.GetReturnValue().Set(v8::Null(env->isolate()));
return;
}

uv_handle_type t = uv_guess_handle(fd);
args.GetReturnValue().Set(GetUVHandleTypeCode(t));
Expand Down
28 changes: 28 additions & 0 deletions test/pseudo-tty/test-os-guessFileDescriptorType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict';

require('../common');
const { strictEqual, throws } = require('assert');
const { guessFileDescriptorType } = require('os');

strictEqual(guessFileDescriptorType(0), 'TTY', 'stdin reported to not be a tty, but it is');
strictEqual(guessFileDescriptorType(1), 'TTY', 'stdout reported to not be a tty, but it is');
strictEqual(guessFileDescriptorType(2), 'TTY', 'stderr reported to not be a tty, but it is');

strictEqual(guessFileDescriptorType(55555), 'UNKNOWN', '55555 reported to be a handle, but it is not');
strictEqual(guessFileDescriptorType(2 ** 31 - 1), 'UNKNOWN', '2^31-1 reported to be a handle, but it is not');

[
-1,
1.1,
'1',
[],
{},
() => {},
2 ** 31,
true,
false,
1n,
Symbol(),
undefined,
null,
].forEach((val) => throws(() => guessFileDescriptorType(val), { code: 'ERR_INVALID_FD' }));
Empty file.