diff options
author | Kevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com> | 2019-09-27 16:09:42 -0700 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-09-27 19:09:42 -0400 |
commit | 6efca6d1a17638136eadf39644f392f9107a4a6c (patch) | |
tree | 6aedab40214d21396122a97b4e6335a0f084a079 /js | |
parent | d36391ad20afe56aaa6e42fd63597221636fdfcb (diff) |
Add Deno.hostname() (#3032)
Diffstat (limited to 'js')
-rw-r--r-- | js/deno.ts | 2 | ||||
-rw-r--r-- | js/dispatch.ts | 1 | ||||
-rw-r--r-- | js/lib.deno_runtime.d.ts | 6 | ||||
-rw-r--r-- | js/os.ts | 9 | ||||
-rw-r--r-- | js/os_test.ts | 16 |
5 files changed, 33 insertions, 1 deletions
diff --git a/js/deno.ts b/js/deno.ts index 916b7471a..511e4f0ec 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 b5116d68a..5dfec625f 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 36e49c9c2..8eb46b410 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 @@ -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 28c8b6a0b..ad3772631 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); +}); |