@atlaspack/workers 2.14.5-canary.48 → 2.14.5-canary.481
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/CHANGELOG.md +567 -0
- package/dist/Handle.js +33 -0
- package/dist/Worker.js +180 -0
- package/dist/WorkerFarm.js +539 -0
- package/dist/backend.js +34 -0
- package/dist/bus.js +24 -0
- package/dist/child.js +290 -0
- package/dist/childState.js +11 -0
- package/dist/cpuCount.js +72 -0
- package/dist/index.js +44 -0
- package/dist/process/ProcessChild.js +48 -0
- package/dist/process/ProcessWorker.js +68 -0
- package/dist/threads/ThreadsChild.js +31 -0
- package/dist/threads/ThreadsWorker.js +47 -0
- package/dist/types.js +2 -0
- package/dist/web/WebChild.js +34 -0
- package/dist/web/WebWorker.js +70 -0
- package/index.d.ts +96 -3
- package/lib/Handle.js +1 -4
- package/lib/Worker.js +8 -2
- package/lib/WorkerFarm.js +14 -10
- package/lib/backend.js +5 -1
- package/lib/bus.js +2 -2
- package/lib/child.js +14 -7
- package/lib/cpuCount.js +7 -3
- package/lib/index.js +2 -3
- package/lib/process/ProcessChild.js +7 -2
- package/lib/process/ProcessWorker.js +10 -3
- package/lib/threads/ThreadsChild.js +4 -1
- package/lib/threads/ThreadsWorker.js +11 -3
- package/lib/types/Handle.d.ts +19 -0
- package/lib/types/Worker.d.ts +40 -0
- package/lib/types/WorkerFarm.d.ts +93 -0
- package/lib/types/backend.d.ts +4 -0
- package/lib/types/bus.d.ts +6 -0
- package/lib/types/child.d.ts +43 -0
- package/lib/types/childState.d.ts +3 -0
- package/lib/types/cpuCount.d.ts +2 -0
- package/lib/types/index.d.ts +6 -0
- package/lib/types/process/ProcessChild.d.ts +9 -0
- package/lib/types/process/ProcessWorker.d.ts +16 -0
- package/lib/types/threads/ThreadsChild.d.ts +8 -0
- package/lib/types/threads/ThreadsWorker.d.ts +15 -0
- package/lib/types/types.d.ts +52 -0
- package/lib/types/web/WebChild.d.ts +8 -0
- package/lib/types/web/WebWorker.d.ts +15 -0
- package/lib/web/WebChild.js +6 -1
- package/lib/web/WebWorker.js +19 -4
- package/package.json +15 -19
- package/src/{Handle.js → Handle.ts} +11 -11
- package/src/{Worker.js → Worker.ts} +66 -54
- package/src/{WorkerFarm.js → WorkerFarm.ts} +195 -142
- package/src/{backend.js → backend.ts} +6 -3
- package/src/{bus.js → bus.ts} +2 -3
- package/src/{child.js → child.ts} +55 -43
- package/src/{childState.js → childState.ts} +1 -2
- package/src/{cpuCount.js → cpuCount.ts} +10 -7
- package/src/{index.js → index.ts} +0 -1
- package/src/process/{ProcessChild.js → ProcessChild.ts} +5 -3
- package/src/process/{ProcessWorker.js → ProcessWorker.ts} +10 -7
- package/src/threads/{ThreadsChild.js → ThreadsChild.ts} +2 -2
- package/src/threads/{ThreadsWorker.js → ThreadsWorker.ts} +14 -8
- package/src/{types.js → types.ts} +34 -35
- package/src/web/{WebChild.js → WebChild.ts} +6 -2
- package/src/web/{WebWorker.js → WebWorker.ts} +19 -7
- package/test/{cpuCount.test.js → cpuCount.test.ts} +0 -1
- package/test/{workerfarm.test.cjs → workerfarm.test.js} +9 -2
- package/tsconfig.json +27 -0
- package/tsconfig.tsbuildinfo +1 -0
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WORKER_PATH = void 0;
|
|
4
|
+
const build_cache_1 = require("@atlaspack/build-cache");
|
|
5
|
+
const utils_1 = require("@atlaspack/utils");
|
|
6
|
+
let id = 0;
|
|
7
|
+
// @ts-expect-error This is actually a module
|
|
8
|
+
exports.WORKER_PATH = new URL('./WebChild.js', import.meta.url);
|
|
9
|
+
if (process.env.ATLASPACK_REGISTER_USE_SRC === 'true') {
|
|
10
|
+
// @ts-expect-error This is actually a module
|
|
11
|
+
exports.WORKER_PATH = new URL('./WebChild.ts', import.meta.url);
|
|
12
|
+
}
|
|
13
|
+
// @ts-expect-error TS2420
|
|
14
|
+
class WebWorker {
|
|
15
|
+
constructor(execArgv, onMessage, onError, onExit) {
|
|
16
|
+
this.execArgv = execArgv;
|
|
17
|
+
this.onMessage = onMessage;
|
|
18
|
+
this.onError = onError;
|
|
19
|
+
this.onExit = onExit;
|
|
20
|
+
}
|
|
21
|
+
start() {
|
|
22
|
+
// @ts-expect-error TS1470
|
|
23
|
+
this.worker = new Worker(new URL('./WebChild.js', import.meta.url), {
|
|
24
|
+
name: `Parcel Worker ${id++}`,
|
|
25
|
+
type: 'module',
|
|
26
|
+
});
|
|
27
|
+
let { deferred, promise } = (0, utils_1.makeDeferredWithPromise)();
|
|
28
|
+
this.worker.onmessage = ({ data }) => {
|
|
29
|
+
if (data === 'online') {
|
|
30
|
+
// @ts-expect-error TS2554
|
|
31
|
+
deferred.resolve();
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
this.handleMessage(data);
|
|
35
|
+
};
|
|
36
|
+
// @ts-expect-error TS2322
|
|
37
|
+
this.worker.onerror = this.onError;
|
|
38
|
+
// Web workers can't crash or intentionally stop on their own, apart from stop() below
|
|
39
|
+
// this.worker.on('exit', this.onExit);
|
|
40
|
+
// @ts-expect-error TS2322
|
|
41
|
+
return promise;
|
|
42
|
+
}
|
|
43
|
+
stop() {
|
|
44
|
+
if (!this.stopping) {
|
|
45
|
+
// @ts-expect-error TS2322
|
|
46
|
+
this.stopping = (async () => {
|
|
47
|
+
this.worker.postMessage('stop');
|
|
48
|
+
let { deferred, promise } = (0, utils_1.makeDeferredWithPromise)();
|
|
49
|
+
this.worker.addEventListener('message', ({ data }) => {
|
|
50
|
+
if (data === 'stopped') {
|
|
51
|
+
// @ts-expect-error TS2554
|
|
52
|
+
deferred.resolve();
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
await promise;
|
|
56
|
+
this.worker.terminate();
|
|
57
|
+
this.onExit(0);
|
|
58
|
+
})();
|
|
59
|
+
}
|
|
60
|
+
// @ts-expect-error TS2322
|
|
61
|
+
return this.stopping;
|
|
62
|
+
}
|
|
63
|
+
handleMessage(data) {
|
|
64
|
+
this.onMessage((0, build_cache_1.restoreDeserializedObject)(data));
|
|
65
|
+
}
|
|
66
|
+
send(data) {
|
|
67
|
+
this.worker.postMessage((0, build_cache_1.prepareForSerialization)(data));
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
exports.default = WebWorker;
|
package/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
2
|
+
import type {FilePath} from '@atlaspack/types';
|
|
3
|
+
import type EventEmitter from 'events';
|
|
2
4
|
|
|
3
5
|
type BackendType = 'process' | 'threads';
|
|
4
6
|
|
|
@@ -14,10 +16,101 @@ export type FarmOptions = {
|
|
|
14
16
|
shouldTrace?: boolean;
|
|
15
17
|
};
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
export class Bus extends EventEmitter {
|
|
20
|
+
emit(event: string, ...args: Array<any>): boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const bus: Bus;
|
|
19
24
|
|
|
25
|
+
export declare class WorkerFarm {
|
|
26
|
+
ending: boolean;
|
|
27
|
+
workerApi: {
|
|
28
|
+
callChild: (
|
|
29
|
+
childId: number,
|
|
30
|
+
request: HandleCallRequest,
|
|
31
|
+
) => Promise<unknown>;
|
|
32
|
+
callMaster: (
|
|
33
|
+
request: CallRequest,
|
|
34
|
+
awaitResponse?: boolean | null | undefined,
|
|
35
|
+
) => Promise<unknown>;
|
|
36
|
+
createReverseHandle: (fn: HandleFunction) => Handle;
|
|
37
|
+
getSharedReference: (ref: SharedReference) => unknown;
|
|
38
|
+
resolveSharedReference: (value: unknown) => undefined | SharedReference;
|
|
39
|
+
runHandle: (handle: Handle, args: Array<any>) => Promise<unknown>;
|
|
40
|
+
};
|
|
41
|
+
constructor(options: Partial<FarmOptions>);
|
|
42
|
+
createSharedReference(
|
|
43
|
+
value: unknown,
|
|
44
|
+
isCacheable?: boolean,
|
|
45
|
+
): {
|
|
46
|
+
ref: SharedReference;
|
|
47
|
+
dispose(): Promise<unknown>;
|
|
48
|
+
};
|
|
49
|
+
startProfile(): Promise<void>;
|
|
50
|
+
endProfile(): Promise<void>;
|
|
51
|
+
takeHeapSnapshot(): Promise<void>;
|
|
52
|
+
createHandle(method: string, useMainThread?: boolean): HandleFunction;
|
|
53
|
+
createReverseHandle(fn: HandleFunction): Handle;
|
|
54
|
+
callAllWorkers(method: string, args: Array<any>): Promise<void>;
|
|
55
|
+
static getWorkerApi(): {
|
|
56
|
+
callMaster: (
|
|
57
|
+
request: CallRequest,
|
|
58
|
+
awaitResponse?: boolean | null | undefined,
|
|
59
|
+
) => Promise<unknown>;
|
|
60
|
+
createReverseHandle: (fn: (...args: Array<any>) => unknown) => Handle;
|
|
61
|
+
getSharedReference: (ref: SharedReference) => unknown;
|
|
62
|
+
resolveSharedReference: (value: unknown) => undefined | SharedReference;
|
|
63
|
+
runHandle: (handle: Handle, args: Array<any>) => Promise<unknown>;
|
|
64
|
+
};
|
|
20
65
|
end(): Promise<void>;
|
|
66
|
+
static isWorker(): boolean;
|
|
21
67
|
}
|
|
22
68
|
|
|
23
69
|
export default WorkerFarm;
|
|
70
|
+
|
|
71
|
+
export type SharedReference = number;
|
|
72
|
+
|
|
73
|
+
export type WorkerApi = {
|
|
74
|
+
callMaster(
|
|
75
|
+
arg1: CallRequest,
|
|
76
|
+
arg2?: boolean | null | undefined,
|
|
77
|
+
): Promise<unknown>;
|
|
78
|
+
createReverseHandle(fn: HandleFunction): Handle;
|
|
79
|
+
getSharedReference(ref: SharedReference): unknown;
|
|
80
|
+
resolveSharedReference(value: unknown): SharedReference | null | undefined;
|
|
81
|
+
callChild?: (childId: number, request: HandleCallRequest) => Promise<unknown>;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export type HandleFunction = (...args: Array<any>) => any;
|
|
85
|
+
|
|
86
|
+
export type LocationCallRequest = {
|
|
87
|
+
args: ReadonlyArray<unknown>;
|
|
88
|
+
location: string;
|
|
89
|
+
method?: string;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export type HandleCallRequest = {
|
|
93
|
+
args: ReadonlyArray<unknown>;
|
|
94
|
+
handle: number;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export type CallRequest = LocationCallRequest | HandleCallRequest;
|
|
98
|
+
|
|
99
|
+
type HandleOpts = {
|
|
100
|
+
fn?: HandleFunction;
|
|
101
|
+
childId?: number | null | undefined;
|
|
102
|
+
id?: number;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export declare class Handle {
|
|
106
|
+
id: number;
|
|
107
|
+
childId: number | null | undefined;
|
|
108
|
+
fn: HandleFunction | null | undefined;
|
|
109
|
+
constructor(opts: HandleOpts);
|
|
110
|
+
dispose(): void;
|
|
111
|
+
serialize(): {
|
|
112
|
+
childId: number | null | undefined;
|
|
113
|
+
id: number;
|
|
114
|
+
};
|
|
115
|
+
static deserialize(opts: HandleOpts): Handle;
|
|
116
|
+
}
|
package/lib/Handle.js
CHANGED
|
@@ -12,11 +12,8 @@ function _buildCache() {
|
|
|
12
12
|
return data;
|
|
13
13
|
}
|
|
14
14
|
var _package = _interopRequireDefault(require("../package.json"));
|
|
15
|
-
function _interopRequireDefault(
|
|
16
|
-
// $FlowFixMe
|
|
15
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
17
16
|
let HANDLE_ID = 0;
|
|
18
|
-
// $FlowFixMe
|
|
19
|
-
|
|
20
17
|
const handleById = new Map();
|
|
21
18
|
class Handle {
|
|
22
19
|
constructor(opts) {
|
package/lib/Worker.js
CHANGED
|
@@ -26,9 +26,11 @@ function _diagnostic() {
|
|
|
26
26
|
return data;
|
|
27
27
|
}
|
|
28
28
|
var _backend = require("./backend");
|
|
29
|
-
function _interopRequireDefault(
|
|
29
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
30
30
|
let WORKER_ID = 0;
|
|
31
31
|
class Worker extends _events().default {
|
|
32
|
+
// @ts-expect-error TS2564
|
|
33
|
+
|
|
32
34
|
id = WORKER_ID++;
|
|
33
35
|
sentSharedReferences = new Set();
|
|
34
36
|
calls = new Map();
|
|
@@ -105,7 +107,9 @@ class Worker extends _events().default {
|
|
|
105
107
|
let refsShared = new Set();
|
|
106
108
|
// in case more refs are created while initial refs are sending
|
|
107
109
|
while (refsShared.size < sharedRefs.size) {
|
|
108
|
-
await Promise.all([...sharedRefs]
|
|
110
|
+
await Promise.all([...sharedRefs]
|
|
111
|
+
// @ts-expect-error TS2769
|
|
112
|
+
.filter(([ref]) => !refsShared.has(ref)).map(async ([ref, value]) => {
|
|
109
113
|
await this.sendSharedReference(ref, value);
|
|
110
114
|
refsShared.add(ref);
|
|
111
115
|
}));
|
|
@@ -144,8 +148,10 @@ class Worker extends _events().default {
|
|
|
144
148
|
args: call.args
|
|
145
149
|
};
|
|
146
150
|
if (this.ready || call.skipReadyCheck === true) {
|
|
151
|
+
// @ts-expect-error TS2345
|
|
147
152
|
this.send(msg);
|
|
148
153
|
} else {
|
|
154
|
+
// @ts-expect-error TS2345
|
|
149
155
|
this.once('ready', () => this.send(msg));
|
|
150
156
|
}
|
|
151
157
|
}
|
package/lib/WorkerFarm.js
CHANGED
|
@@ -72,9 +72,8 @@ function _logger() {
|
|
|
72
72
|
};
|
|
73
73
|
return data;
|
|
74
74
|
}
|
|
75
|
-
function _interopRequireDefault(
|
|
76
|
-
function
|
|
77
|
-
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
75
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
76
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
78
77
|
let referenceId = 1;
|
|
79
78
|
const DEFAULT_MAX_CONCURRENT_CALLS = 30;
|
|
80
79
|
|
|
@@ -108,9 +107,9 @@ class WorkerFarm extends _events().default {
|
|
|
108
107
|
if (!this.options.workerPath) {
|
|
109
108
|
throw new Error('Please provide a worker path!');
|
|
110
109
|
}
|
|
111
|
-
|
|
112
|
-
// $FlowFixMe
|
|
113
110
|
this.localWorker = require(this.options.workerPath);
|
|
111
|
+
|
|
112
|
+
// @ts-expect-error TS2322
|
|
114
113
|
this.localWorkerInit = this.localWorker.childInit != null ? this.localWorker.childInit() : null;
|
|
115
114
|
this.run = this.createHandle('run');
|
|
116
115
|
|
|
@@ -127,9 +126,9 @@ class WorkerFarm extends _events().default {
|
|
|
127
126
|
}
|
|
128
127
|
workerApi = {
|
|
129
128
|
callMaster: async (request, awaitResponse = true) => {
|
|
130
|
-
// $FlowFixMe
|
|
131
129
|
let result = await this.processRequest({
|
|
132
130
|
...request,
|
|
131
|
+
// @ts-expect-error TS2322
|
|
133
132
|
awaitResponse
|
|
134
133
|
});
|
|
135
134
|
return (0, _buildCache().deserialize)((0, _buildCache().serialize)(result));
|
|
@@ -203,6 +202,7 @@ class WorkerFarm extends _events().default {
|
|
|
203
202
|
onError(error, worker) {
|
|
204
203
|
// Handle ipc errors
|
|
205
204
|
if (error.code === 'ERR_IPC_CHANNEL_CLOSED') {
|
|
205
|
+
// @ts-expect-error TS2322
|
|
206
206
|
return this.stopWorker(worker);
|
|
207
207
|
} else {
|
|
208
208
|
_logger().default.error(error, '@atlaspack/workers');
|
|
@@ -261,6 +261,7 @@ class WorkerFarm extends _events().default {
|
|
|
261
261
|
continue;
|
|
262
262
|
}
|
|
263
263
|
if (worker.calls.size < this.options.maxConcurrentCallsPerWorker) {
|
|
264
|
+
// @ts-expect-error TS2345
|
|
264
265
|
this.callWorker(worker, this.callQueue.shift());
|
|
265
266
|
}
|
|
266
267
|
}
|
|
@@ -287,7 +288,7 @@ class WorkerFarm extends _events().default {
|
|
|
287
288
|
var _this$handles$get;
|
|
288
289
|
mod = (0, _nullthrows().default)((_this$handles$get = this.handles.get(handleId)) === null || _this$handles$get === void 0 ? void 0 : _this$handles$get.fn);
|
|
289
290
|
} else if (location) {
|
|
290
|
-
//
|
|
291
|
+
// @ts-expect-error TS2339
|
|
291
292
|
if (process.browser) {
|
|
292
293
|
if (location === '@atlaspack/workers/bus') {
|
|
293
294
|
mod = bus;
|
|
@@ -295,7 +296,6 @@ class WorkerFarm extends _events().default {
|
|
|
295
296
|
throw new Error('No dynamic require possible: ' + location);
|
|
296
297
|
}
|
|
297
298
|
} else {
|
|
298
|
-
// $FlowFixMe this must be dynamic
|
|
299
299
|
mod = require(location);
|
|
300
300
|
}
|
|
301
301
|
} else {
|
|
@@ -316,6 +316,7 @@ class WorkerFarm extends _events().default {
|
|
|
316
316
|
let result;
|
|
317
317
|
if (method == null) {
|
|
318
318
|
try {
|
|
319
|
+
// @ts-expect-error TS2488
|
|
319
320
|
result = responseFromContent(await mod(...args));
|
|
320
321
|
} catch (e) {
|
|
321
322
|
result = errorResponseFromError(e);
|
|
@@ -326,7 +327,6 @@ class WorkerFarm extends _events().default {
|
|
|
326
327
|
mod = mod.default;
|
|
327
328
|
}
|
|
328
329
|
try {
|
|
329
|
-
// $FlowFixMe
|
|
330
330
|
result = responseFromContent(await mod[method](...args));
|
|
331
331
|
} catch (e) {
|
|
332
332
|
result = errorResponseFromError(e);
|
|
@@ -481,6 +481,7 @@ class WorkerFarm extends _events().default {
|
|
|
481
481
|
let filename = `profile-${getTimeId()}.trace`;
|
|
482
482
|
let stream = trace.pipe(_fs().default.createWriteStream(filename));
|
|
483
483
|
for (let profile of profiles) {
|
|
484
|
+
// @ts-expect-error TS2345
|
|
484
485
|
trace.addCPUProfile(names.shift(), profile);
|
|
485
486
|
}
|
|
486
487
|
trace.flush();
|
|
@@ -543,7 +544,10 @@ class WorkerFarm extends _events().default {
|
|
|
543
544
|
return _childState.child.workerApi;
|
|
544
545
|
}
|
|
545
546
|
static getConcurrentCallsPerWorker(defaultValue = DEFAULT_MAX_CONCURRENT_CALLS) {
|
|
546
|
-
return
|
|
547
|
+
return (
|
|
548
|
+
// @ts-expect-error TS2345
|
|
549
|
+
parseInt(process.env.ATLASPACK_MAX_CONCURRENT_CALLS, 10) || defaultValue
|
|
550
|
+
);
|
|
547
551
|
}
|
|
548
552
|
}
|
|
549
553
|
exports.default = WorkerFarm;
|
package/lib/backend.js
CHANGED
|
@@ -5,8 +5,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.detectBackend = detectBackend;
|
|
7
7
|
exports.getWorkerBackend = getWorkerBackend;
|
|
8
|
+
// flow-to-ts helpers
|
|
9
|
+
|
|
10
|
+
// /flow-to-ts helpers
|
|
11
|
+
|
|
8
12
|
function detectBackend() {
|
|
9
|
-
//
|
|
13
|
+
// @ts-expect-error TS2339
|
|
10
14
|
if (process.browser) return 'web';
|
|
11
15
|
switch (process.env.ATLASPACK_WORKER_BACKEND) {
|
|
12
16
|
case 'threads':
|
package/lib/bus.js
CHANGED
|
@@ -12,12 +12,12 @@ function _events() {
|
|
|
12
12
|
return data;
|
|
13
13
|
}
|
|
14
14
|
var _childState = require("./childState");
|
|
15
|
-
function _interopRequireDefault(
|
|
15
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
16
16
|
class Bus extends _events().default {
|
|
17
17
|
emit(event, ...args) {
|
|
18
18
|
if (_childState.child) {
|
|
19
19
|
_childState.child.workerApi.callMaster({
|
|
20
|
-
//
|
|
20
|
+
// @ts-expect-error TS2339
|
|
21
21
|
location: process.browser ? '@atlaspack/workers/bus' : __filename,
|
|
22
22
|
method: 'emit',
|
|
23
23
|
args: [event, ...args]
|
package/lib/child.js
CHANGED
|
@@ -48,9 +48,12 @@ function _profiler() {
|
|
|
48
48
|
return data;
|
|
49
49
|
}
|
|
50
50
|
var _Handle2 = _interopRequireDefault(require("./Handle"));
|
|
51
|
-
function
|
|
52
|
-
function
|
|
53
|
-
|
|
51
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
52
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
53
|
+
// flow-to-ts helpers
|
|
54
|
+
|
|
55
|
+
// /flow-to-ts helpers
|
|
56
|
+
|
|
54
57
|
// The import of './Handle' should really be imported eagerly (with @babel/plugin-transform-modules-commonjs's lazy mode).
|
|
55
58
|
const Handle = _Handle2.default;
|
|
56
59
|
class Child {
|
|
@@ -58,6 +61,7 @@ class Child {
|
|
|
58
61
|
maxConcurrentCalls = 10;
|
|
59
62
|
responseId = 0;
|
|
60
63
|
responseQueue = new Map();
|
|
64
|
+
// @ts-expect-error TS2749
|
|
61
65
|
handles = new Map();
|
|
62
66
|
sharedReferences = new Map();
|
|
63
67
|
sharedReferencesByValue = new Map();
|
|
@@ -78,7 +82,9 @@ class Child {
|
|
|
78
82
|
}
|
|
79
83
|
workerApi = {
|
|
80
84
|
callMaster: (request, awaitResponse = true) => this.addCall(request, awaitResponse),
|
|
85
|
+
// @ts-expect-error TS2749
|
|
81
86
|
createReverseHandle: fn => this.createReverseHandle(fn),
|
|
87
|
+
// @ts-expect-error TS2749
|
|
82
88
|
runHandle: (handle, args) => this.workerApi.callMaster({
|
|
83
89
|
handle: handle.id,
|
|
84
90
|
args
|
|
@@ -97,7 +103,6 @@ class Child {
|
|
|
97
103
|
this.child.send(data);
|
|
98
104
|
}
|
|
99
105
|
async childInit(module, childId) {
|
|
100
|
-
// $FlowFixMe
|
|
101
106
|
this.module = require(module);
|
|
102
107
|
this.childId = childId;
|
|
103
108
|
if (this.module.childInit != null) {
|
|
@@ -189,7 +194,7 @@ class Child {
|
|
|
189
194
|
} else {
|
|
190
195
|
try {
|
|
191
196
|
result = responseFromContent(
|
|
192
|
-
//
|
|
197
|
+
// @ts-expect-error TS2538
|
|
193
198
|
await this.module[method](this.workerApi, ...args));
|
|
194
199
|
} catch (e) {
|
|
195
200
|
result = errorResponseFromError(e);
|
|
@@ -222,12 +227,11 @@ class Child {
|
|
|
222
227
|
|
|
223
228
|
// Keep in mind to make sure responses to these calls are JSON.Stringify safe
|
|
224
229
|
addCall(request, awaitResponse = true) {
|
|
225
|
-
// $FlowFixMe
|
|
226
230
|
let call = {
|
|
227
231
|
...request,
|
|
228
232
|
type: 'request',
|
|
229
233
|
child: this.childId,
|
|
230
|
-
//
|
|
234
|
+
// @ts-expect-error TS2322
|
|
231
235
|
awaitResponse,
|
|
232
236
|
resolve: () => {},
|
|
233
237
|
reject: () => {}
|
|
@@ -265,6 +269,7 @@ class Child {
|
|
|
265
269
|
return;
|
|
266
270
|
}
|
|
267
271
|
if (this.responseQueue.size < this.maxConcurrentCalls) {
|
|
272
|
+
// @ts-expect-error TS2345
|
|
268
273
|
this.sendRequest(this.callQueue.shift());
|
|
269
274
|
}
|
|
270
275
|
}
|
|
@@ -272,6 +277,8 @@ class Child {
|
|
|
272
277
|
this.loggerDisposable.dispose();
|
|
273
278
|
this.tracerDisposable.dispose();
|
|
274
279
|
}
|
|
280
|
+
|
|
281
|
+
// @ts-expect-error TS2749
|
|
275
282
|
createReverseHandle(fn) {
|
|
276
283
|
let handle = new Handle({
|
|
277
284
|
fn,
|
package/lib/cpuCount.js
CHANGED
|
@@ -19,7 +19,7 @@ function _child_process() {
|
|
|
19
19
|
};
|
|
20
20
|
return data;
|
|
21
21
|
}
|
|
22
|
-
function _interopRequireDefault(
|
|
22
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
23
23
|
const exec = command => {
|
|
24
24
|
try {
|
|
25
25
|
let stdout = (0, _child_process().execSync)(command, {
|
|
@@ -50,18 +50,22 @@ function detectRealCores() {
|
|
|
50
50
|
}
|
|
51
51
|
return amount;
|
|
52
52
|
}
|
|
53
|
+
|
|
54
|
+
// @ts-expect-error TS7034
|
|
53
55
|
let cores;
|
|
54
56
|
function getCores(bypassCache = false) {
|
|
55
57
|
// Do not re-run commands if we already have the count...
|
|
58
|
+
// @ts-expect-error TS7005
|
|
56
59
|
if (cores && !bypassCache) {
|
|
57
60
|
return cores;
|
|
58
61
|
}
|
|
59
62
|
|
|
60
|
-
//
|
|
63
|
+
// @ts-expect-error TS2339
|
|
61
64
|
if (process.browser) {
|
|
62
|
-
// eslint-disable-next-line no-undef
|
|
63
65
|
cores = navigator.hardwareConcurrency / 2;
|
|
64
66
|
}
|
|
67
|
+
|
|
68
|
+
// @ts-expect-error TS7005
|
|
65
69
|
if (!cores) {
|
|
66
70
|
try {
|
|
67
71
|
cores = detectRealCores();
|
package/lib/index.js
CHANGED
|
@@ -39,9 +39,8 @@ function _profiler() {
|
|
|
39
39
|
};
|
|
40
40
|
return data;
|
|
41
41
|
}
|
|
42
|
-
function
|
|
43
|
-
function
|
|
44
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
42
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
43
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
45
44
|
if (!_WorkerFarm.default.isWorker()) {
|
|
46
45
|
// Forward all logger events originating from workers into the main process
|
|
47
46
|
_bus.default.on('logEvent', e => {
|
|
@@ -20,7 +20,8 @@ function _nullthrows() {
|
|
|
20
20
|
}
|
|
21
21
|
var _child = require("../child");
|
|
22
22
|
var _childState = require("../childState");
|
|
23
|
-
function _interopRequireDefault(
|
|
23
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
24
|
+
// @ts-expect-error TS2420
|
|
24
25
|
class ProcessChild {
|
|
25
26
|
constructor(onMessage, onExit) {
|
|
26
27
|
if (!process.send) {
|
|
@@ -28,6 +29,7 @@ class ProcessChild {
|
|
|
28
29
|
}
|
|
29
30
|
this.onMessage = onMessage;
|
|
30
31
|
this.onExit = onExit;
|
|
32
|
+
// @ts-expect-error TS2345
|
|
31
33
|
process.on('message', data => this.handleMessage(data));
|
|
32
34
|
}
|
|
33
35
|
handleMessage(data) {
|
|
@@ -38,9 +40,10 @@ class ProcessChild {
|
|
|
38
40
|
}
|
|
39
41
|
send(data) {
|
|
40
42
|
let processSend = (0, _nullthrows().default)(process.send).bind(process);
|
|
43
|
+
// @ts-expect-error TS7006
|
|
41
44
|
processSend((0, _buildCache().serialize)(data).toString('base64'), err => {
|
|
42
45
|
if (err && err instanceof Error) {
|
|
43
|
-
//
|
|
46
|
+
// @ts-expect-error TS2339
|
|
44
47
|
if (err.code === 'ERR_IPC_CHANNEL_CLOSED') {
|
|
45
48
|
// IPC connection closed
|
|
46
49
|
// no need to keep the worker running if it can't send or receive data
|
|
@@ -54,5 +57,7 @@ class ProcessChild {
|
|
|
54
57
|
process.exit();
|
|
55
58
|
}
|
|
56
59
|
}
|
|
60
|
+
|
|
61
|
+
// @ts-expect-error TS2345
|
|
57
62
|
exports.default = ProcessChild;
|
|
58
63
|
(0, _childState.setChild)(new _child.Child(ProcessChild));
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.default = void 0;
|
|
6
|
+
exports.default = exports.WORKER_PATH = void 0;
|
|
7
7
|
function _child_process() {
|
|
8
8
|
const data = _interopRequireDefault(require("child_process"));
|
|
9
9
|
_child_process = function () {
|
|
@@ -25,9 +25,16 @@ function _buildCache() {
|
|
|
25
25
|
};
|
|
26
26
|
return data;
|
|
27
27
|
}
|
|
28
|
-
function _interopRequireDefault(
|
|
29
|
-
|
|
28
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
29
|
+
let WORKER_PATH = exports.WORKER_PATH = _path().default.join(__dirname, 'ProcessChild.js');
|
|
30
|
+
if (process.env.ATLASPACK_REGISTER_USE_SRC === 'true') {
|
|
31
|
+
exports.WORKER_PATH = WORKER_PATH = _path().default.join(__dirname, 'ProcessChild.ts');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// @ts-expect-error TS2420
|
|
30
35
|
class ProcessWorker {
|
|
36
|
+
// @ts-expect-error TS2564
|
|
37
|
+
|
|
31
38
|
processQueue = true;
|
|
32
39
|
sendQueue = [];
|
|
33
40
|
constructor(execArgv, onMessage, onError, onExit) {
|
|
@@ -27,7 +27,8 @@ function _nullthrows() {
|
|
|
27
27
|
}
|
|
28
28
|
var _child = require("../child");
|
|
29
29
|
var _childState = require("../childState");
|
|
30
|
-
function _interopRequireDefault(
|
|
30
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
31
|
+
// @ts-expect-error TS2420
|
|
31
32
|
class ThreadsChild {
|
|
32
33
|
constructor(onMessage, onExit) {
|
|
33
34
|
if (_worker_threads().isMainThread || !_worker_threads().parentPort) {
|
|
@@ -45,5 +46,7 @@ class ThreadsChild {
|
|
|
45
46
|
(0, _nullthrows().default)(_worker_threads().parentPort).postMessage((0, _buildCache().prepareForSerialization)(data));
|
|
46
47
|
}
|
|
47
48
|
}
|
|
49
|
+
|
|
50
|
+
// @ts-expect-error TS2345
|
|
48
51
|
exports.default = ThreadsChild;
|
|
49
52
|
(0, _childState.setChild)(new _child.Child(ThreadsChild));
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.default = void 0;
|
|
6
|
+
exports.default = exports.WORKER_PATH = void 0;
|
|
7
7
|
function _worker_threads() {
|
|
8
8
|
const data = require("worker_threads");
|
|
9
9
|
_worker_threads = function () {
|
|
@@ -25,9 +25,16 @@ function _buildCache() {
|
|
|
25
25
|
};
|
|
26
26
|
return data;
|
|
27
27
|
}
|
|
28
|
-
function _interopRequireDefault(
|
|
29
|
-
|
|
28
|
+
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
29
|
+
let WORKER_PATH = exports.WORKER_PATH = _path().default.join(__dirname, 'ThreadsChild.js');
|
|
30
|
+
if (process.env.ATLASPACK_REGISTER_USE_SRC === 'true') {
|
|
31
|
+
exports.WORKER_PATH = WORKER_PATH = _path().default.join(__dirname, 'ThreadsChild.ts');
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// @ts-expect-error TS2420
|
|
30
35
|
class ThreadsWorker {
|
|
36
|
+
// @ts-expect-error TS2564
|
|
37
|
+
|
|
31
38
|
constructor(execArgv, onMessage, onError, onExit) {
|
|
32
39
|
this.execArgv = execArgv;
|
|
33
40
|
this.onMessage = onMessage;
|
|
@@ -49,6 +56,7 @@ class ThreadsWorker {
|
|
|
49
56
|
stop() {
|
|
50
57
|
// In node 12, this returns a promise, but previously it accepted a callback
|
|
51
58
|
// TODO: Pass a callback in earlier versions of Node
|
|
59
|
+
// @ts-expect-error TS2322
|
|
52
60
|
return Promise.resolve(this.worker.terminate());
|
|
53
61
|
}
|
|
54
62
|
handleMessage(data) {
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type HandleFunction = (...args: Array<any>) => any;
|
|
2
|
+
type HandleOpts = {
|
|
3
|
+
fn?: HandleFunction;
|
|
4
|
+
childId?: number | null | undefined;
|
|
5
|
+
id?: number;
|
|
6
|
+
};
|
|
7
|
+
export default class Handle {
|
|
8
|
+
id: number;
|
|
9
|
+
childId: number | null | undefined;
|
|
10
|
+
fn: HandleFunction | null | undefined;
|
|
11
|
+
constructor(opts: HandleOpts);
|
|
12
|
+
dispose(): void;
|
|
13
|
+
serialize(): {
|
|
14
|
+
childId: number | null | undefined;
|
|
15
|
+
id: number;
|
|
16
|
+
};
|
|
17
|
+
static deserialize(opts: HandleOpts): Handle;
|
|
18
|
+
}
|
|
19
|
+
export {};
|