2024-01-01 14:58:21 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2023-02-07 14:22:46 -05:00
|
|
|
|
2024-03-07 20:51:19 -05:00
|
|
|
import { core, primordials } from "ext:core/mod.js";
|
2024-01-26 17:46:46 -05:00
|
|
|
import {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_create_worker,
|
|
|
|
op_host_post_message,
|
|
|
|
op_host_recv_ctrl,
|
|
|
|
op_host_recv_message,
|
|
|
|
op_host_terminate_worker,
|
2024-01-26 17:46:46 -05:00
|
|
|
} from "ext:core/ops";
|
2023-02-07 14:22:46 -05:00
|
|
|
const {
|
2023-05-01 09:30:02 -04:00
|
|
|
ArrayPrototypeFilter,
|
2023-02-07 14:22:46 -05:00
|
|
|
Error,
|
|
|
|
ObjectPrototypeIsPrototypeOf,
|
|
|
|
String,
|
2023-05-01 09:30:02 -04:00
|
|
|
StringPrototypeStartsWith,
|
2024-03-07 20:51:19 -05:00
|
|
|
Symbol,
|
2023-11-19 03:13:38 -05:00
|
|
|
SymbolFor,
|
2023-02-07 14:22:46 -05:00
|
|
|
SymbolIterator,
|
|
|
|
SymbolToStringTag,
|
|
|
|
} = primordials;
|
2024-01-10 17:37:25 -05:00
|
|
|
|
2023-03-08 06:44:54 -05:00
|
|
|
import * as webidl from "ext:deno_webidl/00_webidl.js";
|
2023-11-19 03:13:38 -05:00
|
|
|
import { createFilteredInspectProxy } from "ext:deno_console/01_console.js";
|
2023-03-08 06:44:54 -05:00
|
|
|
import { URL } from "ext:deno_url/00_url.js";
|
|
|
|
import { getLocationHref } from "ext:deno_web/12_location.js";
|
|
|
|
import { serializePermissions } from "ext:runtime/10_permissions.js";
|
|
|
|
import { log } from "ext:runtime/06_util.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
defineEventHandler,
|
|
|
|
ErrorEvent,
|
|
|
|
EventTarget,
|
|
|
|
MessageEvent,
|
2023-07-31 17:22:07 -04:00
|
|
|
setIsTrusted,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_web/02_event.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
import {
|
|
|
|
deserializeJsMessageData,
|
|
|
|
MessagePortPrototype,
|
|
|
|
serializeJsMessageData,
|
2023-03-08 06:44:54 -05:00
|
|
|
} from "ext:deno_web/13_message_port.js";
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
function createWorker(
|
|
|
|
specifier,
|
|
|
|
hasSourceCode,
|
|
|
|
sourceCode,
|
|
|
|
permissions,
|
|
|
|
name,
|
|
|
|
workerType,
|
2024-03-13 13:22:25 -04:00
|
|
|
closeOnIdle,
|
2023-02-07 14:22:46 -05:00
|
|
|
) {
|
2024-01-10 17:37:25 -05:00
|
|
|
return op_create_worker({
|
2020-07-19 13:49:44 -04:00
|
|
|
hasSourceCode,
|
|
|
|
name,
|
2023-02-07 14:22:46 -05:00
|
|
|
permissions: serializePermissions(permissions),
|
|
|
|
sourceCode,
|
|
|
|
specifier,
|
2021-08-16 08:29:54 -04:00
|
|
|
workerType,
|
2024-03-13 13:22:25 -04:00
|
|
|
closeOnIdle,
|
2023-02-07 14:22:46 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function hostTerminateWorker(id) {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_host_terminate_worker(id);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function hostPostMessage(id, data) {
|
2024-01-10 17:37:25 -05:00
|
|
|
op_host_post_message(id, data);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function hostRecvCtrl(id) {
|
2023-12-26 20:30:26 -05:00
|
|
|
return op_host_recv_ctrl(id);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function hostRecvMessage(id) {
|
2023-12-26 20:30:26 -05:00
|
|
|
return op_host_recv_message(id);
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
|
2024-03-07 20:51:19 -05:00
|
|
|
const privateWorkerRef = Symbol();
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
class Worker extends EventTarget {
|
|
|
|
#id = 0;
|
|
|
|
#name = "";
|
2024-03-07 20:51:19 -05:00
|
|
|
#refCount = 1;
|
|
|
|
#messagePromise = undefined;
|
|
|
|
#controlPromise = undefined;
|
2023-02-07 14:22:46 -05:00
|
|
|
|
|
|
|
// "RUNNING" | "CLOSED" | "TERMINATED"
|
|
|
|
// "TERMINATED" means that any controls or messages received will be
|
|
|
|
// discarded. "CLOSED" means that we have received a control
|
|
|
|
// indicating that the worker is no longer running, but there might
|
|
|
|
// still be messages left to receive.
|
|
|
|
#status = "RUNNING";
|
|
|
|
|
|
|
|
constructor(specifier, options = {}) {
|
|
|
|
super();
|
|
|
|
specifier = String(specifier);
|
|
|
|
const {
|
|
|
|
deno,
|
2020-07-19 13:49:44 -04:00
|
|
|
name,
|
2023-02-07 14:22:46 -05:00
|
|
|
type = "classic",
|
|
|
|
} = options;
|
|
|
|
|
|
|
|
const workerType = webidl.converters["WorkerType"](type);
|
|
|
|
|
|
|
|
if (
|
|
|
|
StringPrototypeStartsWith(specifier, "./") ||
|
|
|
|
StringPrototypeStartsWith(specifier, "../") ||
|
|
|
|
StringPrototypeStartsWith(specifier, "/") || workerType === "classic"
|
|
|
|
) {
|
|
|
|
const baseUrl = getLocationHref();
|
|
|
|
if (baseUrl != null) {
|
|
|
|
specifier = new URL(specifier, baseUrl).href;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.#name = name;
|
|
|
|
let hasSourceCode, sourceCode;
|
|
|
|
if (workerType === "classic") {
|
|
|
|
hasSourceCode = true;
|
|
|
|
sourceCode = `importScripts("#");`;
|
|
|
|
} else {
|
|
|
|
hasSourceCode = false;
|
|
|
|
sourceCode = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = createWorker(
|
2021-01-06 15:31:16 -05:00
|
|
|
specifier,
|
2023-02-07 14:22:46 -05:00
|
|
|
hasSourceCode,
|
|
|
|
sourceCode,
|
|
|
|
deno?.permissions,
|
2024-03-13 13:22:25 -04:00
|
|
|
this.#name,
|
2021-08-16 08:29:54 -04:00
|
|
|
workerType,
|
2024-03-13 13:22:25 -04:00
|
|
|
false,
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
this.#id = id;
|
|
|
|
this.#pollControl();
|
|
|
|
this.#pollMessages();
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2024-03-07 20:51:19 -05:00
|
|
|
[privateWorkerRef](ref) {
|
|
|
|
if (ref) {
|
|
|
|
this.#refCount++;
|
|
|
|
} else {
|
|
|
|
this.#refCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ref && this.#refCount == 0) {
|
|
|
|
if (this.#controlPromise) {
|
|
|
|
core.unrefOpPromise(this.#controlPromise);
|
|
|
|
}
|
|
|
|
if (this.#messagePromise) {
|
|
|
|
core.unrefOpPromise(this.#messagePromise);
|
|
|
|
}
|
|
|
|
} else if (ref && this.#refCount == 1) {
|
|
|
|
if (this.#controlPromise) {
|
|
|
|
core.refOpPromise(this.#controlPromise);
|
|
|
|
}
|
|
|
|
if (this.#messagePromise) {
|
|
|
|
core.refOpPromise(this.#messagePromise);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
#handleError(e) {
|
|
|
|
const event = new ErrorEvent("error", {
|
|
|
|
cancelable: true,
|
|
|
|
message: e.message,
|
|
|
|
lineno: e.lineNumber ? e.lineNumber : undefined,
|
|
|
|
colno: e.columnNumber ? e.columnNumber : undefined,
|
|
|
|
filename: e.fileName,
|
|
|
|
error: null,
|
|
|
|
});
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
this.dispatchEvent(event);
|
|
|
|
// Don't bubble error event to window for loader errors (`!e.fileName`).
|
|
|
|
// TODO(nayeemrmn): It's not correct to use `e.fileName` to detect user
|
|
|
|
// errors. It won't be there for non-awaited async ops for example.
|
|
|
|
if (e.fileName && !event.defaultPrevented) {
|
|
|
|
globalThis.dispatchEvent(event);
|
|
|
|
}
|
2021-06-22 10:30:16 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
return event.defaultPrevented;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
#pollControl = async () => {
|
|
|
|
while (this.#status === "RUNNING") {
|
2024-03-07 20:51:19 -05:00
|
|
|
this.#controlPromise = hostRecvCtrl(this.#id);
|
|
|
|
if (this.#refCount < 1) {
|
|
|
|
core.unrefOpPromise(this.#controlPromise);
|
|
|
|
}
|
|
|
|
const { 0: type, 1: data } = await this.#controlPromise;
|
2021-01-07 13:06:08 -05:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
// If terminate was called then we ignore all messages
|
|
|
|
if (this.#status === "TERMINATED") {
|
|
|
|
return;
|
2021-08-16 08:29:54 -04:00
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
switch (type) {
|
|
|
|
case 1: { // TerminalError
|
|
|
|
this.#status = "CLOSED";
|
|
|
|
} /* falls through */
|
|
|
|
case 2: { // Error
|
|
|
|
if (!this.#handleError(data)) {
|
|
|
|
throw new Error("Unhandled error in child worker.");
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
break;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
case 3: { // Close
|
|
|
|
log(`Host got "close" message from worker: ${this.#name}`);
|
|
|
|
this.#status = "CLOSED";
|
Don't drop messages from workers that have already been closed (#11913)
When `worker.terminate()` is called, the spec requires that the
corresponding port message queue is emptied, so no messages can be
received after the call, even if they were sent from the worker before
it was terminated.
The spec doesn't require this of `self.close()`, and since Deno uses
different channels to send messages and to notify that the worker was
closed, messages might still arrive after the worker is known to be
closed, which are currently being dropped. This change fixes that.
The fix involves two parts: one on the JS side and one on the Rust side.
The JS side was using the `#terminated` flag to keep track of whether
the worker is known to be closed, without distinguishing whether further
messages should be dropped or not. This PR changes that flag to an
enum `#state`, which can be one of `"RUNNING"`, `"CLOSED"` or
`"TERMINATED"`.
The Rust side was removing the `WorkerThread` struct from the workers
table when a close control was received, regardless of whether there
were any messages left to read, which made any subsequent calls to
`op_host_recv_message` to return `Ok(None)`, as if there were no more
mesasges. This change instead waits for both a close control and for
the message channel's sender to be closed before the worker thread is
removed from the table.
2021-09-06 05:05:02 -04:00
|
|
|
return;
|
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
default: {
|
|
|
|
throw new Error(`Unknown worker event: "${type}"`);
|
2021-06-22 10:30:16 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#pollMessages = async () => {
|
|
|
|
while (this.#status !== "TERMINATED") {
|
2024-03-07 20:51:19 -05:00
|
|
|
this.#messagePromise = hostRecvMessage(this.#id);
|
|
|
|
if (this.#refCount < 1) {
|
|
|
|
core.unrefOpPromise(this.#messagePromise);
|
|
|
|
}
|
|
|
|
const data = await this.#messagePromise;
|
2023-02-07 14:22:46 -05:00
|
|
|
if (this.#status === "TERMINATED" || data === null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let message, transferables;
|
|
|
|
try {
|
|
|
|
const v = deserializeJsMessageData(data);
|
|
|
|
message = v[0];
|
|
|
|
transferables = v[1];
|
|
|
|
} catch (err) {
|
|
|
|
const event = new MessageEvent("messageerror", {
|
2021-06-22 10:30:16 -04:00
|
|
|
cancelable: false,
|
2023-02-07 14:22:46 -05:00
|
|
|
data: err,
|
2021-06-22 10:30:16 -04:00
|
|
|
});
|
2023-07-31 17:22:07 -04:00
|
|
|
setIsTrusted(event, true);
|
2021-06-22 10:30:16 -04:00
|
|
|
this.dispatchEvent(event);
|
2023-02-07 14:22:46 -05:00
|
|
|
return;
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const event = new MessageEvent("message", {
|
|
|
|
cancelable: false,
|
|
|
|
data: message,
|
2023-05-01 09:30:02 -04:00
|
|
|
ports: ArrayPrototypeFilter(
|
|
|
|
transferables,
|
|
|
|
(t) => ObjectPrototypeIsPrototypeOf(MessagePortPrototype, t),
|
2023-02-07 14:22:46 -05:00
|
|
|
),
|
|
|
|
});
|
2023-07-31 17:22:07 -04:00
|
|
|
setIsTrusted(event, true);
|
2023-02-07 14:22:46 -05:00
|
|
|
this.dispatchEvent(event);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
};
|
2020-07-19 13:49:44 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
postMessage(message, transferOrOptions = {}) {
|
|
|
|
const prefix = "Failed to execute 'postMessage' on 'MessagePort'";
|
2023-05-01 06:47:13 -04:00
|
|
|
webidl.requiredArguments(arguments.length, 1, prefix);
|
2023-02-07 14:22:46 -05:00
|
|
|
message = webidl.converters.any(message);
|
|
|
|
let options;
|
|
|
|
if (
|
|
|
|
webidl.type(transferOrOptions) === "Object" &&
|
|
|
|
transferOrOptions !== undefined &&
|
|
|
|
transferOrOptions[SymbolIterator] !== undefined
|
|
|
|
) {
|
|
|
|
const transfer = webidl.converters["sequence<object>"](
|
|
|
|
transferOrOptions,
|
2023-05-01 06:47:13 -04:00
|
|
|
prefix,
|
|
|
|
"Argument 2",
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
|
|
|
options = { transfer };
|
|
|
|
} else {
|
|
|
|
options = webidl.converters.StructuredSerializeOptions(
|
|
|
|
transferOrOptions,
|
2023-05-01 06:47:13 -04:00
|
|
|
prefix,
|
|
|
|
"Argument 2",
|
2023-02-07 14:22:46 -05:00
|
|
|
);
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
2023-02-07 14:22:46 -05:00
|
|
|
const { transfer } = options;
|
|
|
|
const data = serializeJsMessageData(message, transfer);
|
|
|
|
if (this.#status === "RUNNING") {
|
|
|
|
hostPostMessage(this.#id, data);
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 13:07:22 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
terminate() {
|
|
|
|
if (this.#status !== "TERMINATED") {
|
|
|
|
this.#status = "TERMINATED";
|
|
|
|
hostTerminateWorker(this.#id);
|
|
|
|
}
|
2020-07-19 13:49:44 -04:00
|
|
|
}
|
|
|
|
|
2023-11-19 03:13:38 -05:00
|
|
|
[SymbolFor("Deno.privateCustomInspect")](inspect, inspectOptions) {
|
|
|
|
return inspect(
|
|
|
|
createFilteredInspectProxy({
|
|
|
|
object: this,
|
|
|
|
evaluate: ObjectPrototypeIsPrototypeOf(WorkerPrototype, this),
|
|
|
|
keys: [
|
|
|
|
"onerror",
|
|
|
|
"onmessage",
|
|
|
|
"onmessageerror",
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
inspectOptions,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
[SymbolToStringTag] = "Worker";
|
|
|
|
}
|
2020-11-10 07:15:42 -05:00
|
|
|
|
2023-11-19 03:13:38 -05:00
|
|
|
const WorkerPrototype = Worker.prototype;
|
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
defineEventHandler(Worker.prototype, "error");
|
|
|
|
defineEventHandler(Worker.prototype, "message");
|
|
|
|
defineEventHandler(Worker.prototype, "messageerror");
|
2021-08-16 08:29:54 -04:00
|
|
|
|
2023-02-07 14:22:46 -05:00
|
|
|
webidl.converters["WorkerType"] = webidl.createEnumConverter("WorkerType", [
|
|
|
|
"classic",
|
|
|
|
"module",
|
|
|
|
]);
|
|
|
|
|
2024-03-13 13:22:25 -04:00
|
|
|
export { Worker };
|