2021-01-10 21:59:07 -05:00
|
|
|
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license.
|
2021-02-04 17:18:32 -05:00
|
|
|
"use strict";
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
((window) => {
|
2020-09-16 16:22:43 -04:00
|
|
|
const core = window.Deno.core;
|
2021-07-03 10:58:08 -04:00
|
|
|
const {
|
|
|
|
ArrayIsArray,
|
|
|
|
ArrayPrototypeMap,
|
|
|
|
Error,
|
|
|
|
StringPrototypeStartsWith,
|
|
|
|
String,
|
|
|
|
SymbolIterator,
|
|
|
|
} = window.__bootstrap.primordials;
|
2021-06-22 10:30:16 -04:00
|
|
|
const webidl = window.__bootstrap.webidl;
|
2021-07-03 10:58:08 -04:00
|
|
|
const { URL } = window.__bootstrap.url;
|
2020-10-20 00:05:42 -04:00
|
|
|
const { Window } = window.__bootstrap.globalInterfaces;
|
2021-01-07 13:06:08 -05:00
|
|
|
const { getLocationHref } = window.__bootstrap.location;
|
2021-01-06 15:31:16 -05:00
|
|
|
const { log, pathFromURL } = window.__bootstrap.util;
|
2020-11-10 07:15:42 -05:00
|
|
|
const { defineEventHandler } = window.__bootstrap.webUtil;
|
2021-06-22 10:30:16 -04:00
|
|
|
const { deserializeJsMessageData, serializeJsMessageData } =
|
|
|
|
window.__bootstrap.messagePort;
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
function createWorker(
|
|
|
|
specifier,
|
|
|
|
hasSourceCode,
|
|
|
|
sourceCode,
|
|
|
|
useDenoNamespace,
|
2021-01-06 15:31:16 -05:00
|
|
|
permissions,
|
2020-07-19 13:49:44 -04:00
|
|
|
name,
|
2021-08-16 08:29:54 -04:00
|
|
|
workerType,
|
2020-07-19 13:49:44 -04:00
|
|
|
) {
|
2021-04-12 15:55:05 -04:00
|
|
|
return core.opSync("op_create_worker", {
|
2020-07-19 13:49:44 -04:00
|
|
|
hasSourceCode,
|
|
|
|
name,
|
2021-01-06 15:31:16 -05:00
|
|
|
permissions,
|
|
|
|
sourceCode,
|
|
|
|
specifier,
|
2020-07-19 13:49:44 -04:00
|
|
|
useDenoNamespace,
|
2021-08-16 08:29:54 -04:00
|
|
|
workerType,
|
2020-07-19 13:49:44 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function hostTerminateWorker(id) {
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opSync("op_host_terminate_worker", id);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function hostPostMessage(id, data) {
|
2021-04-12 15:55:05 -04:00
|
|
|
core.opSync("op_host_post_message", id, data);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-06-22 10:30:16 -04:00
|
|
|
function hostRecvCtrl(id) {
|
|
|
|
return core.opAsync("op_host_recv_ctrl", id);
|
|
|
|
}
|
|
|
|
|
|
|
|
function hostRecvMessage(id) {
|
|
|
|
return core.opAsync("op_host_recv_message", id);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2021-01-06 15:31:16 -05:00
|
|
|
/**
|
|
|
|
* @param {string} permission
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
2021-03-21 08:49:58 -04:00
|
|
|
function parseUnitPermission(
|
2021-01-06 15:31:16 -05:00
|
|
|
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[])}
|
2021-09-02 18:28:12 -04:00
|
|
|
*/
|
2021-01-06 15:31:16 -05:00
|
|
|
function parseArrayPermission(
|
|
|
|
value,
|
|
|
|
permission,
|
|
|
|
) {
|
|
|
|
if (typeof value === "string") {
|
|
|
|
if (value !== "inherit") {
|
|
|
|
throw new Error(
|
|
|
|
`Expected 'array' or 'boolean' for ${permission} permission, "${value}" received`,
|
|
|
|
);
|
|
|
|
}
|
2021-07-03 10:58:08 -04:00
|
|
|
} else if (!ArrayIsArray(value) && typeof value !== "boolean") {
|
2021-01-06 15:31:16 -05:00
|
|
|
throw new Error(
|
|
|
|
`Expected 'array' or 'boolean' for ${permission} permission, ${typeof value} received`,
|
|
|
|
);
|
|
|
|
//Casts URLs to absolute routes
|
2021-07-03 10:58:08 -04:00
|
|
|
} else if (ArrayIsArray(value)) {
|
|
|
|
value = ArrayPrototypeMap(value, (route) => {
|
2021-01-06 15:31:16 -05:00
|
|
|
if (route instanceof URL) {
|
2021-08-06 09:04:00 -04:00
|
|
|
if (permission === "net") {
|
2021-08-06 15:10:04 -04:00
|
|
|
throw new Error(
|
|
|
|
`Expected 'string' for net permission, received 'URL'`,
|
|
|
|
);
|
|
|
|
} else if (permission === "env") {
|
2021-08-06 09:04:00 -04:00
|
|
|
throw new Error(
|
|
|
|
`Expected 'string' for env permission, received 'URL'`,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
route = pathFromURL(route);
|
|
|
|
}
|
2021-01-06 15:31:16 -05:00
|
|
|
}
|
|
|
|
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",
|
2021-08-06 17:28:10 -04:00
|
|
|
ffi = "inherit",
|
2021-01-06 15:31:16 -05:00
|
|
|
read = "inherit",
|
|
|
|
run = "inherit",
|
|
|
|
write = "inherit",
|
|
|
|
}) {
|
|
|
|
return {
|
2021-08-06 15:10:04 -04:00
|
|
|
env: parseUnitPermission(env, "env"),
|
2021-03-21 08:49:58 -04:00
|
|
|
hrtime: parseUnitPermission(hrtime, "hrtime"),
|
2021-01-06 15:31:16 -05:00
|
|
|
net: parseArrayPermission(net, "net"),
|
2021-08-06 17:28:10 -04:00
|
|
|
ffi: parseUnitPermission(ffi, "ffi"),
|
2021-01-06 15:31:16 -05:00
|
|
|
read: parseArrayPermission(read, "read"),
|
2021-08-06 15:10:04 -04:00
|
|
|
run: parseUnitPermission(run, "run"),
|
2021-01-06 15:31:16 -05:00
|
|
|
write: parseArrayPermission(write, "write"),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
class Worker extends EventTarget {
|
|
|
|
#id = 0;
|
|
|
|
#name = "";
|
|
|
|
#terminated = false;
|
|
|
|
|
2021-01-06 15:31:16 -05:00
|
|
|
constructor(specifier, options = {}) {
|
2020-07-19 13:49:44 -04:00
|
|
|
super();
|
2021-01-07 13:06:08 -05:00
|
|
|
specifier = String(specifier);
|
2021-01-06 15:31:16 -05:00
|
|
|
const {
|
|
|
|
deno = {},
|
|
|
|
name = "unknown",
|
|
|
|
type = "classic",
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
// TODO(Soremwar)
|
2021-01-29 08:15:59 -05:00
|
|
|
// `deno: boolean` is kept for backwards compatibility with the previous
|
|
|
|
// worker options implementation. Remove for 2.0
|
2021-01-06 15:31:16 -05:00
|
|
|
let workerDenoAttributes;
|
2021-01-29 08:15:59 -05:00
|
|
|
if (typeof deno == "boolean") {
|
2021-01-06 15:31:16 -05:00
|
|
|
workerDenoAttributes = {
|
|
|
|
// Change this to enable the Deno namespace by default
|
|
|
|
namespace: deno,
|
|
|
|
permissions: null,
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
workerDenoAttributes = {
|
|
|
|
// Change this to enable the Deno namespace by default
|
|
|
|
namespace: !!(deno?.namespace ?? false),
|
|
|
|
permissions: (deno?.permissions ?? "inherit") === "inherit"
|
|
|
|
? null
|
|
|
|
: deno?.permissions,
|
|
|
|
};
|
|
|
|
|
2021-01-07 05:52:30 -05:00
|
|
|
// If the permission option is set to "none", all permissions
|
2021-01-06 15:31:16 -05:00
|
|
|
// must be removed from the worker
|
2021-01-07 05:52:30 -05:00
|
|
|
if (workerDenoAttributes.permissions === "none") {
|
2021-01-06 15:31:16 -05:00
|
|
|
workerDenoAttributes.permissions = {
|
|
|
|
env: false,
|
|
|
|
hrtime: false,
|
|
|
|
net: false,
|
2021-08-06 17:28:10 -04:00
|
|
|
ffi: false,
|
2021-01-06 15:31:16 -05:00
|
|
|
read: false,
|
|
|
|
run: false,
|
|
|
|
write: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-08-16 08:29:54 -04:00
|
|
|
const workerType = webidl.converters["WorkerType"](type);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-01-07 13:06:08 -05:00
|
|
|
if (
|
2021-07-03 10:58:08 -04:00
|
|
|
StringPrototypeStartsWith(specifier, "./") ||
|
|
|
|
StringPrototypeStartsWith(specifier, "../") ||
|
2021-08-16 08:29:54 -04:00
|
|
|
StringPrototypeStartsWith(specifier, "/") || workerType === "classic"
|
2021-01-07 13:06:08 -05:00
|
|
|
) {
|
|
|
|
const baseUrl = getLocationHref();
|
|
|
|
if (baseUrl != null) {
|
|
|
|
specifier = new URL(specifier, baseUrl).href;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-16 08:29:54 -04:00
|
|
|
this.#name = name;
|
|
|
|
let hasSourceCode, sourceCode;
|
|
|
|
if (workerType === "classic") {
|
|
|
|
hasSourceCode = true;
|
|
|
|
sourceCode = `importScripts("#");`;
|
|
|
|
} else {
|
|
|
|
hasSourceCode = false;
|
|
|
|
sourceCode = "";
|
|
|
|
}
|
|
|
|
|
2021-04-05 12:40:24 -04:00
|
|
|
const id = createWorker(
|
2020-07-19 13:49:44 -04:00
|
|
|
specifier,
|
|
|
|
hasSourceCode,
|
|
|
|
sourceCode,
|
2021-01-06 15:31:16 -05:00
|
|
|
workerDenoAttributes.namespace,
|
|
|
|
workerDenoAttributes.permissions === null
|
|
|
|
? null
|
|
|
|
: parsePermissions(workerDenoAttributes.permissions),
|
2020-07-19 13:49:44 -04:00
|
|
|
options?.name,
|
2021-08-16 08:29:54 -04:00
|
|
|
workerType,
|
2020-07-19 13:49:44 -04:00
|
|
|
);
|
|
|
|
this.#id = id;
|
2021-06-22 10:30:16 -04:00
|
|
|
this.#pollControl();
|
|
|
|
this.#pollMessages();
|
2021-05-27 19:33:11 -04:00
|
|
|
}
|
|
|
|
#handleError(e) {
|
2020-07-19 13:49:44 -04:00
|
|
|
const event = new ErrorEvent("error", {
|
|
|
|
cancelable: true,
|
|
|
|
message: e.message,
|
|
|
|
lineno: e.lineNumber ? e.lineNumber + 1 : undefined,
|
|
|
|
colno: e.columnNumber ? e.columnNumber + 1 : undefined,
|
|
|
|
filename: e.fileName,
|
|
|
|
error: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
let handled = false;
|
|
|
|
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
if (event.defaultPrevented) {
|
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return handled;
|
2021-05-27 19:33:11 -04:00
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-06-22 10:30:16 -04:00
|
|
|
#pollControl = async () => {
|
2020-07-19 13:49:44 -04:00
|
|
|
while (!this.#terminated) {
|
2021-06-22 10:30:16 -04:00
|
|
|
const [type, data] = await hostRecvCtrl(this.#id);
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
// If terminate was called then we ignore all messages
|
|
|
|
if (this.#terminated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-05-11 15:09:09 -04:00
|
|
|
switch (type) {
|
|
|
|
case 1: { // TerminalError
|
|
|
|
this.#terminated = true;
|
|
|
|
} /* falls through */
|
|
|
|
case 2: { // Error
|
|
|
|
if (!this.#handleError(data)) {
|
|
|
|
if (globalThis instanceof Window) {
|
|
|
|
throw new Error("Unhandled error event reached main worker.");
|
|
|
|
} else {
|
|
|
|
core.opSync(
|
|
|
|
"op_worker_unhandled_error",
|
|
|
|
data.message,
|
|
|
|
);
|
|
|
|
}
|
2020-10-20 00:05:42 -04:00
|
|
|
}
|
2021-05-11 15:09:09 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: { // Close
|
|
|
|
log(`Host got "close" message from worker: ${this.#name}`);
|
|
|
|
this.#terminated = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
throw new Error(`Unknown worker event: "${type}"`);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-22 10:30:16 -04:00
|
|
|
#pollMessages = async () => {
|
|
|
|
while (!this.terminated) {
|
|
|
|
const data = await hostRecvMessage(this.#id);
|
|
|
|
if (data === null) break;
|
2021-08-25 07:48:53 -04:00
|
|
|
let message, transferables;
|
2021-06-22 10:30:16 -04:00
|
|
|
try {
|
|
|
|
const v = deserializeJsMessageData(data);
|
|
|
|
message = v[0];
|
2021-08-25 07:48:53 -04:00
|
|
|
transferables = v[1];
|
2021-06-22 10:30:16 -04:00
|
|
|
} catch (err) {
|
|
|
|
const event = new MessageEvent("messageerror", {
|
|
|
|
cancelable: false,
|
|
|
|
data: err,
|
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const event = new MessageEvent("message", {
|
|
|
|
cancelable: false,
|
|
|
|
data: message,
|
2021-08-25 07:48:53 -04:00
|
|
|
ports: transferables.filter((t) => t instanceof MessagePort),
|
2021-06-22 10:30:16 -04:00
|
|
|
});
|
|
|
|
this.dispatchEvent(event);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-06-22 10:30:16 -04:00
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2021-06-22 10:30:16 -04:00
|
|
|
postMessage(message, transferOrOptions = {}) {
|
|
|
|
const prefix = "Failed to execute 'postMessage' on 'MessagePort'";
|
|
|
|
webidl.requiredArguments(arguments.length, 1, { prefix });
|
|
|
|
message = webidl.converters.any(message);
|
|
|
|
let options;
|
|
|
|
if (
|
|
|
|
webidl.type(transferOrOptions) === "Object" &&
|
|
|
|
transferOrOptions !== undefined &&
|
2021-07-03 10:58:08 -04:00
|
|
|
transferOrOptions[SymbolIterator] !== undefined
|
2021-06-22 10:30:16 -04:00
|
|
|
) {
|
|
|
|
const transfer = webidl.converters["sequence<object>"](
|
|
|
|
transferOrOptions,
|
|
|
|
{ prefix, context: "Argument 2" },
|
|
|
|
);
|
|
|
|
options = { transfer };
|
|
|
|
} else {
|
2021-08-09 04:39:00 -04:00
|
|
|
options = webidl.converters.StructuredSerializeOptions(
|
|
|
|
transferOrOptions,
|
|
|
|
{
|
|
|
|
prefix,
|
|
|
|
context: "Argument 2",
|
|
|
|
},
|
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2021-06-22 10:30:16 -04:00
|
|
|
const { transfer } = options;
|
|
|
|
const data = serializeJsMessageData(message, transfer);
|
|
|
|
if (this.#terminated) return;
|
|
|
|
hostPostMessage(this.#id, data);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
terminate() {
|
|
|
|
if (!this.#terminated) {
|
|
|
|
this.#terminated = true;
|
|
|
|
hostTerminateWorker(this.#id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-10 07:15:42 -05:00
|
|
|
defineEventHandler(Worker.prototype, "error");
|
|
|
|
defineEventHandler(Worker.prototype, "message");
|
|
|
|
defineEventHandler(Worker.prototype, "messageerror");
|
|
|
|
|
2021-08-16 08:29:54 -04:00
|
|
|
webidl.converters["WorkerType"] = webidl.createEnumConverter("WorkerType", [
|
|
|
|
"classic",
|
|
|
|
"module",
|
|
|
|
]);
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
window.__bootstrap.worker = {
|
2021-04-25 17:38:59 -04:00
|
|
|
parsePermissions,
|
2020-07-19 13:49:44 -04:00
|
|
|
Worker,
|
|
|
|
};
|
|
|
|
})(this);
|