Skip to content

Commit

Permalink
src: return uint32 for guessHandleType
Browse files Browse the repository at this point in the history
PR-URL: #48349
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Robert Nagy <ronagy@icloud.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
  • Loading branch information
anonrig authored and RafaelGSS committed Jul 3, 2023
1 parent 8c2c02d commit 954e46e
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 14 deletions.
3 changes: 1 addition & 2 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ const {
_createSocketHandle,
newHandle,
} = require('internal/dgram');
const { guessHandleType } = internalBinding('util');
const {
ERR_BUFFER_OUT_OF_BOUNDS,
ERR_INVALID_ARG_TYPE,
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/bootstrap/switches/is_main_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const {
const { signals } = internalBinding('constants').os;
const {
isArrayBufferDetached: _isArrayBufferDetached,
guessHandleType: _guessHandleType,
privateSymbols: {
arrow_message_private_symbol,
decorated_private_symbol,
Expand Down Expand Up @@ -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,
Expand All @@ -812,6 +820,7 @@ module.exports = {
getInternalGlobal,
getSystemErrorMap,
getSystemErrorName,
guessHandleType,
isArrayBufferDetached,
isError,
isInsideNodeModules,
Expand Down
3 changes: 1 addition & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ const {
} = internalBinding('uv');

const { Buffer } = require('buffer');
const { guessHandleType } = internalBinding('util');
const { ShutdownWrap } = internalBinding('stream_wrap');
const {
TCP,
Expand Down Expand Up @@ -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,
Expand Down
21 changes: 13 additions & 8 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -289,32 +289,37 @@ static void GuessHandleType(const FunctionCallbackInfo<Value>& 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<Value>& args) {
Expand Down

0 comments on commit 954e46e

Please sign in to comment.