1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

Add Deno.hostname() (#3032)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2019-09-27 16:09:42 -07:00 committed by Ryan Dahl
parent d36391ad20
commit 6efca6d1a1
9 changed files with 60 additions and 1 deletions

11
Cargo.lock generated
View file

@ -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"

View file

@ -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"

View file

@ -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"),
};

View file

@ -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<PinnedBuf>,
) -> Result<JsonOp, ErrBox> {
state.check_env()?;
let hostname = sys_info::hostname().unwrap_or_else(|_| "".to_owned());
Ok(JsonOp::Sync(json!(hostname)))
}

View file

@ -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,

View file

@ -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) {

View file

@ -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

View file

@ -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 });

View file

@ -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);
});