@xylabs/threads 3.5.1 → 3.5.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/common.js +3 -5
- package/{dist-esm → dist/esm}/common.js +0 -1
- package/{dist-esm → dist/esm}/index.js +0 -1
- package/{dist-esm → dist/esm}/master/get-bundle-url.browser.js +0 -2
- package/{dist-esm → dist/esm}/master/implementation.browser.js +1 -5
- package/{dist-esm → dist/esm}/master/implementation.js +0 -8
- package/{dist-esm → dist/esm}/master/implementation.node.js +0 -26
- package/{dist-esm → dist/esm}/master/index.js +0 -2
- package/{dist-esm → dist/esm}/master/invocation-proxy.js +0 -8
- package/{dist-esm → dist/esm}/master/pool-types.js +0 -1
- package/{dist-esm → dist/esm}/master/pool.js +2 -21
- package/{dist-esm → dist/esm}/master/register.js +0 -1
- package/{dist-esm → dist/esm}/master/spawn.js +0 -13
- package/{dist-esm → dist/esm}/master/thread.js +0 -4
- package/{dist-esm → dist/esm}/observable-promise.js +0 -24
- package/{dist-esm → dist/esm}/observable.js +0 -10
- package/{dist-esm → dist/esm}/ponyfills.js +0 -1
- package/{dist-esm → dist/esm}/promise.js +0 -5
- package/{dist-esm → dist/esm}/transferable.js +0 -2
- package/{dist-esm → dist/esm}/types/master.js +0 -4
- package/{dist-esm → dist/esm}/types/messages.js +0 -4
- package/{dist-esm → dist/esm}/worker/bundle-entry.js +0 -1
- package/{dist-esm → dist/esm}/worker/implementation.browser.js +0 -4
- package/{dist-esm → dist/esm}/worker/implementation.js +0 -5
- package/{dist-esm → dist/esm}/worker/implementation.tiny-worker.js +0 -7
- package/{dist-esm → dist/esm}/worker/implementation.worker_threads.js +0 -1
- package/{dist-esm → dist/esm}/worker/index.js +0 -19
- package/{dist-esm → dist/esm}/worker_threads.js +0 -4
- package/dist/index.js +0 -1
- package/dist/master/get-bundle-url.browser.js +2 -5
- package/dist/master/implementation.browser.js +4 -8
- package/dist/master/implementation.d.ts +0 -2
- package/dist/master/implementation.js +0 -8
- package/dist/master/implementation.node.d.ts +0 -1
- package/dist/master/implementation.node.js +3 -29
- package/dist/master/index.d.ts +0 -2
- package/dist/master/index.js +0 -2
- package/dist/master/invocation-proxy.js +2 -11
- package/dist/master/pool-types.d.ts +0 -14
- package/dist/master/pool-types.js +0 -1
- package/dist/master/pool.d.ts +0 -41
- package/dist/master/pool.js +2 -21
- package/dist/master/register.js +0 -1
- package/dist/master/spawn.d.ts +0 -9
- package/dist/master/spawn.js +1 -15
- package/dist/master/thread.d.ts +2 -6
- package/dist/master/thread.js +0 -4
- package/dist/observable-promise.d.ts +0 -13
- package/dist/observable-promise.js +0 -24
- package/dist/observable.d.ts +0 -9
- package/dist/observable.js +0 -10
- package/dist/ponyfills.js +1 -3
- package/dist/promise.d.ts +0 -4
- package/dist/promise.js +1 -7
- package/dist/serializers.js +2 -2
- package/dist/transferable.d.ts +0 -33
- package/dist/transferable.js +2 -5
- package/dist/types/master.d.ts +0 -14
- package/dist/types/master.js +0 -4
- package/dist/types/messages.js +0 -4
- package/dist/worker/bundle-entry.js +0 -1
- package/dist/worker/implementation.browser.d.ts +1 -2
- package/dist/worker/implementation.browser.js +0 -4
- package/dist/worker/implementation.js +0 -5
- package/dist/worker/implementation.tiny-worker.d.ts +1 -2
- package/dist/worker/implementation.tiny-worker.js +0 -7
- package/dist/worker/implementation.worker_threads.d.ts +1 -1
- package/dist/worker/implementation.worker_threads.js +0 -1
- package/dist/worker/index.d.ts +0 -8
- package/dist/worker/index.js +2 -21
- package/dist/worker_threads.js +1 -5
- package/index.mjs +0 -1
- package/observable.d.ts +0 -1
- package/observable.js +0 -2
- package/observable.mjs +0 -1
- package/package.json +16 -17
- package/register.d.ts +0 -2
- package/register.js +0 -2
- package/register.mjs +0 -1
- package/rollup.config.js +0 -1
- package/src/index.ts +0 -1
- package/src/master/pool.ts +2 -1
- package/src/master/spawn.ts +1 -2
- package/src/observable-promise.ts +1 -0
- package/src/worker/implementation.browser.ts +1 -1
- package/src/worker/implementation.tiny-worker.ts +1 -1
- package/src/worker/implementation.ts +1 -1
- package/src/worker/implementation.worker_threads.ts +1 -1
- package/src/worker/index.ts +1 -1
- package/src/worker_threads.ts +1 -1
- package/test/lib/serialization.ts +1 -1
- package/test/observable-promise.test.ts +0 -1
- package/test/observable.test.ts +1 -1
- package/test/pool.test.ts +3 -2
- package/test/serialization.test.ts +0 -1
- package/test/spawn.chromium.mocha.ts +0 -1
- package/test/spawn.test.ts +2 -3
- package/test/streaming.test.ts +0 -1
- package/test/transferables.test.ts +1 -1
- package/test/workers/arraybuffer-xor.ts +0 -1
- package/test/workers/count-to-five.ts +0 -1
- package/test/workers/counter.ts +0 -1
- package/test/workers/faulty-function.ts +0 -1
- package/test/workers/hello-world.ts +0 -1
- package/test/workers/increment.ts +0 -1
- package/test/workers/minmax.ts +0 -1
- package/test/workers/serialization.ts +1 -1
- package/test-tooling/rollup/app.js +0 -1
- package/test-tooling/rollup/rollup.config.ts +1 -1
- package/test-tooling/rollup/worker.js +2 -2
- package/test-tooling/tsconfig/minimal.ts +1 -1
- package/test-tooling/webpack/addition-worker.ts +0 -1
- package/test-tooling/webpack/app-with-inlined-worker.ts +3 -3
- package/test-tooling/webpack/app.ts +2 -2
- package/test-tooling/webpack/pool-worker.ts +0 -1
- package/test-tooling/webpack/webpack.node.config.js +0 -1
- package/test-tooling/webpack/webpack.web.config.js +0 -1
- package/worker.d.ts +0 -1
- package/worker.js +0 -2
- package/worker.mjs +0 -1
- /package/{dist-esm → dist/esm}/serializers.js +0 -0
- /package/{dist-esm → dist/esm}/symbols.js +0 -0
- /package/{dist-esm → dist/esm}/types/worker.js +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// Source: <https://github.com/parcel-bundler/parcel/blob/master/packages/core/parcel-bundler/src/builtins/bundle-url.js>
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.
|
|
3
|
+
exports.getBaseURL = getBaseURL;
|
|
4
|
+
exports.getBundleURL = getBundleURLCached;
|
|
5
5
|
let bundleURL;
|
|
6
6
|
function getBundleURLCached() {
|
|
7
7
|
if (!bundleURL) {
|
|
@@ -9,9 +9,7 @@ function getBundleURLCached() {
|
|
|
9
9
|
}
|
|
10
10
|
return bundleURL;
|
|
11
11
|
}
|
|
12
|
-
exports.getBundleURL = getBundleURLCached;
|
|
13
12
|
function getBundleURL() {
|
|
14
|
-
// Attempt to find the URL of the current script and use that as the base URL
|
|
15
13
|
try {
|
|
16
14
|
throw new Error('getBundleURL failed');
|
|
17
15
|
}
|
|
@@ -26,4 +24,3 @@ function getBundleURL() {
|
|
|
26
24
|
function getBaseURL(url) {
|
|
27
25
|
return ('' + url).replace(/^((?:https?|file|ftp|chrome-extension|moz-extension):\/\/.+)?\/[^/]+(?:\?.*)?$/, '$1') + '/';
|
|
28
26
|
}
|
|
29
|
-
exports.getBaseURL = getBaseURL;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// tslint:disable max-classes-per-file
|
|
3
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
-
exports.
|
|
3
|
+
exports.defaultPoolSize = void 0;
|
|
4
|
+
exports.getWorkerImplementation = getWorkerImplementation;
|
|
5
|
+
exports.isWorkerRuntime = isWorkerRuntime;
|
|
5
6
|
const get_bundle_url_browser_1 = require("./get-bundle-url.browser");
|
|
6
7
|
exports.defaultPoolSize = typeof navigator !== 'undefined' && navigator.hardwareConcurrency ? navigator.hardwareConcurrency : 4;
|
|
7
8
|
const isAbsoluteURL = (value) => /^[A-Za-z][\d+.A-Za-z\-]*:/.test(value);
|
|
@@ -11,8 +12,6 @@ function createSourceBlobURL(code) {
|
|
|
11
12
|
}
|
|
12
13
|
function selectWorkerImplementation() {
|
|
13
14
|
if (typeof Worker === 'undefined') {
|
|
14
|
-
// Might happen on Safari, for instance
|
|
15
|
-
// The idea is to only fail if the constructor is actually used
|
|
16
15
|
return class NoWebWorker {
|
|
17
16
|
constructor() {
|
|
18
17
|
throw new Error("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.");
|
|
@@ -31,8 +30,7 @@ function selectWorkerImplementation() {
|
|
|
31
30
|
}
|
|
32
31
|
}
|
|
33
32
|
if (typeof url === 'string' &&
|
|
34
|
-
isAbsoluteURL(url) &&
|
|
35
|
-
// to circumvent worker CORS restrictions
|
|
33
|
+
isAbsoluteURL(url) &&
|
|
36
34
|
(options?.CORSWorkaround ?? true)) {
|
|
37
35
|
url = createSourceBlobURL(`importScripts(${JSON.stringify(url)});`);
|
|
38
36
|
}
|
|
@@ -61,9 +59,7 @@ function getWorkerImplementation() {
|
|
|
61
59
|
}
|
|
62
60
|
return implementation;
|
|
63
61
|
}
|
|
64
|
-
exports.getWorkerImplementation = getWorkerImplementation;
|
|
65
62
|
function isWorkerRuntime() {
|
|
66
63
|
const isWindowContext = typeof self !== 'undefined' && typeof Window !== 'undefined' && self instanceof Window;
|
|
67
64
|
return typeof self !== 'undefined' && self['postMessage'] && !isWindowContext ? true : false;
|
|
68
65
|
}
|
|
69
|
-
exports.isWorkerRuntime = isWorkerRuntime;
|
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import * as BrowserImplementation from './implementation.browser';
|
|
2
|
-
/** Default size of pools. Depending on the platform the value might vary from device to device. */
|
|
3
2
|
export declare const defaultPoolSize: number;
|
|
4
3
|
export declare const getWorkerImplementation: typeof BrowserImplementation.getWorkerImplementation;
|
|
5
|
-
/** Returns `true` if this code is currently running in a worker. */
|
|
6
4
|
export declare const isWorkerRuntime: typeof BrowserImplementation.isWorkerRuntime;
|
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* This file is only a stub to make './implementation' resolve to the right module.
|
|
4
|
-
*/
|
|
5
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
3
|
if (k2 === undefined) k2 = k;
|
|
7
4
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -27,15 +24,10 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
27
24
|
};
|
|
28
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
26
|
exports.isWorkerRuntime = exports.getWorkerImplementation = exports.defaultPoolSize = void 0;
|
|
30
|
-
// We alias `src/master/implementation` to `src/master/implementation.browser` for web
|
|
31
|
-
// browsers already in the package.json, so if get here, it's safe to pass-through the
|
|
32
|
-
// node implementation
|
|
33
27
|
const BrowserImplementation = __importStar(require("./implementation.browser"));
|
|
34
28
|
const NodeImplementation = __importStar(require("./implementation.node"));
|
|
35
29
|
const runningInNode = typeof process !== 'undefined' && process.arch !== 'browser' && 'pid' in process;
|
|
36
30
|
const implementation = runningInNode ? NodeImplementation : BrowserImplementation;
|
|
37
|
-
/** Default size of pools. Depending on the platform the value might vary from device to device. */
|
|
38
31
|
exports.defaultPoolSize = implementation.defaultPoolSize;
|
|
39
32
|
exports.getWorkerImplementation = implementation.getWorkerImplementation;
|
|
40
|
-
/** Returns `true` if this code is currently running in a worker. */
|
|
41
33
|
exports.isWorkerRuntime = implementation.isWorkerRuntime;
|
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/* eslint-disable unicorn/prefer-logical-operator-over-ternary */
|
|
3
|
-
/* eslint-disable unicorn/prefer-regexp-test */
|
|
4
|
-
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
5
|
-
/* eslint-disable unicorn/prefer-add-event-listener */
|
|
6
|
-
/* eslint-disable unicorn/prefer-event-target */
|
|
7
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
8
|
-
/* eslint-disable unicorn/text-encoding-identifier-case */
|
|
9
|
-
/* eslint-disable unicorn/no-process-exit */
|
|
10
|
-
/// <reference lib="dom" />
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
14
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.
|
|
6
|
+
exports.defaultPoolSize = void 0;
|
|
7
|
+
exports.getWorkerImplementation = getWorkerImplementation;
|
|
8
|
+
exports.isWorkerRuntime = isWorkerRuntime;
|
|
16
9
|
const node_events_1 = require("node:events");
|
|
17
10
|
const node_os_1 = require("node:os");
|
|
18
11
|
const node_path_1 = __importDefault(require("node:path"));
|
|
@@ -22,7 +15,6 @@ let tsNodeAvailable;
|
|
|
22
15
|
exports.defaultPoolSize = (0, node_os_1.cpus)().length;
|
|
23
16
|
function detectTsNode() {
|
|
24
17
|
if (typeof __non_webpack_require__ === 'function') {
|
|
25
|
-
// Webpack build: => No ts-node required or possible
|
|
26
18
|
return false;
|
|
27
19
|
}
|
|
28
20
|
if (tsNodeAvailable) {
|
|
@@ -37,7 +29,6 @@ function detectTsNode() {
|
|
|
37
29
|
tsNodeAvailable = false;
|
|
38
30
|
}
|
|
39
31
|
else {
|
|
40
|
-
// Re-throw
|
|
41
32
|
throw error;
|
|
42
33
|
}
|
|
43
34
|
}
|
|
@@ -65,7 +56,6 @@ function rebaseScriptPath(scriptPath, ignoreRegex) {
|
|
|
65
56
|
}
|
|
66
57
|
function resolveScriptPath(scriptPath, baseURL) {
|
|
67
58
|
const makeRelative = (filePath) => {
|
|
68
|
-
// eval() hack is also webpack-related
|
|
69
59
|
return node_path_1.default.isAbsolute(filePath) ? filePath : node_path_1.default.join(baseURL || eval('__dirname'), filePath);
|
|
70
60
|
};
|
|
71
61
|
const workerFilePath = typeof __non_webpack_require__ === 'function' ?
|
|
@@ -74,7 +64,6 @@ function resolveScriptPath(scriptPath, baseURL) {
|
|
|
74
64
|
return workerFilePath;
|
|
75
65
|
}
|
|
76
66
|
function initWorkerThreadsWorker() {
|
|
77
|
-
// Webpack hack
|
|
78
67
|
const NativeWorker = typeof __non_webpack_require__ === 'function' ? __non_webpack_require__('worker_threads').Worker : eval('require')('worker_threads').Worker;
|
|
79
68
|
let allWorkers = [];
|
|
80
69
|
class Worker extends NativeWorker {
|
|
@@ -82,7 +71,6 @@ function initWorkerThreadsWorker() {
|
|
|
82
71
|
constructor(scriptPath, options) {
|
|
83
72
|
const resolvedScriptPath = options && options.fromSource ? null : resolveScriptPath(scriptPath, (options || {})._baseURL);
|
|
84
73
|
if (!resolvedScriptPath) {
|
|
85
|
-
// `options.fromSource` is true
|
|
86
74
|
const sourceCode = scriptPath;
|
|
87
75
|
super(sourceCode, { ...options, eval: true });
|
|
88
76
|
}
|
|
@@ -90,7 +78,6 @@ function initWorkerThreadsWorker() {
|
|
|
90
78
|
super(createTsNodeModule(resolvedScriptPath), { ...options, eval: true });
|
|
91
79
|
}
|
|
92
80
|
else if (/\.asar[/\\]/.test(resolvedScriptPath)) {
|
|
93
|
-
// See <https://github.com/andywer/threads-plugin/issues/17>
|
|
94
81
|
super(resolvedScriptPath.replace(/\.asar([/\\])/, '.asar.unpacked$1'), options);
|
|
95
82
|
}
|
|
96
83
|
else {
|
|
@@ -112,11 +99,9 @@ function initWorkerThreadsWorker() {
|
|
|
112
99
|
}
|
|
113
100
|
}
|
|
114
101
|
const terminateWorkersAndMaster = () => {
|
|
115
|
-
// we should terminate all workers and then gracefully shutdown self process
|
|
116
102
|
Promise.all(allWorkers.map((worker) => worker.terminate())).then(() => process.exit(0), () => process.exit(1));
|
|
117
103
|
allWorkers = [];
|
|
118
104
|
};
|
|
119
|
-
// Take care to not leave orphaned processes behind. See #147.
|
|
120
105
|
process.on('SIGINT', () => terminateWorkersAndMaster());
|
|
121
106
|
process.on('SIGTERM', () => terminateWorkersAndMaster());
|
|
122
107
|
class BlobWorker extends Worker {
|
|
@@ -138,13 +123,10 @@ function initTinyWorker() {
|
|
|
138
123
|
class Worker extends TinyWorker {
|
|
139
124
|
emitter;
|
|
140
125
|
constructor(scriptPath, options) {
|
|
141
|
-
// Need to apply a work-around for Windows or it will choke upon the absolute path
|
|
142
|
-
// (`Error [ERR_INVALID_PROTOCOL]: Protocol 'c:' not supported`)
|
|
143
126
|
const resolvedScriptPath = options && options.fromSource ? null
|
|
144
127
|
: process.platform === 'win32' ? `file:///${resolveScriptPath(scriptPath).replaceAll('\\', '/')}`
|
|
145
128
|
: resolveScriptPath(scriptPath);
|
|
146
129
|
if (!resolvedScriptPath) {
|
|
147
|
-
// `options.fromSource` is true
|
|
148
130
|
const sourceCode = scriptPath;
|
|
149
131
|
super(new Function(sourceCode), [], { esm: true });
|
|
150
132
|
}
|
|
@@ -152,7 +134,6 @@ function initTinyWorker() {
|
|
|
152
134
|
super(new Function(createTsNodeModule(resolveScriptPath(scriptPath))), [], { esm: true });
|
|
153
135
|
}
|
|
154
136
|
else if (/\.asar[/\\]/.test(resolvedScriptPath)) {
|
|
155
|
-
// See <https://github.com/andywer/threads-plugin/issues/17>
|
|
156
137
|
super(resolvedScriptPath.replace(/\.asar([/\\])/, '.asar.unpacked$1'), [], { esm: true });
|
|
157
138
|
}
|
|
158
139
|
else {
|
|
@@ -175,12 +156,9 @@ function initTinyWorker() {
|
|
|
175
156
|
}
|
|
176
157
|
}
|
|
177
158
|
const terminateWorkersAndMaster = () => {
|
|
178
|
-
// we should terminate all workers and then gracefully shutdown self process
|
|
179
159
|
Promise.all(allWorkers.map((worker) => worker.terminate())).then(() => process.exit(0), () => process.exit(1));
|
|
180
160
|
allWorkers = [];
|
|
181
161
|
};
|
|
182
|
-
// Take care to not leave orphaned processes behind
|
|
183
|
-
// See <https://github.com/avoidwork/tiny-worker#faq>
|
|
184
162
|
process.on('SIGINT', () => terminateWorkersAndMaster());
|
|
185
163
|
process.on('SIGTERM', () => terminateWorkersAndMaster());
|
|
186
164
|
class BlobWorker extends Worker {
|
|
@@ -204,7 +182,6 @@ function selectWorkerImplementation() {
|
|
|
204
182
|
return initWorkerThreadsWorker();
|
|
205
183
|
}
|
|
206
184
|
catch {
|
|
207
|
-
// tslint:disable-next-line no-console
|
|
208
185
|
console.debug('Node worker_threads not available. Trying to fall back to tiny-worker polyfill...');
|
|
209
186
|
isTinyWorker = true;
|
|
210
187
|
return initTinyWorker();
|
|
@@ -216,17 +193,14 @@ function getWorkerImplementation() {
|
|
|
216
193
|
}
|
|
217
194
|
return implementation;
|
|
218
195
|
}
|
|
219
|
-
exports.getWorkerImplementation = getWorkerImplementation;
|
|
220
196
|
function isWorkerRuntime() {
|
|
221
197
|
if (isTinyWorker) {
|
|
222
198
|
return self !== undefined && self['postMessage'] ? true : false;
|
|
223
199
|
}
|
|
224
200
|
else {
|
|
225
|
-
// Webpack hack
|
|
226
201
|
const isMainThread = typeof __non_webpack_require__ === 'function' ?
|
|
227
202
|
__non_webpack_require__('worker_threads').isMainThread
|
|
228
203
|
: eval('require')('worker_threads').isMainThread;
|
|
229
204
|
return !isMainThread;
|
|
230
205
|
}
|
|
231
206
|
}
|
|
232
|
-
exports.isWorkerRuntime = isWorkerRuntime;
|
package/dist/master/index.d.ts
CHANGED
|
@@ -6,8 +6,6 @@ export { spawn } from './spawn';
|
|
|
6
6
|
export { Thread } from './thread';
|
|
7
7
|
export type BlobWorker = typeof BlobWorkerClass;
|
|
8
8
|
export type Worker = WorkerType;
|
|
9
|
-
/** Separate class to spawn workers from source code blobs or strings. */
|
|
10
9
|
export declare const BlobWorker: typeof BlobWorkerClass;
|
|
11
|
-
/** Worker implementation. Either web worker or a node.js Worker class. */
|
|
12
10
|
export declare const Worker: typeof import("../types/master").WorkerImplementation;
|
|
13
11
|
export { isWorkerRuntime } from './implementation';
|
package/dist/master/index.js
CHANGED
|
@@ -8,9 +8,7 @@ var spawn_1 = require("./spawn");
|
|
|
8
8
|
Object.defineProperty(exports, "spawn", { enumerable: true, get: function () { return spawn_1.spawn; } });
|
|
9
9
|
var thread_1 = require("./thread");
|
|
10
10
|
Object.defineProperty(exports, "Thread", { enumerable: true, get: function () { return thread_1.Thread; } });
|
|
11
|
-
/** Separate class to spawn workers from source code blobs or strings. */
|
|
12
11
|
exports.BlobWorker = (0, implementation_1.getWorkerImplementation)().blob;
|
|
13
|
-
/** Worker implementation. Either web worker or a node.js Worker class. */
|
|
14
12
|
exports.Worker = (0, implementation_1.getWorkerImplementation)().default;
|
|
15
13
|
var implementation_2 = require("./implementation");
|
|
16
14
|
Object.defineProperty(exports, "isWorkerRuntime", { enumerable: true, get: function () { return implementation_2.isWorkerRuntime; } });
|
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
3
|
-
/*
|
|
4
|
-
* This source file contains the code for proxying calls in the master thread to calls in the workers
|
|
5
|
-
* by `.postMessage()`-ing.
|
|
6
|
-
*
|
|
7
|
-
* Keep in mind that this code can make or break the program's performance! Need to optimize more…
|
|
8
|
-
*/
|
|
9
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
10
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
11
4
|
};
|
|
12
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.
|
|
6
|
+
exports.createProxyFunction = createProxyFunction;
|
|
7
|
+
exports.createProxyModule = createProxyModule;
|
|
14
8
|
const debug_1 = __importDefault(require("debug"));
|
|
15
9
|
const observable_fns_1 = require("observable-fns");
|
|
16
10
|
const common_1 = require("../common");
|
|
@@ -77,7 +71,6 @@ function createObservableForJob(worker, jobUID) {
|
|
|
77
71
|
}
|
|
78
72
|
function prepareArguments(rawArgs) {
|
|
79
73
|
if (rawArgs.length === 0) {
|
|
80
|
-
// Exit early if possible
|
|
81
74
|
return {
|
|
82
75
|
args: [],
|
|
83
76
|
transferables: [],
|
|
@@ -119,7 +112,6 @@ function createProxyFunction(worker, method) {
|
|
|
119
112
|
return observable_promise_1.ObservablePromise.from((0, observable_fns_1.multicast)(createObservableForJob(worker, uid)));
|
|
120
113
|
});
|
|
121
114
|
}
|
|
122
|
-
exports.createProxyFunction = createProxyFunction;
|
|
123
115
|
function createProxyModule(worker, methodNames) {
|
|
124
116
|
const proxy = {};
|
|
125
117
|
for (const methodName of methodNames) {
|
|
@@ -127,4 +119,3 @@ function createProxyModule(worker, methodNames) {
|
|
|
127
119
|
}
|
|
128
120
|
return proxy;
|
|
129
121
|
}
|
|
130
|
-
exports.createProxyModule = createProxyModule;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Thread } from './thread';
|
|
2
|
-
/** Pool event type. Specifies the type of each `PoolEvent`. */
|
|
3
2
|
export declare enum PoolEventType {
|
|
4
3
|
initialized = "initialized",
|
|
5
4
|
taskCanceled = "taskCanceled",
|
|
@@ -11,7 +10,6 @@ export declare enum PoolEventType {
|
|
|
11
10
|
terminated = "terminated"
|
|
12
11
|
}
|
|
13
12
|
export type TaskRunFunction<ThreadType extends Thread, Return> = (worker: ThreadType) => Promise<Return>;
|
|
14
|
-
/** Pool event. Subscribe to those events using `pool.events()`. Useful for debugging. */
|
|
15
13
|
export type PoolEvent<ThreadType extends Thread> = {
|
|
16
14
|
type: PoolEventType.initialized;
|
|
17
15
|
size: number;
|
|
@@ -45,21 +43,9 @@ export interface WorkerDescriptor<ThreadType extends Thread> {
|
|
|
45
43
|
init: Promise<ThreadType>;
|
|
46
44
|
runningTasks: Array<Promise<any>>;
|
|
47
45
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Task that has been `pool.queued()`-ed.
|
|
50
|
-
*/
|
|
51
46
|
export interface QueuedTask<ThreadType extends Thread, Return> {
|
|
52
|
-
/** @private */
|
|
53
47
|
id: number;
|
|
54
|
-
/** @private */
|
|
55
48
|
run: TaskRunFunction<ThreadType, Return>;
|
|
56
|
-
/**
|
|
57
|
-
* Queued tasks can be cancelled until the pool starts running them on a worker thread.
|
|
58
|
-
*/
|
|
59
49
|
cancel(): void;
|
|
60
|
-
/**
|
|
61
|
-
* `QueuedTask` is thenable, so you can `await` it.
|
|
62
|
-
* Resolves when the task has successfully been executed. Rejects if the task fails.
|
|
63
|
-
*/
|
|
64
50
|
then: Promise<Return>['then'];
|
|
65
51
|
}
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.PoolEventType = void 0;
|
|
4
|
-
/** Pool event type. Specifies the type of each `PoolEvent`. */
|
|
5
4
|
var PoolEventType;
|
|
6
5
|
(function (PoolEventType) {
|
|
7
6
|
PoolEventType["initialized"] = "initialized";
|
package/dist/master/pool.d.ts
CHANGED
|
@@ -5,52 +5,17 @@ export declare namespace Pool {
|
|
|
5
5
|
type Event<ThreadType extends Thread = any> = PoolEvent<ThreadType>;
|
|
6
6
|
type EventType = PoolEventType;
|
|
7
7
|
}
|
|
8
|
-
/**
|
|
9
|
-
* Thread pool managing a set of worker threads.
|
|
10
|
-
* Use it to queue tasks that are run on those threads with limited
|
|
11
|
-
* concurrency.
|
|
12
|
-
*/
|
|
13
8
|
export interface Pool<ThreadType extends Thread> {
|
|
14
|
-
/**
|
|
15
|
-
* Returns a promise that resolves once the task queue is emptied.
|
|
16
|
-
* Promise will be rejected if any task fails.
|
|
17
|
-
*
|
|
18
|
-
* @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.
|
|
19
|
-
*/
|
|
20
9
|
completed(allowResolvingImmediately?: boolean): Promise<any>;
|
|
21
|
-
/**
|
|
22
|
-
* Returns a promise that resolves once the task queue is emptied.
|
|
23
|
-
* Failing tasks will not cause the promise to be rejected.
|
|
24
|
-
*
|
|
25
|
-
* @param allowResolvingImmediately Set to `true` to resolve immediately if task queue is currently empty.
|
|
26
|
-
*/
|
|
27
10
|
settled(allowResolvingImmediately?: boolean): Promise<Error[]>;
|
|
28
|
-
/**
|
|
29
|
-
* Returns an observable that yields pool events.
|
|
30
|
-
*/
|
|
31
11
|
events(): Observable<PoolEvent<ThreadType>>;
|
|
32
|
-
/**
|
|
33
|
-
* Queue a task and return a promise that resolves once the task has been dequeued,
|
|
34
|
-
* started and finished.
|
|
35
|
-
*
|
|
36
|
-
* @param task An async function that takes a thread instance and invokes it.
|
|
37
|
-
*/
|
|
38
12
|
queue<Return>(task: TaskRunFunction<ThreadType, Return>): QueuedTask<ThreadType, Return>;
|
|
39
|
-
/**
|
|
40
|
-
* Terminate all pool threads.
|
|
41
|
-
*
|
|
42
|
-
* @param force Set to `true` to kill the thread even if it cannot be stopped gracefully.
|
|
43
|
-
*/
|
|
44
13
|
terminate(force?: boolean): Promise<void>;
|
|
45
14
|
}
|
|
46
15
|
export interface PoolOptions {
|
|
47
|
-
/** Maximum no. of tasks to run on one worker thread at a time. Defaults to one. */
|
|
48
16
|
concurrency?: number;
|
|
49
|
-
/** Maximum no. of jobs to be queued for execution before throwing an error. */
|
|
50
17
|
maxQueuedJobs?: number;
|
|
51
|
-
/** Gives that pool a name to be used for debug logging, letting you distinguish between log output of different pools. */
|
|
52
18
|
name?: string;
|
|
53
|
-
/** No. of worker threads to spawn and to be managed by the pool. */
|
|
54
19
|
size?: number;
|
|
55
20
|
}
|
|
56
21
|
declare class WorkerPool<ThreadType extends Thread> implements Pool<ThreadType> {
|
|
@@ -76,13 +41,7 @@ declare class WorkerPool<ThreadType extends Thread> implements Pool<ThreadType>
|
|
|
76
41
|
queue(taskFunction: TaskRunFunction<ThreadType, any>): QueuedTask<ThreadType, any>;
|
|
77
42
|
terminate(force?: boolean): Promise<void>;
|
|
78
43
|
}
|
|
79
|
-
/**
|
|
80
|
-
* Thread pool constructor. Creates a new pool and spawns its worker threads.
|
|
81
|
-
*/
|
|
82
44
|
declare function PoolConstructor<ThreadType extends Thread>(spawnWorker: () => Promise<ThreadType>, optionsOrSize?: number | PoolOptions): WorkerPool<ThreadType>;
|
|
83
|
-
/**
|
|
84
|
-
* Thread pool constructor. Creates a new pool and spawns its worker threads.
|
|
85
|
-
*/
|
|
86
45
|
export declare const Pool: typeof PoolConstructor & {
|
|
87
46
|
EventType: typeof PoolEventType;
|
|
88
47
|
};
|
package/dist/master/pool.js
CHANGED
|
@@ -4,14 +4,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.Thread = exports.PoolEventType = exports.Pool = void 0;
|
|
7
|
-
/* eslint-disable unicorn/no-thenable */
|
|
8
|
-
/* eslint-disable @typescript-eslint/no-floating-promises */
|
|
9
|
-
/* eslint-disable require-await */
|
|
10
|
-
/* eslint-disable @typescript-eslint/member-ordering */
|
|
11
|
-
/* eslint-disable unicorn/no-array-reduce */
|
|
12
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
13
|
-
/* eslint-disable import/export */
|
|
14
|
-
/* eslint-disable @typescript-eslint/no-namespace */
|
|
15
7
|
const debug_1 = __importDefault(require("debug"));
|
|
16
8
|
const observable_fns_1 = require("observable-fns");
|
|
17
9
|
const ponyfills_1 = require("../ponyfills");
|
|
@@ -105,7 +97,6 @@ class WorkerPool {
|
|
|
105
97
|
const removeTaskFromWorkersRunningTasks = () => {
|
|
106
98
|
worker.runningTasks = worker.runningTasks.filter((someRunPromise) => someRunPromise !== runPromise);
|
|
107
99
|
};
|
|
108
|
-
// Defer task execution by one tick to give handlers time to subscribe
|
|
109
100
|
await delay(0);
|
|
110
101
|
try {
|
|
111
102
|
await this.runPoolTask(worker, task);
|
|
@@ -173,7 +164,7 @@ class WorkerPool {
|
|
|
173
164
|
subscription.unsubscribe();
|
|
174
165
|
resolve(void 0);
|
|
175
166
|
}
|
|
176
|
-
},
|
|
167
|
+
},
|
|
177
168
|
});
|
|
178
169
|
});
|
|
179
170
|
await (0, ponyfills_1.allSettled)(getCurrentlyRunningTasks());
|
|
@@ -194,7 +185,7 @@ class WorkerPool {
|
|
|
194
185
|
subscription.unsubscribe();
|
|
195
186
|
reject(event.error);
|
|
196
187
|
}
|
|
197
|
-
},
|
|
188
|
+
},
|
|
198
189
|
});
|
|
199
190
|
});
|
|
200
191
|
const errors = await Promise.race([settlementPromise, earlyExitPromise]);
|
|
@@ -216,8 +207,6 @@ class WorkerPool {
|
|
|
216
207
|
const taskID = this.nextTaskID++;
|
|
217
208
|
const taskCompletion = this.taskCompletion(taskID);
|
|
218
209
|
taskCompletion.catch((error) => {
|
|
219
|
-
// Prevent unhandled rejections here as we assume the user will use
|
|
220
|
-
// `pool.completed()`, `pool.settled()` or `task.catch()` to handle errors
|
|
221
210
|
this.debug(`Task #${taskID} errored:`, error);
|
|
222
211
|
});
|
|
223
212
|
const task = {
|
|
@@ -261,19 +250,11 @@ class WorkerPool {
|
|
|
261
250
|
await Promise.all(this.workers.map(async (worker) => thread_1.Thread.terminate(await worker.init)));
|
|
262
251
|
}
|
|
263
252
|
}
|
|
264
|
-
/**
|
|
265
|
-
* Thread pool constructor. Creates a new pool and spawns its worker threads.
|
|
266
|
-
*/
|
|
267
253
|
function PoolConstructor(spawnWorker, optionsOrSize) {
|
|
268
|
-
// The function exists only so we don't need to use `new` to create a pool (we still can, though).
|
|
269
|
-
// If the Pool is a class or not is an implementation detail that should not concern the user.
|
|
270
254
|
return new WorkerPool(spawnWorker, optionsOrSize);
|
|
271
255
|
}
|
|
272
256
|
;
|
|
273
257
|
PoolConstructor.EventType = pool_types_1.PoolEventType;
|
|
274
|
-
/**
|
|
275
|
-
* Thread pool constructor. Creates a new pool and spawns its worker threads.
|
|
276
|
-
*/
|
|
277
258
|
exports.Pool = PoolConstructor;
|
|
278
259
|
var pool_types_2 = require("./pool-types");
|
|
279
260
|
Object.defineProperty(exports, "PoolEventType", { enumerable: true, get: function () { return pool_types_2.PoolEventType; } });
|
package/dist/master/register.js
CHANGED
package/dist/master/spawn.d.ts
CHANGED
|
@@ -5,15 +5,6 @@ type ArbitraryWorkerInterface = WorkerFunction & WorkerModule<string> & {
|
|
|
5
5
|
};
|
|
6
6
|
type ArbitraryThreadType = FunctionThread<any, any> & ModuleThread<any>;
|
|
7
7
|
export type ExposedToThreadType<Exposed extends WorkerFunction | WorkerModule<any>> = Exposed extends ArbitraryWorkerInterface ? ArbitraryThreadType : Exposed extends WorkerFunction ? FunctionThread<Parameters<Exposed>, StripAsync<ReturnType<Exposed>>> : Exposed extends WorkerModule<any> ? ModuleThread<Exposed> : never;
|
|
8
|
-
/**
|
|
9
|
-
* Spawn a new thread. Takes a fresh worker instance, wraps it in a thin
|
|
10
|
-
* abstraction layer to provide the transparent API and verifies that
|
|
11
|
-
* the worker has initialized successfully.
|
|
12
|
-
*
|
|
13
|
-
* @param worker Instance of `Worker`. Either a web worker, `worker_threads` worker or `tiny-worker` worker.
|
|
14
|
-
* @param [options]
|
|
15
|
-
* @param [options.timeout] Init message timeout. Default: 10000 or set by environment variable.
|
|
16
|
-
*/
|
|
17
8
|
export declare function spawn<Exposed extends WorkerFunction | WorkerModule<any> = ArbitraryWorkerInterface>(worker: WorkerType, options?: {
|
|
18
9
|
timeout?: number;
|
|
19
10
|
}): Promise<ExposedToThreadType<Exposed>>;
|
package/dist/master/spawn.js
CHANGED
|
@@ -3,9 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.spawn =
|
|
7
|
-
/* eslint-disable @typescript-eslint/no-floating-promises */
|
|
8
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
6
|
+
exports.spawn = spawn;
|
|
9
7
|
const debug_1 = __importDefault(require("debug"));
|
|
10
8
|
const observable_fns_1 = require("observable-fns");
|
|
11
9
|
const common_1 = require("../common");
|
|
@@ -80,7 +78,6 @@ function createTerminator(worker) {
|
|
|
80
78
|
const [termination, resolver] = (0, promise_1.createPromiseWithResolver)();
|
|
81
79
|
const terminate = async () => {
|
|
82
80
|
debugThreadUtils('Terminating worker');
|
|
83
|
-
// Newer versions of worker_threads workers return a promise
|
|
84
81
|
await worker.terminate();
|
|
85
82
|
resolver();
|
|
86
83
|
};
|
|
@@ -90,7 +87,6 @@ function setPrivateThreadProps(raw, worker, workerEvents, terminate) {
|
|
|
90
87
|
const workerErrors = workerEvents
|
|
91
88
|
.filter((event) => event.type === master_1.WorkerEventType.internalError)
|
|
92
89
|
.map((errorEvent) => errorEvent.error);
|
|
93
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
90
|
return Object.assign(raw, {
|
|
95
91
|
[symbols_1.$errors]: workerErrors,
|
|
96
92
|
[symbols_1.$events]: workerEvents,
|
|
@@ -98,15 +94,6 @@ function setPrivateThreadProps(raw, worker, workerEvents, terminate) {
|
|
|
98
94
|
[symbols_1.$worker]: worker,
|
|
99
95
|
});
|
|
100
96
|
}
|
|
101
|
-
/**
|
|
102
|
-
* Spawn a new thread. Takes a fresh worker instance, wraps it in a thin
|
|
103
|
-
* abstraction layer to provide the transparent API and verifies that
|
|
104
|
-
* the worker has initialized successfully.
|
|
105
|
-
*
|
|
106
|
-
* @param worker Instance of `Worker`. Either a web worker, `worker_threads` worker or `tiny-worker` worker.
|
|
107
|
-
* @param [options]
|
|
108
|
-
* @param [options.timeout] Init message timeout. Default: 10000 or set by environment variable.
|
|
109
|
-
*/
|
|
110
97
|
async function spawn(worker, options) {
|
|
111
98
|
debugSpawn('Initializing new thread');
|
|
112
99
|
const timeout = options && options.timeout ? options.timeout : initMessageTimeout;
|
|
@@ -127,4 +114,3 @@ async function spawn(worker, options) {
|
|
|
127
114
|
throw new Error(`Worker init message states unexpected type of expose(): ${type}`);
|
|
128
115
|
}
|
|
129
116
|
}
|
|
130
|
-
exports.spawn = spawn;
|
package/dist/master/thread.d.ts
CHANGED
|
@@ -1,12 +1,8 @@
|
|
|
1
1
|
import { Observable } from 'observable-fns';
|
|
2
2
|
import { Thread as ThreadType, WorkerEvent } from '../types/master';
|
|
3
3
|
export type Thread = ThreadType;
|
|
4
|
-
/** Thread utility functions. Use them to manage or inspect a `spawn()`-ed thread. */
|
|
5
4
|
export declare const Thread: {
|
|
6
|
-
/** Return an observable that can be used to subscribe to all errors happening in the thread. */
|
|
7
5
|
errors<ThreadT extends ThreadType>(thread: ThreadT): Observable<Error>;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
/** Terminate a thread. Remember to terminate every thread when you are done using it. */
|
|
11
|
-
terminate<ThreadT_2 extends ThreadType>(thread: ThreadT_2): Promise<void>;
|
|
6
|
+
events<ThreadT extends ThreadType>(thread: ThreadT): Observable<WorkerEvent>;
|
|
7
|
+
terminate<ThreadT extends ThreadType>(thread: ThreadT): Promise<void>;
|
|
12
8
|
};
|
package/dist/master/thread.js
CHANGED
|
@@ -5,17 +5,13 @@ const symbols_1 = require("../symbols");
|
|
|
5
5
|
function fail(message) {
|
|
6
6
|
throw new Error(message);
|
|
7
7
|
}
|
|
8
|
-
/** Thread utility functions. Use them to manage or inspect a `spawn()`-ed thread. */
|
|
9
8
|
exports.Thread = {
|
|
10
|
-
/** Return an observable that can be used to subscribe to all errors happening in the thread. */
|
|
11
9
|
errors(thread) {
|
|
12
10
|
return thread[symbols_1.$errors] || fail('Error observable not found. Make sure to pass a thread instance as returned by the spawn() promise.');
|
|
13
11
|
},
|
|
14
|
-
/** Return an observable that can be used to subscribe to internal events happening in the thread. Useful for debugging. */
|
|
15
12
|
events(thread) {
|
|
16
13
|
return thread[symbols_1.$events] || fail('Events observable not found. Make sure to pass a thread instance as returned by the spawn() promise.');
|
|
17
14
|
},
|
|
18
|
-
/** Terminate a thread. Remember to terminate every thread when you are done using it. */
|
|
19
15
|
terminate(thread) {
|
|
20
16
|
return thread[symbols_1.$terminate]();
|
|
21
17
|
},
|
|
@@ -4,19 +4,6 @@ type Thenable<T> = {
|
|
|
4
4
|
then: (onFulfilled?: (value: T) => any, onRejected?: (error: any) => any) => any;
|
|
5
5
|
};
|
|
6
6
|
type UnsubscribeFn = () => void;
|
|
7
|
-
/**
|
|
8
|
-
* Creates a hybrid, combining the APIs of an Observable and a Promise.
|
|
9
|
-
*
|
|
10
|
-
* It is used to proxy async process states when we are initially not sure
|
|
11
|
-
* if that async process will yield values once (-> Promise) or multiple
|
|
12
|
-
* times (-> Observable).
|
|
13
|
-
*
|
|
14
|
-
* Note that the observable promise inherits some of the observable's characteristics:
|
|
15
|
-
* The `init` function will be called *once for every time anyone subscribes to it*.
|
|
16
|
-
*
|
|
17
|
-
* If this is undesired, derive a hot observable from it using `makeHot()` and
|
|
18
|
-
* subscribe to that.
|
|
19
|
-
*/
|
|
20
7
|
export declare class ObservablePromise<T> extends Observable<T> implements Promise<T> {
|
|
21
8
|
readonly [Symbol.toStringTag] = "[object ObservablePromise]";
|
|
22
9
|
private initHasRun;
|