2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2020-09-05 20:34:02 -04:00
|
|
|
|
2020-09-19 19:17:35 -04:00
|
|
|
use crate::permissions::Permissions;
|
2021-02-23 04:24:59 -05:00
|
|
|
use deno_core::error::{type_error, AnyError};
|
2020-09-21 12:36:37 -04:00
|
|
|
use deno_core::serde_json::json;
|
|
|
|
use deno_core::serde_json::Value;
|
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;
|
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);
|
2020-10-26 10:54:27 -04:00
|
|
|
super::reg_json_sync(rt, "op_system_cpu_info", op_system_cpu_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,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<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-19 19:17:35 -04:00
|
|
|
state
|
|
|
|
.borrow::<Permissions>()
|
2021-03-17 17:45:12 -04:00
|
|
|
.read
|
|
|
|
.check_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)]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct SetEnv {
|
2019-08-26 08:50:21 -04:00
|
|
|
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,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: SetEnv,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().env.check()?;
|
2021-02-23 04:24:59 -05:00
|
|
|
let invalid_key =
|
|
|
|
args.key.is_empty() || args.key.contains(&['=', '\0'] as &[char]);
|
|
|
|
let invalid_value = args.value.contains('\0');
|
|
|
|
if invalid_key || invalid_value {
|
|
|
|
return Err(type_error("Key or value contains invalid characters."));
|
|
|
|
}
|
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,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().env.check()?;
|
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)]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct GetEnv {
|
2019-10-02 11:55:28 -04:00
|
|
|
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,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: GetEnv,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().env.check()?;
|
2021-02-23 04:24:59 -05:00
|
|
|
if args.key.is_empty() || args.key.contains(&['=', '\0'] as &[char]) {
|
|
|
|
return Err(type_error("Key contains invalid characters."));
|
|
|
|
}
|
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)]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct DeleteEnv {
|
2020-06-09 08:58:30 -04:00
|
|
|
key: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn op_delete_env(
|
2020-09-10 09:57:45 -04:00
|
|
|
state: &mut OpState,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: DeleteEnv,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().env.check()?;
|
2021-02-23 04:24:59 -05:00
|
|
|
if args.key.is_empty() || args.key.contains(&['=', '\0'] as &[char]) {
|
|
|
|
return Err(type_error("Key contains invalid characters."));
|
|
|
|
}
|
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)]
|
2021-03-18 14:42:01 -04:00
|
|
|
pub struct Exit {
|
2019-08-23 01:30:14 -04:00
|
|
|
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,
|
2021-03-18 14:42:01 -04:00
|
|
|
args: Exit,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2019-08-23 01:30:14 -04:00
|
|
|
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,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.loadavg");
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().env.check()?;
|
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,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.hostname");
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().env.check()?;
|
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,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.osRelease");
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().env.check()?;
|
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,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-09-14 12:48:57 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
2020-09-26 14:26:51 -04:00
|
|
|
super::check_unstable(state, "Deno.systemMemoryInfo");
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().env.check()?;
|
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!({})),
|
|
|
|
}
|
|
|
|
}
|
2020-10-26 10:54:27 -04:00
|
|
|
|
|
|
|
fn op_system_cpu_info(
|
|
|
|
state: &mut OpState,
|
|
|
|
_args: Value,
|
2021-04-02 09:47:57 -04:00
|
|
|
_zero_copy: Option<ZeroCopyBuf>,
|
2020-10-26 10:54:27 -04:00
|
|
|
) -> Result<Value, AnyError> {
|
|
|
|
super::check_unstable(state, "Deno.systemCpuInfo");
|
2021-03-17 17:45:12 -04:00
|
|
|
state.borrow::<Permissions>().env.check()?;
|
2020-10-26 10:54:27 -04:00
|
|
|
|
|
|
|
let cores = sys_info::cpu_num().ok();
|
|
|
|
let speed = sys_info::cpu_speed().ok();
|
|
|
|
|
|
|
|
Ok(json!({
|
|
|
|
"cores": cores,
|
|
|
|
"speed": speed
|
|
|
|
}))
|
|
|
|
}
|