From 8d901bb44e52077c4778261764892c959a94a6f2 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Thu, 18 Apr 2019 10:46:39 +0800 Subject: 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 --- src/node_util.cc | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'src/node_util.cc') diff --git a/src/node_util.cc b/src/node_util.cc index 960e194f76..d13624a436 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 -- cgit v1.2.3