Skip to content

Commit c06c228

Browse files
committed
chore: requested changes
1 parent 356995f commit c06c228

4 files changed

Lines changed: 18 additions & 16 deletions

File tree

doc/api/os.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,9 @@ added: REPLACEME
516516

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

519-
* Returns: {string}
519+
* Returns: {string|null}
520520

521-
Returns the type of the file descriptor passed in, or `'INVALID'` if the provided file descriptor
521+
Returns the type of the file descriptor passed in, or `null` if the provided file descriptor
522522
is invalid.
523523

524524
Currently, the following types for a file descriptor can be returned:
@@ -529,7 +529,6 @@ Currently, the following types for a file descriptor can be returned:
529529
* `'FILE'`
530530
* `'PIPE'`
531531
* `'UNKNOWN'`
532-
* `'INVALID'`
533532

534533
## OS constants
535534

lib/internal/util.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const {
5252

5353
const {
5454
codes: {
55+
ERR_INVALID_FD,
5556
ERR_NO_CRYPTO,
5657
ERR_NO_TYPESCRIPT,
5758
ERR_UNKNOWN_SIGNAL,
@@ -899,11 +900,11 @@ function getCIDR(address, netmask, family) {
899900
}
900901

901902
const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN'];
902-
setOwnProperty(handleTypes, -1, 'INVALID');
903+
setOwnProperty(handleTypes, -1, null);
903904

904905
function guessHandleType(fd) {
905-
if (!NumberIsInteger(fd)) {
906-
return 'INVALID';
906+
if (fd >> 0 !== fd || fd < 0) {
907+
throw new ERR_INVALID_FD(fd);
907908
}
908909

909910
const type = _guessHandleType(fd);

src/node_util.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ static uint32_t GetUVHandleTypeCode(const uv_handle_type type) {
208208
return 5;
209209
default:
210210
// For an unhandled handle type, we want to return `UNKNOWN` instead of
211-
// `INVALID` since the type is "known" by UV, just not exposed further to
211+
// `null` since the type is "known" by UV, just not exposed further to
212212
// JS land
213213
return 5;
214214
}
@@ -221,7 +221,7 @@ static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
221221
if (!args[0]->Int32Value(context).To(&fd)) return;
222222

223223
// If the provided file descriptor is not valid, we return `-1`, which in JS
224-
// land will be marked as "INVALID"
224+
// land will be marked as null
225225
if (fd < 0) [[unlikely]] {
226226
args.GetReturnValue().Set(-1);
227227
return;
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
'use strict';
22

33
require('../common');
4-
const { strictEqual } = require('assert');
4+
const { strictEqual, throws } = require('assert');
55
const { guessFileDescriptorType } = require('os');
66

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

11-
strictEqual(guessFileDescriptorType(-1), 'INVALID', '-1 reported to be a tty, but it is not');
12-
strictEqual(guessFileDescriptorType(55555), 'UNKNOWN', '55555 reported to be a tty, but it is not');
13-
strictEqual(guessFileDescriptorType(2 ** 31), 'INVALID', '2^31 reported to be a tty, but it is not');
14-
strictEqual(guessFileDescriptorType(1.1), 'INVALID', '1.1 reported to be a tty, but it is not');
15-
strictEqual(guessFileDescriptorType('1'), 'INVALID', '\'1\' reported to be a tty, but it is not');
16-
strictEqual(guessFileDescriptorType({}), 'INVALID', '{} reported to be a tty, but it is not');
17-
strictEqual(guessFileDescriptorType(() => {}), 'INVALID', '() => {} reported to be a tty, but it is not');
11+
strictEqual(guessFileDescriptorType(55555), 'UNKNOWN', '55555 reported to be a handle, but it is not');
12+
strictEqual(guessFileDescriptorType(2 ** 31 - 1), 'UNKNOWN', '2^31-1 reported to be a handle, but it is not');
13+
14+
throws(() => guessFileDescriptorType(-1), /"fd" must be a positive integer/, '-1 reported to be a handle, but it is not');
15+
throws(() => guessFileDescriptorType(1.1), /"fd" must be a positive integer/, '1.1 reported to be a handle, but it is not');
16+
throws(() => guessFileDescriptorType('1'), /"fd" must be a positive integer/, '\'1\' reported to be a tty, but it is not');
17+
throws(() => guessFileDescriptorType({}), /"fd" must be a positive integer/, '{} reported to be a tty, but it is not');
18+
throws(() => guessFileDescriptorType(() => {}), /"fd" must be a positive integer/, '() => {} reported to be a tty, but it is not');
19+
throws(() => guessFileDescriptorType(2 ** 31), /"fd" must be a positive integer/, '2^31 reported to be a handle, but it is not (because the fd check rolls over the input to negative of it)');

0 commit comments

Comments
 (0)