From 6c6bbeb97495e8c3e8eac7bea27bf836f02b575f Mon Sep 17 00:00:00 2001 From: Nathan Whitaker <17734409+nathanwhit@users.noreply.github.com> Date: Thu, 31 Oct 2024 22:18:33 -0700 Subject: fix(node): Implement `os.userInfo` properly, add missing `toPrimitive` (#24702) Fixes the implementation of `os.userInfo`, and adds a missing `toPrimitive` for `tmpdir`. This allows us to enable the corresponding node_compat test. --- ext/node/lib.rs | 2 +- ext/node/ops/os/mod.rs | 156 ++++++++++++++++++++++++++++++++-- ext/node/polyfills/internal/errors.ts | 13 --- ext/node/polyfills/os.ts | 53 +++++------- 4 files changed, 175 insertions(+), 49 deletions(-) (limited to 'ext') diff --git a/ext/node/lib.rs b/ext/node/lib.rs index 32624f38b..b34bea815 100644 --- a/ext/node/lib.rs +++ b/ext/node/lib.rs @@ -348,7 +348,7 @@ deno_core::extension!(deno_node, ops::http2::op_http2_send_response, ops::os::op_node_os_get_priority
, ops::os::op_node_os_set_priority
, - ops::os::op_node_os_username
, + ops::os::op_node_os_user_info
, ops::os::op_geteuid
, ops::os::op_getegid
, ops::os::op_cpus
,
diff --git a/ext/node/ops/os/mod.rs b/ext/node/ops/os/mod.rs
index b4c9eaa8c..ea7e6b99f 100644
--- a/ext/node/ops/os/mod.rs
+++ b/ext/node/ops/os/mod.rs
@@ -1,5 +1,7 @@
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
+use std::mem::MaybeUninit;
+
use crate::NodePermissions;
use deno_core::op2;
use deno_core::OpState;
@@ -15,6 +17,8 @@ pub enum OsError {
Permission(deno_core::error::AnyError),
#[error("Failed to get cpu info")]
FailedToGetCpuInfo,
+ #[error("Failed to get user info")]
+ FailedToGetUserInfo(#[source] std::io::Error),
}
#[op2(fast)]
@@ -54,20 +58,162 @@ where
priority::set_priority(pid, priority).map_err(OsError::Priority)
}
+#[derive(serde::Serialize)]
+pub struct UserInfo {
+ username: String,
+ homedir: String,
+ shell: Option (
+#[serde]
+pub fn op_node_os_user_info (
state: &mut OpState,
-) -> Result ();
- permissions.check_sys("username", "node:os.userInfo()")?;
+ permissions
+ .check_sys("userInfo", "node:os.userInfo()")
+ .map_err(OsError::Permission)?;
}
- Ok(deno_whoami::username())
+ get_user_info(uid)
}
#[op2(fast)]
diff --git a/ext/node/polyfills/internal/errors.ts b/ext/node/polyfills/internal/errors.ts
index 51bd7a025..5a3d4437a 100644
--- a/ext/node/polyfills/internal/errors.ts
+++ b/ext/node/polyfills/internal/errors.ts
@@ -2558,19 +2558,6 @@ export class ERR_FS_RMDIR_ENOTDIR extends NodeSystemError {
}
}
-export class ERR_OS_NO_HOMEDIR extends NodeSystemError {
- constructor() {
- const code = isWindows ? "ENOENT" : "ENOTDIR";
- const ctx: NodeSystemErrorCtx = {
- message: "not a directory",
- syscall: "home",
- code,
- errno: isWindows ? osConstants.errno.ENOENT : osConstants.errno.ENOTDIR,
- };
- super(code, ctx, "Path is not a directory");
- }
-}
-
export class ERR_HTTP_SOCKET_ASSIGNED extends NodeError {
constructor() {
super(
diff --git a/ext/node/polyfills/os.ts b/ext/node/polyfills/os.ts
index e47e8679e..edc89ed2c 100644
--- a/ext/node/polyfills/os.ts
+++ b/ext/node/polyfills/os.ts
@@ -28,16 +28,17 @@ import {
op_homedir,
op_node_os_get_priority,
op_node_os_set_priority,
- op_node_os_username,
+ op_node_os_user_info,
} from "ext:core/ops";
import { validateIntegerRange } from "ext:deno_node/_utils.ts";
import process from "node:process";
import { isWindows } from "ext:deno_node/_util/os.ts";
-import { ERR_OS_NO_HOMEDIR } from "ext:deno_node/internal/errors.ts";
import { os } from "ext:deno_node/internal_binding/constants.ts";
import { osUptime } from "ext:runtime/30_os.js";
import { Buffer } from "ext:deno_node/internal/buffer.mjs";
+import { primordials } from "ext:core/mod.js";
+const { StringPrototypeEndsWith, StringPrototypeSlice } = primordials;
export const constants = os;
@@ -136,6 +137,8 @@ export function arch(): string {
(uptime as any)[Symbol.toPrimitive] = (): number => uptime();
// deno-lint-ignore no-explicit-any
(machine as any)[Symbol.toPrimitive] = (): string => machine();
+// deno-lint-ignore no-explicit-any
+(tmpdir as any)[Symbol.toPrimitive] = (): string | null => tmpdir();
export function cpus(): CPUCoreInfo[] {
return op_cpus();
@@ -268,26 +271,27 @@ export function setPriority(pid: number, priority?: number) {
export function tmpdir(): string | null {
/* This follows the node js implementation, but has a few
differences:
- * On windows, if none of the environment variables are defined,
- we return null.
- * On unix we use a plain Deno.env.get, instead of safeGetenv,
+ * We use a plain Deno.env.get, instead of safeGetenv,
which special cases setuid binaries.
- * Node removes a single trailing / or \, we remove all.
*/
if (isWindows) {
- const temp = Deno.env.get("TEMP") || Deno.env.get("TMP");
- if (temp) {
- return temp.replace(/(? 1 && StringPrototypeEndsWith(temp, "\\") &&
+ !StringPrototypeEndsWith(temp, ":\\")
+ ) {
+ temp = StringPrototypeSlice(temp, 0, -1);
}
- return null;
+
+ return temp;
} else { // !isWindows
- const temp = Deno.env.get("TMPDIR") || Deno.env.get("TMP") ||
+ let temp = Deno.env.get("TMPDIR") || Deno.env.get("TMP") ||
Deno.env.get("TEMP") || "/tmp";
- return temp.replace(/(? 1 && StringPrototypeEndsWith(temp, "/")) {
+ temp = StringPrototypeSlice(temp, 0, -1);
+ }
+ return temp;
}
}
@@ -320,7 +324,6 @@ export function uptime(): number {
return osUptime();
}
-/** Not yet implemented */
export function userInfo(
options: UserInfoOptions = { encoding: "utf-8" },
): UserInfo {
@@ -331,20 +334,10 @@ export function userInfo(
uid = -1;
gid = -1;
}
-
- // TODO(@crowlKats): figure out how to do this correctly:
- // The value of homedir returned by os.userInfo() is provided by the operating system.
- // This differs from the result of os.homedir(), which queries environment
- // variables for the home directory before falling back to the operating system response.
- let _homedir = homedir();
- if (!_homedir) {
- throw new ERR_OS_NO_HOMEDIR();
- }
- let shell = isWindows ? null : (Deno.env.get("SHELL") || null);
- let username = op_node_os_username();
+ let { username, homedir, shell } = op_node_os_user_info(uid);
if (options?.encoding === "buffer") {
- _homedir = _homedir ? Buffer.from(_homedir) : _homedir;
+ homedir = homedir ? Buffer.from(homedir) : homedir;
shell = shell ? Buffer.from(shell) : shell;
username = Buffer.from(username);
}
@@ -352,7 +345,7 @@ export function userInfo(
return {
uid,
gid,
- homedir: _homedir,
+ homedir,
shell,
username,
};
--
cgit v1.2.3