2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-09-14 12:48:57 -04:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-16 14:28:07 -04:00
|
|
|
use deno_core::url::Url;
|
2020-09-10 09:57:45 -04:00
|
|
|
use deno_core::OpState;
|
2020-04-23 05:51:07 -04:00
|
|
|
use deno_core::ZeroCopyBuf;
|
2020-09-16 12:43:08 -04:00
|
|
|
use serde::Deserialize;
|
2020-09-05 20:34:02 -04:00
|
|
|
use serde_json::Value;
|
2019-08-23 01:30:14 -04:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::env;
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2020-09-10 09:57:45 -04:00
|
|
|
pub fn init(rt: &mut deno_core::JsRuntime) {
|
|
|
|
super::reg_json_sync(rt, "op_exit", op_exit);
|
|
|
|
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);
|
|
|
|
super::reg_json_sync(rt, "op_get_env", op_get_env);
|
|
|
|
super::reg_json_sync(rt, "op_delete_env", op_delete_env);
|
|
|
|
super::reg_json_sync(rt, "op_hostname", op_hostname);
|
|
|
|
super::reg_json_sync(rt, "op_loadavg", op_loadavg);
|
|
|
|
super::reg_json_sync(rt, "op_os_release", op_os_release);
|
|
|
|
super::reg_json_sync(rt, "op_system_memory_info", op_system_memory_info);
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_exec_path(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2019-08-23 01:30:14 -04:00
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2019-08-23 01:30:14 -04:00
|
|
|
let current_exe = env::current_exe().unwrap();
|
2020-09-10 09:57:45 -04:00
|
|
|
let cli_state = super::cli_state(state);
|
|
|
|
cli_state.check_read_blind(¤t_exe, "exec_path")?;
|
2019-08-23 01:30:14 -04:00
|
|
|
// Now apply URL parser to current exe to get fully resolved path, otherwise
|
|
|
|
// we might get `./` and `../` bits in `exec_path`
|
2019-08-14 11:03:02 -04:00
|
|
|
let exe_url = Url::from_file_path(current_exe).unwrap();
|
2019-08-23 01:30:14 -04:00
|
|
|
let path = exe_url.to_file_path().unwrap();
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(path))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-08-26 08:50:21 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SetEnv {
|
|
|
|
key: String,
|
|
|
|
value: String,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_set_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2019-08-26 08:50:21 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2019-08-26 08:50:21 -04:00
|
|
|
let args: SetEnv = serde_json::from_value(args)?;
|
2020-09-10 09:57:45 -04:00
|
|
|
let cli_state = super::cli_state(state);
|
|
|
|
cli_state.check_env()?;
|
2019-08-26 08:50:21 -04:00
|
|
|
env::set_var(args.key, args.value);
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({}))
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2019-08-23 01:30:14 -04:00
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-10 09:57:45 -04:00
|
|
|
let cli_state = super::cli_state(state);
|
|
|
|
cli_state.check_env()?;
|
2019-08-23 01:30:14 -04:00
|
|
|
let v = env::vars().collect::<HashMap<String, String>>();
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(v))
|
2019-08-23 01:30:14 -04:00
|
|
|
}
|
2019-08-14 11:03:02 -04:00
|
|
|
|
2019-10-02 11:55:28 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct GetEnv {
|
|
|
|
key: String,
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_get_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2019-10-02 11:55:28 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2019-10-02 11:55:28 -04:00
|
|
|
let args: GetEnv = serde_json::from_value(args)?;
|
2020-09-10 09:57:45 -04:00
|
|
|
let cli_state = super::cli_state(state);
|
|
|
|
cli_state.check_env()?;
|
2019-10-02 11:55:28 -04:00
|
|
|
let r = match env::var(args.key) {
|
|
|
|
Err(env::VarError::NotPresent) => json!([]),
|
|
|
|
v => json!([v?]),
|
|
|
|
};
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(r)
|
2019-10-02 11:55:28 -04:00
|
|
|
}
|
|
|
|
|
2020-06-09 08:58:30 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct DeleteEnv {
|
|
|
|
key: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_delete_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-06-09 08:58:30 -04:00
|
|
|
args: Value,
|
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-06-09 08:58:30 -04:00
|
|
|
let args: DeleteEnv = serde_json::from_value(args)?;
|
2020-09-10 09:57:45 -04:00
|
|
|
let cli_state = super::cli_state(state);
|
|
|
|
cli_state.check_env()?;
|
2020-06-09 08:58:30 -04:00
|
|
|
env::remove_var(args.key);
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!({}))
|
2020-06-09 08:58:30 -04:00
|
|
|
}
|
|
|
|
|
2019-08-23 01:30:14 -04:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct Exit {
|
|
|
|
code: i32,
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_exit(
|
2020-09-10 09:57:45 -04:00
|
|
|
_state: &mut OpState,
|
2019-08-23 01:30:14 -04:00
|
|
|
args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2019-08-23 01:30:14 -04:00
|
|
|
let args: Exit = serde_json::from_value(args)?;
|
|
|
|
std::process::exit(args.code)
|
2019-08-14 11:03:02 -04:00
|
|
|
}
|
|
|
|
|
2020-02-22 18:46:52 -05:00
|
|
|
fn op_loadavg(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-02-22 18:46:52 -05:00
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-10 09:57:45 -04:00
|
|
|
let cli_state = super::cli_state(state);
|
|
|
|
cli_state.check_unstable("Deno.loadavg");
|
|
|
|
cli_state.check_env()?;
|
2020-02-22 18:46:52 -05:00
|
|
|
match sys_info::loadavg() {
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(loadavg) => Ok(json!([loadavg.one, loadavg.five, loadavg.fifteen])),
|
|
|
|
Err(_) => Ok(json!([0f64, 0f64, 0f64])),
|
2020-02-22 18:46:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
fn op_hostname(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2019-09-27 19:09:42 -04:00
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-10 09:57:45 -04:00
|
|
|
let cli_state = super::cli_state(state);
|
|
|
|
cli_state.check_unstable("Deno.hostname");
|
|
|
|
cli_state.check_env()?;
|
2020-02-24 08:35:45 -05:00
|
|
|
let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_string());
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(hostname))
|
2019-09-27 19:09:42 -04:00
|
|
|
}
|
2020-02-24 08:35:45 -05:00
|
|
|
|
|
|
|
fn op_os_release(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-02-24 08:35:45 -05:00
|
|
|
_args: Value,
|
2020-06-01 14:20:47 -04:00
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-10 09:57:45 -04:00
|
|
|
let cli_state = super::cli_state(state);
|
|
|
|
cli_state.check_unstable("Deno.osRelease");
|
|
|
|
cli_state.check_env()?;
|
2020-02-24 08:35:45 -05:00
|
|
|
let release = sys_info::os_release().unwrap_or_else(|_| "".to_string());
|
2020-08-28 11:08:24 -04:00
|
|
|
Ok(json!(release))
|
2020-02-24 08:35:45 -05:00
|
|
|
}
|
2020-09-10 04:38:17 -04:00
|
|
|
|
|
|
|
fn op_system_memory_info(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2020-09-10 04:38:17 -04:00
|
|
|
_args: Value,
|
|
|
|
_zero_copy: &mut [ZeroCopyBuf],
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-10 09:57:45 -04:00
|
|
|
let cli_state = super::cli_state(state);
|
|
|
|
cli_state.check_unstable("Deno.systemMemoryInfo");
|
|
|
|
cli_state.check_env()?;
|
2020-09-10 04:38:17 -04:00
|
|
|
match sys_info::mem_info() {
|
|
|
|
Ok(info) => Ok(json!({
|
|
|
|
"total": info.total,
|
|
|
|
"free": info.free,
|
|
|
|
"available": info.avail,
|
|
|
|
"buffers": info.buffers,
|
|
|
|
"cached": info.cached,
|
|
|
|
"swapTotal": info.swap_total,
|
|
|
|
"swapFree": info.swap_free
|
|
|
|
})),
|
|
|
|
Err(_) => Ok(json!({})),
|
|
|
|
}
|
|
|
|
}
|