From bba553bea5938932518dc6382e464968ce8374b4 Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Wed, 15 May 2024 22:22:40 -0700 Subject: fix(ext/node): homedir() `getpwuid`/`SHGetKnownFolderPath` fallback (#23841) **Unix**: Returns the value of the HOME environment variable if it is set even if it is an empty string. Otherwise, it tries to determine the home directory by invoking the [getpwuid_r](https://linux.die.net/man/3/getpwuid_r) function with the UID of the current user. **Windows**: Returns the value of the USERPROFILE environment variable if it is set and it is not an empty string. Otherwise, it tries to determine the home directory by invoking the [SHGetKnownFolderPath](https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath) function with [FOLDERID_Profile](https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid). Fixes https://github.com/denoland/deno/issues/23824 --- ext/node/ops/os/mod.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'ext/node/ops/os') diff --git a/ext/node/ops/os/mod.rs b/ext/node/ops/os/mod.rs index 603f678e0..5b32113e5 100644 --- a/ext/node/ops/os/mod.rs +++ b/ext/node/ops/os/mod.rs @@ -88,3 +88,17 @@ where cpus::cpu_info().ok_or_else(|| type_error("Failed to get cpu info")) } + +#[op2] +#[string] +pub fn op_homedir

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

(); + permissions.check_sys("homedir", "node:os.homedir()")?; + } + + Ok(home::home_dir().map(|path| path.to_string_lossy().to_string())) +} -- cgit v1.2.3