-
-
Notifications
You must be signed in to change notification settings - Fork 35.3k
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
2827aaa
35b42c2
fe58d8b
e8940d8
8f2da7c
68b1b96
d2ae448
b374862
89497c9
7190ab0
c6421fd
98b1ad3
5246a4c
8481ecf
520693d
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. | ||
|
Member
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.
Author
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 |
|---|---|---|
|
|
@@ -215,7 +215,10 @@ 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; | ||
|
Member
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
Author
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 |
||
| } | ||
| } | ||
|
|
||
|
|
@@ -224,7 +227,12 @@ static void GuessHandleType(const FunctionCallbackInfo<Value>& args) { | |
| Local<Context> context = isolate->GetCurrentContext(); | ||
| int fd; | ||
| if (!args[0]->Int32Value(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(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 assert = require('node:assert'); | ||
| const { guessFileDescriptorType } = require('os'); | ||
|
|
||
| assert.strictEqual(guessFileDescriptorType(0), 'TTY'); | ||
| assert.strictEqual(guessFileDescriptorType(1), 'TTY'); | ||
| assert.strictEqual(guessFileDescriptorType(2), 'TTY'); | ||
|
|
||
| assert.strictEqual(guessFileDescriptorType(55555), 'UNKNOWN'); | ||
| assert.strictEqual(guessFileDescriptorType(2 ** 31 - 1), 'UNKNOWN'); | ||
|
|
||
| [ | ||
| -1, | ||
| 1.1, | ||
| '1', | ||
| [], | ||
| {}, | ||
| () => {}, | ||
| 2 ** 31, | ||
| true, | ||
| false, | ||
| 1n, | ||
| Symbol(), | ||
| undefined, | ||
| null, | ||
| ].forEach((val) => assert.throws(() => guessFileDescriptorType(val), { code: 'ERR_INVALID_FD' })); |
Uh oh!
There was an error while loading. Please reload this page.