@atlaspack/workers 2.14.5-canary.137 → 2.14.5-canary.139
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 +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 +15 -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
|
@@ -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));
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { WorkerImpl, MessageHandler, ErrorHandler, ExitHandler, WorkerMessage } from '../types';
|
|
2
|
+
import { ChildProcess } from 'child_process';
|
|
3
|
+
export declare let WORKER_PATH: string;
|
|
4
|
+
export default class ProcessWorker implements WorkerImpl {
|
|
5
|
+
execArgv: any;
|
|
6
|
+
onMessage: MessageHandler;
|
|
7
|
+
onError: ErrorHandler;
|
|
8
|
+
onExit: ExitHandler;
|
|
9
|
+
child: ChildProcess;
|
|
10
|
+
processQueue: boolean;
|
|
11
|
+
sendQueue: Array<any>;
|
|
12
|
+
constructor(execArgv: any, onMessage: MessageHandler, onError: ErrorHandler, onExit: ExitHandler);
|
|
13
|
+
start(): Promise<void>;
|
|
14
|
+
stop(): Promise<void>;
|
|
15
|
+
send(data: WorkerMessage): void;
|
|
16
|
+
}
|
|
@@ -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) {
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ChildImpl, MessageHandler, ExitHandler, WorkerMessage } from '../types';
|
|
2
|
+
export default class ThreadsChild implements ChildImpl {
|
|
3
|
+
onMessage: MessageHandler;
|
|
4
|
+
onExit: ExitHandler;
|
|
5
|
+
constructor(onMessage: MessageHandler, onExit: ExitHandler);
|
|
6
|
+
handleMessage(data: WorkerMessage): void;
|
|
7
|
+
send(data: WorkerMessage): void;
|
|
8
|
+
}
|
|
@@ -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));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { WorkerImpl, MessageHandler, ErrorHandler, ExitHandler, WorkerMessage } from '../types';
|
|
2
|
+
import { Worker } from 'worker_threads';
|
|
3
|
+
export declare let WORKER_PATH: string;
|
|
4
|
+
export default class ThreadsWorker implements WorkerImpl {
|
|
5
|
+
execArgv: any;
|
|
6
|
+
onMessage: MessageHandler;
|
|
7
|
+
onError: ErrorHandler;
|
|
8
|
+
onExit: ExitHandler;
|
|
9
|
+
worker: Worker;
|
|
10
|
+
constructor(execArgv: any, onMessage: MessageHandler, onError: ErrorHandler, onExit: ExitHandler);
|
|
11
|
+
start(): Promise<void>;
|
|
12
|
+
stop(): Promise<void>;
|
|
13
|
+
handleMessage(data: WorkerMessage): void;
|
|
14
|
+
send(data: WorkerMessage): void;
|
|
15
|
+
}
|
|
@@ -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) {
|
package/lib/types.d.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Diagnostic } from '@atlaspack/diagnostic';
|
|
2
|
+
import type { FilePath } from '@atlaspack/types-internal';
|
|
3
|
+
export type LocationCallRequest = {
|
|
4
|
+
args: ReadonlyArray<unknown>;
|
|
5
|
+
location: string;
|
|
6
|
+
method?: string;
|
|
7
|
+
};
|
|
8
|
+
export type HandleCallRequest = {
|
|
9
|
+
args: ReadonlyArray<unknown>;
|
|
10
|
+
handle: number;
|
|
11
|
+
};
|
|
12
|
+
export type CallRequest = LocationCallRequest | HandleCallRequest;
|
|
13
|
+
export type WorkerRequest = {
|
|
14
|
+
args: ReadonlyArray<any>;
|
|
15
|
+
awaitResponse?: boolean;
|
|
16
|
+
child?: number | null | undefined;
|
|
17
|
+
idx?: number;
|
|
18
|
+
location?: FilePath;
|
|
19
|
+
method?: string | null | undefined;
|
|
20
|
+
type: 'request';
|
|
21
|
+
handle?: number;
|
|
22
|
+
};
|
|
23
|
+
export type WorkerDataResponse = {
|
|
24
|
+
idx?: number;
|
|
25
|
+
child?: number;
|
|
26
|
+
type: 'response';
|
|
27
|
+
contentType: 'data';
|
|
28
|
+
content: string;
|
|
29
|
+
};
|
|
30
|
+
export type WorkerErrorResponse = {
|
|
31
|
+
idx?: number;
|
|
32
|
+
child?: number;
|
|
33
|
+
type: 'response';
|
|
34
|
+
contentType: 'error';
|
|
35
|
+
content: Diagnostic | Array<Diagnostic>;
|
|
36
|
+
};
|
|
37
|
+
export type WorkerResponse = WorkerDataResponse | WorkerErrorResponse;
|
|
38
|
+
export type WorkerMessage = WorkerRequest | WorkerResponse;
|
|
39
|
+
export type MessageHandler = (data: WorkerMessage) => void;
|
|
40
|
+
export type ErrorHandler = (err: Error) => void;
|
|
41
|
+
export type ExitHandler = (code: number) => void;
|
|
42
|
+
export interface WorkerImpl {
|
|
43
|
+
constructor(execArgv: any, onMessage: MessageHandler, onError: ErrorHandler, onExit: ExitHandler): void;
|
|
44
|
+
start(): Promise<void>;
|
|
45
|
+
stop(): Promise<void>;
|
|
46
|
+
send(data: WorkerMessage): void;
|
|
47
|
+
}
|
|
48
|
+
export interface ChildImpl {
|
|
49
|
+
constructor(onMessage: MessageHandler, onExit: ExitHandler): void;
|
|
50
|
+
send(data: WorkerMessage): void;
|
|
51
|
+
}
|
|
52
|
+
export type BackendType = 'threads' | 'process' | 'web';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ChildImpl, MessageHandler, ExitHandler, WorkerMessage } from '../types';
|
|
2
|
+
export default class WebChild implements ChildImpl {
|
|
3
|
+
onMessage: MessageHandler;
|
|
4
|
+
onExit: ExitHandler;
|
|
5
|
+
constructor(onMessage: MessageHandler, onExit: ExitHandler);
|
|
6
|
+
handleMessage(data: WorkerMessage): void;
|
|
7
|
+
send(data: WorkerMessage): void;
|
|
8
|
+
}
|
package/lib/web/WebChild.js
CHANGED
|
@@ -14,6 +14,10 @@ function _buildCache() {
|
|
|
14
14
|
var _child = require("../child");
|
|
15
15
|
var _childState = require("../childState");
|
|
16
16
|
/* eslint-env worker*/
|
|
17
|
+
|
|
18
|
+
// Type declarations for Web Worker environment
|
|
19
|
+
|
|
20
|
+
// @ts-expect-error TS2420
|
|
17
21
|
class WebChild {
|
|
18
22
|
constructor(onMessage, onExit) {
|
|
19
23
|
if (!(typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope)) {
|
|
@@ -28,7 +32,6 @@ class WebChild {
|
|
|
28
32
|
this.onExit(0);
|
|
29
33
|
self.postMessage('stopped');
|
|
30
34
|
}
|
|
31
|
-
// $FlowFixMe assume WorkerMessage as data
|
|
32
35
|
this.handleMessage(data);
|
|
33
36
|
});
|
|
34
37
|
self.postMessage('online');
|
|
@@ -40,5 +43,7 @@ class WebChild {
|
|
|
40
43
|
self.postMessage((0, _buildCache().prepareForSerialization)(data));
|
|
41
44
|
}
|
|
42
45
|
}
|
|
46
|
+
|
|
47
|
+
// @ts-expect-error TS2345
|
|
43
48
|
exports.default = WebChild;
|
|
44
49
|
(0, _childState.setChild)(new _child.Child(WebChild));
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { WorkerImpl, MessageHandler, ErrorHandler, ExitHandler, WorkerMessage } from '../types';
|
|
2
|
+
export declare let WORKER_PATH: import("url").URL;
|
|
3
|
+
export default class WebWorker implements WorkerImpl {
|
|
4
|
+
execArgv: any;
|
|
5
|
+
onMessage: MessageHandler;
|
|
6
|
+
onError: ErrorHandler;
|
|
7
|
+
onExit: ExitHandler;
|
|
8
|
+
worker: Worker;
|
|
9
|
+
stopping: Promise<undefined> | null | undefined;
|
|
10
|
+
constructor(execArgv: any, onMessage: MessageHandler, onError: ErrorHandler, onExit: ExitHandler);
|
|
11
|
+
start(): Promise<void>;
|
|
12
|
+
stop(): Promise<void>;
|
|
13
|
+
handleMessage(data: WorkerMessage): void;
|
|
14
|
+
send(data: WorkerMessage): void;
|
|
15
|
+
}
|
package/lib/web/WebWorker.js
CHANGED
|
@@ -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 _buildCache() {
|
|
8
8
|
const data = require("@atlaspack/build-cache");
|
|
9
9
|
_buildCache = function () {
|
|
@@ -19,7 +19,18 @@ function _utils() {
|
|
|
19
19
|
return data;
|
|
20
20
|
}
|
|
21
21
|
let id = 0;
|
|
22
|
+
|
|
23
|
+
// @ts-expect-error This is actually a module
|
|
24
|
+
let WORKER_PATH = exports.WORKER_PATH = new URL('./WebChild.js', import.meta.url);
|
|
25
|
+
if (process.env.ATLASPACK_REGISTER_USE_SRC === 'true') {
|
|
26
|
+
// @ts-expect-error This is actually a module
|
|
27
|
+
exports.WORKER_PATH = WORKER_PATH = new URL('./WebChild.ts', import.meta.url);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// @ts-expect-error TS2420
|
|
22
31
|
class WebWorker {
|
|
32
|
+
// @ts-expect-error TS2564
|
|
33
|
+
|
|
23
34
|
constructor(execArgv, onMessage, onError, onExit) {
|
|
24
35
|
this.execArgv = execArgv;
|
|
25
36
|
this.onMessage = onMessage;
|
|
@@ -27,7 +38,7 @@ class WebWorker {
|
|
|
27
38
|
this.onExit = onExit;
|
|
28
39
|
}
|
|
29
40
|
start() {
|
|
30
|
-
//
|
|
41
|
+
// @ts-expect-error TS1470
|
|
31
42
|
this.worker = new Worker(new URL('./WebChild.js', import.meta.url), {
|
|
32
43
|
name: `Parcel Worker ${id++}`,
|
|
33
44
|
type: 'module'
|
|
@@ -36,25 +47,28 @@ class WebWorker {
|
|
|
36
47
|
deferred,
|
|
37
48
|
promise
|
|
38
49
|
} = (0, _utils().makeDeferredWithPromise)();
|
|
50
|
+
|
|
51
|
+
// @ts-expect-error TS7031
|
|
39
52
|
this.worker.onmessage = ({
|
|
40
53
|
data
|
|
41
54
|
}) => {
|
|
42
55
|
if (data === 'online') {
|
|
56
|
+
// @ts-expect-error TS2554
|
|
43
57
|
deferred.resolve();
|
|
44
58
|
return;
|
|
45
59
|
}
|
|
46
|
-
|
|
47
|
-
// $FlowFixMe assume WorkerMessage as data
|
|
48
60
|
this.handleMessage(data);
|
|
49
61
|
};
|
|
50
62
|
this.worker.onerror = this.onError;
|
|
51
63
|
// Web workers can't crash or intentionally stop on their own, apart from stop() below
|
|
52
64
|
// this.worker.on('exit', this.onExit);
|
|
53
65
|
|
|
66
|
+
// @ts-expect-error TS2322
|
|
54
67
|
return promise;
|
|
55
68
|
}
|
|
56
69
|
stop() {
|
|
57
70
|
if (!this.stopping) {
|
|
71
|
+
// @ts-expect-error TS2322
|
|
58
72
|
this.stopping = (async () => {
|
|
59
73
|
this.worker.postMessage('stop');
|
|
60
74
|
let {
|
|
@@ -65,6 +79,7 @@ class WebWorker {
|
|
|
65
79
|
data
|
|
66
80
|
}) => {
|
|
67
81
|
if (data === 'stopped') {
|
|
82
|
+
// @ts-expect-error TS2554
|
|
68
83
|
deferred.resolve();
|
|
69
84
|
}
|
|
70
85
|
});
|
|
@@ -73,6 +88,7 @@ class WebWorker {
|
|
|
73
88
|
this.onExit(0);
|
|
74
89
|
})();
|
|
75
90
|
}
|
|
91
|
+
// @ts-expect-error TS2322
|
|
76
92
|
return this.stopping;
|
|
77
93
|
}
|
|
78
94
|
handleMessage(data) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@atlaspack/workers",
|
|
3
|
-
"version": "2.14.5-canary.
|
|
3
|
+
"version": "2.14.5-canary.139+d2fd84977",
|
|
4
4
|
"description": "Blazing fast, zero configuration web application bundler",
|
|
5
5
|
"license": "(MIT OR Apache-2.0)",
|
|
6
6
|
"publishConfig": {
|
|
@@ -10,19 +10,19 @@
|
|
|
10
10
|
"type": "git",
|
|
11
11
|
"url": "https://github.com/atlassian-labs/atlaspack.git"
|
|
12
12
|
},
|
|
13
|
-
"main": "lib/index.js",
|
|
14
|
-
"source": "src/index.
|
|
15
|
-
"types": "index.d.ts",
|
|
13
|
+
"main": "./lib/index.js",
|
|
14
|
+
"source": "./src/index.ts",
|
|
15
|
+
"types": "./lib/index.d.ts",
|
|
16
16
|
"engines": {
|
|
17
17
|
"node": ">= 16.0.0"
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@atlaspack/build-cache": "2.13.3-canary.
|
|
21
|
-
"@atlaspack/diagnostic": "2.14.1-canary.
|
|
22
|
-
"@atlaspack/logger": "2.14.5-canary.
|
|
23
|
-
"@atlaspack/profiler": "2.14.1-canary.
|
|
24
|
-
"@atlaspack/types-internal": "2.14.1-canary.
|
|
25
|
-
"@atlaspack/utils": "2.14.5-canary.
|
|
20
|
+
"@atlaspack/build-cache": "2.13.3-canary.207+d2fd84977",
|
|
21
|
+
"@atlaspack/diagnostic": "2.14.1-canary.207+d2fd84977",
|
|
22
|
+
"@atlaspack/logger": "2.14.5-canary.139+d2fd84977",
|
|
23
|
+
"@atlaspack/profiler": "2.14.1-canary.207+d2fd84977",
|
|
24
|
+
"@atlaspack/types-internal": "2.14.1-canary.207+d2fd84977",
|
|
25
|
+
"@atlaspack/utils": "2.14.5-canary.139+d2fd84977",
|
|
26
26
|
"nullthrows": "^1.1.1"
|
|
27
27
|
},
|
|
28
28
|
"browser": {
|
|
@@ -30,5 +30,8 @@
|
|
|
30
30
|
"./src/threads/ThreadsWorker.js": false
|
|
31
31
|
},
|
|
32
32
|
"type": "commonjs",
|
|
33
|
-
"
|
|
34
|
-
|
|
33
|
+
"scripts": {
|
|
34
|
+
"check-ts": "tsc --emitDeclarationOnly --rootDir src"
|
|
35
|
+
},
|
|
36
|
+
"gitHead": "d2fd849770fe6305e9c694bd97b1bd905abd9d94"
|
|
37
|
+
}
|
|
@@ -1,24 +1,21 @@
|
|
|
1
|
-
// @flow strict-local
|
|
2
1
|
import {registerSerializableClass} from '@atlaspack/build-cache';
|
|
3
|
-
// $FlowFixMe
|
|
4
2
|
import packageJson from '../package.json';
|
|
5
3
|
|
|
6
4
|
let HANDLE_ID = 0;
|
|
7
|
-
// $FlowFixMe
|
|
8
5
|
export type HandleFunction = (...args: Array<any>) => any;
|
|
9
6
|
|
|
10
|
-
type HandleOpts = {
|
|
11
|
-
fn?: HandleFunction
|
|
12
|
-
childId?:
|
|
13
|
-
id?: number
|
|
14
|
-
|
|
7
|
+
type HandleOpts = {
|
|
8
|
+
fn?: HandleFunction;
|
|
9
|
+
childId?: number | null | undefined;
|
|
10
|
+
id?: number;
|
|
11
|
+
};
|
|
15
12
|
|
|
16
13
|
const handleById: Map<number, Handle> = new Map();
|
|
17
14
|
|
|
18
15
|
export default class Handle {
|
|
19
16
|
id: number;
|
|
20
|
-
childId:
|
|
21
|
-
fn:
|
|
17
|
+
childId: number | null | undefined;
|
|
18
|
+
fn: HandleFunction | null | undefined;
|
|
22
19
|
|
|
23
20
|
constructor(opts: HandleOpts) {
|
|
24
21
|
this.id = opts.id ?? ++HANDLE_ID;
|
|
@@ -31,7 +28,10 @@ export default class Handle {
|
|
|
31
28
|
handleById.delete(this.id);
|
|
32
29
|
}
|
|
33
30
|
|
|
34
|
-
serialize(): {
|
|
31
|
+
serialize(): {
|
|
32
|
+
childId: number | null | undefined;
|
|
33
|
+
id: number;
|
|
34
|
+
} {
|
|
35
35
|
return {
|
|
36
36
|
id: this.id,
|
|
37
37
|
childId: this.childId,
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
// @flow
|
|
2
|
-
|
|
3
1
|
import type {FilePath} from '@atlaspack/types-internal';
|
|
4
2
|
import type {BackendType, WorkerImpl, WorkerMessage} from './types';
|
|
5
3
|
import type {SharedReference} from './WorkerFarm';
|
|
@@ -9,33 +7,34 @@ import EventEmitter from 'events';
|
|
|
9
7
|
import ThrowableDiagnostic from '@atlaspack/diagnostic';
|
|
10
8
|
import {getWorkerBackend} from './backend';
|
|
11
9
|
|
|
12
|
-
export type WorkerCall = {
|
|
13
|
-
method?: string
|
|
14
|
-
handle?: number
|
|
15
|
-
args:
|
|
16
|
-
retries: number
|
|
17
|
-
skipReadyCheck?: boolean
|
|
18
|
-
resolve: (result: Promise<any> | any) => void
|
|
19
|
-
reject: (error
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
type WorkerOpts = {
|
|
23
|
-
forcedKillTime: number
|
|
24
|
-
backend: BackendType
|
|
25
|
-
shouldPatchConsole?: boolean
|
|
26
|
-
shouldTrace?: boolean
|
|
27
|
-
sharedReferences:
|
|
28
|
-
|
|
10
|
+
export type WorkerCall = {
|
|
11
|
+
method?: string;
|
|
12
|
+
handle?: number;
|
|
13
|
+
args: ReadonlyArray<any>;
|
|
14
|
+
retries: number;
|
|
15
|
+
skipReadyCheck?: boolean;
|
|
16
|
+
resolve: (result: Promise<any> | any) => void;
|
|
17
|
+
reject: (error?: any) => void;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
type WorkerOpts = {
|
|
21
|
+
forcedKillTime: number;
|
|
22
|
+
backend: BackendType;
|
|
23
|
+
shouldPatchConsole?: boolean;
|
|
24
|
+
shouldTrace?: boolean;
|
|
25
|
+
sharedReferences: ReadonlyMap<SharedReference, unknown>;
|
|
26
|
+
};
|
|
29
27
|
|
|
30
28
|
let WORKER_ID = 0;
|
|
31
29
|
export default class Worker extends EventEmitter {
|
|
32
|
-
|
|
30
|
+
readonly options: WorkerOpts;
|
|
31
|
+
// @ts-expect-error TS2564
|
|
33
32
|
worker: WorkerImpl;
|
|
34
33
|
id: number = WORKER_ID++;
|
|
35
34
|
sentSharedReferences: Set<SharedReference> = new Set();
|
|
36
35
|
|
|
37
36
|
calls: Map<number, WorkerCall> = new Map();
|
|
38
|
-
exitCode:
|
|
37
|
+
exitCode: number | null | undefined = null;
|
|
39
38
|
callId: number = 0;
|
|
40
39
|
|
|
41
40
|
ready: boolean = false;
|
|
@@ -48,7 +47,7 @@ export default class Worker extends EventEmitter {
|
|
|
48
47
|
}
|
|
49
48
|
|
|
50
49
|
async fork(forkModule: FilePath) {
|
|
51
|
-
let filteredArgs = [];
|
|
50
|
+
let filteredArgs: Array<string> | Array<any | string> = [];
|
|
52
51
|
if (process.execArgv) {
|
|
53
52
|
filteredArgs = process.execArgv.filter(
|
|
54
53
|
(v) =>
|
|
@@ -94,13 +93,13 @@ export default class Worker extends EventEmitter {
|
|
|
94
93
|
}
|
|
95
94
|
}
|
|
96
95
|
|
|
97
|
-
let onMessage = (data) => this.receive(data);
|
|
98
|
-
let onExit = (code) => {
|
|
96
|
+
let onMessage = (data: WorkerMessage) => this.receive(data);
|
|
97
|
+
let onExit = (code: number) => {
|
|
99
98
|
this.exitCode = code;
|
|
100
99
|
this.emit('exit', code);
|
|
101
100
|
};
|
|
102
101
|
|
|
103
|
-
let onError = (err) => {
|
|
102
|
+
let onError = (err: Error) => {
|
|
104
103
|
this.emit('error', err);
|
|
105
104
|
};
|
|
106
105
|
|
|
@@ -108,22 +107,27 @@ export default class Worker extends EventEmitter {
|
|
|
108
107
|
this.worker = new WorkerBackend(filteredArgs, onMessage, onError, onExit);
|
|
109
108
|
await this.worker.start();
|
|
110
109
|
|
|
111
|
-
await new Promise(
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
110
|
+
await new Promise(
|
|
111
|
+
(
|
|
112
|
+
resolve: (result: Promise<any> | any) => void,
|
|
113
|
+
reject: (error?: any) => void,
|
|
114
|
+
) => {
|
|
115
|
+
this.call({
|
|
116
|
+
method: 'childInit',
|
|
117
|
+
args: [
|
|
118
|
+
forkModule,
|
|
119
|
+
{
|
|
120
|
+
shouldPatchConsole: !!this.options.shouldPatchConsole,
|
|
121
|
+
shouldTrace: !!this.options.shouldTrace,
|
|
122
|
+
},
|
|
123
|
+
],
|
|
124
|
+
retries: 0,
|
|
125
|
+
skipReadyCheck: true,
|
|
126
|
+
resolve,
|
|
127
|
+
reject,
|
|
128
|
+
});
|
|
129
|
+
},
|
|
130
|
+
);
|
|
127
131
|
|
|
128
132
|
let sharedRefs = this.options.sharedReferences;
|
|
129
133
|
let refsShared = new Set();
|
|
@@ -131,8 +135,9 @@ export default class Worker extends EventEmitter {
|
|
|
131
135
|
while (refsShared.size < sharedRefs.size) {
|
|
132
136
|
await Promise.all(
|
|
133
137
|
[...sharedRefs]
|
|
134
|
-
|
|
135
|
-
.
|
|
138
|
+
// @ts-expect-error TS2769
|
|
139
|
+
.filter(([ref]: [any]) => !refsShared.has(ref))
|
|
140
|
+
.map(async ([ref, value]: [any, any]) => {
|
|
136
141
|
await this.sendSharedReference(ref, value);
|
|
137
142
|
refsShared.add(ref);
|
|
138
143
|
}),
|
|
@@ -143,18 +148,23 @@ export default class Worker extends EventEmitter {
|
|
|
143
148
|
this.emit('ready');
|
|
144
149
|
}
|
|
145
150
|
|
|
146
|
-
sendSharedReference(ref: SharedReference, value:
|
|
151
|
+
sendSharedReference(ref: SharedReference, value: unknown): Promise<any> {
|
|
147
152
|
this.sentSharedReferences.add(ref);
|
|
148
|
-
return new Promise(
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
153
|
+
return new Promise(
|
|
154
|
+
(
|
|
155
|
+
resolve: (result: Promise<any> | any) => void,
|
|
156
|
+
reject: (error?: any) => void,
|
|
157
|
+
) => {
|
|
158
|
+
this.call({
|
|
159
|
+
method: 'createSharedReference',
|
|
160
|
+
args: [ref, value],
|
|
161
|
+
resolve,
|
|
162
|
+
reject,
|
|
163
|
+
retries: 0,
|
|
164
|
+
skipReadyCheck: true,
|
|
165
|
+
});
|
|
166
|
+
},
|
|
167
|
+
);
|
|
158
168
|
}
|
|
159
169
|
|
|
160
170
|
send(data: WorkerMessage): void {
|
|
@@ -179,8 +189,10 @@ export default class Worker extends EventEmitter {
|
|
|
179
189
|
};
|
|
180
190
|
|
|
181
191
|
if (this.ready || call.skipReadyCheck === true) {
|
|
192
|
+
// @ts-expect-error TS2345
|
|
182
193
|
this.send(msg);
|
|
183
194
|
} else {
|
|
195
|
+
// @ts-expect-error TS2345
|
|
184
196
|
this.once('ready', () => this.send(msg));
|
|
185
197
|
}
|
|
186
198
|
}
|