diff options
author | Kamil Ogórek <kamil.ogorek@gmail.com> | 2022-12-27 00:16:12 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-27 00:16:12 +0100 |
commit | 7ce2b58bcf412924464578f3469c210b34894c8b (patch) | |
tree | 571ef978ec8ff59bc2f3c229ea44b0786251e6b9 /cli | |
parent | a67fd3e23e6965ce0bf25e19e5467bc5cc538d23 (diff) |
feat(unstable): Add "Deno.osUptime()" API (#17179)
This PR adds support for `Deno.osUptime` which reports number of seconds
since os was booted. It will allow us to be compatible with Node's `os.uptime` -
https://nodejs.org/api/os.html#osuptime
Partially based on
https://docs.rs/uptime_lib/latest/src/uptime_lib/lib.rs.html
Diffstat (limited to 'cli')
-rw-r--r-- | cli/tests/unit/os_test.ts | 12 | ||||
-rw-r--r-- | cli/tests/unit/permissions_test.ts | 1 | ||||
-rw-r--r-- | cli/tsc/diagnostics.rs | 1 | ||||
-rw-r--r-- | cli/tsc/dts/lib.deno.ns.d.ts | 1 | ||||
-rw-r--r-- | cli/tsc/dts/lib.deno.unstable.d.ts | 15 |
5 files changed, 30 insertions, 0 deletions
diff --git a/cli/tests/unit/os_test.ts b/cli/tests/unit/os_test.ts index 5e88f02c1..ae2bee72e 100644 --- a/cli/tests/unit/os_test.ts +++ b/cli/tests/unit/os_test.ts @@ -239,6 +239,18 @@ Deno.test({ permissions: { sys: false } }, function releasePerm() { }, Deno.errors.PermissionDenied); }); +Deno.test({ permissions: { sys: ["osUptime"] } }, function osUptime() { + const uptime = Deno.osUptime(); + assert(typeof uptime === "number"); + assert(uptime > 0); +}); + +Deno.test({ permissions: { sys: false } }, function osUptimePerm() { + assertThrows(() => { + Deno.osUptime(); + }, Deno.errors.PermissionDenied); +}); + Deno.test( { permissions: { sys: ["systemMemoryInfo"] } }, function systemMemoryInfo() { diff --git a/cli/tests/unit/permissions_test.ts b/cli/tests/unit/permissions_test.ts index 3387913e8..3af2e35ea 100644 --- a/cli/tests/unit/permissions_test.ts +++ b/cli/tests/unit/permissions_test.ts @@ -22,6 +22,7 @@ Deno.test(async function permissionNetInvalidHost() { Deno.test(async function permissionSysValidKind() { await Deno.permissions.query({ name: "sys", kind: "loadavg" }); await Deno.permissions.query({ name: "sys", kind: "osRelease" }); + await Deno.permissions.query({ name: "sys", kind: "osUptime" }); await Deno.permissions.query({ name: "sys", kind: "networkInterfaces" }); await Deno.permissions.query({ name: "sys", kind: "systemMemoryInfo" }); await Deno.permissions.query({ name: "sys", kind: "hostname" }); diff --git a/cli/tsc/diagnostics.rs b/cli/tsc/diagnostics.rs index 4413398c2..8323fd7ac 100644 --- a/cli/tsc/diagnostics.rs +++ b/cli/tsc/diagnostics.rs @@ -46,6 +46,7 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[ "ServeInit", "ServeTlsInit", "Handler", + "osUptime", ]; static MSG_MISSING_PROPERTY_DENO: Lazy<Regex> = Lazy::new(|| { diff --git a/cli/tsc/dts/lib.deno.ns.d.ts b/cli/tsc/dts/lib.deno.ns.d.ts index fa68dc1bd..4dafd7388 100644 --- a/cli/tsc/dts/lib.deno.ns.d.ts +++ b/cli/tsc/dts/lib.deno.ns.d.ts @@ -4120,6 +4120,7 @@ declare namespace Deno { | "systemMemoryInfo" | "networkInterfaces" | "osRelease" + | "osUptime" | "uid" | "gid"; } diff --git a/cli/tsc/dts/lib.deno.unstable.d.ts b/cli/tsc/dts/lib.deno.unstable.d.ts index f1b8d99e9..77d350503 100644 --- a/cli/tsc/dts/lib.deno.unstable.d.ts +++ b/cli/tsc/dts/lib.deno.unstable.d.ts @@ -1691,6 +1691,21 @@ declare namespace Deno { /** The buffered output from the child process' `stderr`. */ readonly stderr: Uint8Array; } + + /** **UNSTABLE**: New API, yet to be vetted. + * + * Returns the Operating System uptime in number of seconds. + * + * ```ts + * console.log(Deno.osUptime()); + * ``` + * + * Requires `allow-sys` permission. + * + * @tags allow-sys + * @category Runtime Environment + */ + export function osUptime(): number; } /** **UNSTABLE**: New API, yet to be vetted. |