Skip to content

Commit 954e46e

Browse files
anonrigRafaelGSS
authored andcommitted
src: return uint32 for guessHandleType
PR-URL: #48349 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Minwoo Jung <[email protected]>
1 parent 8c2c02d commit 954e46e

File tree

6 files changed

+26
-14
lines changed

6 files changed

+26
-14
lines changed

lib/dgram.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ const {
3838
_createSocketHandle,
3939
newHandle,
4040
} = require('internal/dgram');
41-
const { guessHandleType } = internalBinding('util');
4241
const {
4342
ERR_BUFFER_OUT_OF_BOUNDS,
4443
ERR_INVALID_ARG_TYPE,
@@ -59,7 +58,7 @@ const {
5958
validatePort,
6059
} = require('internal/validators');
6160
const { Buffer } = require('buffer');
62-
const { deprecate } = require('internal/util');
61+
const { deprecate, guessHandleType } = require('internal/util');
6362
const { isArrayBufferView } = require('internal/util/types');
6463
const EventEmitter = require('events');
6564
const {

lib/internal/bootstrap/switches/is_main_thread.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ process.on('removeListener', stopListeningIfSignal);
4242
// ---- keep the attachment of the wrappers above so that it's easier to ----
4343
// ---- compare the setups side-by-side -----
4444

45-
const { guessHandleType } = internalBinding('util');
45+
const { guessHandleType } = require('internal/util');
4646

4747
function createWritableStdioStream(fd) {
4848
let stream;

lib/internal/dgram.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const {
77

88
const { codes } = require('internal/errors');
99
const { UDP } = internalBinding('udp_wrap');
10-
const { guessHandleType } = internalBinding('util');
10+
const { guessHandleType } = require('internal/util');
1111
const {
1212
isInt32,
1313
validateFunction,

lib/internal/util.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ const {
5454
const { signals } = internalBinding('constants').os;
5555
const {
5656
isArrayBufferDetached: _isArrayBufferDetached,
57+
guessHandleType: _guessHandleType,
5758
privateSymbols: {
5859
arrow_message_private_symbol,
5960
decorated_private_symbol,
@@ -789,6 +790,13 @@ function setupCoverageHooks(dir) {
789790
return coverageDirectory;
790791
}
791792

793+
794+
const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN'];
795+
function guessHandleType(fd) {
796+
const type = _guessHandleType(fd);
797+
return handleTypes[type];
798+
}
799+
792800
module.exports = {
793801
getLazy,
794802
assertCrypto,
@@ -812,6 +820,7 @@ module.exports = {
812820
getInternalGlobal,
813821
getSystemErrorMap,
814822
getSystemErrorName,
823+
guessHandleType,
815824
isArrayBufferDetached,
816825
isError,
817826
isInsideNodeModules,

lib/net.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ const {
5959
} = internalBinding('uv');
6060

6161
const { Buffer } = require('buffer');
62-
const { guessHandleType } = internalBinding('util');
6362
const { ShutdownWrap } = internalBinding('stream_wrap');
6463
const {
6564
TCP,
@@ -111,7 +110,7 @@ const {
111110
} = require('internal/errors');
112111
const { isUint8Array } = require('internal/util/types');
113112
const { queueMicrotask } = require('internal/process/task_queues');
114-
const { kEmptyObject } = require('internal/util');
113+
const { kEmptyObject, guessHandleType } = require('internal/util');
115114
const {
116115
validateAbortSignal,
117116
validateBoolean,

src/node_util.cc

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -289,32 +289,37 @@ static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
289289
CHECK_GE(fd, 0);
290290

291291
uv_handle_type t = uv_guess_handle(fd);
292-
const char* type = nullptr;
292+
// TODO(anonrig): We can use an enum here and then create the array in the
293+
// binding, which will remove the hard-coding in C++ and JS land.
294+
uint32_t type{0};
293295

296+
// Currently, the return type of this function corresponds to the index of the
297+
// array defined in the JS land. This is done as an optimization to reduce the
298+
// string serialization overhead.
294299
switch (t) {
295300
case UV_TCP:
296-
type = "TCP";
301+
type = 0;
297302
break;
298303
case UV_TTY:
299-
type = "TTY";
304+
type = 1;
300305
break;
301306
case UV_UDP:
302-
type = "UDP";
307+
type = 2;
303308
break;
304309
case UV_FILE:
305-
type = "FILE";
310+
type = 3;
306311
break;
307312
case UV_NAMED_PIPE:
308-
type = "PIPE";
313+
type = 4;
309314
break;
310315
case UV_UNKNOWN_HANDLE:
311-
type = "UNKNOWN";
316+
type = 5;
312317
break;
313318
default:
314319
ABORT();
315320
}
316321

317-
args.GetReturnValue().Set(OneByteString(env->isolate(), type));
322+
args.GetReturnValue().Set(type);
318323
}
319324

320325
static void ToUSVString(const FunctionCallbackInfo<Value>& args) {

0 commit comments

Comments
 (0)