Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(doc): Added uptime function for Deno #8540

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cli/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[
"umask",
"utime",
"utimeSync",
"uptime",
];

lazy_static! {
Expand Down
2 changes: 2 additions & 0 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ declare namespace Deno {
*/
export function loadavg(): number[];

export function uptime(): { uptime: number };

/** **Unstable** new API. yet to be vetted. Under consideration to possibly move to
* Deno.build or Deno.versions and if it should depend sys-info, which may not
* be desireable.
Expand Down
17 changes: 17 additions & 0 deletions cli/ops/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::env;

pub fn init(rt: &mut deno_core::JsRuntime) {
super::reg_json_sync(rt, "op_exit", op_exit);
super::reg_json_sync(rt, "op_uptime", op_uptime);
super::reg_json_sync(rt, "op_env", op_env);
super::reg_json_sync(rt, "op_exec_path", op_exec_path);
super::reg_json_sync(rt, "op_set_env", op_set_env);
Expand Down Expand Up @@ -131,6 +132,22 @@ fn op_loadavg(
}
}

fn op_uptime(
state: &mut OpState,
_args: Value,
_zero_copy: &mut [ZeroCopyBuf],
) -> Result<Value, AnyError> {
super::check_unstable(state, "Deno.uptime");
state.borrow::<Permissions>().check_env()?;

match sys_info::boottime() {
Ok(uptime) => Ok(json!({
"uptime": uptime.tv_sec
})),
Err(_) => Ok(json!({})),
}
}

fn op_hostname(
state: &mut OpState,
_args: Value,
Expand Down
5 changes: 5 additions & 0 deletions cli/rt/30_os.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
return core.jsonOpSync("op_loadavg");
}

function uptime() {
return core.jsonOpSync("op_uptime");
}

function hostname() {
return core.jsonOpSync("op_hostname");
}
Expand Down Expand Up @@ -62,5 +66,6 @@
systemCpuInfo,
hostname,
loadavg,
uptime,
};
})(this);
1 change: 1 addition & 0 deletions cli/rt/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
linkSync: __bootstrap.fs.linkSync,
futime: __bootstrap.fs.futime,
futimeSync: __bootstrap.fs.futimeSync,
uptime: __bootstrap.os.uptime,
utime: __bootstrap.fs.utime,
utimeSync: __bootstrap.fs.utimeSync,
symlink: __bootstrap.fs.symlink,
Expand Down