diff --git a/Cargo.lock b/Cargo.lock index b53197223e..20e94e0c9d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -296,6 +296,7 @@ dependencies = [ "serde_derive 1.0.100 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.40 (registry+https://github.com/rust-lang/crates.io-index)", "source-map-mappings 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", + "sys-info 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)", "tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.22 (registry+https://github.com/rust-lang/crates.io-index)", @@ -1378,6 +1379,15 @@ dependencies = [ "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "sys-info" +version = "0.5.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tempfile" version = "3.1.0" @@ -2103,6 +2113,7 @@ dependencies = [ "checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5" "checksum syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "66850e97125af79138385e9b88339cbcd037e3f28ceab8c5ad98e64f0f1f80bf" "checksum synstructure 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "02353edf96d6e4dc81aea2d8490a7e9db177bf8acb0e951c24940bf866cb313f" +"checksum sys-info 0.5.8 (registry+https://github.com/rust-lang/crates.io-index)" = "0079fe39cec2c8215e21b0bc4ccec9031004c160b88358f531b601e96b77f0df" "checksum tempfile 3.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" "checksum termcolor 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "96d6098003bde162e4277c70665bd87c326f5a0c3f3fbfb285787fa482d54e6e" "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" diff --git a/cli/Cargo.toml b/cli/Cargo.toml index 4cb59f1712..8894980d7b 100644 --- a/cli/Cargo.toml +++ b/cli/Cargo.toml @@ -45,6 +45,7 @@ serde = { version = "1.0.100", features = ["derive"] } serde_derive = "1.0.100" serde_json = { version = "1.0.40", features = [ "preserve_order" ] } source-map-mappings = "0.5.0" +sys-info = "0.5.8" tempfile = "3.1.0" termcolor = "1.0.5" tokio = "0.1.22" diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs index ea71fee9fa..ab318438f0 100644 --- a/cli/ops/mod.rs +++ b/cli/ops/mod.rs @@ -83,6 +83,7 @@ pub const OP_MAKE_TEMP_DIR: OpId = 55; pub const OP_CWD: OpId = 56; pub const OP_FETCH_ASSET: OpId = 57; pub const OP_DIAL_TLS: OpId = 58; +pub const OP_HOSTNAME: OpId = 59; pub fn dispatch( state: &ThreadSafeState, @@ -305,6 +306,9 @@ pub fn dispatch( OP_DIAL_TLS => { dispatch_json::dispatch(tls::op_dial_tls, state, control, zero_copy) } + OP_HOSTNAME => { + dispatch_json::dispatch(os::op_hostname, state, control, zero_copy) + } _ => panic!("bad op_id"), }; diff --git a/cli/ops/os.rs b/cli/ops/os.rs index d033dc9c31..770af404c7 100644 --- a/cli/ops/os.rs +++ b/cli/ops/os.rs @@ -9,6 +9,7 @@ use deno::*; use log; use std::collections::HashMap; use std::env; +use sys_info; use url::Url; /// BUILD_OS and BUILD_ARCH match the values in Deno.build. See js/build.ts. @@ -126,3 +127,13 @@ pub fn op_is_tty( "stderr": atty::is(atty::Stream::Stderr), }))) } + +pub fn op_hostname( + state: &ThreadSafeState, + _args: Value, + _zero_copy: Option, +) -> Result { + state.check_env()?; + let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_owned()); + Ok(JsonOp::Sync(json!(hostname))) +} diff --git a/js/deno.ts b/js/deno.ts index 916b7471a8..511e4f0ecc 100644 --- a/js/deno.ts +++ b/js/deno.ts @@ -1,7 +1,7 @@ // Copyright 2018-2019 the Deno authors. All rights reserved. MIT license. // Public deno module. -export { env, exit, isTTY, execPath, homeDir } from "./os.ts"; +export { env, exit, isTTY, execPath, homeDir, hostname } from "./os.ts"; export { chdir, cwd } from "./dir.ts"; export { File, diff --git a/js/dispatch.ts b/js/dispatch.ts index b5116d68a3..5dfec625fa 100644 --- a/js/dispatch.ts +++ b/js/dispatch.ts @@ -61,6 +61,7 @@ export const OP_MAKE_TEMP_DIR = 55; export const OP_CWD = 56; export const OP_FETCH_ASSET = 57; export const OP_DIAL_TLS = 58; +export const OP_HOSTNAME = 59; export function asyncMsgFromRust(opId: number, ui8: Uint8Array): void { switch (opId) { diff --git a/js/lib.deno_runtime.d.ts b/js/lib.deno_runtime.d.ts index 36e49c9c23..8eb46b410e 100644 --- a/js/lib.deno_runtime.d.ts +++ b/js/lib.deno_runtime.d.ts @@ -22,6 +22,12 @@ declare namespace Deno { stdout: boolean; stderr: boolean; }; + /** Get the hostname. + * Requires the `--allow-env` flag. + * + * console.log(Deno.hostname()); + */ + export function hostname(): string; /** Exit the Deno process with optional exit code. */ export function exit(code?: number): never; /** Returns a snapshot of the environment variables at invocation. Mutating a diff --git a/js/os.ts b/js/os.ts index 0e4d86917c..a26f57cb39 100644 --- a/js/os.ts +++ b/js/os.ts @@ -18,6 +18,15 @@ export function isTTY(): { stdin: boolean; stdout: boolean; stderr: boolean } { return sendSync(dispatch.OP_IS_TTY); } +/** Get the hostname. + * Requires the `--allow-env` flag. + * + * console.log(Deno.hostname()); + */ +export function hostname(): string { + return sendSync(dispatch.OP_HOSTNAME); +} + /** Exit the Deno process with optional exit code. */ export function exit(code = 0): never { sendSync(dispatch.OP_EXIT, { code }); diff --git a/js/os_test.ts b/js/os_test.ts index 28c8b6a0be..ad37726313 100644 --- a/js/os_test.ts +++ b/js/os_test.ts @@ -70,3 +70,19 @@ testPerm({ env: false }, function execPathPerm(): void { } assert(caughtError); }); + +testPerm({ env: true }, function hostnameDir(): void { + assertNotEquals(Deno.hostname(), ""); +}); + +testPerm({ env: false }, function hostnamePerm(): void { + let caughtError = false; + try { + Deno.hostname(); + } catch (err) { + caughtError = true; + assertEquals(err.kind, Deno.ErrorKind.PermissionDenied); + assertEquals(err.name, "PermissionDenied"); + } + assert(caughtError); +});