summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
Diffstat (limited to 'js')
-rw-r--r--js/deno.ts2
-rw-r--r--js/dispatch.ts1
-rw-r--r--js/lib.deno_runtime.d.ts6
-rw-r--r--js/os.ts9
-rw-r--r--js/os_test.ts16
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
diff --git a/js/os.ts b/js/os.ts
index 0e4d86917..a26f57cb3 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 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);
+});