diff options
Diffstat (limited to 'ext/node/polyfills/os.ts')
-rw-r--r-- | ext/node/polyfills/os.ts | 49 |
1 files changed, 40 insertions, 9 deletions
diff --git a/ext/node/polyfills/os.ts b/ext/node/polyfills/os.ts index acdc1c977..a874c942c 100644 --- a/ext/node/polyfills/os.ts +++ b/ext/node/polyfills/os.ts @@ -23,15 +23,16 @@ // TODO(petamoriken): enable prefer-primordials for node polyfills // deno-lint-ignore-file prefer-primordials -import { notImplemented } from "ext:deno_node/_utils.ts"; +const core = globalThis.__bootstrap.core; import { validateIntegerRange } from "ext:deno_node/_utils.ts"; import process from "node:process"; import { isWindows, osType } 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"; -export const constants = os; +import { Buffer } from "ext:deno_node/internal/buffer.mjs"; -const SEE_GITHUB_ISSUE = "See https://github.com/denoland/deno_std/issues/1436"; +export const constants = os; interface CPUTimes { /** The number of milliseconds the CPU has spent in user mode */ @@ -93,8 +94,8 @@ interface UserInfo { username: string; uid: number; gid: number; - shell: string; - homedir: string; + shell: string | null; + homedir: string | null; } export function arch(): string { @@ -161,7 +162,7 @@ export function freemem(): number { /** Not yet implemented */ export function getPriority(pid = 0): number { validateIntegerRange(pid, "pid"); - notImplemented(SEE_GITHUB_ISSUE); + return core.ops.op_node_os_get_priority(pid); } /** Returns the string path of the current user's home directory. */ @@ -257,7 +258,7 @@ export function setPriority(pid: number, priority?: number) { validateIntegerRange(pid, "pid"); validateIntegerRange(priority, "priority", -20, 19); - notImplemented(SEE_GITHUB_ISSUE); + core.ops.op_node_os_set_priority(pid, priority); } /** Returns the operating system's default directory for temporary files as a string. */ @@ -317,10 +318,40 @@ export function uptime(): number { /** Not yet implemented */ export function userInfo( - // deno-lint-ignore no-unused-vars options: UserInfoOptions = { encoding: "utf-8" }, ): UserInfo { - notImplemented(SEE_GITHUB_ISSUE); + const uid = Deno.uid(); + const gid = Deno.gid(); + + if (isWindows) { + 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 ? (Deno.env.get("SHELL") || null) : null; + let username = core.ops.op_node_os_username(); + + if (options?.encoding === "buffer") { + _homedir = _homedir ? Buffer.from(_homedir) : _homedir; + shell = shell ? Buffer.from(shell) : shell; + username = Buffer.from(username); + } + + return { + uid, + gid, + homedir: _homedir, + shell, + username, + }; } export const EOL = isWindows ? "\r\n" : "\n"; |