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;
|
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-01-06 15:31:16 -05:00
|
|
|
const build = window.__bootstrap.build.build;
|
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,
|
|
|
|
) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpSync("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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function hostTerminateWorker(id) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_host_terminate_worker", { id });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function hostPostMessage(id, data) {
|
2020-09-16 16:22:43 -04:00
|
|
|
core.jsonOpSync("op_host_post_message", { id }, data);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function hostGetMessage(id) {
|
2020-09-16 16:22:43 -04:00
|
|
|
return core.jsonOpAsync("op_host_get_message", { id });
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
|
|
|
function encodeMessage(data) {
|
|
|
|
const dataJson = JSON.stringify(data);
|
|
|
|
return encoder.encode(dataJson);
|
|
|
|
}
|
|
|
|
|
|
|
|
function decodeMessage(dataIntArray) {
|
2021-01-08 09:44:24 -05:00
|
|
|
// Temporary solution until structured clone arrives in v8.
|
|
|
|
// Current clone is made by parsing json to byte array and from byte array back to json.
|
|
|
|
// In that case "undefined" transforms to empty byte array, but empty byte array does not transform back to undefined.
|
|
|
|
// Thats why this special is statement is needed.
|
|
|
|
if (dataIntArray.length == 0) {
|
|
|
|
return undefined;
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
const dataJson = decoder.decode(dataIntArray);
|
|
|
|
return JSON.parse(dataJson);
|
|
|
|
}
|
|
|
|
|
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[])}
|
|
|
|
* */
|
|
|
|
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 (!Array.isArray(value) && typeof value !== "boolean") {
|
|
|
|
throw new Error(
|
|
|
|
`Expected 'array' or 'boolean' for ${permission} permission, ${typeof value} received`,
|
|
|
|
);
|
|
|
|
//Casts URLs to absolute routes
|
|
|
|
} else if (Array.isArray(value)) {
|
|
|
|
value = value.map((route) => {
|
|
|
|
if (route instanceof URL) {
|
|
|
|
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",
|
|
|
|
plugin = "inherit",
|
|
|
|
read = "inherit",
|
|
|
|
run = "inherit",
|
|
|
|
write = "inherit",
|
|
|
|
}) {
|
|
|
|
return {
|
2021-03-21 08:49:58 -04:00
|
|
|
env: parseUnitPermission(env, "env"),
|
|
|
|
hrtime: parseUnitPermission(hrtime, "hrtime"),
|
2021-01-06 15:31:16 -05:00
|
|
|
net: parseArrayPermission(net, "net"),
|
2021-03-21 08:49:58 -04:00
|
|
|
plugin: parseUnitPermission(plugin, "plugin"),
|
2021-01-06 15:31:16 -05:00
|
|
|
read: parseArrayPermission(read, "read"),
|
2021-03-21 08:49:58 -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,
|
|
|
|
plugin: false,
|
|
|
|
read: false,
|
|
|
|
run: false,
|
|
|
|
write: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
|
|
|
|
if (type !== "module") {
|
|
|
|
throw new Error(
|
|
|
|
'Not yet implemented: only "module" type workers are supported',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#name = name;
|
|
|
|
const hasSourceCode = false;
|
|
|
|
const sourceCode = decoder.decode(new Uint8Array());
|
|
|
|
|
2021-01-07 13:06:08 -05:00
|
|
|
if (
|
|
|
|
specifier.startsWith("./") || specifier.startsWith("../") ||
|
|
|
|
specifier.startsWith("/") || type == "classic"
|
|
|
|
) {
|
|
|
|
const baseUrl = getLocationHref();
|
|
|
|
if (baseUrl != null) {
|
|
|
|
specifier = new URL(specifier, baseUrl).href;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
const { id } = createWorker(
|
|
|
|
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,
|
|
|
|
);
|
|
|
|
this.#id = id;
|
|
|
|
this.#poll();
|
|
|
|
}
|
|
|
|
|
|
|
|
#handleMessage = (msgData) => {
|
|
|
|
let data;
|
|
|
|
try {
|
|
|
|
data = decodeMessage(new Uint8Array(msgData));
|
|
|
|
} catch (e) {
|
|
|
|
const msgErrorEvent = new MessageEvent("messageerror", {
|
|
|
|
cancelable: false,
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const msgEvent = new MessageEvent("message", {
|
|
|
|
cancelable: false,
|
|
|
|
data,
|
|
|
|
});
|
|
|
|
|
|
|
|
this.dispatchEvent(msgEvent);
|
|
|
|
};
|
|
|
|
|
|
|
|
#handleError = (e) => {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
#poll = async () => {
|
|
|
|
while (!this.#terminated) {
|
|
|
|
const event = await hostGetMessage(this.#id);
|
|
|
|
|
|
|
|
// If terminate was called then we ignore all messages
|
|
|
|
if (this.#terminated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const type = event.type;
|
|
|
|
|
|
|
|
if (type === "terminalError") {
|
|
|
|
this.#terminated = true;
|
|
|
|
if (!this.#handleError(event.error)) {
|
2020-10-20 00:05:42 -04:00
|
|
|
if (globalThis instanceof Window) {
|
|
|
|
throw new Error("Unhandled error event reached main worker.");
|
|
|
|
} else {
|
|
|
|
core.jsonOpSync(
|
|
|
|
"op_host_unhandled_error",
|
|
|
|
{ message: event.error.message },
|
|
|
|
);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === "msg") {
|
|
|
|
this.#handleMessage(event.data);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === "error") {
|
|
|
|
if (!this.#handleError(event.error)) {
|
2020-10-20 00:05:42 -04:00
|
|
|
if (globalThis instanceof Window) {
|
|
|
|
throw new Error("Unhandled error event reached main worker.");
|
|
|
|
} else {
|
|
|
|
core.jsonOpSync(
|
|
|
|
"op_host_unhandled_error",
|
|
|
|
{ message: event.error.message },
|
|
|
|
);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === "close") {
|
|
|
|
log(`Host got "close" message from worker: ${this.#name}`);
|
|
|
|
this.#terminated = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error(`Unknown worker event: "${type}"`);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
postMessage(message, transferOrOptions) {
|
|
|
|
if (transferOrOptions) {
|
|
|
|
throw new Error(
|
|
|
|
"Not yet implemented: `transfer` and `options` are not supported.",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.#terminated) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
hostPostMessage(this.#id, encodeMessage(message));
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
2020-07-19 13:49:44 -04:00
|
|
|
window.__bootstrap.worker = {
|
|
|
|
Worker,
|
|
|
|
};
|
|
|
|
})(this);
|