diff --git a/cli/js/dispatch.ts b/cli/js/dispatch.ts index 64a392ab9b5dec..8e773dc8b125a9 100644 --- a/cli/js/dispatch.ts +++ b/cli/js/dispatch.ts @@ -78,6 +78,7 @@ export let OP_TRANSPILE: number; export let OP_SIGNAL_BIND: number; export let OP_SIGNAL_UNBIND: number; export let OP_SIGNAL_POLL: number; +export let OP_OS_RELEASE: number; const PLUGIN_ASYNC_HANDLER_MAP: Map = new Map(); diff --git a/cli/js/lib.deno.ns.d.ts b/cli/js/lib.deno.ns.d.ts index 1839c813a41c65..163e0293efeb41 100644 --- a/cli/js/lib.deno.ns.d.ts +++ b/cli/js/lib.deno.ns.d.ts @@ -46,6 +46,12 @@ declare namespace Deno { */ export function hostname(): string; + /** Get the OS release. Requires the `--allow-env` flag. + * + * console.log(Deno.op_release()); + */ + export function osRelease(): string; + /** Exit the Deno process with optional exit code. */ export function exit(code?: number): never; diff --git a/cli/js/os.ts b/cli/js/os.ts index 275dbdf1d599be..e4fdaa4fb85bda 100644 --- a/cli/js/os.ts +++ b/cli/js/os.ts @@ -21,6 +21,15 @@ export function hostname(): string { return sendSync(dispatch.OP_HOSTNAME); } +/** Get OS release. + * Requires the `--allow-env` flag. + * + * console.log(Deno.osRelease()); + */ +export function osRelease(): string { + return sendSync(dispatch.OP_OS_RELEASE); +} + /** Exit the Deno process with optional exit code. */ export function exit(code = 0): never { sendSync(dispatch.OP_EXIT, { code }); diff --git a/cli/js/os_test.ts b/cli/js/os_test.ts index fa4bf636b3a146..97f0ff076c52c4 100644 --- a/cli/js/os_test.ts +++ b/cli/js/os_test.ts @@ -298,3 +298,19 @@ testPerm({ env: false }, function hostnamePerm(): void { } assert(caughtError); }); + +testPerm({ env: true }, function releaseDir(): void { + assertNotEquals(Deno.osRelease(), ""); +}); + +testPerm({ env: false }, function releasePerm(): void { + let caughtError = false; + try { + Deno.osRelease(); + } catch (err) { + caughtError = true; + assertEquals(err.kind, Deno.Err.PermissionDenied); + assertEquals(err.name, "PermissionDenied"); + } + assert(caughtError); +}); diff --git a/cli/ops/os.rs b/cli/ops/os.rs index 8def5ac1ef4441..1798246f714f9e 100644 --- a/cli/ops/os.rs +++ b/cli/ops/os.rs @@ -19,6 +19,10 @@ pub fn init(i: &mut Isolate, s: &State) { i.register_op("get_env", s.core_op(json_op(s.stateful_op(op_get_env)))); i.register_op("get_dir", s.core_op(json_op(s.stateful_op(op_get_dir)))); i.register_op("hostname", s.core_op(json_op(s.stateful_op(op_hostname)))); + i.register_op( + "os_release", + s.core_op(json_op(s.stateful_op(op_os_release))), + ); } #[derive(Deserialize)] @@ -167,3 +171,13 @@ fn op_hostname( let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_owned()); Ok(JsonOp::Sync(json!(hostname))) } + +fn op_os_release( + state: &State, + _args: Value, + _zero_copy: Option, +) -> Result { + state.check_env()?; + let release = sys_info::os_release().unwrap_or_else(|_| "".to_owned()); + Ok(JsonOp::Sync(json!(release))) +} diff --git a/std/node/os.ts b/std/node/os.ts index 8facde2921c072..9ff03b9427bfec 100644 --- a/std/node/os.ts +++ b/std/node/os.ts @@ -150,9 +150,9 @@ export function platform(): string { return process.platform; } -/** Not yet implemented */ +/** Returns the operating system as a string */ export function release(): string { - notImplemented(SEE_GITHUB_ISSUE); + return Deno.osRelease(); } /** Not yet implemented */ diff --git a/std/node/os_test.ts b/std/node/os_test.ts index f13589a4bcdf1e..9626c32cff59fd 100644 --- a/std/node/os_test.ts +++ b/std/node/os_test.ts @@ -35,6 +35,13 @@ test({ } }); +test({ + name: "release is a string", + fn() { + assertEquals(typeof os.release(), "string"); + } +}); + test({ name: "getPriority(): PID must be a 32 bit integer", fn() {