@atlaspack/workers 2.14.20 → 2.14.21-typescript-80839fbd5.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/lib/Handle.js +0 -3
- package/lib/Worker.js +7 -1
- package/lib/WorkerFarm.js +17 -7
- package/lib/backend.js +1 -1
- package/lib/bus.js +1 -1
- package/lib/child.js +8 -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/web/WebChild.js +6 -1
- package/lib/web/WebWorker.js +20 -4
- package/package.json +14 -10
- 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} +4 -4
- package/src/{bus.js → bus.ts} +2 -3
- package/src/{child.js → child.ts} +53 -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,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
import {Flow} from 'flow-to-typescript-codemod';
|
|
2
2
|
|
|
3
3
|
import type {
|
|
4
4
|
CallRequest,
|
|
@@ -24,29 +24,30 @@ import _Handle from './Handle';
|
|
|
24
24
|
// The import of './Handle' should really be imported eagerly (with @babel/plugin-transform-modules-commonjs's lazy mode).
|
|
25
25
|
const Handle = _Handle;
|
|
26
26
|
|
|
27
|
-
type ChildCall = WorkerRequest & {
|
|
28
|
-
resolve: (result: Promise<any> | any) => void
|
|
29
|
-
reject: (error
|
|
30
|
-
|
|
27
|
+
type ChildCall = WorkerRequest & {
|
|
28
|
+
resolve: (result: Promise<any> | any) => void;
|
|
29
|
+
reject: (error?: any) => void;
|
|
30
|
+
};
|
|
31
31
|
|
|
32
32
|
export class Child {
|
|
33
33
|
callQueue: Array<ChildCall> = [];
|
|
34
|
-
childId:
|
|
34
|
+
childId: number | null | undefined;
|
|
35
35
|
maxConcurrentCalls: number = 10;
|
|
36
|
-
module:
|
|
36
|
+
module: any | null | undefined;
|
|
37
37
|
responseId: number = 0;
|
|
38
38
|
responseQueue: Map<number, ChildCall> = new Map();
|
|
39
39
|
loggerDisposable: IDisposable;
|
|
40
40
|
tracerDisposable: IDisposable;
|
|
41
41
|
child: ChildImpl;
|
|
42
|
-
profiler:
|
|
42
|
+
profiler: SamplingProfiler | null | undefined;
|
|
43
|
+
// @ts-expect-error TS2749
|
|
43
44
|
handles: Map<number, Handle> = new Map();
|
|
44
|
-
sharedReferences: Map<SharedReference,
|
|
45
|
-
sharedReferencesByValue: Map<
|
|
45
|
+
sharedReferences: Map<SharedReference, unknown> = new Map();
|
|
46
|
+
sharedReferencesByValue: Map<unknown, SharedReference> = new Map();
|
|
46
47
|
|
|
47
|
-
constructor(ChildBackend: Class<ChildImpl>) {
|
|
48
|
+
constructor(ChildBackend: Flow.Class<ChildImpl>) {
|
|
48
49
|
this.child = new ChildBackend(
|
|
49
|
-
(m) => {
|
|
50
|
+
(m: WorkerMessage) => {
|
|
50
51
|
this.messageListener(m);
|
|
51
52
|
},
|
|
52
53
|
() => this.handleEnd(),
|
|
@@ -63,27 +64,31 @@ export class Child {
|
|
|
63
64
|
});
|
|
64
65
|
}
|
|
65
66
|
|
|
66
|
-
workerApi: {
|
|
67
|
+
workerApi: {
|
|
67
68
|
callMaster: (
|
|
68
69
|
request: CallRequest,
|
|
69
|
-
awaitResponse?:
|
|
70
|
-
) => Promise<
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
70
|
+
awaitResponse?: boolean | null | undefined,
|
|
71
|
+
) => Promise<unknown>;
|
|
72
|
+
// @ts-expect-error TS2749
|
|
73
|
+
createReverseHandle: (fn: (...args: Array<any>) => unknown) => Handle;
|
|
74
|
+
getSharedReference: (ref: SharedReference) => unknown;
|
|
75
|
+
resolveSharedReference: (value: unknown) => undefined | SharedReference;
|
|
76
|
+
// @ts-expect-error TS2749
|
|
77
|
+
runHandle: (handle: Handle, args: Array<any>) => Promise<unknown>;
|
|
78
|
+
} = {
|
|
76
79
|
callMaster: (
|
|
77
80
|
request: CallRequest,
|
|
78
|
-
awaitResponse:
|
|
79
|
-
): Promise<
|
|
80
|
-
|
|
81
|
+
awaitResponse: boolean | null = true,
|
|
82
|
+
): Promise<unknown> => this.addCall(request, awaitResponse),
|
|
83
|
+
// @ts-expect-error TS2749
|
|
84
|
+
createReverseHandle: (fn: (...args: Array<any>) => unknown): Handle =>
|
|
81
85
|
this.createReverseHandle(fn),
|
|
82
|
-
|
|
86
|
+
// @ts-expect-error TS2749
|
|
87
|
+
runHandle: (handle: Handle, args: Array<any>): Promise<unknown> =>
|
|
83
88
|
this.workerApi.callMaster({handle: handle.id, args}, true),
|
|
84
89
|
getSharedReference: (ref: SharedReference) =>
|
|
85
90
|
this.sharedReferences.get(ref),
|
|
86
|
-
resolveSharedReference: (value:
|
|
91
|
+
resolveSharedReference: (value: unknown) =>
|
|
87
92
|
this.sharedReferencesByValue.get(value),
|
|
88
93
|
};
|
|
89
94
|
|
|
@@ -100,7 +105,6 @@ export class Child {
|
|
|
100
105
|
}
|
|
101
106
|
|
|
102
107
|
async childInit(module: string, childId: number): Promise<void> {
|
|
103
|
-
// $FlowFixMe
|
|
104
108
|
this.module = require(module);
|
|
105
109
|
this.childId = childId;
|
|
106
110
|
|
|
@@ -134,7 +138,7 @@ export class Child {
|
|
|
134
138
|
try {
|
|
135
139
|
let fn = nullthrows(this.handles.get(handleId)?.fn);
|
|
136
140
|
result = responseFromContent(fn(...args));
|
|
137
|
-
} catch (e) {
|
|
141
|
+
} catch (e: any) {
|
|
138
142
|
result = errorResponseFromError(e);
|
|
139
143
|
}
|
|
140
144
|
} else if (method === 'childInit') {
|
|
@@ -151,21 +155,21 @@ export class Child {
|
|
|
151
155
|
}
|
|
152
156
|
|
|
153
157
|
result = responseFromContent(await this.childInit(moduleName, child));
|
|
154
|
-
} catch (e) {
|
|
158
|
+
} catch (e: any) {
|
|
155
159
|
result = errorResponseFromError(e);
|
|
156
160
|
}
|
|
157
161
|
} else if (method === 'startProfile') {
|
|
158
162
|
this.profiler = new SamplingProfiler();
|
|
159
163
|
try {
|
|
160
164
|
result = responseFromContent(await this.profiler.startProfiling());
|
|
161
|
-
} catch (e) {
|
|
165
|
+
} catch (e: any) {
|
|
162
166
|
result = errorResponseFromError(e);
|
|
163
167
|
}
|
|
164
168
|
} else if (method === 'endProfile') {
|
|
165
169
|
try {
|
|
166
170
|
let res = this.profiler ? await this.profiler.stopProfiling() : null;
|
|
167
171
|
result = responseFromContent(res);
|
|
168
|
-
} catch (e) {
|
|
172
|
+
} catch (e: any) {
|
|
169
173
|
result = errorResponseFromError(e);
|
|
170
174
|
}
|
|
171
175
|
} else if (method === 'takeHeapSnapshot') {
|
|
@@ -180,7 +184,7 @@ export class Child {
|
|
|
180
184
|
'.heapsnapshot',
|
|
181
185
|
),
|
|
182
186
|
);
|
|
183
|
-
} catch (e) {
|
|
187
|
+
} catch (e: any) {
|
|
184
188
|
result = errorResponseFromError(e);
|
|
185
189
|
}
|
|
186
190
|
} else if (method === 'createSharedReference') {
|
|
@@ -203,17 +207,17 @@ export class Child {
|
|
|
203
207
|
} else {
|
|
204
208
|
try {
|
|
205
209
|
result = responseFromContent(
|
|
206
|
-
//
|
|
210
|
+
// @ts-expect-error TS2538
|
|
207
211
|
await this.module[method](this.workerApi, ...args),
|
|
208
212
|
);
|
|
209
|
-
} catch (e) {
|
|
213
|
+
} catch (e: any) {
|
|
210
214
|
result = errorResponseFromError(e);
|
|
211
215
|
}
|
|
212
216
|
}
|
|
213
217
|
|
|
214
218
|
try {
|
|
215
219
|
this.send(result);
|
|
216
|
-
} catch (e) {
|
|
220
|
+
} catch (e: any) {
|
|
217
221
|
result = this.send(errorResponseFromError(e));
|
|
218
222
|
}
|
|
219
223
|
}
|
|
@@ -240,14 +244,13 @@ export class Child {
|
|
|
240
244
|
// Keep in mind to make sure responses to these calls are JSON.Stringify safe
|
|
241
245
|
addCall(
|
|
242
246
|
request: CallRequest,
|
|
243
|
-
awaitResponse:
|
|
244
|
-
): Promise<
|
|
245
|
-
// $FlowFixMe
|
|
247
|
+
awaitResponse: boolean | null = true,
|
|
248
|
+
): Promise<unknown> {
|
|
246
249
|
let call: ChildCall = {
|
|
247
250
|
...request,
|
|
248
251
|
type: 'request',
|
|
249
252
|
child: this.childId,
|
|
250
|
-
//
|
|
253
|
+
// @ts-expect-error TS2322
|
|
251
254
|
awaitResponse,
|
|
252
255
|
resolve: () => {},
|
|
253
256
|
reject: () => {},
|
|
@@ -255,10 +258,15 @@ export class Child {
|
|
|
255
258
|
|
|
256
259
|
let promise;
|
|
257
260
|
if (awaitResponse) {
|
|
258
|
-
promise = new Promise(
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
261
|
+
promise = new Promise(
|
|
262
|
+
(
|
|
263
|
+
resolve: (result: Promise<any> | any) => void,
|
|
264
|
+
reject: (error?: any) => void,
|
|
265
|
+
) => {
|
|
266
|
+
call.resolve = resolve;
|
|
267
|
+
call.reject = reject;
|
|
268
|
+
},
|
|
269
|
+
);
|
|
262
270
|
}
|
|
263
271
|
|
|
264
272
|
this.callQueue.push(call);
|
|
@@ -292,6 +300,7 @@ export class Child {
|
|
|
292
300
|
}
|
|
293
301
|
|
|
294
302
|
if (this.responseQueue.size < this.maxConcurrentCalls) {
|
|
303
|
+
// @ts-expect-error TS2345
|
|
295
304
|
this.sendRequest(this.callQueue.shift());
|
|
296
305
|
}
|
|
297
306
|
}
|
|
@@ -301,7 +310,8 @@ export class Child {
|
|
|
301
310
|
this.tracerDisposable.dispose();
|
|
302
311
|
}
|
|
303
312
|
|
|
304
|
-
|
|
313
|
+
// @ts-expect-error TS2749
|
|
314
|
+
createReverseHandle(fn: (...args: Array<any>) => unknown): Handle {
|
|
305
315
|
let handle = new Handle({
|
|
306
316
|
fn,
|
|
307
317
|
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);
|
package/tsconfig.json
ADDED