@atlaspack/workers 2.14.5-canary.24 → 2.14.5-canary.240
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 +276 -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 +0 -3
- package/lib/Worker.js +7 -1
- package/lib/WorkerFarm.js +12 -7
- package/lib/backend.js +5 -1
- package/lib/bus.js +1 -1
- package/lib/child.js +11 -4
- package/lib/cpuCount.js +6 -2
- package/lib/process/ProcessChild.js +6 -1
- package/lib/process/ProcessWorker.js +9 -2
- package/lib/threads/ThreadsChild.js +3 -0
- package/lib/threads/ThreadsWorker.js +10 -2
- 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
|
@@ -13,10 +13,7 @@ function _buildCache() {
|
|
|
13
13
|
}
|
|
14
14
|
var _package = _interopRequireDefault(require("../package.json"));
|
|
15
15
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
|
-
// $FlowFixMe
|
|
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
|
@@ -29,6 +29,8 @@ var _backend = require("./backend");
|
|
|
29
29
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
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
|
@@ -108,9 +108,9 @@ class WorkerFarm extends _events().default {
|
|
|
108
108
|
if (!this.options.workerPath) {
|
|
109
109
|
throw new Error('Please provide a worker path!');
|
|
110
110
|
}
|
|
111
|
-
|
|
112
|
-
// $FlowFixMe
|
|
113
111
|
this.localWorker = require(this.options.workerPath);
|
|
112
|
+
|
|
113
|
+
// @ts-expect-error TS2322
|
|
114
114
|
this.localWorkerInit = this.localWorker.childInit != null ? this.localWorker.childInit() : null;
|
|
115
115
|
this.run = this.createHandle('run');
|
|
116
116
|
|
|
@@ -127,9 +127,9 @@ class WorkerFarm extends _events().default {
|
|
|
127
127
|
}
|
|
128
128
|
workerApi = {
|
|
129
129
|
callMaster: async (request, awaitResponse = true) => {
|
|
130
|
-
// $FlowFixMe
|
|
131
130
|
let result = await this.processRequest({
|
|
132
131
|
...request,
|
|
132
|
+
// @ts-expect-error TS2322
|
|
133
133
|
awaitResponse
|
|
134
134
|
});
|
|
135
135
|
return (0, _buildCache().deserialize)((0, _buildCache().serialize)(result));
|
|
@@ -203,6 +203,7 @@ class WorkerFarm extends _events().default {
|
|
|
203
203
|
onError(error, worker) {
|
|
204
204
|
// Handle ipc errors
|
|
205
205
|
if (error.code === 'ERR_IPC_CHANNEL_CLOSED') {
|
|
206
|
+
// @ts-expect-error TS2322
|
|
206
207
|
return this.stopWorker(worker);
|
|
207
208
|
} else {
|
|
208
209
|
_logger().default.error(error, '@atlaspack/workers');
|
|
@@ -261,6 +262,7 @@ class WorkerFarm extends _events().default {
|
|
|
261
262
|
continue;
|
|
262
263
|
}
|
|
263
264
|
if (worker.calls.size < this.options.maxConcurrentCallsPerWorker) {
|
|
265
|
+
// @ts-expect-error TS2345
|
|
264
266
|
this.callWorker(worker, this.callQueue.shift());
|
|
265
267
|
}
|
|
266
268
|
}
|
|
@@ -287,7 +289,7 @@ class WorkerFarm extends _events().default {
|
|
|
287
289
|
var _this$handles$get;
|
|
288
290
|
mod = (0, _nullthrows().default)((_this$handles$get = this.handles.get(handleId)) === null || _this$handles$get === void 0 ? void 0 : _this$handles$get.fn);
|
|
289
291
|
} else if (location) {
|
|
290
|
-
//
|
|
292
|
+
// @ts-expect-error TS2339
|
|
291
293
|
if (process.browser) {
|
|
292
294
|
if (location === '@atlaspack/workers/bus') {
|
|
293
295
|
mod = bus;
|
|
@@ -295,7 +297,6 @@ class WorkerFarm extends _events().default {
|
|
|
295
297
|
throw new Error('No dynamic require possible: ' + location);
|
|
296
298
|
}
|
|
297
299
|
} else {
|
|
298
|
-
// $FlowFixMe this must be dynamic
|
|
299
300
|
mod = require(location);
|
|
300
301
|
}
|
|
301
302
|
} else {
|
|
@@ -316,6 +317,7 @@ class WorkerFarm extends _events().default {
|
|
|
316
317
|
let result;
|
|
317
318
|
if (method == null) {
|
|
318
319
|
try {
|
|
320
|
+
// @ts-expect-error TS2488
|
|
319
321
|
result = responseFromContent(await mod(...args));
|
|
320
322
|
} catch (e) {
|
|
321
323
|
result = errorResponseFromError(e);
|
|
@@ -326,7 +328,6 @@ class WorkerFarm extends _events().default {
|
|
|
326
328
|
mod = mod.default;
|
|
327
329
|
}
|
|
328
330
|
try {
|
|
329
|
-
// $FlowFixMe
|
|
330
331
|
result = responseFromContent(await mod[method](...args));
|
|
331
332
|
} catch (e) {
|
|
332
333
|
result = errorResponseFromError(e);
|
|
@@ -481,6 +482,7 @@ class WorkerFarm extends _events().default {
|
|
|
481
482
|
let filename = `profile-${getTimeId()}.trace`;
|
|
482
483
|
let stream = trace.pipe(_fs().default.createWriteStream(filename));
|
|
483
484
|
for (let profile of profiles) {
|
|
485
|
+
// @ts-expect-error TS2345
|
|
484
486
|
trace.addCPUProfile(names.shift(), profile);
|
|
485
487
|
}
|
|
486
488
|
trace.flush();
|
|
@@ -543,7 +545,10 @@ class WorkerFarm extends _events().default {
|
|
|
543
545
|
return _childState.child.workerApi;
|
|
544
546
|
}
|
|
545
547
|
static getConcurrentCallsPerWorker(defaultValue = DEFAULT_MAX_CONCURRENT_CALLS) {
|
|
546
|
-
return
|
|
548
|
+
return (
|
|
549
|
+
// @ts-expect-error TS2345
|
|
550
|
+
parseInt(process.env.ATLASPACK_MAX_CONCURRENT_CALLS, 10) || defaultValue
|
|
551
|
+
);
|
|
547
552
|
}
|
|
548
553
|
}
|
|
549
554
|
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
|
@@ -17,7 +17,7 @@ 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
|
@@ -51,6 +51,9 @@ var _Handle2 = _interopRequireDefault(require("./Handle"));
|
|
|
51
51
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
52
52
|
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; }
|
|
53
53
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
54
|
+
// flow-to-ts helpers
|
|
55
|
+
|
|
56
|
+
// /flow-to-ts helpers
|
|
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
|
@@ -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();
|
|
@@ -21,6 +21,7 @@ function _nullthrows() {
|
|
|
21
21
|
var _child = require("../child");
|
|
22
22
|
var _childState = require("../childState");
|
|
23
23
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
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 () {
|
|
@@ -26,8 +26,15 @@ function _buildCache() {
|
|
|
26
26
|
return data;
|
|
27
27
|
}
|
|
28
28
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
29
|
-
|
|
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) {
|
|
@@ -28,6 +28,7 @@ function _nullthrows() {
|
|
|
28
28
|
var _child = require("../child");
|
|
29
29
|
var _childState = require("../childState");
|
|
30
30
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
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 () {
|
|
@@ -26,8 +26,15 @@ function _buildCache() {
|
|
|
26
26
|
return data;
|
|
27
27
|
}
|
|
28
28
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
29
|
-
|
|
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 {};
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { FilePath } from '@atlaspack/types-internal';
|
|
2
|
+
import type { BackendType, WorkerImpl, WorkerMessage } from './types';
|
|
3
|
+
import type { SharedReference } from './WorkerFarm';
|
|
4
|
+
import EventEmitter from 'events';
|
|
5
|
+
export type WorkerCall = {
|
|
6
|
+
method?: string;
|
|
7
|
+
handle?: number;
|
|
8
|
+
args: ReadonlyArray<any>;
|
|
9
|
+
retries: number;
|
|
10
|
+
skipReadyCheck?: boolean;
|
|
11
|
+
resolve: (result: Promise<any> | any) => void;
|
|
12
|
+
reject: (error?: any) => void;
|
|
13
|
+
};
|
|
14
|
+
type WorkerOpts = {
|
|
15
|
+
forcedKillTime: number;
|
|
16
|
+
backend: BackendType;
|
|
17
|
+
shouldPatchConsole?: boolean;
|
|
18
|
+
shouldTrace?: boolean;
|
|
19
|
+
sharedReferences: ReadonlyMap<SharedReference, unknown>;
|
|
20
|
+
};
|
|
21
|
+
export default class Worker extends EventEmitter {
|
|
22
|
+
readonly options: WorkerOpts;
|
|
23
|
+
worker: WorkerImpl;
|
|
24
|
+
id: number;
|
|
25
|
+
sentSharedReferences: Set<SharedReference>;
|
|
26
|
+
calls: Map<number, WorkerCall>;
|
|
27
|
+
exitCode: number | null | undefined;
|
|
28
|
+
callId: number;
|
|
29
|
+
ready: boolean;
|
|
30
|
+
stopped: boolean;
|
|
31
|
+
isStopping: boolean;
|
|
32
|
+
constructor(options: WorkerOpts);
|
|
33
|
+
fork(forkModule: FilePath): Promise<void>;
|
|
34
|
+
sendSharedReference(ref: SharedReference, value: unknown): Promise<any>;
|
|
35
|
+
send(data: WorkerMessage): void;
|
|
36
|
+
call(call: WorkerCall): void;
|
|
37
|
+
receive(message: WorkerMessage): void;
|
|
38
|
+
stop(): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
export {};
|