summaryrefslogtreecommitdiff
path: root/ext/node/polyfills
diff options
context:
space:
mode:
authorLeo Kettmeir <crowlkats@toaxl.com>2023-07-31 22:29:09 +0200
committerGitHub <noreply@github.com>2023-07-31 22:29:09 +0200
commitaa8078b6888ee4d55ef348e336e076676dffc25f (patch)
tree94eb64853c52c62864cfe3d3efc08edfdfa3f63b /ext/node/polyfills
parent78ceeec6bedb521dfd2a44530bee9fea62afb289 (diff)
feat(node/os): implement getPriority, setPriority & userInfo (#19370)
Takes #4202 over Closes #17850 --------- Co-authored-by: ecyrbe <ecyrbe@gmail.com>
Diffstat (limited to 'ext/node/polyfills')
-rw-r--r--ext/node/polyfills/internal/errors.ts13
-rw-r--r--ext/node/polyfills/os.ts49
2 files changed, 53 insertions, 9 deletions
diff --git a/ext/node/polyfills/internal/errors.ts b/ext/node/polyfills/internal/errors.ts
index 55098d79b..2ba7ec28e 100644
--- a/ext/node/polyfills/internal/errors.ts
+++ b/ext/node/polyfills/internal/errors.ts
@@ -2497,6 +2497,19 @@ 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");
+ }
+}
+
interface UvExceptionContext {
syscall: string;
path?: string;
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";