From 6d0bf5ae74c869e381e3712b8c2c7948cbb91112 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 4 Nov 2024 16:10:15 +0000 Subject: [PATCH] src: improve `node:os` userInfo performance --- lib/os.js | 5 ----- src/node_os.cc | 47 ++++++++++++++++++++++++++++------------------- 2 files changed, 28 insertions(+), 24 deletions(-) diff --git a/lib/os.js b/lib/os.js index c6e2f220fd223b..4f8dda1531b5dc 100644 --- a/lib/os.js +++ b/lib/os.js @@ -358,11 +358,6 @@ function userInfo(options) { if (user === undefined) throw new ERR_SYSTEM_ERROR(ctx); - if (isWindows) { - user.uid |= 0; - user.gid |= 0; - } - return user; } diff --git a/src/node_os.cc b/src/node_os.cc index 7318c1a368d871..ce2af8d83b7443 100644 --- a/src/node_os.cc +++ b/src/node_os.cc @@ -287,21 +287,29 @@ static void GetUserInfo(const FunctionCallbackInfo& args) { encoding = UTF8; } - const int err = uv_os_get_passwd(&pwd); - - if (err) { + if (const int err = uv_os_get_passwd(&pwd)) { CHECK_GE(args.Length(), 2); env->CollectUVExceptionInfo(args[args.Length() - 1], err, "uv_os_get_passwd"); return args.GetReturnValue().SetUndefined(); } - auto free_passwd = OnScopeLeave([&]() { uv_os_free_passwd(&pwd); }); + auto free_passwd = OnScopeLeave([&] { uv_os_free_passwd(&pwd); }); Local error; +#ifdef _WIN32 + Local uid = Number::New( + env->isolate(), + static_cast(static_cast(pwd.uid & 0xFFFFFFFF))); + Local gid = Number::New( + env->isolate(), + static_cast(static_cast(pwd.gid & 0xFFFFFFFF))); +#else Local uid = Number::New(env->isolate(), pwd.uid); Local gid = Number::New(env->isolate(), pwd.gid); +#endif + MaybeLocal username = StringBytes::Encode(env->isolate(), pwd.username, encoding, @@ -323,21 +331,22 @@ static void GetUserInfo(const FunctionCallbackInfo& args) { return; } - Local entry = Object::New(env->isolate()); - - entry->Set(env->context(), env->uid_string(), uid).Check(); - entry->Set(env->context(), env->gid_string(), gid).Check(); - entry->Set(env->context(), - env->username_string(), - username.ToLocalChecked()).Check(); - entry->Set(env->context(), - env->homedir_string(), - homedir.ToLocalChecked()).Check(); - entry->Set(env->context(), - env->shell_string(), - shell.ToLocalChecked()).Check(); - - args.GetReturnValue().Set(entry); + constexpr size_t kRetLength = 5; + std::array, kRetLength> names = {env->uid_string(), + env->gid_string(), + env->username_string(), + env->homedir_string(), + env->shell_string()}; + std::array values = {uid, + gid, + username.ToLocalChecked(), + homedir.ToLocalChecked(), + shell.ToLocalChecked()}; + args.GetReturnValue().Set(Object::New(env->isolate(), + Null(env->isolate()), + names.data(), + values.data(), + kRetLength)); }