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