@atlaspack/workers 2.14.21-typescript-bc4459c37.0 → 2.14.21
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 +9 -0
- package/index.d.ts +2 -94
- package/lib/Handle.js +3 -0
- package/lib/Worker.js +1 -7
- package/lib/WorkerFarm.js +7 -17
- package/lib/backend.js +1 -5
- package/lib/bus.js +1 -1
- package/lib/child.js +4 -11
- package/lib/cpuCount.js +2 -6
- package/lib/process/ProcessChild.js +1 -6
- package/lib/process/ProcessWorker.js +2 -9
- package/lib/threads/ThreadsChild.js +0 -3
- package/lib/threads/ThreadsWorker.js +2 -10
- package/lib/web/WebChild.js +1 -6
- package/lib/web/WebWorker.js +4 -20
- package/package.json +12 -16
- package/src/{Handle.ts → Handle.js} +11 -11
- package/src/{Worker.ts → Worker.js} +54 -66
- package/src/{WorkerFarm.ts → WorkerFarm.js} +141 -198
- package/src/{backend.ts → backend.js} +3 -6
- package/src/{bus.ts → bus.js} +3 -2
- package/src/{child.ts → child.js} +43 -55
- package/src/{childState.ts → childState.js} +2 -1
- package/src/{cpuCount.ts → cpuCount.js} +7 -10
- package/src/{index.ts → index.js} +1 -0
- package/src/process/{ProcessChild.ts → ProcessChild.js} +3 -5
- package/src/process/{ProcessWorker.ts → ProcessWorker.js} +7 -10
- package/src/threads/{ThreadsChild.ts → ThreadsChild.js} +2 -2
- package/src/threads/{ThreadsWorker.ts → ThreadsWorker.js} +8 -14
- package/src/{types.ts → types.js} +35 -34
- package/src/web/{WebChild.ts → WebChild.js} +2 -6
- package/src/web/{WebWorker.ts → WebWorker.js} +7 -19
- package/test/{cpuCount.test.ts → cpuCount.test.js} +1 -0
- package/test/{workerfarm.test.js → workerfarm.test.cjs} +2 -4
- package/LICENSE +0 -201
- package/lib/Handle.d.ts +0 -19
- package/lib/Worker.d.ts +0 -40
- package/lib/WorkerFarm.d.ts +0 -93
- package/lib/backend.d.ts +0 -4
- package/lib/bus.d.ts +0 -6
- package/lib/child.d.ts +0 -43
- package/lib/childState.d.ts +0 -3
- package/lib/cpuCount.d.ts +0 -2
- package/lib/index.d.ts +0 -6
- package/lib/process/ProcessChild.d.ts +0 -9
- package/lib/process/ProcessWorker.d.ts +0 -16
- package/lib/threads/ThreadsChild.d.ts +0 -8
- package/lib/threads/ThreadsWorker.d.ts +0 -15
- package/lib/types.d.ts +0 -52
- package/lib/web/WebChild.d.ts +0 -8
- package/lib/web/WebWorker.d.ts +0 -15
- package/tsconfig.json +0 -4
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
1
3
|
import type {
|
|
2
4
|
CallRequest,
|
|
3
5
|
WorkerDataResponse,
|
|
@@ -19,37 +21,32 @@ import bus from './bus';
|
|
|
19
21
|
import {SamplingProfiler, tracer} from '@atlaspack/profiler';
|
|
20
22
|
import _Handle from './Handle';
|
|
21
23
|
|
|
22
|
-
// flow-to-ts helpers
|
|
23
|
-
export type Class<T> = new (...args: any[]) => T;
|
|
24
|
-
// /flow-to-ts helpers
|
|
25
|
-
|
|
26
24
|
// The import of './Handle' should really be imported eagerly (with @babel/plugin-transform-modules-commonjs's lazy mode).
|
|
27
25
|
const Handle = _Handle;
|
|
28
26
|
|
|
29
|
-
type ChildCall = WorkerRequest & {
|
|
30
|
-
resolve: (result: Promise<any> | any) => void
|
|
31
|
-
reject: (error
|
|
32
|
-
};
|
|
27
|
+
type ChildCall = WorkerRequest & {|
|
|
28
|
+
resolve: (result: Promise<any> | any) => void,
|
|
29
|
+
reject: (error: any) => void,
|
|
30
|
+
|};
|
|
33
31
|
|
|
34
32
|
export class Child {
|
|
35
33
|
callQueue: Array<ChildCall> = [];
|
|
36
|
-
childId: number
|
|
34
|
+
childId: ?number;
|
|
37
35
|
maxConcurrentCalls: number = 10;
|
|
38
|
-
module: any
|
|
36
|
+
module: ?any;
|
|
39
37
|
responseId: number = 0;
|
|
40
38
|
responseQueue: Map<number, ChildCall> = new Map();
|
|
41
39
|
loggerDisposable: IDisposable;
|
|
42
40
|
tracerDisposable: IDisposable;
|
|
43
41
|
child: ChildImpl;
|
|
44
|
-
profiler: SamplingProfiler
|
|
45
|
-
// @ts-expect-error TS2749
|
|
42
|
+
profiler: ?SamplingProfiler;
|
|
46
43
|
handles: Map<number, Handle> = new Map();
|
|
47
|
-
sharedReferences: Map<SharedReference,
|
|
48
|
-
sharedReferencesByValue: Map<
|
|
44
|
+
sharedReferences: Map<SharedReference, mixed> = new Map();
|
|
45
|
+
sharedReferencesByValue: Map<mixed, SharedReference> = new Map();
|
|
49
46
|
|
|
50
47
|
constructor(ChildBackend: Class<ChildImpl>) {
|
|
51
48
|
this.child = new ChildBackend(
|
|
52
|
-
(m
|
|
49
|
+
(m) => {
|
|
53
50
|
this.messageListener(m);
|
|
54
51
|
},
|
|
55
52
|
() => this.handleEnd(),
|
|
@@ -66,31 +63,27 @@ export class Child {
|
|
|
66
63
|
});
|
|
67
64
|
}
|
|
68
65
|
|
|
69
|
-
workerApi: {
|
|
66
|
+
workerApi: {|
|
|
70
67
|
callMaster: (
|
|
71
68
|
request: CallRequest,
|
|
72
|
-
awaitResponse?: boolean
|
|
73
|
-
) => Promise<
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
runHandle: (handle: Handle, args: Array<any>) => Promise<unknown>;
|
|
80
|
-
} = {
|
|
69
|
+
awaitResponse?: ?boolean,
|
|
70
|
+
) => Promise<mixed>,
|
|
71
|
+
createReverseHandle: (fn: (...args: Array<any>) => mixed) => Handle,
|
|
72
|
+
getSharedReference: (ref: SharedReference) => mixed,
|
|
73
|
+
resolveSharedReference: (value: mixed) => void | SharedReference,
|
|
74
|
+
runHandle: (handle: Handle, args: Array<any>) => Promise<mixed>,
|
|
75
|
+
|} = {
|
|
81
76
|
callMaster: (
|
|
82
77
|
request: CallRequest,
|
|
83
|
-
awaitResponse: boolean
|
|
84
|
-
): Promise<
|
|
85
|
-
|
|
86
|
-
createReverseHandle: (fn: (...args: Array<any>) => unknown): Handle =>
|
|
78
|
+
awaitResponse: ?boolean = true,
|
|
79
|
+
): Promise<mixed> => this.addCall(request, awaitResponse),
|
|
80
|
+
createReverseHandle: (fn: (...args: Array<any>) => mixed): Handle =>
|
|
87
81
|
this.createReverseHandle(fn),
|
|
88
|
-
|
|
89
|
-
runHandle: (handle: Handle, args: Array<any>): Promise<unknown> =>
|
|
82
|
+
runHandle: (handle: Handle, args: Array<any>): Promise<mixed> =>
|
|
90
83
|
this.workerApi.callMaster({handle: handle.id, args}, true),
|
|
91
84
|
getSharedReference: (ref: SharedReference) =>
|
|
92
85
|
this.sharedReferences.get(ref),
|
|
93
|
-
resolveSharedReference: (value:
|
|
86
|
+
resolveSharedReference: (value: mixed) =>
|
|
94
87
|
this.sharedReferencesByValue.get(value),
|
|
95
88
|
};
|
|
96
89
|
|
|
@@ -107,6 +100,7 @@ export class Child {
|
|
|
107
100
|
}
|
|
108
101
|
|
|
109
102
|
async childInit(module: string, childId: number): Promise<void> {
|
|
103
|
+
// $FlowFixMe
|
|
110
104
|
this.module = require(module);
|
|
111
105
|
this.childId = childId;
|
|
112
106
|
|
|
@@ -140,7 +134,7 @@ export class Child {
|
|
|
140
134
|
try {
|
|
141
135
|
let fn = nullthrows(this.handles.get(handleId)?.fn);
|
|
142
136
|
result = responseFromContent(fn(...args));
|
|
143
|
-
} catch (e
|
|
137
|
+
} catch (e) {
|
|
144
138
|
result = errorResponseFromError(e);
|
|
145
139
|
}
|
|
146
140
|
} else if (method === 'childInit') {
|
|
@@ -157,21 +151,21 @@ export class Child {
|
|
|
157
151
|
}
|
|
158
152
|
|
|
159
153
|
result = responseFromContent(await this.childInit(moduleName, child));
|
|
160
|
-
} catch (e
|
|
154
|
+
} catch (e) {
|
|
161
155
|
result = errorResponseFromError(e);
|
|
162
156
|
}
|
|
163
157
|
} else if (method === 'startProfile') {
|
|
164
158
|
this.profiler = new SamplingProfiler();
|
|
165
159
|
try {
|
|
166
160
|
result = responseFromContent(await this.profiler.startProfiling());
|
|
167
|
-
} catch (e
|
|
161
|
+
} catch (e) {
|
|
168
162
|
result = errorResponseFromError(e);
|
|
169
163
|
}
|
|
170
164
|
} else if (method === 'endProfile') {
|
|
171
165
|
try {
|
|
172
166
|
let res = this.profiler ? await this.profiler.stopProfiling() : null;
|
|
173
167
|
result = responseFromContent(res);
|
|
174
|
-
} catch (e
|
|
168
|
+
} catch (e) {
|
|
175
169
|
result = errorResponseFromError(e);
|
|
176
170
|
}
|
|
177
171
|
} else if (method === 'takeHeapSnapshot') {
|
|
@@ -186,7 +180,7 @@ export class Child {
|
|
|
186
180
|
'.heapsnapshot',
|
|
187
181
|
),
|
|
188
182
|
);
|
|
189
|
-
} catch (e
|
|
183
|
+
} catch (e) {
|
|
190
184
|
result = errorResponseFromError(e);
|
|
191
185
|
}
|
|
192
186
|
} else if (method === 'createSharedReference') {
|
|
@@ -209,17 +203,17 @@ export class Child {
|
|
|
209
203
|
} else {
|
|
210
204
|
try {
|
|
211
205
|
result = responseFromContent(
|
|
212
|
-
//
|
|
206
|
+
// $FlowFixMe
|
|
213
207
|
await this.module[method](this.workerApi, ...args),
|
|
214
208
|
);
|
|
215
|
-
} catch (e
|
|
209
|
+
} catch (e) {
|
|
216
210
|
result = errorResponseFromError(e);
|
|
217
211
|
}
|
|
218
212
|
}
|
|
219
213
|
|
|
220
214
|
try {
|
|
221
215
|
this.send(result);
|
|
222
|
-
} catch (e
|
|
216
|
+
} catch (e) {
|
|
223
217
|
result = this.send(errorResponseFromError(e));
|
|
224
218
|
}
|
|
225
219
|
}
|
|
@@ -246,13 +240,14 @@ export class Child {
|
|
|
246
240
|
// Keep in mind to make sure responses to these calls are JSON.Stringify safe
|
|
247
241
|
addCall(
|
|
248
242
|
request: CallRequest,
|
|
249
|
-
awaitResponse: boolean
|
|
250
|
-
): Promise<
|
|
243
|
+
awaitResponse: ?boolean = true,
|
|
244
|
+
): Promise<mixed> {
|
|
245
|
+
// $FlowFixMe
|
|
251
246
|
let call: ChildCall = {
|
|
252
247
|
...request,
|
|
253
248
|
type: 'request',
|
|
254
249
|
child: this.childId,
|
|
255
|
-
//
|
|
250
|
+
// $FlowFixMe Added in Flow 0.121.0 upgrade in #4381
|
|
256
251
|
awaitResponse,
|
|
257
252
|
resolve: () => {},
|
|
258
253
|
reject: () => {},
|
|
@@ -260,15 +255,10 @@ export class Child {
|
|
|
260
255
|
|
|
261
256
|
let promise;
|
|
262
257
|
if (awaitResponse) {
|
|
263
|
-
promise = new Promise(
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
) => {
|
|
268
|
-
call.resolve = resolve;
|
|
269
|
-
call.reject = reject;
|
|
270
|
-
},
|
|
271
|
-
);
|
|
258
|
+
promise = new Promise((resolve, reject) => {
|
|
259
|
+
call.resolve = resolve;
|
|
260
|
+
call.reject = reject;
|
|
261
|
+
});
|
|
272
262
|
}
|
|
273
263
|
|
|
274
264
|
this.callQueue.push(call);
|
|
@@ -302,7 +292,6 @@ export class Child {
|
|
|
302
292
|
}
|
|
303
293
|
|
|
304
294
|
if (this.responseQueue.size < this.maxConcurrentCalls) {
|
|
305
|
-
// @ts-expect-error TS2345
|
|
306
295
|
this.sendRequest(this.callQueue.shift());
|
|
307
296
|
}
|
|
308
297
|
}
|
|
@@ -312,8 +301,7 @@ export class Child {
|
|
|
312
301
|
this.tracerDisposable.dispose();
|
|
313
302
|
}
|
|
314
303
|
|
|
315
|
-
|
|
316
|
-
createReverseHandle(fn: (...args: Array<any>) => unknown): Handle {
|
|
304
|
+
createReverseHandle(fn: (...args: Array<any>) => mixed): Handle {
|
|
317
305
|
let handle = new Handle({
|
|
318
306
|
fn,
|
|
319
307
|
childId: this.childId,
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
+
// @flow
|
|
1
2
|
import type {Child} from './child';
|
|
2
3
|
|
|
3
4
|
// This file is imported by both the WorkerFarm and child implementation.
|
|
4
5
|
// When a worker is inited, it sets the state in this file.
|
|
5
6
|
// This way, WorkerFarm can access the state without directly importing the child code.
|
|
6
|
-
export let child: Child
|
|
7
|
+
export let child: ?Child = null;
|
|
7
8
|
export function setChild(c: Child) {
|
|
8
9
|
child = c;
|
|
9
10
|
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
+
// @flow
|
|
1
2
|
import os from 'os';
|
|
2
3
|
import {execSync} from 'child_process';
|
|
3
4
|
|
|
4
|
-
declare const navigator: {hardwareConcurrency: number} | undefined;
|
|
5
|
-
|
|
6
5
|
const exec = (command: string): string => {
|
|
7
6
|
try {
|
|
8
7
|
let stdout = execSync(command, {
|
|
@@ -11,7 +10,7 @@ const exec = (command: string): string => {
|
|
|
11
10
|
stdio: [null, null, null],
|
|
12
11
|
});
|
|
13
12
|
return stdout.trim();
|
|
14
|
-
} catch (e
|
|
13
|
+
} catch (e) {
|
|
15
14
|
return '';
|
|
16
15
|
}
|
|
17
16
|
};
|
|
@@ -41,25 +40,23 @@ export function detectRealCores(): number {
|
|
|
41
40
|
return amount;
|
|
42
41
|
}
|
|
43
42
|
|
|
44
|
-
// @ts-expect-error TS7034
|
|
45
43
|
let cores;
|
|
46
|
-
export default function getCores(bypassCache
|
|
44
|
+
export default function getCores(bypassCache?: boolean = false): number {
|
|
47
45
|
// Do not re-run commands if we already have the count...
|
|
48
|
-
// @ts-expect-error TS7005
|
|
49
46
|
if (cores && !bypassCache) {
|
|
50
47
|
return cores;
|
|
51
48
|
}
|
|
52
49
|
|
|
53
|
-
//
|
|
50
|
+
// $FlowFixMe
|
|
54
51
|
if (process.browser) {
|
|
55
|
-
|
|
52
|
+
// eslint-disable-next-line no-undef
|
|
53
|
+
cores = navigator.hardwareConcurrency / 2;
|
|
56
54
|
}
|
|
57
55
|
|
|
58
|
-
// @ts-expect-error TS7005
|
|
59
56
|
if (!cores) {
|
|
60
57
|
try {
|
|
61
58
|
cores = detectRealCores();
|
|
62
|
-
} catch (e
|
|
59
|
+
} catch (e) {
|
|
63
60
|
// Guess the amount of real cores
|
|
64
61
|
cores = os
|
|
65
62
|
.cpus()
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
1
3
|
import type {
|
|
2
4
|
ChildImpl,
|
|
3
5
|
MessageHandler,
|
|
@@ -11,7 +13,6 @@ import nullthrows from 'nullthrows';
|
|
|
11
13
|
import {Child} from '../child';
|
|
12
14
|
import {setChild} from '../childState';
|
|
13
15
|
|
|
14
|
-
// @ts-expect-error TS2420
|
|
15
16
|
export default class ProcessChild implements ChildImpl {
|
|
16
17
|
onMessage: MessageHandler;
|
|
17
18
|
onExit: ExitHandler;
|
|
@@ -23,7 +24,6 @@ export default class ProcessChild implements ChildImpl {
|
|
|
23
24
|
|
|
24
25
|
this.onMessage = onMessage;
|
|
25
26
|
this.onExit = onExit;
|
|
26
|
-
// @ts-expect-error TS2345
|
|
27
27
|
process.on('message', (data) => this.handleMessage(data));
|
|
28
28
|
}
|
|
29
29
|
|
|
@@ -37,10 +37,9 @@ export default class ProcessChild implements ChildImpl {
|
|
|
37
37
|
|
|
38
38
|
send(data: WorkerMessage) {
|
|
39
39
|
let processSend = nullthrows(process.send).bind(process);
|
|
40
|
-
// @ts-expect-error TS7006
|
|
41
40
|
processSend(serialize(data).toString('base64'), (err) => {
|
|
42
41
|
if (err && err instanceof Error) {
|
|
43
|
-
//
|
|
42
|
+
// $FlowFixMe[prop-missing]
|
|
44
43
|
if (err.code === 'ERR_IPC_CHANNEL_CLOSED') {
|
|
45
44
|
// IPC connection closed
|
|
46
45
|
// no need to keep the worker running if it can't send or receive data
|
|
@@ -56,5 +55,4 @@ export default class ProcessChild implements ChildImpl {
|
|
|
56
55
|
}
|
|
57
56
|
}
|
|
58
57
|
|
|
59
|
-
// @ts-expect-error TS2345
|
|
60
58
|
setChild(new Child(ProcessChild));
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
1
3
|
import type {
|
|
2
4
|
WorkerImpl,
|
|
3
5
|
MessageHandler,
|
|
@@ -6,29 +8,24 @@ import type {
|
|
|
6
8
|
WorkerMessage,
|
|
7
9
|
} from '../types';
|
|
8
10
|
|
|
9
|
-
import childProcess, {ChildProcess} from 'child_process';
|
|
11
|
+
import childProcess, {type ChildProcess} from 'child_process';
|
|
10
12
|
import path from 'path';
|
|
11
13
|
|
|
12
14
|
import {serialize, deserialize} from '@atlaspack/build-cache';
|
|
13
15
|
|
|
14
|
-
|
|
15
|
-
if (process.env.ATLASPACK_REGISTER_USE_SRC === 'true') {
|
|
16
|
-
WORKER_PATH = path.join(__dirname, 'ProcessChild.ts');
|
|
17
|
-
}
|
|
16
|
+
const WORKER_PATH = path.join(__dirname, './ProcessChild.js');
|
|
18
17
|
|
|
19
|
-
// @ts-expect-error TS2420
|
|
20
18
|
export default class ProcessWorker implements WorkerImpl {
|
|
21
|
-
execArgv:
|
|
19
|
+
execArgv: Object;
|
|
22
20
|
onMessage: MessageHandler;
|
|
23
21
|
onError: ErrorHandler;
|
|
24
22
|
onExit: ExitHandler;
|
|
25
|
-
// @ts-expect-error TS2564
|
|
26
23
|
child: ChildProcess;
|
|
27
24
|
processQueue: boolean = true;
|
|
28
25
|
sendQueue: Array<any> = [];
|
|
29
26
|
|
|
30
27
|
constructor(
|
|
31
|
-
execArgv:
|
|
28
|
+
execArgv: Object,
|
|
32
29
|
onMessage: MessageHandler,
|
|
33
30
|
onError: ErrorHandler,
|
|
34
31
|
onExit: ExitHandler,
|
|
@@ -60,7 +57,7 @@ export default class ProcessWorker implements WorkerImpl {
|
|
|
60
57
|
this.child.send('die');
|
|
61
58
|
|
|
62
59
|
let forceKill = setTimeout(() => this.child.kill('SIGINT'), 500);
|
|
63
|
-
await new Promise((resolve
|
|
60
|
+
await new Promise((resolve) => {
|
|
64
61
|
this.child.once('exit', resolve);
|
|
65
62
|
});
|
|
66
63
|
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
1
3
|
import type {
|
|
2
4
|
ChildImpl,
|
|
3
5
|
MessageHandler,
|
|
@@ -16,7 +18,6 @@ import nullthrows from 'nullthrows';
|
|
|
16
18
|
import {Child} from '../child';
|
|
17
19
|
import {setChild} from '../childState';
|
|
18
20
|
|
|
19
|
-
// @ts-expect-error TS2420
|
|
20
21
|
export default class ThreadsChild implements ChildImpl {
|
|
21
22
|
onMessage: MessageHandler;
|
|
22
23
|
onExit: ExitHandler;
|
|
@@ -41,5 +42,4 @@ export default class ThreadsChild implements ChildImpl {
|
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
|
|
44
|
-
// @ts-expect-error TS2345
|
|
45
45
|
setChild(new Child(ThreadsChild));
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
1
3
|
import type {
|
|
2
4
|
WorkerImpl,
|
|
3
5
|
MessageHandler,
|
|
@@ -14,22 +16,17 @@ import {
|
|
|
14
16
|
restoreDeserializedObject,
|
|
15
17
|
} from '@atlaspack/build-cache';
|
|
16
18
|
|
|
17
|
-
|
|
18
|
-
if (process.env.ATLASPACK_REGISTER_USE_SRC === 'true') {
|
|
19
|
-
WORKER_PATH = path.join(__dirname, 'ThreadsChild.ts');
|
|
20
|
-
}
|
|
19
|
+
const WORKER_PATH = path.join(__dirname, './ThreadsChild.js');
|
|
21
20
|
|
|
22
|
-
// @ts-expect-error TS2420
|
|
23
21
|
export default class ThreadsWorker implements WorkerImpl {
|
|
24
|
-
execArgv:
|
|
22
|
+
execArgv: Object;
|
|
25
23
|
onMessage: MessageHandler;
|
|
26
24
|
onError: ErrorHandler;
|
|
27
25
|
onExit: ExitHandler;
|
|
28
|
-
// @ts-expect-error TS2564
|
|
29
26
|
worker: Worker;
|
|
30
27
|
|
|
31
28
|
constructor(
|
|
32
|
-
execArgv:
|
|
29
|
+
execArgv: Object,
|
|
33
30
|
onMessage: MessageHandler,
|
|
34
31
|
onError: ErrorHandler,
|
|
35
32
|
onExit: ExitHandler,
|
|
@@ -50,17 +47,14 @@ export default class ThreadsWorker implements WorkerImpl {
|
|
|
50
47
|
this.worker.on('error', this.onError);
|
|
51
48
|
this.worker.on('exit', this.onExit);
|
|
52
49
|
|
|
53
|
-
return new Promise<
|
|
54
|
-
(resolve
|
|
55
|
-
|
|
56
|
-
},
|
|
57
|
-
);
|
|
50
|
+
return new Promise<void>((resolve) => {
|
|
51
|
+
this.worker.on('online', resolve);
|
|
52
|
+
});
|
|
58
53
|
}
|
|
59
54
|
|
|
60
55
|
stop(): Promise<void> {
|
|
61
56
|
// In node 12, this returns a promise, but previously it accepted a callback
|
|
62
57
|
// TODO: Pass a callback in earlier versions of Node
|
|
63
|
-
// @ts-expect-error TS2322
|
|
64
58
|
return Promise.resolve(this.worker.terminate());
|
|
65
59
|
}
|
|
66
60
|
|
|
@@ -1,45 +1,46 @@
|
|
|
1
|
+
// @flow
|
|
1
2
|
import type {Diagnostic} from '@atlaspack/diagnostic';
|
|
2
3
|
import type {FilePath} from '@atlaspack/types-internal';
|
|
3
4
|
|
|
4
|
-
export type LocationCallRequest = {
|
|
5
|
-
args:
|
|
6
|
-
location: string
|
|
7
|
-
method?: string
|
|
8
|
-
};
|
|
5
|
+
export type LocationCallRequest = {|
|
|
6
|
+
args: $ReadOnlyArray<mixed>,
|
|
7
|
+
location: string,
|
|
8
|
+
method?: string,
|
|
9
|
+
|};
|
|
9
10
|
|
|
10
|
-
export type HandleCallRequest = {
|
|
11
|
-
args:
|
|
12
|
-
handle: number
|
|
13
|
-
};
|
|
11
|
+
export type HandleCallRequest = {|
|
|
12
|
+
args: $ReadOnlyArray<mixed>,
|
|
13
|
+
handle: number,
|
|
14
|
+
|};
|
|
14
15
|
|
|
15
16
|
export type CallRequest = LocationCallRequest | HandleCallRequest;
|
|
16
17
|
|
|
17
|
-
export type WorkerRequest = {
|
|
18
|
-
args:
|
|
19
|
-
awaitResponse?: boolean
|
|
20
|
-
child?: number
|
|
21
|
-
idx?: number
|
|
22
|
-
location?: FilePath
|
|
23
|
-
method?: string
|
|
24
|
-
type: 'request'
|
|
25
|
-
handle?: number
|
|
26
|
-
};
|
|
18
|
+
export type WorkerRequest = {|
|
|
19
|
+
args: $ReadOnlyArray<any>,
|
|
20
|
+
awaitResponse?: boolean,
|
|
21
|
+
child?: ?number,
|
|
22
|
+
idx?: number,
|
|
23
|
+
location?: FilePath,
|
|
24
|
+
method?: ?string,
|
|
25
|
+
type: 'request',
|
|
26
|
+
handle?: number,
|
|
27
|
+
|};
|
|
27
28
|
|
|
28
|
-
export type WorkerDataResponse = {
|
|
29
|
-
idx?: number
|
|
30
|
-
child?: number
|
|
31
|
-
type: 'response'
|
|
32
|
-
contentType: 'data'
|
|
33
|
-
content: string
|
|
34
|
-
};
|
|
29
|
+
export type WorkerDataResponse = {|
|
|
30
|
+
idx?: number,
|
|
31
|
+
child?: number,
|
|
32
|
+
type: 'response',
|
|
33
|
+
contentType: 'data',
|
|
34
|
+
content: string,
|
|
35
|
+
|};
|
|
35
36
|
|
|
36
|
-
export type WorkerErrorResponse = {
|
|
37
|
-
idx?: number
|
|
38
|
-
child?: number
|
|
39
|
-
type: 'response'
|
|
40
|
-
contentType: 'error'
|
|
41
|
-
content: Diagnostic | Array<Diagnostic
|
|
42
|
-
};
|
|
37
|
+
export type WorkerErrorResponse = {|
|
|
38
|
+
idx?: number,
|
|
39
|
+
child?: number,
|
|
40
|
+
type: 'response',
|
|
41
|
+
contentType: 'error',
|
|
42
|
+
content: Diagnostic | Array<Diagnostic>,
|
|
43
|
+
|};
|
|
43
44
|
|
|
44
45
|
export type WorkerResponse = WorkerDataResponse | WorkerErrorResponse;
|
|
45
46
|
export type WorkerMessage = WorkerRequest | WorkerResponse;
|
|
@@ -49,7 +50,7 @@ export type ErrorHandler = (err: Error) => void;
|
|
|
49
50
|
export type ExitHandler = (code: number) => void;
|
|
50
51
|
export interface WorkerImpl {
|
|
51
52
|
constructor(
|
|
52
|
-
execArgv:
|
|
53
|
+
execArgv: Object,
|
|
53
54
|
onMessage: MessageHandler,
|
|
54
55
|
onError: ErrorHandler,
|
|
55
56
|
onExit: ExitHandler,
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
// @flow
|
|
1
2
|
/* eslint-env worker*/
|
|
2
3
|
|
|
3
4
|
import type {
|
|
@@ -15,11 +16,6 @@ import {
|
|
|
15
16
|
import {Child} from '../child';
|
|
16
17
|
import {setChild} from '../childState';
|
|
17
18
|
|
|
18
|
-
// Type declarations for Web Worker environment
|
|
19
|
-
declare const WorkerGlobalScope: any;
|
|
20
|
-
declare const self: any;
|
|
21
|
-
|
|
22
|
-
// @ts-expect-error TS2420
|
|
23
19
|
export default class WebChild implements ChildImpl {
|
|
24
20
|
onMessage: MessageHandler;
|
|
25
21
|
onExit: ExitHandler;
|
|
@@ -41,6 +37,7 @@ export default class WebChild implements ChildImpl {
|
|
|
41
37
|
this.onExit(0);
|
|
42
38
|
self.postMessage('stopped');
|
|
43
39
|
}
|
|
40
|
+
// $FlowFixMe assume WorkerMessage as data
|
|
44
41
|
this.handleMessage(data);
|
|
45
42
|
});
|
|
46
43
|
self.postMessage('online');
|
|
@@ -55,5 +52,4 @@ export default class WebChild implements ChildImpl {
|
|
|
55
52
|
}
|
|
56
53
|
}
|
|
57
54
|
|
|
58
|
-
// @ts-expect-error TS2345
|
|
59
55
|
setChild(new Child(WebChild));
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// @flow
|
|
2
|
+
|
|
1
3
|
import type {
|
|
2
4
|
WorkerImpl,
|
|
3
5
|
MessageHandler,
|
|
@@ -14,25 +16,16 @@ import {makeDeferredWithPromise} from '@atlaspack/utils';
|
|
|
14
16
|
|
|
15
17
|
let id = 0;
|
|
16
18
|
|
|
17
|
-
// @ts-expect-error This is actually a module
|
|
18
|
-
export let WORKER_PATH = new URL('./WebChild.js', import.meta.url);
|
|
19
|
-
if (process.env.ATLASPACK_REGISTER_USE_SRC === 'true') {
|
|
20
|
-
// @ts-expect-error This is actually a module
|
|
21
|
-
WORKER_PATH = new URL('./WebChild.ts', import.meta.url);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
// @ts-expect-error TS2420
|
|
25
19
|
export default class WebWorker implements WorkerImpl {
|
|
26
|
-
execArgv:
|
|
20
|
+
execArgv: Object;
|
|
27
21
|
onMessage: MessageHandler;
|
|
28
22
|
onError: ErrorHandler;
|
|
29
23
|
onExit: ExitHandler;
|
|
30
|
-
// @ts-expect-error TS2564
|
|
31
24
|
worker: Worker;
|
|
32
|
-
stopping: Promise<
|
|
25
|
+
stopping: ?Promise<void>;
|
|
33
26
|
|
|
34
27
|
constructor(
|
|
35
|
-
execArgv:
|
|
28
|
+
execArgv: Object,
|
|
36
29
|
onMessage: MessageHandler,
|
|
37
30
|
onError: ErrorHandler,
|
|
38
31
|
onExit: ExitHandler,
|
|
@@ -44,7 +37,7 @@ export default class WebWorker implements WorkerImpl {
|
|
|
44
37
|
}
|
|
45
38
|
|
|
46
39
|
start(): Promise<void> {
|
|
47
|
-
//
|
|
40
|
+
// $FlowFixMe[incompatible-call]
|
|
48
41
|
this.worker = new Worker(new URL('./WebChild.js', import.meta.url), {
|
|
49
42
|
name: `Parcel Worker ${id++}`,
|
|
50
43
|
type: 'module',
|
|
@@ -52,33 +45,29 @@ export default class WebWorker implements WorkerImpl {
|
|
|
52
45
|
|
|
53
46
|
let {deferred, promise} = makeDeferredWithPromise();
|
|
54
47
|
|
|
55
|
-
// @ts-expect-error TS7031
|
|
56
48
|
this.worker.onmessage = ({data}) => {
|
|
57
49
|
if (data === 'online') {
|
|
58
|
-
// @ts-expect-error TS2554
|
|
59
50
|
deferred.resolve();
|
|
60
51
|
return;
|
|
61
52
|
}
|
|
62
53
|
|
|
54
|
+
// $FlowFixMe assume WorkerMessage as data
|
|
63
55
|
this.handleMessage(data);
|
|
64
56
|
};
|
|
65
57
|
this.worker.onerror = this.onError;
|
|
66
58
|
// Web workers can't crash or intentionally stop on their own, apart from stop() below
|
|
67
59
|
// this.worker.on('exit', this.onExit);
|
|
68
60
|
|
|
69
|
-
// @ts-expect-error TS2322
|
|
70
61
|
return promise;
|
|
71
62
|
}
|
|
72
63
|
|
|
73
64
|
stop(): Promise<void> {
|
|
74
65
|
if (!this.stopping) {
|
|
75
|
-
// @ts-expect-error TS2322
|
|
76
66
|
this.stopping = (async () => {
|
|
77
67
|
this.worker.postMessage('stop');
|
|
78
68
|
let {deferred, promise} = makeDeferredWithPromise();
|
|
79
69
|
this.worker.addEventListener('message', ({data}: MessageEvent) => {
|
|
80
70
|
if (data === 'stopped') {
|
|
81
|
-
// @ts-expect-error TS2554
|
|
82
71
|
deferred.resolve();
|
|
83
72
|
}
|
|
84
73
|
});
|
|
@@ -87,7 +76,6 @@ export default class WebWorker implements WorkerImpl {
|
|
|
87
76
|
this.onExit(0);
|
|
88
77
|
})();
|
|
89
78
|
}
|
|
90
|
-
// @ts-expect-error TS2322
|
|
91
79
|
return this.stopping;
|
|
92
80
|
}
|
|
93
81
|
|
|
@@ -1,11 +1,9 @@
|
|
|
1
1
|
// NOTE: @atlaspack/logger exports object instances from the module.
|
|
2
2
|
// If there are issues, check all imports are using the same module instance/path
|
|
3
|
-
//
|
|
4
|
-
// NOTE2: @atlaspack/babel-register needs to run on the fixtures under ./integration
|
|
5
|
-
// otherwise the fixtures will import "package.json#main" files rather than "package.json#source"
|
|
6
3
|
const Logger = require('@atlaspack/logger').default;
|
|
7
4
|
const assert = require('assert');
|
|
8
|
-
|
|
5
|
+
// eslint-disable-next-line @atlaspack/no-self-package-imports
|
|
6
|
+
const WorkerFarm = require('@atlaspack/workers').default;
|
|
9
7
|
|
|
10
8
|
describe('WorkerFarm', function () {
|
|
11
9
|
this.timeout(30000);
|