Skip to content

Commit

Permalink
src: move guessHandleType in the util binding
Browse files Browse the repository at this point in the history
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: #27289
Reviewed-By: Richard Lau <[email protected]>
Reviewed-By: Daniel Bevenius <[email protected]>
Reviewed-By: Yongsheng Zhang <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
joyeecheung committed Apr 20, 2019
1 parent b581d59 commit 8d901bb
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 42 deletions.
2 changes: 1 addition & 1 deletion lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const {
kStateSymbol,
_createSocketHandle,
newHandle,
guessHandleType,
} = require('internal/dgram');
const { guessHandleType } = internalBinding('util');
const {
isLegalPort,
} = require('internal/net');
Expand Down
9 changes: 2 additions & 7 deletions lib/internal/dgram.js
Original file line number Diff line number Diff line change
@@ -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');
Expand All @@ -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) {
Expand Down Expand Up @@ -81,6 +77,5 @@ function _createSocketHandle(address, port, addressType, fd, flags) {
module.exports = {
kStateSymbol,
_createSocketHandle,
newHandle,
guessHandleType,
newHandle
};
9 changes: 3 additions & 6 deletions lib/internal/process/stdio.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const { guessHandleType } = internalBinding('util');
exports.getMainThreadStdio = getMainThreadStdio;

function dummyDestroy(err, cb) {
Expand Down Expand Up @@ -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, {
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,41 @@ class WeakReference : public BaseObject {
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,
Local<Value> unused,
Local<Context> context,
Expand Down Expand Up @@ -280,6 +315,8 @@ void Initialize(Local<Object> 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
Expand Down
25 changes: 0 additions & 25 deletions src/tty_wrap.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ void TTYWrap::Initialize(Local<Object> target,
env->SetProtoMethod(t, "setRawMode", SetRawMode);

env->SetMethodNoSideEffect(target, "isTTY", IsTTY);
env->SetMethodNoSideEffect(target, "guessHandleType", GuessHandleType);

Local<Value> func;
if (t->GetFunction(env->context()).ToLocal(&func) &&
Expand All @@ -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) {
Environment* env = Environment::GetCurrent(args);
int fd;
Expand Down
1 change: 0 additions & 1 deletion src/tty_wrap.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ class TTYWrap : public LibuvStreamWrap {
bool readable,
int* init_err);

static void GuessHandleType(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 SetRawMode(const v8::FunctionCallbackInfo<v8::Value>& args);
Expand Down

0 comments on commit 8d901bb

Please sign in to comment.