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 <riclau@uk.ibm.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Joyee Cheung 2019-04-18 10:46:39 +08:00
parent b581d59655
commit 8d901bb44e
No known key found for this signature in database
GPG Key ID: 92B78A53C8303B8D
7 changed files with 45 additions and 42 deletions

View File

@ -28,8 +28,8 @@ const {
kStateSymbol, kStateSymbol,
_createSocketHandle, _createSocketHandle,
newHandle, newHandle,
guessHandleType,
} = require('internal/dgram'); } = require('internal/dgram');
const { guessHandleType } = internalBinding('util');
const { const {
isLegalPort, isLegalPort,
} = require('internal/net'); } = require('internal/net');

View File

@ -1,8 +1,8 @@
'use strict'; 'use strict';
const { codes } = require('internal/errors'); const { codes } = require('internal/errors');
const { UDP } = internalBinding('udp_wrap'); const { UDP } = internalBinding('udp_wrap');
const { guessHandleType } = internalBinding('util');
const { isInt32 } = require('internal/validators'); const { isInt32 } = require('internal/validators');
const TTYWrap = internalBinding('tty_wrap');
const { UV_EINVAL } = internalBinding('uv'); const { UV_EINVAL } = internalBinding('uv');
const { ERR_INVALID_ARG_TYPE, ERR_SOCKET_BAD_TYPE } = codes; const { ERR_INVALID_ARG_TYPE, ERR_SOCKET_BAD_TYPE } = codes;
const kStateSymbol = Symbol('state symbol'); const kStateSymbol = Symbol('state symbol');
@ -18,10 +18,6 @@ function lookup6(lookup, address, callback) {
return lookup(address || '::1', 6, callback); return lookup(address || '::1', 6, callback);
} }
const guessHandleType = TTYWrap.guessHandleType;
function newHandle(type, lookup) { function newHandle(type, lookup) {
if (lookup === undefined) { if (lookup === undefined) {
if (dns === undefined) { if (dns === undefined) {
@ -81,6 +77,5 @@ function _createSocketHandle(address, port, addressType, fd, flags) {
module.exports = { module.exports = {
kStateSymbol, kStateSymbol,
_createSocketHandle, _createSocketHandle,
newHandle, newHandle
guessHandleType,
}; };

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
const { guessHandleType } = internalBinding('util');
exports.getMainThreadStdio = getMainThreadStdio; exports.getMainThreadStdio = getMainThreadStdio;
function dummyDestroy(err, cb) { function dummyDestroy(err, cb) {
@ -48,10 +49,9 @@ function getMainThreadStdio() {
function getStdin() { function getStdin() {
if (stdin) return stdin; if (stdin) return stdin;
const tty_wrap = internalBinding('tty_wrap');
const fd = 0; const fd = 0;
switch (tty_wrap.guessHandleType(fd)) { switch (guessHandleType(fd)) {
case 'TTY': case 'TTY':
var tty = require('tty'); var tty = require('tty');
stdin = new tty.ReadStream(fd, { stdin = new tty.ReadStream(fd, {
@ -148,11 +148,8 @@ function getMainThreadStdio() {
function createWritableStdioStream(fd) { function createWritableStdioStream(fd) {
var stream; var stream;
const tty_wrap = internalBinding('tty_wrap');
// Note stream._type is used for test-module-load-list.js // Note stream._type is used for test-module-load-list.js
switch (guessHandleType(fd)) {
switch (tty_wrap.guessHandleType(fd)) {
case 'TTY': case 'TTY':
var tty = require('tty'); var tty = require('tty');
stream = new tty.WriteStream(fd); stream = new tty.WriteStream(fd);

View File

@ -43,7 +43,7 @@ const {
} = internalBinding('uv'); } = internalBinding('uv');
const { Buffer } = require('buffer'); const { Buffer } = require('buffer');
const TTYWrap = internalBinding('tty_wrap'); const { guessHandleType } = internalBinding('util');
const { ShutdownWrap } = internalBinding('stream_wrap'); const { ShutdownWrap } = internalBinding('stream_wrap');
const { const {
TCP, TCP,
@ -111,7 +111,7 @@ function getFlags(ipv6Only) {
function createHandle(fd, is_server) { function createHandle(fd, is_server) {
validateInt32(fd, 'fd', 0); validateInt32(fd, 'fd', 0);
const type = TTYWrap.guessHandleType(fd); const type = guessHandleType(fd);
if (type === 'PIPE') { if (type === 'PIPE') {
return new Pipe( return new Pipe(
is_server ? PipeConstants.SERVER : PipeConstants.SOCKET is_server ? PipeConstants.SERVER : PipeConstants.SOCKET

View File

@ -211,6 +211,41 @@ class WeakReference : public BaseObject {
Persistent<Object> target_; Persistent<Object> target_;
}; };
static void GuessHandleType(const FunctionCallbackInfo<Value>& 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<Object> target, void Initialize(Local<Object> target,
Local<Value> unused, Local<Value> unused,
Local<Context> context, Local<Context> context,
@ -280,6 +315,8 @@ void Initialize(Local<Object> target,
env->SetProtoMethod(weak_ref, "get", WeakReference::Get); env->SetProtoMethod(weak_ref, "get", WeakReference::Get);
target->Set(context, weak_ref_string, target->Set(context, weak_ref_string,
weak_ref->GetFunction(context).ToLocalChecked()).Check(); weak_ref->GetFunction(context).ToLocalChecked()).Check();
env->SetMethod(target, "guessHandleType", GuessHandleType);
} }
} // namespace util } // namespace util

View File

@ -59,7 +59,6 @@ void TTYWrap::Initialize(Local<Object> target,
env->SetProtoMethod(t, "setRawMode", SetRawMode); env->SetProtoMethod(t, "setRawMode", SetRawMode);
env->SetMethodNoSideEffect(target, "isTTY", IsTTY); env->SetMethodNoSideEffect(target, "isTTY", IsTTY);
env->SetMethodNoSideEffect(target, "guessHandleType", GuessHandleType);
Local<Value> func; Local<Value> func;
if (t->GetFunction(env->context()).ToLocal(&func) && if (t->GetFunction(env->context()).ToLocal(&func) &&
@ -69,30 +68,6 @@ void TTYWrap::Initialize(Local<Object> target,
} }
void TTYWrap::GuessHandleType(const FunctionCallbackInfo<Value>& 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<Value>& args) { void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args); Environment* env = Environment::GetCurrent(args);
int fd; int fd;

View File

@ -48,7 +48,6 @@ class TTYWrap : public LibuvStreamWrap {
bool readable, bool readable,
int* init_err); int* init_err);
static void GuessHandleType(const v8::FunctionCallbackInfo<v8::Value>& args);
static void IsTTY(const v8::FunctionCallbackInfo<v8::Value>& args); static void IsTTY(const v8::FunctionCallbackInfo<v8::Value>& args);
static void GetWindowSize(const v8::FunctionCallbackInfo<v8::Value>& args); static void GetWindowSize(const v8::FunctionCallbackInfo<v8::Value>& args);
static void SetRawMode(const v8::FunctionCallbackInfo<v8::Value>& args); static void SetRawMode(const v8::FunctionCallbackInfo<v8::Value>& args);