diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2024-05-15 22:22:40 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-16 10:52:40 +0530 |
commit | bba553bea5938932518dc6382e464968ce8374b4 (patch) | |
tree | efd68b86734bf8703608f0165a473ac04c300272 /ext/node/polyfills/os.ts | |
parent | a31b81394b7d78e6a0e678ba4d18dda62a3b2bc5 (diff) |
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
Diffstat (limited to 'ext/node/polyfills/os.ts')
-rw-r--r-- | ext/node/polyfills/os.ts | 19 |
1 files changed, 3 insertions, 16 deletions
diff --git a/ext/node/polyfills/os.ts b/ext/node/polyfills/os.ts index bc88b0601..753e39319 100644 --- a/ext/node/polyfills/os.ts +++ b/ext/node/polyfills/os.ts @@ -25,6 +25,7 @@ import { op_cpus, + op_homedir, op_node_os_get_priority, op_node_os_set_priority, op_node_os_username, @@ -32,7 +33,7 @@ import { import { validateIntegerRange } from "ext:deno_node/_utils.ts"; import process from "node:process"; -import { isWindows, osType } from "ext:deno_node/_util/os.ts"; +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"; @@ -173,21 +174,7 @@ export function getPriority(pid = 0): number { /** Returns the string path of the current user's home directory. */ export function homedir(): string | null { - // Note: Node/libuv calls getpwuid() / GetUserProfileDirectory() when the - // environment variable isn't set but that's the (very uncommon) fallback - // path. IMO, it's okay to punt on that for now. - switch (osType) { - case "windows": - return Deno.env.get("USERPROFILE") || null; - case "linux": - case "android": - case "darwin": - case "freebsd": - case "openbsd": - return Deno.env.get("HOME") || null; - default: - throw Error("unreachable"); - } + return op_homedir(); } /** Returns the host name of the operating system as a string. */ |