Skip to content

Commit d803730

Browse files
committed
chore: requested changes
1 parent 404707c commit d803730

File tree

4 files changed

+18
-16
lines changed

4 files changed

+18
-16
lines changed

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
@@ -49,6 +49,7 @@ const {
4949

5050
const {
5151
codes: {
52+
ERR_INVALID_FD,
5253
ERR_NO_CRYPTO,
5354
ERR_NO_TYPESCRIPT,
5455
ERR_UNKNOWN_SIGNAL,
@@ -866,11 +867,11 @@ function getCIDR(address, netmask, family) {
866867
}
867868

868869
const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN'];
869-
setOwnProperty(handleTypes, -1, 'INVALID');
870+
setOwnProperty(handleTypes, -1, null);
870871

871872
function guessHandleType(fd) {
872-
if (!NumberIsInteger(fd)) {
873-
return 'INVALID';
873+
if (fd >> 0 !== fd || fd < 0) {
874+
throw new ERR_INVALID_FD(fd);
874875
}
875876

876877
const type = _guessHandleType(fd);

src/node_util.cc

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

220220
// If the provided file descriptor is not valid, we return `-1`, which in JS
221-
// land will be marked as "INVALID"
221+
// land will be marked as null
222222
if (fd < 0) [[unlikely]] {
223223
args.GetReturnValue().Set(-1);
224224
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)