summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYoshiya Hinosawa <stibium121@gmail.com>2022-01-31 14:44:19 +0900
committerGitHub <noreply@github.com>2022-01-31 14:44:19 +0900
commit49a0db0d2a2a697320ea95bacaca9bc61199c951 (patch)
tree4c5d7557afe1c93821ac5259e999e776f537041d
parent245f69256b9e22f7759b887e82138ad3844a8cf4 (diff)
feat(unstable): add Deno.getUid (#13496)
-rw-r--r--cli/dts/lib.deno.unstable.d.ts12
-rw-r--r--cli/tests/unit/os_test.ts10
-rw-r--r--runtime/js/30_os.js11
-rw-r--r--runtime/js/90_deno_ns.js1
-rw-r--r--runtime/ops/os.rs29
5 files changed, 57 insertions, 6 deletions
diff --git a/cli/dts/lib.deno.unstable.d.ts b/cli/dts/lib.deno.unstable.d.ts
index bc87ec02a..b8402cd9b 100644
--- a/cli/dts/lib.deno.unstable.d.ts
+++ b/cli/dts/lib.deno.unstable.d.ts
@@ -133,6 +133,18 @@ declare namespace Deno {
*/
export function networkInterfaces(): NetworkInterfaceInfo[];
+ /** **Unstable** new API. yet to be vetted.
+ *
+ * Returns the user id of the process on POSIX platforms. Returns null on windows.
+ *
+ * ```ts
+ * console.log(Deno.getUid());
+ * ```
+ *
+ * Requires `allow-env` permission.
+ */
+ export function getUid(): number | null;
+
/** All possible types for interfacing with foreign functions */
export type NativeType =
| "void"
diff --git a/cli/tests/unit/os_test.ts b/cli/tests/unit/os_test.ts
index bd6bc5747..a469c8c9f 100644
--- a/cli/tests/unit/os_test.ts
+++ b/cli/tests/unit/os_test.ts
@@ -202,3 +202,13 @@ Deno.test({ permissions: { env: true } }, function systemMemoryInfo() {
assert(info.swapTotal >= 0);
assert(info.swapFree >= 0);
});
+
+Deno.test({ permissions: { env: true } }, function getUid() {
+ if (Deno.build.os === "windows") {
+ assertEquals(Deno.getUid(), null);
+ } else {
+ const uid = Deno.getUid();
+ assert(typeof uid === "number");
+ assert(uid > 0);
+ }
+});
diff --git a/runtime/js/30_os.js b/runtime/js/30_os.js
index 38b0acb32..e8467c268 100644
--- a/runtime/js/30_os.js
+++ b/runtime/js/30_os.js
@@ -28,6 +28,10 @@
return core.opSync("op_network_interfaces");
}
+ function getUid() {
+ return core.opSync("op_getuid");
+ }
+
// This is an internal only method used by the test harness to override the
// behavior of exit when the exit sanitizer is enabled.
let exitHandler = null;
@@ -87,12 +91,13 @@
window.__bootstrap.os = {
env,
execPath,
- setExitHandler,
exit,
- osRelease,
- systemMemoryInfo,
+ getUid,
hostname,
loadavg,
networkInterfaces,
+ osRelease,
+ setExitHandler,
+ systemMemoryInfo,
};
})(this);
diff --git a/runtime/js/90_deno_ns.js b/runtime/js/90_deno_ns.js
index 55cd34914..871062394 100644
--- a/runtime/js/90_deno_ns.js
+++ b/runtime/js/90_deno_ns.js
@@ -122,6 +122,7 @@
osRelease: __bootstrap.os.osRelease,
systemMemoryInfo: __bootstrap.os.systemMemoryInfo,
networkInterfaces: __bootstrap.os.networkInterfaces,
+ getUid: __bootstrap.os.getUid,
applySourceMap: __bootstrap.errorStack.opApplySourceMap,
formatDiagnostics: __bootstrap.errorStack.opFormatDiagnostics,
sleepSync: __bootstrap.timers.sleepSync,
diff --git a/runtime/ops/os.rs b/runtime/ops/os.rs
index 877dbce21..405faf45a 100644
--- a/runtime/ops/os.rs
+++ b/runtime/ops/os.rs
@@ -17,16 +17,17 @@ use std::sync::Arc;
pub fn init(maybe_exit_code: Option<Arc<AtomicI32>>) -> Extension {
Extension::builder()
.ops(vec![
- ("op_exit", op_sync(op_exit)),
("op_env", op_sync(op_env)),
("op_exec_path", op_sync(op_exec_path)),
- ("op_set_env", op_sync(op_set_env)),
- ("op_get_env", op_sync(op_get_env)),
+ ("op_exit", op_sync(op_exit)),
("op_delete_env", op_sync(op_delete_env)),
+ ("op_get_env", op_sync(op_get_env)),
+ ("op_getuid", op_sync(op_getuid)),
("op_hostname", op_sync(op_hostname)),
("op_loadavg", op_sync(op_loadavg)),
("op_network_interfaces", op_sync(op_network_interfaces)),
("op_os_release", op_sync(op_os_release)),
+ ("op_set_env", op_sync(op_set_env)),
("op_set_exit_code", op_sync(op_set_exit_code)),
("op_system_memory_info", op_sync(op_system_memory_info)),
])
@@ -237,3 +238,25 @@ fn op_system_memory_info(
Err(_) => Ok(None),
}
}
+
+#[cfg(not(windows))]
+fn op_getuid(
+ state: &mut OpState,
+ _: (),
+ _: (),
+) -> Result<Option<u32>, AnyError> {
+ super::check_unstable(state, "Deno.getUid");
+ state.borrow_mut::<Permissions>().env.check_all()?;
+ unsafe { Ok(Some(libc::getuid())) }
+}
+
+#[cfg(windows)]
+fn op_getuid(
+ state: &mut OpState,
+ _: (),
+ _: (),
+) -> Result<Option<u32>, AnyError> {
+ super::check_unstable(state, "Deno.getUid");
+ state.borrow_mut::<Permissions>().env.check_all()?;
+ Ok(None)
+}