From c08319262afeca47d1b9f3dbfa3254e692a48a2d Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Thu, 28 Dec 2023 09:08:50 +0530 Subject: fix(node): Implement os.cpus() (#21697) Fixes https://github.com/denoland/deno/issues/21666 Zero added dependency and tries to match the libuv implementation --- ext/node/ops/os/mod.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 ext/node/ops/os/mod.rs (limited to 'ext/node/ops/os/mod.rs') diff --git a/ext/node/ops/os/mod.rs b/ext/node/ops/os/mod.rs new file mode 100644 index 000000000..4fadc1ff8 --- /dev/null +++ b/ext/node/ops/os/mod.rs @@ -0,0 +1,90 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +use crate::NodePermissions; +use deno_core::error::type_error; +use deno_core::error::AnyError; +use deno_core::op2; +use deno_core::OpState; + +mod cpus; +mod priority; + +#[op2(fast)] +pub fn op_node_os_get_priority

( + state: &mut OpState, + pid: u32, +) -> Result +where + P: NodePermissions + 'static, +{ + { + let permissions = state.borrow_mut::

(); + permissions.check_sys("getPriority", "node:os.getPriority()")?; + } + + priority::get_priority(pid) +} + +#[op2(fast)] +pub fn op_node_os_set_priority

( + state: &mut OpState, + pid: u32, + priority: i32, +) -> Result<(), AnyError> +where + P: NodePermissions + 'static, +{ + { + let permissions = state.borrow_mut::

(); + permissions.check_sys("setPriority", "node:os.setPriority()")?; + } + + priority::set_priority(pid, priority) +} + +#[op2] +#[string] +pub fn op_node_os_username

(state: &mut OpState) -> Result +where + P: NodePermissions + 'static, +{ + { + let permissions = state.borrow_mut::

(); + permissions.check_sys("userInfo", "node:os.userInfo()")?; + } + + Ok(deno_whoami::username()) +} + +#[op2(fast)] +pub fn op_geteuid

(state: &mut OpState) -> Result +where + P: NodePermissions + 'static, +{ + { + let permissions = state.borrow_mut::

(); + permissions.check_sys("geteuid", "node:os.geteuid()")?; + } + + #[cfg(windows)] + let euid = 0; + #[cfg(unix)] + // SAFETY: Call to libc geteuid. + let euid = unsafe { libc::geteuid() }; + + Ok(euid) +} + +#[op2] +#[serde] +pub fn op_cpus

(state: &mut OpState) -> Result, AnyError> +where + P: NodePermissions + 'static, +{ + { + let permissions = state.borrow_mut::

(); + permissions.check_sys("cpus", "node:os.cpus()")?; + } + + cpus::cpu_info().ok_or_else(|| type_error("Failed to get cpu info")) +} -- cgit v1.2.3