@xylabs/threads 4.7.7 → 4.8.0-rc.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{node/master/index-node.mjs → neutral/index.mjs} +196 -55
- package/dist/neutral/index.mjs.map +1 -0
- package/dist/neutral/master/implementation.mjs +264 -0
- package/dist/neutral/master/implementation.mjs.map +1 -0
- package/dist/{node/index-node.mjs → neutral/master/index.mjs} +153 -80
- package/dist/neutral/master/index.mjs.map +1 -0
- package/dist/{node/master/pool-node.mjs → neutral/master/pool.mjs} +130 -20
- package/dist/neutral/master/pool.mjs.map +1 -0
- package/dist/neutral/master/register.mjs +131 -21
- package/dist/neutral/master/register.mjs.map +1 -1
- package/dist/types/{index-node.d.ts → index.d.ts} +3 -3
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/master/implementation.d.ts +7 -0
- package/dist/types/master/implementation.d.ts.map +1 -0
- package/dist/types/master/{index-node.d.ts → index.d.ts} +3 -3
- package/dist/types/master/index.d.ts.map +1 -0
- package/dist/types/master/{pool-node.d.ts → pool.d.ts} +1 -1
- package/dist/types/master/pool.d.ts.map +1 -0
- package/package.json +13 -28
- package/dist/browser/index-browser.mjs +0 -847
- package/dist/browser/index-browser.mjs.map +0 -1
- package/dist/browser/master/index-browser.mjs +0 -813
- package/dist/browser/master/index-browser.mjs.map +0 -1
- package/dist/browser/master/pool-browser.mjs +0 -323
- package/dist/browser/master/pool-browser.mjs.map +0 -1
- package/dist/node/index-node.mjs.map +0 -1
- package/dist/node/master/index-node.mjs.map +0 -1
- package/dist/node/master/pool-node.mjs.map +0 -1
- package/dist/types/index-browser.d.ts +0 -9
- package/dist/types/index-browser.d.ts.map +0 -1
- package/dist/types/index-node.d.ts.map +0 -1
- package/dist/types/master/index-browser.d.ts +0 -13
- package/dist/types/master/index-browser.d.ts.map +0 -1
- package/dist/types/master/index-node.d.ts.map +0 -1
- package/dist/types/master/pool-browser.d.ts +0 -93
- package/dist/types/master/pool-browser.d.ts.map +0 -1
- package/dist/types/master/pool-node.d.ts.map +0 -1
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
3
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
4
|
+
}) : x)(function(x) {
|
|
5
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
6
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
7
|
+
});
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// src/master/implementation.browser.ts
|
|
14
|
+
var implementation_browser_exports = {};
|
|
15
|
+
__export(implementation_browser_exports, {
|
|
16
|
+
defaultPoolSize: () => defaultPoolSize,
|
|
17
|
+
getWorkerImplementation: () => getWorkerImplementation,
|
|
18
|
+
isWorkerRuntime: () => isWorkerRuntime
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// src/master/get-bundle-url.browser.ts
|
|
22
|
+
var bundleURL;
|
|
23
|
+
function getBundleURLCached() {
|
|
24
|
+
if (!bundleURL) {
|
|
25
|
+
bundleURL = getBundleURL();
|
|
26
|
+
}
|
|
27
|
+
return bundleURL;
|
|
28
|
+
}
|
|
29
|
+
function getBundleURL() {
|
|
30
|
+
try {
|
|
31
|
+
throw new Error("getBundleURL failed");
|
|
32
|
+
} catch (ex) {
|
|
33
|
+
const err = ex;
|
|
34
|
+
const matches = ("" + err.stack).match(/(https?|file|ftp|chrome-extension|moz-extension):\/\/[^\n)]+/g);
|
|
35
|
+
if (matches) {
|
|
36
|
+
return getBaseURL(matches[0]);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return "/";
|
|
40
|
+
}
|
|
41
|
+
function getBaseURL(url) {
|
|
42
|
+
return ("" + url).replace(/^((?:https?|file|ftp|chrome-extension|moz-extension):\/\/.+)?\/[^/]+(?:\?.*)?$/, "$1") + "/";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/master/implementation.browser.ts
|
|
46
|
+
var defaultPoolSize = typeof navigator !== "undefined" && navigator.hardwareConcurrency ? navigator.hardwareConcurrency : 4;
|
|
47
|
+
var isAbsoluteURL = (value) => /^[A-Za-z][\d+.A-Za-z\-]*:/.test(value);
|
|
48
|
+
function createSourceBlobURL(code) {
|
|
49
|
+
const blob = new Blob([code], { type: "application/javascript" });
|
|
50
|
+
return URL.createObjectURL(blob);
|
|
51
|
+
}
|
|
52
|
+
function selectWorkerImplementation() {
|
|
53
|
+
if (typeof Worker === "undefined") {
|
|
54
|
+
return class NoWebWorker {
|
|
55
|
+
constructor() {
|
|
56
|
+
throw new Error(
|
|
57
|
+
"No web worker implementation available. You might have tried to spawn a worker within a worker in a browser that doesn't support workers in workers."
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
class WebWorker extends Worker {
|
|
63
|
+
constructor(url, options) {
|
|
64
|
+
if (typeof url === "string" && options && options._baseURL) {
|
|
65
|
+
url = new URL(url, options._baseURL);
|
|
66
|
+
} else if (typeof url === "string" && !isAbsoluteURL(url) && /^file:\/\//i.test(getBundleURLCached())) {
|
|
67
|
+
url = new URL(url, getBundleURLCached().replace(/\/[^/]+$/, "/"));
|
|
68
|
+
if (options?.CORSWorkaround ?? true) {
|
|
69
|
+
url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (typeof url === "string" && isAbsoluteURL(url) && (options?.CORSWorkaround ?? true)) {
|
|
73
|
+
url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`);
|
|
74
|
+
}
|
|
75
|
+
super(url, options);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
class BlobWorker extends WebWorker {
|
|
79
|
+
constructor(blob, options) {
|
|
80
|
+
const url = globalThis.URL.createObjectURL(blob);
|
|
81
|
+
super(url, options);
|
|
82
|
+
}
|
|
83
|
+
static fromText(source, options) {
|
|
84
|
+
const blob = new globalThis.Blob([source], { type: "text/javascript" });
|
|
85
|
+
return new BlobWorker(blob, options);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
blob: BlobWorker,
|
|
90
|
+
default: WebWorker
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
var implementation;
|
|
94
|
+
function getWorkerImplementation() {
|
|
95
|
+
if (!implementation) {
|
|
96
|
+
implementation = selectWorkerImplementation();
|
|
97
|
+
}
|
|
98
|
+
return implementation;
|
|
99
|
+
}
|
|
100
|
+
function isWorkerRuntime() {
|
|
101
|
+
const isWindowContext = typeof globalThis !== "undefined" && typeof Window !== "undefined" && globalThis instanceof Window;
|
|
102
|
+
return typeof globalThis !== "undefined" && self["postMessage"] && !isWindowContext ? true : false;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// src/master/implementation.node.ts
|
|
106
|
+
var implementation_node_exports = {};
|
|
107
|
+
__export(implementation_node_exports, {
|
|
108
|
+
defaultPoolSize: () => defaultPoolSize2,
|
|
109
|
+
getWorkerImplementation: () => getWorkerImplementation2,
|
|
110
|
+
isWorkerRuntime: () => isWorkerRuntime2
|
|
111
|
+
});
|
|
112
|
+
import { EventEmitter } from "node:events";
|
|
113
|
+
import { cpus } from "node:os";
|
|
114
|
+
import path from "node:path";
|
|
115
|
+
import { cwd } from "node:process";
|
|
116
|
+
import { Worker as NativeWorker } from "node:worker_threads";
|
|
117
|
+
var defaultPoolSize2 = cpus().length;
|
|
118
|
+
function resolveScriptPath(scriptPath, baseURL) {
|
|
119
|
+
const makeAbsolute = (filePath) => {
|
|
120
|
+
return path.isAbsolute(filePath) ? filePath : path.join(baseURL ?? cwd(), filePath);
|
|
121
|
+
};
|
|
122
|
+
const absolutePath = makeAbsolute(scriptPath);
|
|
123
|
+
return absolutePath;
|
|
124
|
+
}
|
|
125
|
+
function initWorkerThreadsWorker() {
|
|
126
|
+
let allWorkers = [];
|
|
127
|
+
class Worker2 extends NativeWorker {
|
|
128
|
+
mappedEventListeners;
|
|
129
|
+
constructor(scriptPath, options) {
|
|
130
|
+
const resolvedScriptPath = options && options.fromSource ? null : resolveScriptPath(scriptPath, (options ?? {})._baseURL);
|
|
131
|
+
if (resolvedScriptPath) {
|
|
132
|
+
super(resolvedScriptPath, options);
|
|
133
|
+
} else {
|
|
134
|
+
const sourceCode = scriptPath;
|
|
135
|
+
super(sourceCode, { ...options, eval: true });
|
|
136
|
+
}
|
|
137
|
+
this.mappedEventListeners = /* @__PURE__ */ new WeakMap();
|
|
138
|
+
allWorkers.push(this);
|
|
139
|
+
}
|
|
140
|
+
addEventListener(eventName, rawListener) {
|
|
141
|
+
const listener = (message) => {
|
|
142
|
+
rawListener({ data: message });
|
|
143
|
+
};
|
|
144
|
+
this.mappedEventListeners.set(rawListener, listener);
|
|
145
|
+
this.on(eventName, listener);
|
|
146
|
+
}
|
|
147
|
+
removeEventListener(eventName, rawListener) {
|
|
148
|
+
const listener = this.mappedEventListeners.get(rawListener) || rawListener;
|
|
149
|
+
this.off(eventName, listener);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const terminateWorkersAndMaster = () => {
|
|
153
|
+
Promise.all(allWorkers.map((worker) => worker.terminate())).then(
|
|
154
|
+
() => process.exit(0),
|
|
155
|
+
() => process.exit(1)
|
|
156
|
+
);
|
|
157
|
+
allWorkers = [];
|
|
158
|
+
};
|
|
159
|
+
process.on("SIGINT", () => terminateWorkersAndMaster());
|
|
160
|
+
process.on("SIGTERM", () => terminateWorkersAndMaster());
|
|
161
|
+
class BlobWorker extends Worker2 {
|
|
162
|
+
constructor(blob, options) {
|
|
163
|
+
super(Buffer.from(blob).toString("utf-8"), { ...options, fromSource: true });
|
|
164
|
+
}
|
|
165
|
+
static fromText(source, options) {
|
|
166
|
+
return new Worker2(source, { ...options, fromSource: true });
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return {
|
|
170
|
+
blob: BlobWorker,
|
|
171
|
+
default: Worker2
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
function initTinyWorker() {
|
|
175
|
+
const TinyWorker = __require("tiny-worker");
|
|
176
|
+
let allWorkers = [];
|
|
177
|
+
class Worker2 extends TinyWorker {
|
|
178
|
+
emitter;
|
|
179
|
+
constructor(scriptPath, options) {
|
|
180
|
+
const resolvedScriptPath = options && options.fromSource ? null : process.platform === "win32" ? `file:///${resolveScriptPath(scriptPath).replaceAll("\\", "/")}` : resolveScriptPath(scriptPath);
|
|
181
|
+
if (resolvedScriptPath) {
|
|
182
|
+
super(resolvedScriptPath, [], { esm: true });
|
|
183
|
+
} else {
|
|
184
|
+
const sourceCode = scriptPath;
|
|
185
|
+
super(new Function(sourceCode), [], { esm: true });
|
|
186
|
+
}
|
|
187
|
+
allWorkers.push(this);
|
|
188
|
+
this.emitter = new EventEmitter();
|
|
189
|
+
this.onerror = (error) => this.emitter.emit("error", error);
|
|
190
|
+
this.onmessage = (message) => this.emitter.emit("message", message);
|
|
191
|
+
}
|
|
192
|
+
addEventListener(eventName, listener) {
|
|
193
|
+
this.emitter.addListener(eventName, listener);
|
|
194
|
+
}
|
|
195
|
+
removeEventListener(eventName, listener) {
|
|
196
|
+
this.emitter.removeListener(eventName, listener);
|
|
197
|
+
}
|
|
198
|
+
terminate() {
|
|
199
|
+
allWorkers = allWorkers.filter((worker) => worker !== this);
|
|
200
|
+
return super.terminate();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
const terminateWorkersAndMaster = () => {
|
|
204
|
+
Promise.all(allWorkers.map((worker) => worker.terminate())).then(
|
|
205
|
+
() => process.exit(0),
|
|
206
|
+
() => process.exit(1)
|
|
207
|
+
);
|
|
208
|
+
allWorkers = [];
|
|
209
|
+
};
|
|
210
|
+
process.on("SIGINT", () => terminateWorkersAndMaster());
|
|
211
|
+
process.on("SIGTERM", () => terminateWorkersAndMaster());
|
|
212
|
+
class BlobWorker extends Worker2 {
|
|
213
|
+
constructor(blob, options) {
|
|
214
|
+
super(Buffer.from(blob).toString("utf-8"), { ...options, fromSource: true });
|
|
215
|
+
}
|
|
216
|
+
static fromText(source, options) {
|
|
217
|
+
return new Worker2(source, { ...options, fromSource: true });
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
blob: BlobWorker,
|
|
222
|
+
default: Worker2
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
var implementation2;
|
|
226
|
+
var isTinyWorker;
|
|
227
|
+
function selectWorkerImplementation2() {
|
|
228
|
+
try {
|
|
229
|
+
isTinyWorker = false;
|
|
230
|
+
return initWorkerThreadsWorker();
|
|
231
|
+
} catch (ex) {
|
|
232
|
+
console.error(ex);
|
|
233
|
+
console.debug("Node worker_threads not available. Trying to fall back to tiny-worker polyfill...");
|
|
234
|
+
isTinyWorker = true;
|
|
235
|
+
return initTinyWorker();
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function getWorkerImplementation2() {
|
|
239
|
+
if (!implementation2) {
|
|
240
|
+
implementation2 = selectWorkerImplementation2();
|
|
241
|
+
}
|
|
242
|
+
return implementation2;
|
|
243
|
+
}
|
|
244
|
+
function isWorkerRuntime2() {
|
|
245
|
+
if (isTinyWorker) {
|
|
246
|
+
return globalThis !== void 0 && self["postMessage"] ? true : false;
|
|
247
|
+
} else {
|
|
248
|
+
const isMainThread = typeof __non_webpack_require__ === "function" ? __non_webpack_require__("worker_threads").isMainThread : eval("require")("worker_threads").isMainThread;
|
|
249
|
+
return !isMainThread;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// src/master/implementation.ts
|
|
254
|
+
var runningInNode = typeof process !== "undefined" && process.arch !== "browser" && "pid" in process;
|
|
255
|
+
var implementation3 = runningInNode ? implementation_node_exports : implementation_browser_exports;
|
|
256
|
+
var defaultPoolSize3 = implementation3.defaultPoolSize;
|
|
257
|
+
var getWorkerImplementation3 = implementation3.getWorkerImplementation;
|
|
258
|
+
var isWorkerRuntime3 = implementation3.isWorkerRuntime;
|
|
259
|
+
export {
|
|
260
|
+
defaultPoolSize3 as defaultPoolSize,
|
|
261
|
+
getWorkerImplementation3 as getWorkerImplementation,
|
|
262
|
+
isWorkerRuntime3 as isWorkerRuntime
|
|
263
|
+
};
|
|
264
|
+
//# sourceMappingURL=implementation.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/master/implementation.browser.ts","../../../src/master/get-bundle-url.browser.ts","../../../src/master/implementation.node.ts","../../../src/master/implementation.ts"],"sourcesContent":["/* eslint-disable @stylistic/max-len */\n/* eslint-disable import-x/no-internal-modules */\n// tslint:disable max-classes-per-file\n\nimport type { ImplementationExport, ThreadsWorkerOptions } from '../types/master.ts'\nimport { getBundleURL } from './get-bundle-url.browser.ts'\n\nexport const defaultPoolSize = typeof navigator !== 'undefined' && navigator.hardwareConcurrency ? navigator.hardwareConcurrency : 4\n\nconst isAbsoluteURL = (value: string) => /^[A-Za-z][\\d+.A-Za-z\\-]*:/.test(value)\n\nfunction createSourceBlobURL(code: string): string {\n const blob = new Blob([code], { type: 'application/javascript' })\n return URL.createObjectURL(blob)\n}\n\nfunction selectWorkerImplementation(): ImplementationExport {\n if (typeof Worker === 'undefined') {\n // Might happen on Safari, for instance\n // The idea is to only fail if the constructor is actually used\n return class NoWebWorker {\n constructor() {\n throw new Error(\n \"No web worker implementation available. You might have tried to spawn a worker within a worker in a browser that doesn't support workers in workers.\",\n )\n }\n } as unknown as ImplementationExport\n }\n\n class WebWorker extends Worker {\n constructor(url: string | URL, options?: ThreadsWorkerOptions) {\n if (typeof url === 'string' && options && options._baseURL) {\n url = new URL(url, options._baseURL)\n } else if (typeof url === 'string' && !isAbsoluteURL(url) && /^file:\\/\\//i.test(getBundleURL())) {\n url = new URL(url, getBundleURL().replace(/\\/[^/]+$/, '/'))\n if (options?.CORSWorkaround ?? true) {\n url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`)\n }\n }\n if (\n typeof url === 'string'\n && isAbsoluteURL(url) // Create source code blob loading JS file via `importScripts()`\n // to circumvent worker CORS restrictions\n && (options?.CORSWorkaround ?? true)\n ) {\n url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`)\n }\n super(url, options)\n }\n }\n\n class BlobWorker extends WebWorker {\n constructor(blob: Blob, options?: ThreadsWorkerOptions) {\n const url = globalThis.URL.createObjectURL(blob)\n super(url, options)\n }\n\n static fromText(source: string, options?: ThreadsWorkerOptions): WebWorker {\n const blob = new globalThis.Blob([source], { type: 'text/javascript' })\n return new BlobWorker(blob, options)\n }\n }\n\n return {\n blob: BlobWorker,\n default: WebWorker,\n }\n}\n\nlet implementation: ImplementationExport\n\nexport function getWorkerImplementation(): ImplementationExport {\n if (!implementation) {\n implementation = selectWorkerImplementation()\n }\n return implementation\n}\n\nexport function isWorkerRuntime() {\n const isWindowContext = typeof globalThis !== 'undefined' && typeof Window !== 'undefined' && globalThis instanceof Window\n return typeof globalThis !== 'undefined' && self['postMessage'] && !isWindowContext ? true : false\n}\n","// Source: <https://github.com/parcel-bundler/parcel/blob/master/packages/core/parcel-bundler/src/builtins/bundle-url.js>\n\nlet bundleURL: string | undefined\n\nfunction getBundleURLCached(): string {\n if (!bundleURL) {\n bundleURL = getBundleURL()\n }\n\n return bundleURL\n}\n\nfunction getBundleURL(): string {\n // Attempt to find the URL of the current script and use that as the base URL\n try {\n throw new Error('getBundleURL failed')\n } catch (ex) {\n const err = ex as Error\n const matches = ('' + err.stack).match(/(https?|file|ftp|chrome-extension|moz-extension):\\/\\/[^\\n)]+/g)\n if (matches) {\n return getBaseURL(matches[0])\n }\n }\n\n return '/'\n}\n\nfunction getBaseURL(url: string): string {\n return ('' + url).replace(/^((?:https?|file|ftp|chrome-extension|moz-extension):\\/\\/.+)?\\/[^/]+(?:\\?.*)?$/, '$1') + '/'\n}\n\nexport { getBundleURLCached as getBundleURL }\n","/* eslint-disable @typescript-eslint/no-require-imports */\n\n/* eslint-disable unicorn/prefer-add-event-listener */\n/* eslint-disable unicorn/prefer-event-target */\n/* eslint-disable @typescript-eslint/no-explicit-any */\n/* eslint-disable unicorn/text-encoding-identifier-case */\n\nimport { EventEmitter } from 'node:events'\nimport { cpus } from 'node:os'\nimport path from 'node:path'\nimport { cwd } from 'node:process'\nimport { Worker as NativeWorker } from 'node:worker_threads'\n\nimport type {\n ImplementationExport, ThreadsWorkerOptions, WorkerImplementation,\n// eslint-disable-next-line import-x/no-internal-modules\n} from '../types/master.ts'\n\ndeclare const __non_webpack_require__: typeof require\n\ntype WorkerEventName = 'error' | 'message'\n\nexport const defaultPoolSize = cpus().length\n\nfunction resolveScriptPath(scriptPath: string, baseURL?: string | undefined) {\n const makeAbsolute = (filePath: string) => {\n return path.isAbsolute(filePath) ? filePath : path.join(baseURL ?? cwd(), filePath)\n }\n\n const absolutePath = makeAbsolute(scriptPath)\n return absolutePath\n}\n\nfunction initWorkerThreadsWorker(): ImplementationExport {\n let allWorkers: Array<NativeWorker> = []\n\n class Worker extends NativeWorker {\n private mappedEventListeners: WeakMap<EventListener, EventListener>\n\n constructor(scriptPath: string, options?: ThreadsWorkerOptions & { fromSource: boolean }) {\n const resolvedScriptPath = options && options.fromSource ? null : resolveScriptPath(scriptPath, (options ?? {})._baseURL)\n if (resolvedScriptPath) {\n super(resolvedScriptPath, options)\n } else {\n // `options.fromSource` is true\n const sourceCode = scriptPath\n super(sourceCode, { ...options, eval: true })\n }\n\n this.mappedEventListeners = new WeakMap()\n allWorkers.push(this)\n }\n\n addEventListener(eventName: string, rawListener: EventListener) {\n const listener = (message: any) => {\n rawListener({ data: message } as any)\n }\n this.mappedEventListeners.set(rawListener, listener)\n this.on(eventName, listener)\n }\n\n removeEventListener(eventName: string, rawListener: EventListener) {\n const listener = this.mappedEventListeners.get(rawListener) || rawListener\n this.off(eventName, listener)\n }\n }\n\n const terminateWorkersAndMaster = () => {\n // we should terminate all workers and then gracefully shutdown self process\n Promise.all(allWorkers.map(worker => worker.terminate())).then(\n () => process.exit(0),\n () => process.exit(1),\n )\n allWorkers = []\n }\n\n // Take care to not leave orphaned processes behind. See #147.\n process.on('SIGINT', () => terminateWorkersAndMaster())\n process.on('SIGTERM', () => terminateWorkersAndMaster())\n\n class BlobWorker extends Worker {\n constructor(blob: Uint8Array, options?: ThreadsWorkerOptions) {\n super(Buffer.from(blob).toString('utf-8'), { ...options, fromSource: true })\n }\n\n static fromText(source: string, options?: ThreadsWorkerOptions): WorkerImplementation {\n return new Worker(source, { ...options, fromSource: true }) as any\n }\n }\n\n return {\n blob: BlobWorker as any,\n default: Worker as any,\n }\n}\n\nfunction initTinyWorker(): ImplementationExport {\n const TinyWorker = require('tiny-worker')\n\n let allWorkers: Array<typeof TinyWorker> = []\n\n class Worker extends TinyWorker {\n private emitter: EventEmitter\n\n constructor(scriptPath: string, options?: ThreadsWorkerOptions & { fromSource?: boolean }) {\n // Need to apply a work-around for Windows or it will choke upon the absolute path\n // (`Error [ERR_INVALID_PROTOCOL]: Protocol 'c:' not supported`)\n const resolvedScriptPath\n = options && options.fromSource\n ? null\n : process.platform === 'win32'\n ? `file:///${resolveScriptPath(scriptPath).replaceAll('\\\\', '/')}`\n : resolveScriptPath(scriptPath)\n\n if (resolvedScriptPath) {\n super(resolvedScriptPath, [], { esm: true })\n } else {\n // `options.fromSource` is true\n const sourceCode = scriptPath\n super(new Function(sourceCode), [], { esm: true })\n }\n\n allWorkers.push(this)\n\n this.emitter = new EventEmitter()\n this.onerror = (error: Error) => this.emitter.emit('error', error)\n this.onmessage = (message: MessageEvent) => this.emitter.emit('message', message)\n }\n\n addEventListener(eventName: WorkerEventName, listener: EventListener) {\n this.emitter.addListener(eventName, listener)\n }\n\n removeEventListener(eventName: WorkerEventName, listener: EventListener) {\n this.emitter.removeListener(eventName, listener)\n }\n\n terminate() {\n allWorkers = allWorkers.filter(worker => worker !== this)\n return super.terminate()\n }\n }\n\n const terminateWorkersAndMaster = () => {\n // we should terminate all workers and then gracefully shutdown self process\n Promise.all(allWorkers.map(worker => worker.terminate())).then(\n () => process.exit(0),\n () => process.exit(1),\n )\n allWorkers = []\n }\n\n // Take care to not leave orphaned processes behind\n // See <https://github.com/avoidwork/tiny-worker#faq>\n process.on('SIGINT', () => terminateWorkersAndMaster())\n process.on('SIGTERM', () => terminateWorkersAndMaster())\n\n class BlobWorker extends Worker {\n constructor(blob: Uint8Array, options?: ThreadsWorkerOptions) {\n super(Buffer.from(blob).toString('utf-8'), { ...options, fromSource: true })\n }\n\n static fromText(source: string, options?: ThreadsWorkerOptions): WorkerImplementation {\n return new Worker(source, { ...options, fromSource: true }) as any\n }\n }\n\n return {\n blob: BlobWorker as any,\n default: Worker as any,\n }\n}\n\nlet implementation: ImplementationExport\nlet isTinyWorker: boolean\n\nfunction selectWorkerImplementation(): ImplementationExport {\n try {\n isTinyWorker = false\n return initWorkerThreadsWorker()\n } catch (ex) {\n console.error(ex)\n // tslint:disable-next-line no-console\n console.debug('Node worker_threads not available. Trying to fall back to tiny-worker polyfill...')\n isTinyWorker = true\n return initTinyWorker()\n }\n}\n\nexport function getWorkerImplementation(): ImplementationExport {\n if (!implementation) {\n implementation = selectWorkerImplementation()\n }\n return implementation\n}\n\nexport function isWorkerRuntime() {\n if (isTinyWorker) {\n return globalThis !== undefined && self['postMessage'] ? true : false\n } else {\n // Webpack hack\n const isMainThread\n = typeof __non_webpack_require__ === 'function'\n ? __non_webpack_require__('worker_threads').isMainThread\n : eval('require')('worker_threads').isMainThread\n return !isMainThread\n }\n}\n","/*\n * This file is only a stub to make './implementation' resolve to the right module.\n */\n\n// We alias `src/master/implementation` to `src/master/implementation.browser` for web\n// browsers already in the package.json, so if get here, it's safe to pass-through the\n// node implementation\n\nimport * as BrowserImplementation from './implementation.browser.ts'\nimport * as NodeImplementation from './implementation.node.ts'\n\nconst runningInNode = typeof process !== 'undefined' && (process.arch as string) !== 'browser' && 'pid' in process\nconst implementation = runningInNode ? NodeImplementation : BrowserImplementation\n\n/** Default size of pools. Depending on the platform the value might vary from device to device. */\nexport const defaultPoolSize = implementation.defaultPoolSize\n\nexport const getWorkerImplementation = implementation.getWorkerImplementation\n\n/** Returns `true` if this code is currently running in a worker. */\nexport const isWorkerRuntime = implementation.isWorkerRuntime\n"],"mappings":";;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEA,IAAI;AAEJ,SAAS,qBAA6B;AACpC,MAAI,CAAC,WAAW;AACd,gBAAY,aAAa;AAAA,EAC3B;AAEA,SAAO;AACT;AAEA,SAAS,eAAuB;AAE9B,MAAI;AACF,UAAM,IAAI,MAAM,qBAAqB;AAAA,EACvC,SAAS,IAAI;AACX,UAAM,MAAM;AACZ,UAAM,WAAW,KAAK,IAAI,OAAO,MAAM,+DAA+D;AACtG,QAAI,SAAS;AACX,aAAO,WAAW,QAAQ,CAAC,CAAC;AAAA,IAC9B;AAAA,EACF;AAEA,SAAO;AACT;AAEA,SAAS,WAAW,KAAqB;AACvC,UAAQ,KAAK,KAAK,QAAQ,kFAAkF,IAAI,IAAI;AACtH;;;ADtBO,IAAM,kBAAkB,OAAO,cAAc,eAAe,UAAU,sBAAsB,UAAU,sBAAsB;AAEnI,IAAM,gBAAgB,CAAC,UAAkB,4BAA4B,KAAK,KAAK;AAE/E,SAAS,oBAAoB,MAAsB;AACjD,QAAM,OAAO,IAAI,KAAK,CAAC,IAAI,GAAG,EAAE,MAAM,yBAAyB,CAAC;AAChE,SAAO,IAAI,gBAAgB,IAAI;AACjC;AAEA,SAAS,6BAAmD;AAC1D,MAAI,OAAO,WAAW,aAAa;AAGjC,WAAO,MAAM,YAAY;AAAA,MACvB,cAAc;AACZ,cAAM,IAAI;AAAA,UACR;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAM,kBAAkB,OAA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,mBAAe;AACf,WAAO,eAAe;AAAA,EACxB;AACF;AAEO,SAASJ,2BAAgD;AAC9D,MAAI,CAACG,iBAAgB;AACnB,IAAAA,kBAAiBC,4BAA2B;AAAA,EAC9C;AACA,SAAOD;AACT;AAEO,SAASF,mBAAkB;AAChC,MAAI,cAAc;AAChB,WAAO,eAAe,UAAa,KAAK,aAAa,IAAI,OAAO;AAAA,EAClE,OAAO;AAEL,UAAM,eACF,OAAO,4BAA4B,aACjC,wBAAwB,gBAAgB,EAAE,eAC1C,KAAK,SAAS,EAAE,gBAAgB,EAAE;AACxC,WAAO,CAAC;AAAA,EACV;AACF;;;ACpMA,IAAM,gBAAgB,OAAO,YAAY,eAAgB,QAAQ,SAAoB,aAAa,SAAS;AAC3G,IAAMI,kBAAiB,gBAAgB,8BAAqB;AAGrD,IAAMC,mBAAkBD,gBAAe;AAEvC,IAAME,2BAA0BF,gBAAe;AAG/C,IAAMG,mBAAkBH,gBAAe;","names":["defaultPoolSize","getWorkerImplementation","isWorkerRuntime","Worker","implementation","selectWorkerImplementation","implementation","defaultPoolSize","getWorkerImplementation","isWorkerRuntime"]}
|
|
@@ -1,68 +1,120 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
1
2
|
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
3
|
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
4
|
}) : x)(function(x) {
|
|
4
5
|
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
6
|
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
7
|
});
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
7
12
|
|
|
8
|
-
// src/
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
// src/master/implementation.browser.ts
|
|
14
|
+
var implementation_browser_exports = {};
|
|
15
|
+
__export(implementation_browser_exports, {
|
|
16
|
+
defaultPoolSize: () => defaultPoolSize,
|
|
17
|
+
getWorkerImplementation: () => getWorkerImplementation,
|
|
18
|
+
isWorkerRuntime: () => isWorkerRuntime
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
// src/master/get-bundle-url.browser.ts
|
|
22
|
+
var bundleURL;
|
|
23
|
+
function getBundleURLCached() {
|
|
24
|
+
if (!bundleURL) {
|
|
25
|
+
bundleURL = getBundleURL();
|
|
26
|
+
}
|
|
27
|
+
return bundleURL;
|
|
28
|
+
}
|
|
29
|
+
function getBundleURL() {
|
|
30
|
+
try {
|
|
31
|
+
throw new Error("getBundleURL failed");
|
|
32
|
+
} catch (ex) {
|
|
33
|
+
const err = ex;
|
|
34
|
+
const matches = ("" + err.stack).match(/(https?|file|ftp|chrome-extension|moz-extension):\/\/[^\n)]+/g);
|
|
35
|
+
if (matches) {
|
|
36
|
+
return getBaseURL(matches[0]);
|
|
18
37
|
}
|
|
19
|
-
}
|
|
38
|
+
}
|
|
39
|
+
return "/";
|
|
20
40
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
41
|
+
function getBaseURL(url) {
|
|
42
|
+
return ("" + url).replace(/^((?:https?|file|ftp|chrome-extension|moz-extension):\/\/.+)?\/[^/]+(?:\?.*)?$/, "$1") + "/";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// src/master/implementation.browser.ts
|
|
46
|
+
var defaultPoolSize = typeof navigator !== "undefined" && navigator.hardwareConcurrency ? navigator.hardwareConcurrency : 4;
|
|
47
|
+
var isAbsoluteURL = (value) => /^[A-Za-z][\d+.A-Za-z\-]*:/.test(value);
|
|
48
|
+
function createSourceBlobURL(code) {
|
|
49
|
+
const blob = new Blob([code], { type: "application/javascript" });
|
|
50
|
+
return URL.createObjectURL(blob);
|
|
51
|
+
}
|
|
52
|
+
function selectWorkerImplementation() {
|
|
53
|
+
if (typeof Worker === "undefined") {
|
|
54
|
+
return class NoWebWorker {
|
|
55
|
+
constructor() {
|
|
56
|
+
throw new Error(
|
|
57
|
+
"No web worker implementation available. You might have tried to spawn a worker within a worker in a browser that doesn't support workers in workers."
|
|
58
|
+
);
|
|
59
|
+
}
|
|
34
60
|
};
|
|
35
61
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
62
|
+
class WebWorker extends Worker {
|
|
63
|
+
constructor(url, options) {
|
|
64
|
+
if (typeof url === "string" && options && options._baseURL) {
|
|
65
|
+
url = new URL(url, options._baseURL);
|
|
66
|
+
} else if (typeof url === "string" && !isAbsoluteURL(url) && /^file:\/\//i.test(getBundleURLCached())) {
|
|
67
|
+
url = new URL(url, getBundleURLCached().replace(/\/[^/]+$/, "/"));
|
|
68
|
+
if (options?.CORSWorkaround ?? true) {
|
|
69
|
+
url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (typeof url === "string" && isAbsoluteURL(url) && (options?.CORSWorkaround ?? true)) {
|
|
73
|
+
url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`);
|
|
74
|
+
}
|
|
75
|
+
super(url, options);
|
|
76
|
+
}
|
|
44
77
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
78
|
+
class BlobWorker2 extends WebWorker {
|
|
79
|
+
constructor(blob, options) {
|
|
80
|
+
const url = globalThis.URL.createObjectURL(blob);
|
|
81
|
+
super(url, options);
|
|
82
|
+
}
|
|
83
|
+
static fromText(source, options) {
|
|
84
|
+
const blob = new globalThis.Blob([source], { type: "text/javascript" });
|
|
85
|
+
return new BlobWorker2(blob, options);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
blob: BlobWorker2,
|
|
90
|
+
default: WebWorker
|
|
91
|
+
};
|
|
51
92
|
}
|
|
52
|
-
|
|
53
|
-
|
|
93
|
+
var implementation;
|
|
94
|
+
function getWorkerImplementation() {
|
|
95
|
+
if (!implementation) {
|
|
96
|
+
implementation = selectWorkerImplementation();
|
|
97
|
+
}
|
|
98
|
+
return implementation;
|
|
54
99
|
}
|
|
55
|
-
function
|
|
56
|
-
|
|
100
|
+
function isWorkerRuntime() {
|
|
101
|
+
const isWindowContext = typeof globalThis !== "undefined" && typeof Window !== "undefined" && globalThis instanceof Window;
|
|
102
|
+
return typeof globalThis !== "undefined" && self["postMessage"] && !isWindowContext ? true : false;
|
|
57
103
|
}
|
|
58
104
|
|
|
59
105
|
// src/master/implementation.node.ts
|
|
106
|
+
var implementation_node_exports = {};
|
|
107
|
+
__export(implementation_node_exports, {
|
|
108
|
+
defaultPoolSize: () => defaultPoolSize2,
|
|
109
|
+
getWorkerImplementation: () => getWorkerImplementation2,
|
|
110
|
+
isWorkerRuntime: () => isWorkerRuntime2
|
|
111
|
+
});
|
|
60
112
|
import { EventEmitter } from "node:events";
|
|
61
113
|
import { cpus } from "node:os";
|
|
62
114
|
import path from "node:path";
|
|
63
115
|
import { cwd } from "node:process";
|
|
64
116
|
import { Worker as NativeWorker } from "node:worker_threads";
|
|
65
|
-
var
|
|
117
|
+
var defaultPoolSize2 = cpus().length;
|
|
66
118
|
function resolveScriptPath(scriptPath, baseURL) {
|
|
67
119
|
const makeAbsolute = (filePath) => {
|
|
68
120
|
return path.isAbsolute(filePath) ? filePath : path.join(baseURL ?? cwd(), filePath);
|
|
@@ -170,9 +222,9 @@ function initTinyWorker() {
|
|
|
170
222
|
default: Worker3
|
|
171
223
|
};
|
|
172
224
|
}
|
|
173
|
-
var
|
|
225
|
+
var implementation2;
|
|
174
226
|
var isTinyWorker;
|
|
175
|
-
function
|
|
227
|
+
function selectWorkerImplementation2() {
|
|
176
228
|
try {
|
|
177
229
|
isTinyWorker = false;
|
|
178
230
|
return initWorkerThreadsWorker();
|
|
@@ -183,13 +235,13 @@ function selectWorkerImplementation() {
|
|
|
183
235
|
return initTinyWorker();
|
|
184
236
|
}
|
|
185
237
|
}
|
|
186
|
-
function
|
|
187
|
-
if (!
|
|
188
|
-
|
|
238
|
+
function getWorkerImplementation2() {
|
|
239
|
+
if (!implementation2) {
|
|
240
|
+
implementation2 = selectWorkerImplementation2();
|
|
189
241
|
}
|
|
190
|
-
return
|
|
242
|
+
return implementation2;
|
|
191
243
|
}
|
|
192
|
-
function
|
|
244
|
+
function isWorkerRuntime2() {
|
|
193
245
|
if (isTinyWorker) {
|
|
194
246
|
return globalThis !== void 0 && self["postMessage"] ? true : false;
|
|
195
247
|
} else {
|
|
@@ -198,7 +250,14 @@ function isWorkerRuntime() {
|
|
|
198
250
|
}
|
|
199
251
|
}
|
|
200
252
|
|
|
201
|
-
// src/master/
|
|
253
|
+
// src/master/implementation.ts
|
|
254
|
+
var runningInNode = typeof process !== "undefined" && process.arch !== "browser" && "pid" in process;
|
|
255
|
+
var implementation3 = runningInNode ? implementation_node_exports : implementation_browser_exports;
|
|
256
|
+
var defaultPoolSize3 = implementation3.defaultPoolSize;
|
|
257
|
+
var getWorkerImplementation3 = implementation3.getWorkerImplementation;
|
|
258
|
+
var isWorkerRuntime3 = implementation3.isWorkerRuntime;
|
|
259
|
+
|
|
260
|
+
// src/master/pool.ts
|
|
202
261
|
import DebugLogger from "debug";
|
|
203
262
|
import {
|
|
204
263
|
multicast,
|
|
@@ -226,9 +285,6 @@ function allSettled(values) {
|
|
|
226
285
|
);
|
|
227
286
|
}
|
|
228
287
|
|
|
229
|
-
// src/master/implementation.browser.ts
|
|
230
|
-
var defaultPoolSize2 = typeof navigator !== "undefined" && navigator.hardwareConcurrency ? navigator.hardwareConcurrency : 4;
|
|
231
|
-
|
|
232
288
|
// src/master/pool-types.ts
|
|
233
289
|
var PoolEventType = /* @__PURE__ */ ((PoolEventType2) => {
|
|
234
290
|
PoolEventType2["initialized"] = "initialized";
|
|
@@ -268,7 +324,7 @@ var Thread = {
|
|
|
268
324
|
}
|
|
269
325
|
};
|
|
270
326
|
|
|
271
|
-
// src/master/pool
|
|
327
|
+
// src/master/pool.ts
|
|
272
328
|
var nextPoolID = 1;
|
|
273
329
|
function createArray(size) {
|
|
274
330
|
const array = [];
|
|
@@ -307,7 +363,7 @@ var WorkerPool = class {
|
|
|
307
363
|
taskQueue = [];
|
|
308
364
|
constructor(spawnWorker, optionsOrSize) {
|
|
309
365
|
const options = typeof optionsOrSize === "number" ? { size: optionsOrSize } : optionsOrSize || {};
|
|
310
|
-
const { size =
|
|
366
|
+
const { size = defaultPoolSize3 } = options;
|
|
311
367
|
this.debug = DebugLogger(`threads:pool:${slugify(options.name || String(nextPoolID++))}`);
|
|
312
368
|
this.options = options;
|
|
313
369
|
this.workers = spawnWorkers(spawnWorker, size);
|
|
@@ -520,6 +576,42 @@ var Pool = PoolConstructor;
|
|
|
520
576
|
import DebugLogger3 from "debug";
|
|
521
577
|
import { Observable as Observable4 } from "observable-fns";
|
|
522
578
|
|
|
579
|
+
// src/serializers.ts
|
|
580
|
+
var DefaultErrorSerializer = {
|
|
581
|
+
deserialize(message) {
|
|
582
|
+
return Object.assign(new Error(message.message), {
|
|
583
|
+
name: message.name,
|
|
584
|
+
stack: message.stack
|
|
585
|
+
});
|
|
586
|
+
},
|
|
587
|
+
serialize(error) {
|
|
588
|
+
return {
|
|
589
|
+
__error_marker: "$$error",
|
|
590
|
+
message: error.message,
|
|
591
|
+
name: error.name,
|
|
592
|
+
stack: error.stack
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
};
|
|
596
|
+
var isSerializedError = (thing) => thing && typeof thing === "object" && "__error_marker" in thing && thing.__error_marker === "$$error";
|
|
597
|
+
var DefaultSerializer = {
|
|
598
|
+
deserialize(message) {
|
|
599
|
+
return isSerializedError(message) ? DefaultErrorSerializer.deserialize(message) : message;
|
|
600
|
+
},
|
|
601
|
+
serialize(input) {
|
|
602
|
+
return input instanceof Error ? DefaultErrorSerializer.serialize(input) : input;
|
|
603
|
+
}
|
|
604
|
+
};
|
|
605
|
+
|
|
606
|
+
// src/common.ts
|
|
607
|
+
globalThis.registeredSerializer = globalThis.registeredSerializer ?? DefaultSerializer;
|
|
608
|
+
function deserialize(message) {
|
|
609
|
+
return globalThis.registeredSerializer.deserialize(message);
|
|
610
|
+
}
|
|
611
|
+
function serialize(input) {
|
|
612
|
+
return globalThis.registeredSerializer.serialize(input);
|
|
613
|
+
}
|
|
614
|
+
|
|
523
615
|
// src/promise.ts
|
|
524
616
|
var doNothing = () => void 0;
|
|
525
617
|
function createPromiseWithResolver() {
|
|
@@ -675,25 +767,9 @@ var ObservablePromise = class _ObservablePromise extends Observable2 {
|
|
|
675
767
|
};
|
|
676
768
|
|
|
677
769
|
// src/transferable.ts
|
|
678
|
-
function isTransferable(thing) {
|
|
679
|
-
if (!thing || typeof thing !== "object") return false;
|
|
680
|
-
return true;
|
|
681
|
-
}
|
|
682
770
|
function isTransferDescriptor(thing) {
|
|
683
771
|
return thing && typeof thing === "object" && thing[$transferable];
|
|
684
772
|
}
|
|
685
|
-
function Transfer(payload, transferables) {
|
|
686
|
-
console.log("Transfer");
|
|
687
|
-
if (!transferables) {
|
|
688
|
-
if (!isTransferable(payload)) throw new Error("Not transferable");
|
|
689
|
-
transferables = [payload];
|
|
690
|
-
}
|
|
691
|
-
return {
|
|
692
|
-
[$transferable]: true,
|
|
693
|
-
send: payload,
|
|
694
|
-
transferables
|
|
695
|
-
};
|
|
696
|
-
}
|
|
697
773
|
|
|
698
774
|
// src/master/invocation-proxy.ts
|
|
699
775
|
var debugMessages = DebugLogger2("threads:master:messages");
|
|
@@ -898,18 +974,15 @@ async function spawn(worker, options) {
|
|
|
898
974
|
}
|
|
899
975
|
}
|
|
900
976
|
|
|
901
|
-
// src/master/index
|
|
902
|
-
var BlobWorker =
|
|
903
|
-
var Worker2 =
|
|
977
|
+
// src/master/index.ts
|
|
978
|
+
var BlobWorker = getWorkerImplementation3().blob;
|
|
979
|
+
var Worker2 = getWorkerImplementation3().default;
|
|
904
980
|
export {
|
|
905
981
|
BlobWorker,
|
|
906
|
-
DefaultSerializer,
|
|
907
982
|
Pool,
|
|
908
983
|
Thread,
|
|
909
|
-
Transfer,
|
|
910
984
|
Worker2 as Worker,
|
|
911
|
-
isWorkerRuntime,
|
|
912
|
-
registerSerializer,
|
|
985
|
+
isWorkerRuntime3 as isWorkerRuntime,
|
|
913
986
|
spawn
|
|
914
987
|
};
|
|
915
|
-
//# sourceMappingURL=index
|
|
988
|
+
//# sourceMappingURL=index.mjs.map
|