Skip to content

Commit 8d901bb

Browse files
committedApr 20, 2019
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: #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]>
1 parent b581d59 commit 8d901bb

File tree

7 files changed

+45
-42
lines changed

7 files changed

+45
-42
lines changed
 

‎lib/dgram.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const {
2828
kStateSymbol,
2929
_createSocketHandle,
3030
newHandle,
31-
guessHandleType,
3231
} = require('internal/dgram');
32+
const { guessHandleType } = internalBinding('util');
3333
const {
3434
isLegalPort,
3535
} = require('internal/net');

‎lib/internal/dgram.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22
const { codes } = require('internal/errors');
33
const { UDP } = internalBinding('udp_wrap');
4+
const { guessHandleType } = internalBinding('util');
45
const { isInt32 } = require('internal/validators');
5-
const TTYWrap = internalBinding('tty_wrap');
66
const { UV_EINVAL } = internalBinding('uv');
77
const { ERR_INVALID_ARG_TYPE, ERR_SOCKET_BAD_TYPE } = codes;
88
const kStateSymbol = Symbol('state symbol');
@@ -18,10 +18,6 @@ function lookup6(lookup, address, callback) {
1818
return lookup(address || '::1', 6, callback);
1919
}
2020

21-
22-
const guessHandleType = TTYWrap.guessHandleType;
23-
24-
2521
function newHandle(type, lookup) {
2622
if (lookup === undefined) {
2723
if (dns === undefined) {
@@ -81,6 +77,5 @@ function _createSocketHandle(address, port, addressType, fd, flags) {
8177
module.exports = {
8278
kStateSymbol,
8379
_createSocketHandle,
84-
newHandle,
85-
guessHandleType,
80+
newHandle
8681
};

‎lib/internal/process/stdio.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
22

3+
const { guessHandleType } = internalBinding('util');
34
exports.getMainThreadStdio = getMainThreadStdio;
45

56
function dummyDestroy(err, cb) {
@@ -48,10 +49,9 @@ function getMainThreadStdio() {
4849

4950
function getStdin() {
5051
if (stdin) return stdin;
51-
const tty_wrap = internalBinding('tty_wrap');
5252
const fd = 0;
5353

54-
switch (tty_wrap.guessHandleType(fd)) {
54+
switch (guessHandleType(fd)) {
5555
case 'TTY':
5656
var tty = require('tty');
5757
stdin = new tty.ReadStream(fd, {
@@ -148,11 +148,8 @@ function getMainThreadStdio() {
148148

149149
function createWritableStdioStream(fd) {
150150
var stream;
151-
const tty_wrap = internalBinding('tty_wrap');
152-
153151
// Note stream._type is used for test-module-load-list.js
154-
155-
switch (tty_wrap.guessHandleType(fd)) {
152+
switch (guessHandleType(fd)) {
156153
case 'TTY':
157154
var tty = require('tty');
158155
stream = new tty.WriteStream(fd);

‎lib/net.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const {
4343
} = internalBinding('uv');
4444

4545
const { Buffer } = require('buffer');
46-
const TTYWrap = internalBinding('tty_wrap');
46+
const { guessHandleType } = internalBinding('util');
4747
const { ShutdownWrap } = internalBinding('stream_wrap');
4848
const {
4949
TCP,
@@ -111,7 +111,7 @@ function getFlags(ipv6Only) {
111111

112112
function createHandle(fd, is_server) {
113113
validateInt32(fd, 'fd', 0);
114-
const type = TTYWrap.guessHandleType(fd);
114+
const type = guessHandleType(fd);
115115
if (type === 'PIPE') {
116116
return new Pipe(
117117
is_server ? PipeConstants.SERVER : PipeConstants.SOCKET

‎src/node_util.cc

+37
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,41 @@ class WeakReference : public BaseObject {
211211
Persistent<Object> target_;
212212
};
213213

214+
static void GuessHandleType(const FunctionCallbackInfo<Value>& args) {
215+
Environment* env = Environment::GetCurrent(args);
216+
int fd;
217+
if (!args[0]->Int32Value(env->context()).To(&fd)) return;
218+
CHECK_GE(fd, 0);
219+
220+
uv_handle_type t = uv_guess_handle(fd);
221+
const char* type = nullptr;
222+
223+
switch (t) {
224+
case UV_TCP:
225+
type = "TCP";
226+
break;
227+
case UV_TTY:
228+
type = "TTY";
229+
break;
230+
case UV_UDP:
231+
type = "UDP";
232+
break;
233+
case UV_FILE:
234+
type = "FILE";
235+
break;
236+
case UV_NAMED_PIPE:
237+
type = "PIPE";
238+
break;
239+
case UV_UNKNOWN_HANDLE:
240+
type = "UNKNOWN";
241+
break;
242+
default:
243+
ABORT();
244+
}
245+
246+
args.GetReturnValue().Set(OneByteString(env->isolate(), type));
247+
}
248+
214249
void Initialize(Local<Object> target,
215250
Local<Value> unused,
216251
Local<Context> context,
@@ -280,6 +315,8 @@ void Initialize(Local<Object> target,
280315
env->SetProtoMethod(weak_ref, "get", WeakReference::Get);
281316
target->Set(context, weak_ref_string,
282317
weak_ref->GetFunction(context).ToLocalChecked()).Check();
318+
319+
env->SetMethod(target, "guessHandleType", GuessHandleType);
283320
}
284321

285322
} // namespace util

‎src/tty_wrap.cc

-25
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ void TTYWrap::Initialize(Local<Object> target,
5959
env->SetProtoMethod(t, "setRawMode", SetRawMode);
6060

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

6463
Local<Value> func;
6564
if (t->GetFunction(env->context()).ToLocal(&func) &&
@@ -69,30 +68,6 @@ void TTYWrap::Initialize(Local<Object> target,
6968
}
7069

7170

72-
void TTYWrap::GuessHandleType(const FunctionCallbackInfo<Value>& args) {
73-
Environment* env = Environment::GetCurrent(args);
74-
int fd;
75-
if (!args[0]->Int32Value(env->context()).To(&fd)) return;
76-
CHECK_GE(fd, 0);
77-
78-
uv_handle_type t = uv_guess_handle(fd);
79-
const char* type = nullptr;
80-
81-
switch (t) {
82-
case UV_TCP: type = "TCP"; break;
83-
case UV_TTY: type = "TTY"; break;
84-
case UV_UDP: type = "UDP"; break;
85-
case UV_FILE: type = "FILE"; break;
86-
case UV_NAMED_PIPE: type = "PIPE"; break;
87-
case UV_UNKNOWN_HANDLE: type = "UNKNOWN"; break;
88-
default:
89-
ABORT();
90-
}
91-
92-
args.GetReturnValue().Set(OneByteString(env->isolate(), type));
93-
}
94-
95-
9671
void TTYWrap::IsTTY(const FunctionCallbackInfo<Value>& args) {
9772
Environment* env = Environment::GetCurrent(args);
9873
int fd;

‎src/tty_wrap.h

-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class TTYWrap : public LibuvStreamWrap {
4848
bool readable,
4949
int* init_err);
5050

51-
static void GuessHandleType(const v8::FunctionCallbackInfo<v8::Value>& args);
5251
static void IsTTY(const v8::FunctionCallbackInfo<v8::Value>& args);
5352
static void GetWindowSize(const v8::FunctionCallbackInfo<v8::Value>& args);
5453
static void SetRawMode(const v8::FunctionCallbackInfo<v8::Value>& args);

0 commit comments

Comments
 (0)