summaryrefslogtreecommitdiff
path: root/js
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2018-10-30 20:58:55 +0100
committerRyan Dahl <ry@tinyclouds.org>2018-10-30 12:58:55 -0700
commit946acbc559fab050d13b5f2f66088254ec96f83d (patch)
tree93cd12b60e8d91a482b550949c857bbf4a83cbd0 /js
parent8b39d2c99ef41736bb1d5b74ccda2f3aa6223e84 (diff)
Add resources op (#1119)
Diffstat (limited to 'js')
-rw-r--r--js/deno.ts1
-rw-r--r--js/resources.ts25
-rw-r--r--js/resources_test.ts43
-rw-r--r--js/unit_tests.ts1
4 files changed, 70 insertions, 0 deletions
diff --git a/js/deno.ts b/js/deno.ts
index 8a6b627b6..346997d4d 100644
--- a/js/deno.ts
+++ b/js/deno.ts
@@ -38,6 +38,7 @@ export { truncateSync, truncate } from "./truncate";
export { FileInfo } from "./file_info";
export { connect, dial, listen, Listener, Conn } from "./net";
export { metrics } from "./metrics";
+export { resources } from "./resources";
export const args: string[] = [];
// Provide the compiler API in an obfuscated way
diff --git a/js/resources.ts b/js/resources.ts
new file mode 100644
index 000000000..a28270fca
--- /dev/null
+++ b/js/resources.ts
@@ -0,0 +1,25 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+import * as msg from "gen/msg_generated";
+import * as flatbuffers from "./flatbuffers";
+import { assert } from "./util";
+import * as dispatch from "./dispatch";
+
+export function resources(): { [key: number]: string } {
+ const builder = flatbuffers.createBuilder();
+ msg.Resources.startResources(builder);
+ const inner = msg.Resource.endResource(builder);
+ const baseRes = dispatch.sendSync(builder, msg.Any.Resources, inner);
+ assert(baseRes !== null);
+ assert(msg.Any.ResourcesRes === baseRes!.innerType());
+ const res = new msg.ResourcesRes();
+ assert(baseRes!.inner(res) !== null);
+
+ const resources: { [key: number]: string } = {};
+
+ for (let i = 0; i < res.resourcesLength(); i++) {
+ const item = res.resources(i)!;
+ resources[item.rid()!] = item.repr()!;
+ }
+
+ return resources;
+}
diff --git a/js/resources_test.ts b/js/resources_test.ts
new file mode 100644
index 000000000..3b34d7395
--- /dev/null
+++ b/js/resources_test.ts
@@ -0,0 +1,43 @@
+// Copyright 2018 the Deno authors. All rights reserved. MIT license.
+import { test, testPerm, assert, assertEqual } from "./test_util.ts";
+import * as deno from "deno";
+
+test(function resourcesStdio() {
+ const res = deno.resources();
+
+ assertEqual(res[0], "stdin");
+ assertEqual(res[1], "stdout");
+ assertEqual(res[2], "stderr");
+});
+
+testPerm({ net: true }, async function resourcesNet() {
+ const addr = "127.0.0.1:4501";
+ const listener = deno.listen("tcp", addr);
+
+ const dialerConn = await deno.dial("tcp", addr);
+ const listenerConn = await listener.accept();
+
+ const res = deno.resources();
+ assertEqual(Object.values(res).filter(r => r === "tcpListener").length, 1);
+ assertEqual(Object.values(res).filter(r => r === "tcpStream").length, 2);
+
+ listenerConn.close();
+ dialerConn.close();
+ listener.close();
+});
+
+test(async function resourcesFile() {
+ const resourcesBefore = deno.resources();
+ await deno.open("tests/hello.txt");
+ const resourcesAfter = deno.resources();
+
+ // check that exactly one new resource (file) was added
+ assertEqual(
+ Object.keys(resourcesAfter).length,
+ Object.keys(resourcesBefore).length + 1
+ );
+ const newRid = Object.keys(resourcesAfter).find(rid => {
+ return !resourcesBefore.hasOwnProperty(rid);
+ });
+ assertEqual(resourcesAfter[newRid], "fsFile");
+});
diff --git a/js/unit_tests.ts b/js/unit_tests.ts
index af7b421e6..22e5fbdc0 100644
--- a/js/unit_tests.ts
+++ b/js/unit_tests.ts
@@ -22,6 +22,7 @@ import "./read_dir_test.ts";
import "./read_file_test.ts";
import "./read_link_test.ts";
import "./rename_test.ts";
+import "./resources_test.ts";
import "./stat_test.ts";
import "./symlink_test.ts";
import "./text_encoding_test.ts";