summaryrefslogtreecommitdiff
path: root/runtime/js/11_workers.js
diff options
context:
space:
mode:
authorNayeem Rahman <nayeemrmn99@gmail.com>2021-10-13 18:04:44 +0100
committerGitHub <noreply@github.com>2021-10-13 13:04:44 -0400
commit7a22df9b7641274b2a83ce53845215d17cfda2c8 (patch)
tree6cf99f74682635a0128c5cf79f5f7d3ecfbc85f1 /runtime/js/11_workers.js
parent43a63530acb16e57cbb190eacedbd097c536a775 (diff)
fix(runtime/ops/worker_host): move permission arg parsing to Rust (#12297)
Diffstat (limited to 'runtime/js/11_workers.js')
-rw-r--r--runtime/js/11_workers.js144
1 files changed, 20 insertions, 124 deletions
diff --git a/runtime/js/11_workers.js b/runtime/js/11_workers.js
index 53b256334..8f0095056 100644
--- a/runtime/js/11_workers.js
+++ b/runtime/js/11_workers.js
@@ -4,8 +4,6 @@
((window) => {
const core = window.Deno.core;
const {
- ArrayIsArray,
- ArrayPrototypeMap,
Error,
StringPrototypeStartsWith,
String,
@@ -15,7 +13,8 @@
const webidl = window.__bootstrap.webidl;
const { URL } = window.__bootstrap.url;
const { getLocationHref } = window.__bootstrap.location;
- const { log, pathFromURL } = window.__bootstrap.util;
+ const { serializePermissions } = window.__bootstrap.permissions;
+ const { log } = window.__bootstrap.util;
const { defineEventHandler } = window.__bootstrap.event;
const { deserializeJsMessageData, serializeJsMessageData } =
window.__bootstrap.messagePort;
@@ -32,7 +31,7 @@
return core.opSync("op_create_worker", {
hasSourceCode,
name,
- permissions,
+ permissions: serializePermissions(permissions),
sourceCode,
specifier,
useDenoNamespace,
@@ -56,87 +55,6 @@
return core.opAsync("op_host_recv_message", id);
}
- /**
- * @param {"inherit" | boolean} value
- * @param {string} permission
- * @return {boolean}
- */
- function parseUnitPermission(
- value,
- permission,
- ) {
- if (value !== "inherit" && typeof value !== "boolean") {
- throw new Error(
- `Expected 'boolean' for ${permission} permission, ${typeof value} received`,
- );
- }
- return value === "inherit" ? undefined : value;
- }
-
- /**
- * @param {string} permission
- * @return {(boolean | string[])}
- */
- function parseArrayPermission(
- value,
- permission,
- ) {
- if (typeof value === "string") {
- if (value !== "inherit") {
- throw new Error(
- `Expected 'array' or 'boolean' for ${permission} permission, "${value}" received`,
- );
- }
- } else if (!ArrayIsArray(value) && typeof value !== "boolean") {
- throw new Error(
- `Expected 'array' or 'boolean' for ${permission} permission, ${typeof value} received`,
- );
- //Casts URLs to absolute routes
- } else if (ArrayIsArray(value)) {
- value = ArrayPrototypeMap(value, (route) => {
- if (route instanceof URL) {
- if (permission === "net") {
- throw new Error(
- `Expected 'string' for net permission, received 'URL'`,
- );
- } else if (permission === "env") {
- throw new Error(
- `Expected 'string' for env permission, received 'URL'`,
- );
- } else {
- route = pathFromURL(route);
- }
- }
- return route;
- });
- }
-
- return value === "inherit" ? undefined : value;
- }
-
- /**
- * Normalizes data, runs checks on parameters and deletes inherited permissions
- */
- function parsePermissions({
- env = "inherit",
- hrtime = "inherit",
- net = "inherit",
- ffi = "inherit",
- read = "inherit",
- run = "inherit",
- write = "inherit",
- }) {
- return {
- env: parseArrayPermission(env, "env"),
- hrtime: parseUnitPermission(hrtime, "hrtime"),
- net: parseArrayPermission(net, "net"),
- ffi: parseUnitPermission(ffi, "ffi"),
- read: parseArrayPermission(read, "read"),
- run: parseUnitPermission(run, "run"),
- write: parseArrayPermission(write, "write"),
- };
- }
-
class Worker extends EventTarget {
#id = 0;
#name = "";
@@ -152,43 +70,23 @@
super();
specifier = String(specifier);
const {
- deno = {},
- name = "unknown",
+ deno,
+ name,
type = "classic",
} = options;
- // TODO(Soremwar)
- // `deno: boolean` is kept for backwards compatibility with the previous
- // worker options implementation. Remove for 2.0
- let workerDenoAttributes;
- if (typeof deno == "boolean") {
- workerDenoAttributes = {
- // Change this to enable the Deno namespace by default
- namespace: deno,
- permissions: null,
- };
+ let namespace;
+ let permissions;
+ if (typeof deno == "object") {
+ namespace = deno.namespace ?? false;
+ permissions = deno.permissions ?? undefined;
} else {
- workerDenoAttributes = {
- // Change this to enable the Deno namespace by default
- namespace: !!(deno?.namespace ?? false),
- permissions: (deno?.permissions ?? "inherit") === "inherit"
- ? null
- : deno?.permissions,
- };
-
- // If the permission option is set to "none", all permissions
- // must be removed from the worker
- if (workerDenoAttributes.permissions === "none") {
- workerDenoAttributes.permissions = {
- env: false,
- hrtime: false,
- net: false,
- ffi: false,
- read: false,
- run: false,
- write: false,
- };
- }
+ // Assume `deno: boolean | undefined`.
+ // TODO(Soremwar)
+ // `deno: boolean` is kept for backwards compatibility with the previous
+ // worker options implementation. Remove for 2.0
+ namespace = !!deno;
+ permissions = undefined;
}
const workerType = webidl.converters["WorkerType"](type);
@@ -218,17 +116,16 @@
specifier,
hasSourceCode,
sourceCode,
- workerDenoAttributes.namespace,
- workerDenoAttributes.permissions === null
- ? null
- : parsePermissions(workerDenoAttributes.permissions),
- options?.name,
+ namespace,
+ permissions,
+ name,
workerType,
);
this.#id = id;
this.#pollControl();
this.#pollMessages();
}
+
#handleError(e) {
const event = new ErrorEvent("error", {
cancelable: true,
@@ -359,7 +256,6 @@
]);
window.__bootstrap.worker = {
- parsePermissions,
Worker,
};
})(this);