From 8d901bb44e52077c4778261764892c959a94a6f2 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 18 Apr 2019 10:46:39 +0800 Subject: [PATCH] src: move guessHandleType in the util binding It does not make too much sense to have modules unrelated to TTY load the TTY binding just to use this method. Put this in the util binding instead. PR-URL: https://github.com/nodejs/node/pull/27289 Reviewed-By: Richard Lau Reviewed-By: Daniel Bevenius Reviewed-By: Yongsheng Zhang Reviewed-By: Ruben Bridgewater Reviewed-By: Colin Ihrig --- lib/dgram.js | 2 +- lib/internal/dgram.js | 9 ++------- lib/internal/process/stdio.js | 9 +++------ lib/net.js | 4 ++-- src/node_util.cc | 37 +++++++++++++++++++++++++++++++++++ src/tty_wrap.cc | 25 ----------------------- src/tty_wrap.h | 1 - 7 files changed, 45 insertions(+), 42 deletions(-) diff --git a/lib/dgram.js b/lib/dgram.js index 64192c4b52e65d..5a6d5aca2c3cf4 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -28,8 +28,8 @@ const { kStateSymbol, _createSocketHandle, newHandle, - guessHandleType, } = require('internal/dgram'); +const { guessHandleType } = internalBinding('util'); const { isLegalPort, } = require('internal/net'); diff --git a/lib/internal/dgram.js b/lib/internal/dgram.js index bb5e25a62e2197..0c0fefa27c1225 100644 --- a/lib/internal/dgram.js +++ b/lib/internal/dgram.js @@ -1,8 +1,8 @@ 'use strict'; const { codes } = require('internal/errors'); const { UDP } = internalBinding('udp_wrap'); +const { guessHandleType } = internalBinding('util'); const { isInt32 } = require('internal/validators'); -const TTYWrap = internalBinding('tty_wrap'); const { UV_EINVAL } = internalBinding('uv'); const { ERR_INVALID_ARG_TYPE, ERR_SOCKET_BAD_TYPE } = codes; const kStateSymbol = Symbol('state symbol'); @@ -18,10 +18,6 @@ function lookup6(lookup, address, callback) { return lookup(address || '::1', 6, callback); } - -const guessHandleType = TTYWrap.guessHandleType; - - function newHandle(type, lookup) { if (lookup === undefined) { if (dns === undefined) { @@ -81,6 +77,5 @@ function _createSocketHandle(address, port, addressType, fd, flags) { module.exports = { kStateSymbol, _createSocketHandle, - newHandle, - guessHandleType, + newHandle }; diff --git a/lib/internal/process/stdio.js b/lib/internal/process/stdio.js index 385225cabc402e..61892165999d92 100644 --- a/lib/internal/process/stdio.js +++ b/lib/internal/process/stdio.js @@ -1,5 +1,6 @@ 'use strict'; +const { guessHandleType } = internalBinding('util'); exports.getMainThreadStdio = getMainThreadStdio; function dummyDestroy(err, cb) { @@ -48,10 +49,9 @@ function getMainThreadStdio() { function getStdin() { if (stdin) return stdin; - const tty_wrap = internalBinding('tty_wrap'); const fd = 0; - switch (tty_wrap.guessHandleType(fd)) { + switch (guessHandleType(fd)) { case 'TTY': var tty = require('tty'); stdin = new tty.ReadStream(fd, { @@ -148,11 +148,8 @@ function getMainThreadStdio() { function createWritableStdioStream(fd) { var stream; - const tty_wrap = internalBinding('tty_wrap'); - // Note stream._type is used for test-module-load-list.js - - switch (tty_wrap.guessHandleType(fd)) { + switch (guessHandleType(fd)) { case 'TTY': var tty = require('tty'); stream = new tty.WriteStream(fd); diff --git a/lib/net.js b/lib/net.js index 9842338f188a96..54ddaa3b403044 100644 --- a/lib/net.js +++ b/lib/net.js @@ -43,7 +43,7 @@ const { } = internalBinding('uv'); const { Buffer } = require('buffer'); -const TTYWrap = internalBinding('tty_wrap'); +const { guessHandleType } = internalBinding('util'); const { ShutdownWrap } = internalBinding('stream_wrap'); const { TCP, @@ -111,7 +111,7 @@ function getFlags(ipv6Only) { function createHandle(fd, is_server) { validateInt32(fd, 'fd', 0); - const type = TTYWrap.guessHandleType(fd); + const type = guessHandleType(fd); if (type === 'PIPE') { return new Pipe( is_server ? PipeConstants.SERVER : PipeConstants.SOCKET diff --git a/src/node_util.cc b/src/node_util.cc index 960e194f76711a..d13624a436a9b9 100644 --- a/src/node_util.cc +++ b/src/node_util.cc @@ -211,6 +211,41 @@ class WeakReference : public BaseObject { Persistent target_; }; +static void GuessHandleType(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); + int fd; + if (!args[0]->Int32Value(env->context()).To(&fd)) return; + CHECK_GE(fd, 0); + + uv_handle_type t = uv_guess_handle(fd); + const char* type = nullptr; + + switch (t) { + case UV_TCP: + type = "TCP"; + break; + case UV_TTY: + type = "TTY"; + break; + case UV_UDP: + type = "UDP"; + break; + case UV_FILE: + type = "FILE"; + break; + case UV_NAMED_PIPE: + type = "PIPE"; + break; + case UV_UNKNOWN_HANDLE: + type = "UNKNOWN"; + break; + default: + ABORT(); + } + + args.GetReturnValue().Set(OneByteString(env->isolate(), type)); +} + void Initialize(Local target, Local unused, Local context, @@ -280,6 +315,8 @@ void Initialize(Local target, env->SetProtoMethod(weak_ref, "get", WeakReference::Get); target->Set(context, weak_ref_string, weak_ref->GetFunction(context).ToLocalChecked()).Check(); + + env->SetMethod(target, "guessHandleType", GuessHandleType); } } // namespace util diff --git a/src/tty_wrap.cc b/src/tty_wrap.cc index 557f95f26fd231..7dface926e4349 100644 --- a/src/tty_wrap.cc +++ b/src/tty_wrap.cc @@ -59,7 +59,6 @@ void TTYWrap::Initialize(Local target, env->SetProtoMethod(t, "setRawMode", SetRawMode); env->SetMethodNoSideEffect(target, "isTTY", IsTTY); - env->SetMethodNoSideEffect(target, "guessHandleType", GuessHandleType); Local func; if (t->GetFunction(env->context()).ToLocal(&func) && @@ -69,30 +68,6 @@ void TTYWrap::Initialize(Local target, } -void TTYWrap::GuessHandleType(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - int fd; - if (!args[0]->Int32Value(env->context()).To(&fd)) return; - CHECK_GE(fd, 0); - - uv_handle_type t = uv_guess_handle(fd); - const char* type = nullptr; - - switch (t) { - case UV_TCP: type = "TCP"; break; - case UV_TTY: type = "TTY"; break; - case UV_UDP: type = "UDP"; break; - case UV_FILE: type = "FILE"; break; - case UV_NAMED_PIPE: type = "PIPE"; break; - case UV_UNKNOWN_HANDLE: type = "UNKNOWN"; break; - default: - ABORT(); - } - - args.GetReturnValue().Set(OneByteString(env->isolate(), type)); -} - - void TTYWrap::IsTTY(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); int fd; diff --git a/src/tty_wrap.h b/src/tty_wrap.h index 63a340fd8d5388..df0c4b7c145612 100644 --- a/src/tty_wrap.h +++ b/src/tty_wrap.h @@ -48,7 +48,6 @@ class TTYWrap : public LibuvStreamWrap { bool readable, int* init_err); - static void GuessHandleType(const v8::FunctionCallbackInfo& args); static void IsTTY(const v8::FunctionCallbackInfo& args); static void GetWindowSize(const v8::FunctionCallbackInfo& args); static void SetRawMode(const v8::FunctionCallbackInfo& args);