-
-
Notifications
You must be signed in to change notification settings - Fork 32k
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
base: main
Are you sure you want to change the base?
Changes from all commits
a3158f4
53db798
7833ca7
7287665
d0f4463
aa5d90f
fc85d5b
a65ced5
655f052
7c4c796
3b85e0b
e986407
b891487
52dd182
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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`. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
|
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' })); |
Uh oh!
There was an error while loading. Please reload this page.