From 954e46e79232f25795a9836be6553e623e872323 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 5 Jun 2023 12:33:13 -0400 Subject: [PATCH] src: return uint32 for `guessHandleType` PR-URL: https://github.com/nodejs/node/pull/48349 Reviewed-By: Matteo Collina Reviewed-By: Robert Nagy Reviewed-By: Minwoo Jung --- lib/dgram.js | 3 +-- .../bootstrap/switches/is_main_thread.js | 2 +- lib/internal/dgram.js | 2 +- lib/internal/util.js | 9 ++++++++ lib/net.js | 3 +-- src/node_util.cc | 21 ++++++++++++------- 6 files changed, 26 insertions(+), 14 deletions(-) diff --git a/lib/dgram.js b/lib/dgram.js index 6af6460f4eb78d..b28d727c8a83ce 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -38,7 +38,6 @@ const { _createSocketHandle, newHandle, } = require('internal/dgram'); -const { guessHandleType } = internalBinding('util'); const { ERR_BUFFER_OUT_OF_BOUNDS, ERR_INVALID_ARG_TYPE, @@ -59,7 +58,7 @@ const { validatePort, } = require('internal/validators'); const { Buffer } = require('buffer'); -const { deprecate } = require('internal/util'); +const { deprecate, guessHandleType } = require('internal/util'); const { isArrayBufferView } = require('internal/util/types'); const EventEmitter = require('events'); const { diff --git a/lib/internal/bootstrap/switches/is_main_thread.js b/lib/internal/bootstrap/switches/is_main_thread.js index 2767ee12584e02..f2c3478e8bb5bf 100644 --- a/lib/internal/bootstrap/switches/is_main_thread.js +++ b/lib/internal/bootstrap/switches/is_main_thread.js @@ -42,7 +42,7 @@ process.on('removeListener', stopListeningIfSignal); // ---- keep the attachment of the wrappers above so that it's easier to ---- // ---- compare the setups side-by-side ----- -const { guessHandleType } = internalBinding('util'); +const { guessHandleType } = require('internal/util'); function createWritableStdioStream(fd) { let stream; diff --git a/lib/internal/dgram.js b/lib/internal/dgram.js index f602f39614f690..435adaeaec7e73 100644 --- a/lib/internal/dgram.js +++ b/lib/internal/dgram.js @@ -7,7 +7,7 @@ const { const { codes } = require('internal/errors'); const { UDP } = internalBinding('udp_wrap'); -const { guessHandleType } = internalBinding('util'); +const { guessHandleType } = require('internal/util'); const { isInt32, validateFunction, diff --git a/lib/internal/util.js b/lib/internal/util.js index 2ec432f838087b..1e1a647e693876 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -54,6 +54,7 @@ const { const { signals } = internalBinding('constants').os; const { isArrayBufferDetached: _isArrayBufferDetached, + guessHandleType: _guessHandleType, privateSymbols: { arrow_message_private_symbol, decorated_private_symbol, @@ -789,6 +790,13 @@ function setupCoverageHooks(dir) { return coverageDirectory; } + +const handleTypes = ['TCP', 'TTY', 'UDP', 'FILE', 'PIPE', 'UNKNOWN']; +function guessHandleType(fd) { + const type = _guessHandleType(fd); + return handleTypes[type]; +} + module.exports = { getLazy, assertCrypto, @@ -812,6 +820,7 @@ module.exports = { getInternalGlobal, getSystemErrorMap, getSystemErrorName, + guessHandleType, isArrayBufferDetached, isError, isInsideNodeModules, diff --git a/lib/net.js b/lib/net.js index 88d2fe5421ef29..116e6833bfb9b0 100644 --- a/lib/net.js +++ b/lib/net.js @@ -59,7 +59,6 @@ const { } = internalBinding('uv'); const { Buffer } = require('buffer'); -const { guessHandleType } = internalBinding('util'); const { ShutdownWrap } = internalBinding('stream_wrap'); const { TCP, @@ -111,7 +110,7 @@ const { } = require('internal/errors'); const { isUint8Array } = require('internal/util/types'); const { queueMicrotask } = require('internal/process/task_queues'); -const { kEmptyObject } = require('internal/util'); +const { kEmptyObject, guessHandleType } = require('internal/util'); const { validateAbortSignal, validateBoolean, diff --git a/src/node_util.cc b/src/node_util.cc index c434f201e592a4..ef78ed99eeb3e2 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -289,32 +289,37 @@ static void GuessHandleType(const FunctionCallbackInfo& args) { CHECK_GE(fd, 0); uv_handle_type t = uv_guess_handle(fd); - const char* type = nullptr; + // TODO(anonrig): We can use an enum here and then create the array in the + // binding, which will remove the hard-coding in C++ and JS land. + uint32_t type{0}; + // Currently, the return type of this function corresponds to the index of the + // array defined in the JS land. This is done as an optimization to reduce the + // string serialization overhead. switch (t) { case UV_TCP: - type = "TCP"; + type = 0; break; case UV_TTY: - type = "TTY"; + type = 1; break; case UV_UDP: - type = "UDP"; + type = 2; break; case UV_FILE: - type = "FILE"; + type = 3; break; case UV_NAMED_PIPE: - type = "PIPE"; + type = 4; break; case UV_UNKNOWN_HANDLE: - type = "UNKNOWN"; + type = 5; break; default: ABORT(); } - args.GetReturnValue().Set(OneByteString(env->isolate(), type)); + args.GetReturnValue().Set(type); } static void ToUSVString(const FunctionCallbackInfo& args) {