Skip to content

Commit

Permalink
src: improve node:os userInfo performance
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Nov 4, 2024
1 parent bdc2662 commit 55614e5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
5 changes: 0 additions & 5 deletions lib/os.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
42 changes: 23 additions & 19 deletions src/node_os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,22 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& 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<Value> error;

#ifdef _WIN32
pwd.uid |= 0;
pwd.gid |= 0;
#endif

Local<Value> uid = Number::New(env->isolate(), pwd.uid);
Local<Value> gid = Number::New(env->isolate(), pwd.gid);
MaybeLocal<Value> username = StringBytes::Encode(env->isolate(),
Expand All @@ -323,21 +326,22 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
return;
}

Local<Object> 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<Local<v8::Name>, 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));
}


Expand Down

0 comments on commit 55614e5

Please sign in to comment.