2023-02-14 11:38:45 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
|
|
|
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
|
|
|
|
|
2023-06-26 00:08:17 -04:00
|
|
|
import { isAbsolute, resolve } from "ext:deno_node/path.ts";
|
2023-03-08 06:44:54 -05:00
|
|
|
import { notImplemented } from "ext:deno_node/_utils.ts";
|
2023-05-23 14:56:29 -04:00
|
|
|
import { EventEmitter, once } from "ext:deno_node/events.ts";
|
2023-05-11 06:32:19 -04:00
|
|
|
import { BroadcastChannel } from "ext:deno_broadcast_channel/01_broadcast_channel.js";
|
|
|
|
import { MessageChannel, MessagePort } from "ext:deno_web/13_message_port.js";
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2023-05-23 14:56:29 -04:00
|
|
|
let environmentData = new Map();
|
2023-02-14 11:38:45 -05:00
|
|
|
let threads = 0;
|
2023-06-15 11:00:30 -04:00
|
|
|
const { core } = globalThis.__bootstrap;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
export interface WorkerOptions {
|
|
|
|
// only for typings
|
|
|
|
argv?: unknown[];
|
|
|
|
env?: Record<string, unknown>;
|
|
|
|
execArgv?: string[];
|
|
|
|
stdin?: boolean;
|
|
|
|
stdout?: boolean;
|
|
|
|
stderr?: boolean;
|
|
|
|
trackUnmanagedFds?: boolean;
|
|
|
|
resourceLimits?: {
|
|
|
|
maxYoungGenerationSizeMb?: number;
|
|
|
|
maxOldGenerationSizeMb?: number;
|
|
|
|
codeRangeSizeMb?: number;
|
|
|
|
stackSizeMb?: number;
|
|
|
|
};
|
|
|
|
|
|
|
|
eval?: boolean;
|
|
|
|
transferList?: Transferable[];
|
|
|
|
workerData?: unknown;
|
|
|
|
}
|
|
|
|
|
2023-06-26 00:08:17 -04:00
|
|
|
const WHITESPACE_ENCODINGS: Record<string, string> = {
|
|
|
|
"\u0009": "%09",
|
|
|
|
"\u000A": "%0A",
|
|
|
|
"\u000B": "%0B",
|
|
|
|
"\u000C": "%0C",
|
|
|
|
"\u000D": "%0D",
|
|
|
|
"\u0020": "%20",
|
|
|
|
};
|
|
|
|
|
|
|
|
function encodeWhitespace(string: string): string {
|
|
|
|
return string.replaceAll(/[\s]/g, (c) => {
|
|
|
|
return WHITESPACE_ENCODINGS[c] ?? c;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function toFileUrlPosix(path: string): URL {
|
|
|
|
if (!isAbsolute(path)) {
|
|
|
|
throw new TypeError("Must be an absolute path.");
|
|
|
|
}
|
|
|
|
const url = new URL("file:///");
|
|
|
|
url.pathname = encodeWhitespace(
|
|
|
|
path.replace(/%/g, "%25").replace(/\\/g, "%5C"),
|
|
|
|
);
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
function toFileUrlWin32(path: string): URL {
|
|
|
|
if (!isAbsolute(path)) {
|
|
|
|
throw new TypeError("Must be an absolute path.");
|
|
|
|
}
|
|
|
|
const [, hostname, pathname] = path.match(
|
|
|
|
/^(?:[/\\]{2}([^/\\]+)(?=[/\\](?:[^/\\]|$)))?(.*)/,
|
|
|
|
)!;
|
|
|
|
const url = new URL("file:///");
|
|
|
|
url.pathname = encodeWhitespace(pathname.replace(/%/g, "%25"));
|
|
|
|
if (hostname != null && hostname != "localhost") {
|
|
|
|
url.hostname = hostname;
|
|
|
|
if (!url.hostname) {
|
|
|
|
throw new TypeError("Invalid hostname.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a path string to a file URL.
|
|
|
|
*
|
|
|
|
* ```ts
|
|
|
|
* toFileUrl("/home/foo"); // new URL("file:///home/foo")
|
|
|
|
* toFileUrl("\\home\\foo"); // new URL("file:///home/foo")
|
|
|
|
* toFileUrl("C:\\Users\\foo"); // new URL("file:///C:/Users/foo")
|
|
|
|
* toFileUrl("\\\\127.0.0.1\\home\\foo"); // new URL("file://127.0.0.1/home/foo")
|
|
|
|
* ```
|
|
|
|
* @param path to convert to file URL
|
|
|
|
*/
|
|
|
|
function toFileUrl(path: string): URL {
|
|
|
|
return core.build.os == "windows"
|
|
|
|
? toFileUrlWin32(path)
|
|
|
|
: toFileUrlPosix(path);
|
|
|
|
}
|
|
|
|
|
2023-02-14 11:38:45 -05:00
|
|
|
const kHandle = Symbol("kHandle");
|
|
|
|
const PRIVATE_WORKER_THREAD_NAME = "$DENO_STD_NODE_WORKER_THREAD";
|
|
|
|
class _Worker extends EventEmitter {
|
|
|
|
readonly threadId: number;
|
|
|
|
readonly resourceLimits: Required<
|
|
|
|
NonNullable<WorkerOptions["resourceLimits"]>
|
|
|
|
> = {
|
|
|
|
maxYoungGenerationSizeMb: -1,
|
|
|
|
maxOldGenerationSizeMb: -1,
|
|
|
|
codeRangeSizeMb: -1,
|
|
|
|
stackSizeMb: 4,
|
|
|
|
};
|
|
|
|
private readonly [kHandle]: Worker;
|
|
|
|
|
|
|
|
postMessage: Worker["postMessage"];
|
|
|
|
|
|
|
|
constructor(specifier: URL | string, options?: WorkerOptions) {
|
|
|
|
super();
|
|
|
|
if (options?.eval === true) {
|
|
|
|
specifier = `data:text/javascript,${specifier}`;
|
|
|
|
} else if (typeof specifier === "string") {
|
2023-05-23 14:56:29 -04:00
|
|
|
specifier = resolve(specifier);
|
2023-06-15 11:00:30 -04:00
|
|
|
let pkg;
|
|
|
|
try {
|
|
|
|
pkg = core.ops.op_require_read_closest_package_json(specifier);
|
|
|
|
} catch (_) {
|
|
|
|
// empty catch block when package json might not be present
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
!(specifier.toString().endsWith(".mjs") ||
|
|
|
|
(pkg && pkg.exists && pkg.typ == "module"))
|
|
|
|
) {
|
2023-05-23 14:56:29 -04:00
|
|
|
const cwdFileUrl = toFileUrl(Deno.cwd());
|
|
|
|
specifier =
|
|
|
|
`data:text/javascript,(async function() {const { createRequire } = await import("node:module");const require = createRequire("${cwdFileUrl}");require("${specifier}");})();`;
|
|
|
|
} else {
|
2023-06-26 00:08:17 -04:00
|
|
|
specifier = toFileUrl(specifier as string);
|
2023-05-23 14:56:29 -04:00
|
|
|
}
|
2023-02-14 11:38:45 -05:00
|
|
|
}
|
|
|
|
const handle = this[kHandle] = new Worker(
|
|
|
|
specifier,
|
|
|
|
{
|
|
|
|
name: PRIVATE_WORKER_THREAD_NAME,
|
|
|
|
type: "module",
|
|
|
|
} as globalThis.WorkerOptions, // bypass unstable type error
|
|
|
|
);
|
|
|
|
handle.addEventListener(
|
|
|
|
"error",
|
|
|
|
(event) => this.emit("error", event.error || event.message),
|
|
|
|
);
|
|
|
|
handle.addEventListener(
|
|
|
|
"messageerror",
|
|
|
|
(event) => this.emit("messageerror", event.data),
|
|
|
|
);
|
|
|
|
handle.addEventListener(
|
|
|
|
"message",
|
|
|
|
(event) => this.emit("message", event.data),
|
|
|
|
);
|
|
|
|
handle.postMessage({
|
|
|
|
environmentData,
|
|
|
|
threadId: (this.threadId = ++threads),
|
|
|
|
workerData: options?.workerData,
|
|
|
|
}, options?.transferList || []);
|
|
|
|
this.postMessage = handle.postMessage.bind(handle);
|
|
|
|
this.emit("online");
|
|
|
|
}
|
|
|
|
|
|
|
|
terminate() {
|
|
|
|
this[kHandle].terminate();
|
|
|
|
this.emit("exit", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
readonly getHeapSnapshot = () =>
|
|
|
|
notImplemented("Worker.prototype.getHeapSnapshot");
|
|
|
|
// fake performance
|
|
|
|
readonly performance = globalThis.performance;
|
|
|
|
}
|
|
|
|
|
2023-05-23 14:56:29 -04:00
|
|
|
export let isMainThread;
|
|
|
|
export let resourceLimits;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2023-05-23 14:56:29 -04:00
|
|
|
let threadId = 0;
|
|
|
|
let workerData: unknown = null;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
// Like https://github.com/nodejs/node/blob/48655e17e1d84ba5021d7a94b4b88823f7c9c6cf/lib/internal/event_target.js#L611
|
|
|
|
interface NodeEventTarget extends
|
|
|
|
Pick<
|
|
|
|
EventEmitter,
|
|
|
|
"eventNames" | "listenerCount" | "emit" | "removeAllListeners"
|
|
|
|
> {
|
|
|
|
setMaxListeners(n: number): void;
|
|
|
|
getMaxListeners(): number;
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
off(eventName: string, listener: (...args: any[]) => void): NodeEventTarget;
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
on(eventName: string, listener: (...args: any[]) => void): NodeEventTarget;
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
once(eventName: string, listener: (...args: any[]) => void): NodeEventTarget;
|
|
|
|
addListener: NodeEventTarget["on"];
|
|
|
|
removeListener: NodeEventTarget["off"];
|
|
|
|
}
|
|
|
|
|
|
|
|
type ParentPort = typeof self & NodeEventTarget;
|
|
|
|
|
|
|
|
// deno-lint-ignore no-explicit-any
|
2023-05-23 14:56:29 -04:00
|
|
|
let parentPort: ParentPort = null as any;
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2023-05-23 14:56:29 -04:00
|
|
|
globalThis.__bootstrap.internals.__initWorkerThreads = () => {
|
|
|
|
isMainThread =
|
2023-02-14 11:38:45 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2023-05-23 14:56:29 -04:00
|
|
|
(globalThis as any).name !== PRIVATE_WORKER_THREAD_NAME;
|
|
|
|
|
|
|
|
defaultExport.isMainThread = isMainThread;
|
|
|
|
// fake resourceLimits
|
|
|
|
resourceLimits = isMainThread ? {} : {
|
|
|
|
maxYoungGenerationSizeMb: 48,
|
|
|
|
maxOldGenerationSizeMb: 2048,
|
|
|
|
codeRangeSizeMb: 0,
|
|
|
|
stackSizeMb: 4,
|
2023-02-14 11:38:45 -05:00
|
|
|
};
|
2023-05-23 14:56:29 -04:00
|
|
|
defaultExport.resourceLimits = resourceLimits;
|
|
|
|
|
|
|
|
if (!isMainThread) {
|
2023-02-14 11:38:45 -05:00
|
|
|
// deno-lint-ignore no-explicit-any
|
2023-05-23 14:56:29 -04:00
|
|
|
delete (globalThis as any).name;
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const listeners = new WeakMap<(...args: any[]) => void, (ev: any) => any>();
|
2023-02-14 11:38:45 -05:00
|
|
|
|
2023-05-23 14:56:29 -04:00
|
|
|
parentPort = self as ParentPort;
|
|
|
|
|
|
|
|
const initPromise = once(
|
|
|
|
parentPort,
|
|
|
|
"message",
|
|
|
|
).then((result) => {
|
|
|
|
// TODO(kt3k): The below values are set asynchronously
|
|
|
|
// using the first message from the parent.
|
|
|
|
// This should be done synchronously.
|
|
|
|
threadId = result[0].data.threadId;
|
|
|
|
workerData = result[0].data.workerData;
|
|
|
|
environmentData = result[0].data.environmentData;
|
|
|
|
|
|
|
|
defaultExport.threadId = threadId;
|
|
|
|
defaultExport.workerData = workerData;
|
|
|
|
});
|
|
|
|
|
|
|
|
parentPort.off = parentPort.removeListener = function (
|
|
|
|
this: ParentPort,
|
|
|
|
name,
|
|
|
|
listener,
|
|
|
|
) {
|
|
|
|
this.removeEventListener(name, listeners.get(listener)!);
|
|
|
|
listeners.delete(listener);
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
parentPort.on = parentPort.addListener = function (
|
|
|
|
this: ParentPort,
|
|
|
|
name,
|
|
|
|
listener,
|
|
|
|
) {
|
|
|
|
initPromise.then(() => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const _listener = (ev: any) => listener(ev.data);
|
|
|
|
listeners.set(listener, _listener);
|
|
|
|
this.addEventListener(name, _listener);
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
parentPort.once = function (this: ParentPort, name, listener) {
|
|
|
|
initPromise.then(() => {
|
|
|
|
// deno-lint-ignore no-explicit-any
|
|
|
|
const _listener = (ev: any) => listener(ev.data);
|
|
|
|
listeners.set(listener, _listener);
|
|
|
|
this.addEventListener(name, _listener);
|
|
|
|
});
|
|
|
|
return this;
|
|
|
|
};
|
|
|
|
|
|
|
|
// mocks
|
|
|
|
parentPort.setMaxListeners = () => {};
|
|
|
|
parentPort.getMaxListeners = () => Infinity;
|
|
|
|
parentPort.eventNames = () => [""];
|
|
|
|
parentPort.listenerCount = () => 0;
|
|
|
|
|
|
|
|
parentPort.emit = () => notImplemented("parentPort.emit");
|
|
|
|
parentPort.removeAllListeners = () =>
|
|
|
|
notImplemented("parentPort.removeAllListeners");
|
|
|
|
|
|
|
|
parentPort.addEventListener("offline", () => {
|
|
|
|
parentPort.emit("close");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
2023-02-14 11:38:45 -05:00
|
|
|
|
|
|
|
export function getEnvironmentData(key: unknown) {
|
|
|
|
return environmentData.get(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setEnvironmentData(key: unknown, value?: unknown) {
|
|
|
|
if (value === undefined) {
|
|
|
|
environmentData.delete(key);
|
|
|
|
} else {
|
|
|
|
environmentData.set(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const SHARE_ENV = Symbol.for("nodejs.worker_threads.SHARE_ENV");
|
|
|
|
export function markAsUntransferable() {
|
|
|
|
notImplemented("markAsUntransferable");
|
|
|
|
}
|
|
|
|
export function moveMessagePortToContext() {
|
|
|
|
notImplemented("moveMessagePortToContext");
|
|
|
|
}
|
|
|
|
export function receiveMessageOnPort() {
|
|
|
|
notImplemented("receiveMessageOnPort");
|
|
|
|
}
|
|
|
|
export {
|
|
|
|
_Worker as Worker,
|
2023-05-11 06:32:19 -04:00
|
|
|
BroadcastChannel,
|
|
|
|
MessageChannel,
|
|
|
|
MessagePort,
|
2023-02-14 11:38:45 -05:00
|
|
|
parentPort,
|
|
|
|
threadId,
|
|
|
|
workerData,
|
|
|
|
};
|
|
|
|
|
2023-05-23 14:56:29 -04:00
|
|
|
const defaultExport = {
|
2023-02-14 11:38:45 -05:00
|
|
|
markAsUntransferable,
|
|
|
|
moveMessagePortToContext,
|
|
|
|
receiveMessageOnPort,
|
2023-05-11 06:32:19 -04:00
|
|
|
MessagePort,
|
|
|
|
MessageChannel,
|
2023-02-14 11:38:45 -05:00
|
|
|
BroadcastChannel,
|
|
|
|
Worker: _Worker,
|
|
|
|
getEnvironmentData,
|
|
|
|
setEnvironmentData,
|
|
|
|
SHARE_ENV,
|
|
|
|
threadId,
|
|
|
|
workerData,
|
|
|
|
resourceLimits,
|
|
|
|
parentPort,
|
|
|
|
isMainThread,
|
|
|
|
};
|
2023-05-23 14:56:29 -04:00
|
|
|
|
|
|
|
export default defaultExport;
|