@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
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright (c) 2024 Atlassian US., Inc.
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// eslint-disable-next-line import/no-extraneous-dependencies
|
|
2
2
|
import type {FilePath} from '@atlaspack/types';
|
|
3
|
+
import type EventEmitter from 'events';
|
|
3
4
|
|
|
4
5
|
type BackendType = 'process' | 'threads';
|
|
5
6
|
|
|
@@ -15,10 +16,101 @@ export type FarmOptions = {
|
|
|
15
16
|
shouldTrace?: boolean;
|
|
16
17
|
};
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
export class Bus extends EventEmitter {
|
|
20
|
+
emit(event: string, ...args: Array<any>): boolean;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const bus: Bus;
|
|
20
24
|
|
|
25
|
+
export declare class WorkerFarm {
|
|
26
|
+
ending: boolean;
|
|
27
|
+
workerApi: {
|
|
28
|
+
callChild: (
|
|
29
|
+
childId: number,
|
|
30
|
+
request: HandleCallRequest,
|
|
31
|
+
) => Promise<unknown>;
|
|
32
|
+
callMaster: (
|
|
33
|
+
request: CallRequest,
|
|
34
|
+
awaitResponse?: boolean | null | undefined,
|
|
35
|
+
) => Promise<unknown>;
|
|
36
|
+
createReverseHandle: (fn: HandleFunction) => Handle;
|
|
37
|
+
getSharedReference: (ref: SharedReference) => unknown;
|
|
38
|
+
resolveSharedReference: (value: unknown) => undefined | SharedReference;
|
|
39
|
+
runHandle: (handle: Handle, args: Array<any>) => Promise<unknown>;
|
|
40
|
+
};
|
|
41
|
+
constructor(options: Partial<FarmOptions>);
|
|
42
|
+
createSharedReference(
|
|
43
|
+
value: unknown,
|
|
44
|
+
isCacheable?: boolean,
|
|
45
|
+
): {
|
|
46
|
+
ref: SharedReference;
|
|
47
|
+
dispose(): Promise<unknown>;
|
|
48
|
+
};
|
|
49
|
+
startProfile(): Promise<void>;
|
|
50
|
+
endProfile(): Promise<void>;
|
|
51
|
+
takeHeapSnapshot(): Promise<void>;
|
|
52
|
+
createHandle(method: string, useMainThread?: boolean): HandleFunction;
|
|
53
|
+
createReverseHandle(fn: HandleFunction): Handle;
|
|
54
|
+
callAllWorkers(method: string, args: Array<any>): Promise<void>;
|
|
55
|
+
static getWorkerApi(): {
|
|
56
|
+
callMaster: (
|
|
57
|
+
request: CallRequest,
|
|
58
|
+
awaitResponse?: boolean | null | undefined,
|
|
59
|
+
) => Promise<unknown>;
|
|
60
|
+
createReverseHandle: (fn: (...args: Array<any>) => unknown) => Handle;
|
|
61
|
+
getSharedReference: (ref: SharedReference) => unknown;
|
|
62
|
+
resolveSharedReference: (value: unknown) => undefined | SharedReference;
|
|
63
|
+
runHandle: (handle: Handle, args: Array<any>) => Promise<unknown>;
|
|
64
|
+
};
|
|
21
65
|
end(): Promise<void>;
|
|
66
|
+
static isWorker(): boolean;
|
|
22
67
|
}
|
|
23
68
|
|
|
24
69
|
export default WorkerFarm;
|
|
70
|
+
|
|
71
|
+
export type SharedReference = number;
|
|
72
|
+
|
|
73
|
+
export type WorkerApi = {
|
|
74
|
+
callMaster(
|
|
75
|
+
arg1: CallRequest,
|
|
76
|
+
arg2?: boolean | null | undefined,
|
|
77
|
+
): Promise<unknown>;
|
|
78
|
+
createReverseHandle(fn: HandleFunction): Handle;
|
|
79
|
+
getSharedReference(ref: SharedReference): unknown;
|
|
80
|
+
resolveSharedReference(value: unknown): SharedReference | null | undefined;
|
|
81
|
+
callChild?: (childId: number, request: HandleCallRequest) => Promise<unknown>;
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export type HandleFunction = (...args: Array<any>) => any;
|
|
85
|
+
|
|
86
|
+
export type LocationCallRequest = {
|
|
87
|
+
args: ReadonlyArray<unknown>;
|
|
88
|
+
location: string;
|
|
89
|
+
method?: string;
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export type HandleCallRequest = {
|
|
93
|
+
args: ReadonlyArray<unknown>;
|
|
94
|
+
handle: number;
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
export type CallRequest = LocationCallRequest | HandleCallRequest;
|
|
98
|
+
|
|
99
|
+
type HandleOpts = {
|
|
100
|
+
fn?: HandleFunction;
|
|
101
|
+
childId?: number | null | undefined;
|
|
102
|
+
id?: number;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
export declare class Handle {
|
|
106
|
+
id: number;
|
|
107
|
+
childId: number | null | undefined;
|
|
108
|
+
fn: HandleFunction | null | undefined;
|
|
109
|
+
constructor(opts: HandleOpts);
|
|
110
|
+
dispose(): void;
|
|
111
|
+
serialize(): {
|
|
112
|
+
childId: number | null | undefined;
|
|
113
|
+
id: number;
|
|
114
|
+
};
|
|
115
|
+
static deserialize(opts: HandleOpts): Handle;
|
|
116
|
+
}
|
package/lib/Handle.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export type HandleFunction = (...args: Array<any>) => any;
|
|
2
|
+
type HandleOpts = {
|
|
3
|
+
fn?: HandleFunction;
|
|
4
|
+
childId?: number | null | undefined;
|
|
5
|
+
id?: number;
|
|
6
|
+
};
|
|
7
|
+
export default class Handle {
|
|
8
|
+
id: number;
|
|
9
|
+
childId: number | null | undefined;
|
|
10
|
+
fn: HandleFunction | null | undefined;
|
|
11
|
+
constructor(opts: HandleOpts);
|
|
12
|
+
dispose(): void;
|
|
13
|
+
serialize(): {
|
|
14
|
+
childId: number | null | undefined;
|
|
15
|
+
id: number;
|
|
16
|
+
};
|
|
17
|
+
static deserialize(opts: HandleOpts): Handle;
|
|
18
|
+
}
|
|
19
|
+
export {};
|
package/lib/Handle.js
CHANGED
|
@@ -13,10 +13,7 @@ function _buildCache() {
|
|
|
13
13
|
}
|
|
14
14
|
var _package = _interopRequireDefault(require("../package.json"));
|
|
15
15
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
|
-
// $FlowFixMe
|
|
17
16
|
let HANDLE_ID = 0;
|
|
18
|
-
// $FlowFixMe
|
|
19
|
-
|
|
20
17
|
const handleById = new Map();
|
|
21
18
|
class Handle {
|
|
22
19
|
constructor(opts) {
|
package/lib/Worker.d.ts
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { FilePath } from '@atlaspack/types-internal';
|
|
2
|
+
import type { BackendType, WorkerImpl, WorkerMessage } from './types';
|
|
3
|
+
import type { SharedReference } from './WorkerFarm';
|
|
4
|
+
import EventEmitter from 'events';
|
|
5
|
+
export type WorkerCall = {
|
|
6
|
+
method?: string;
|
|
7
|
+
handle?: number;
|
|
8
|
+
args: ReadonlyArray<any>;
|
|
9
|
+
retries: number;
|
|
10
|
+
skipReadyCheck?: boolean;
|
|
11
|
+
resolve: (result: Promise<any> | any) => void;
|
|
12
|
+
reject: (error?: any) => void;
|
|
13
|
+
};
|
|
14
|
+
type WorkerOpts = {
|
|
15
|
+
forcedKillTime: number;
|
|
16
|
+
backend: BackendType;
|
|
17
|
+
shouldPatchConsole?: boolean;
|
|
18
|
+
shouldTrace?: boolean;
|
|
19
|
+
sharedReferences: ReadonlyMap<SharedReference, unknown>;
|
|
20
|
+
};
|
|
21
|
+
export default class Worker extends EventEmitter {
|
|
22
|
+
readonly options: WorkerOpts;
|
|
23
|
+
worker: WorkerImpl;
|
|
24
|
+
id: number;
|
|
25
|
+
sentSharedReferences: Set<SharedReference>;
|
|
26
|
+
calls: Map<number, WorkerCall>;
|
|
27
|
+
exitCode: number | null | undefined;
|
|
28
|
+
callId: number;
|
|
29
|
+
ready: boolean;
|
|
30
|
+
stopped: boolean;
|
|
31
|
+
isStopping: boolean;
|
|
32
|
+
constructor(options: WorkerOpts);
|
|
33
|
+
fork(forkModule: FilePath): Promise<void>;
|
|
34
|
+
sendSharedReference(ref: SharedReference, value: unknown): Promise<any>;
|
|
35
|
+
send(data: WorkerMessage): void;
|
|
36
|
+
call(call: WorkerCall): void;
|
|
37
|
+
receive(message: WorkerMessage): void;
|
|
38
|
+
stop(): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
export {};
|
package/lib/Worker.js
CHANGED
|
@@ -29,6 +29,8 @@ var _backend = require("./backend");
|
|
|
29
29
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
30
30
|
let WORKER_ID = 0;
|
|
31
31
|
class Worker extends _events().default {
|
|
32
|
+
// @ts-expect-error TS2564
|
|
33
|
+
|
|
32
34
|
id = WORKER_ID++;
|
|
33
35
|
sentSharedReferences = new Set();
|
|
34
36
|
calls = new Map();
|
|
@@ -105,7 +107,9 @@ class Worker extends _events().default {
|
|
|
105
107
|
let refsShared = new Set();
|
|
106
108
|
// in case more refs are created while initial refs are sending
|
|
107
109
|
while (refsShared.size < sharedRefs.size) {
|
|
108
|
-
await Promise.all([...sharedRefs]
|
|
110
|
+
await Promise.all([...sharedRefs]
|
|
111
|
+
// @ts-expect-error TS2769
|
|
112
|
+
.filter(([ref]) => !refsShared.has(ref)).map(async ([ref, value]) => {
|
|
109
113
|
await this.sendSharedReference(ref, value);
|
|
110
114
|
refsShared.add(ref);
|
|
111
115
|
}));
|
|
@@ -144,8 +148,10 @@ class Worker extends _events().default {
|
|
|
144
148
|
args: call.args
|
|
145
149
|
};
|
|
146
150
|
if (this.ready || call.skipReadyCheck === true) {
|
|
151
|
+
// @ts-expect-error TS2345
|
|
147
152
|
this.send(msg);
|
|
148
153
|
} else {
|
|
154
|
+
// @ts-expect-error TS2345
|
|
149
155
|
this.once('ready', () => this.send(msg));
|
|
150
156
|
}
|
|
151
157
|
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import type { ErrorWithCode, FilePath } from '@atlaspack/types-internal';
|
|
2
|
+
import type { CallRequest, HandleCallRequest, WorkerRequest, BackendType } from './types';
|
|
3
|
+
import type { HandleFunction } from './Handle';
|
|
4
|
+
import EventEmitter from 'events';
|
|
5
|
+
import Worker, { WorkerCall } from './Worker';
|
|
6
|
+
import Handle from './Handle';
|
|
7
|
+
import { SamplingProfiler } from '@atlaspack/profiler';
|
|
8
|
+
export type SharedReference = number;
|
|
9
|
+
export type FarmOptions = {
|
|
10
|
+
maxConcurrentWorkers: number;
|
|
11
|
+
maxConcurrentCallsPerWorker: number;
|
|
12
|
+
forcedKillTime: number;
|
|
13
|
+
useLocalWorker: boolean;
|
|
14
|
+
warmWorkers: boolean;
|
|
15
|
+
workerPath?: FilePath;
|
|
16
|
+
backend: BackendType;
|
|
17
|
+
shouldPatchConsole?: boolean;
|
|
18
|
+
shouldTrace?: boolean;
|
|
19
|
+
};
|
|
20
|
+
type WorkerModule = {
|
|
21
|
+
readonly [key: string]: (...args: Array<unknown>) => Promise<unknown>;
|
|
22
|
+
};
|
|
23
|
+
export type WorkerApi = {
|
|
24
|
+
callMaster(arg1: CallRequest, arg2?: boolean | null | undefined): Promise<unknown>;
|
|
25
|
+
createReverseHandle(fn: HandleFunction): Handle;
|
|
26
|
+
getSharedReference(ref: SharedReference): unknown;
|
|
27
|
+
resolveSharedReference(value: unknown): SharedReference | null | undefined;
|
|
28
|
+
callChild?: (childId: number, request: HandleCallRequest) => Promise<unknown>;
|
|
29
|
+
};
|
|
30
|
+
export { Handle };
|
|
31
|
+
/**
|
|
32
|
+
* workerPath should always be defined inside farmOptions
|
|
33
|
+
*/
|
|
34
|
+
export default class WorkerFarm extends EventEmitter {
|
|
35
|
+
callQueue: Array<WorkerCall>;
|
|
36
|
+
ending: boolean;
|
|
37
|
+
localWorker: WorkerModule;
|
|
38
|
+
localWorkerInit: Promise<undefined> | null | undefined;
|
|
39
|
+
options: FarmOptions;
|
|
40
|
+
run: HandleFunction;
|
|
41
|
+
warmWorkers: number;
|
|
42
|
+
readyWorkers: number;
|
|
43
|
+
workers: Map<number, Worker>;
|
|
44
|
+
handles: Map<number, Handle>;
|
|
45
|
+
sharedReferences: Map<SharedReference, unknown>;
|
|
46
|
+
sharedReferencesByValue: Map<unknown, SharedReference>;
|
|
47
|
+
serializedSharedReferences: Map<SharedReference, ArrayBuffer | null | undefined>;
|
|
48
|
+
profiler: SamplingProfiler | null | undefined;
|
|
49
|
+
constructor(farmOptions?: Partial<FarmOptions>);
|
|
50
|
+
workerApi: {
|
|
51
|
+
callChild: (childId: number, request: HandleCallRequest) => Promise<unknown>;
|
|
52
|
+
callMaster: (request: CallRequest, awaitResponse?: boolean | null | undefined) => Promise<unknown>;
|
|
53
|
+
createReverseHandle: (fn: HandleFunction) => Handle;
|
|
54
|
+
getSharedReference: (ref: SharedReference) => unknown;
|
|
55
|
+
resolveSharedReference: (value: unknown) => undefined | SharedReference;
|
|
56
|
+
runHandle: (handle: Handle, args: Array<any>) => Promise<unknown>;
|
|
57
|
+
};
|
|
58
|
+
warmupWorker(method: string, args: Array<any>): void;
|
|
59
|
+
shouldStartRemoteWorkers(): boolean;
|
|
60
|
+
createHandle(method: string, useMainThread?: boolean): HandleFunction;
|
|
61
|
+
onError(error: ErrorWithCode, worker: Worker): undefined | Promise<undefined>;
|
|
62
|
+
startChild(): void;
|
|
63
|
+
stopWorker(worker: Worker): Promise<void>;
|
|
64
|
+
processQueue(): void;
|
|
65
|
+
callWorker(worker: Worker, call: WorkerCall): Promise<void>;
|
|
66
|
+
processRequest(data: {
|
|
67
|
+
location: FilePath;
|
|
68
|
+
} & Partial<WorkerRequest>, worker?: Worker): Promise<string | null | undefined>;
|
|
69
|
+
addCall(method: string, args: Array<any>): Promise<any>;
|
|
70
|
+
end(): Promise<void>;
|
|
71
|
+
startMaxWorkers(): void;
|
|
72
|
+
shouldUseRemoteWorkers(): boolean;
|
|
73
|
+
createReverseHandle(fn: HandleFunction): Handle;
|
|
74
|
+
createSharedReference(value: unknown, isCacheable?: boolean): {
|
|
75
|
+
ref: SharedReference;
|
|
76
|
+
dispose(): Promise<unknown>;
|
|
77
|
+
};
|
|
78
|
+
getSerializedSharedReference(ref: SharedReference): ArrayBuffer;
|
|
79
|
+
startProfile(): Promise<void>;
|
|
80
|
+
endProfile(): Promise<void>;
|
|
81
|
+
callAllWorkers(method: string, args: Array<any>): Promise<void>;
|
|
82
|
+
takeHeapSnapshot(): Promise<void>;
|
|
83
|
+
static getNumWorkers(): number;
|
|
84
|
+
static isWorker(): boolean;
|
|
85
|
+
static getWorkerApi(): {
|
|
86
|
+
callMaster: (request: CallRequest, awaitResponse?: boolean | null | undefined) => Promise<unknown>;
|
|
87
|
+
createReverseHandle: (fn: (...args: Array<any>) => unknown) => Handle;
|
|
88
|
+
getSharedReference: (ref: SharedReference) => unknown;
|
|
89
|
+
resolveSharedReference: (value: unknown) => undefined | SharedReference;
|
|
90
|
+
runHandle: (handle: Handle, args: Array<any>) => Promise<unknown>;
|
|
91
|
+
};
|
|
92
|
+
static getConcurrentCallsPerWorker(defaultValue?: number): number;
|
|
93
|
+
}
|
package/lib/WorkerFarm.js
CHANGED
|
@@ -108,9 +108,9 @@ class WorkerFarm extends _events().default {
|
|
|
108
108
|
if (!this.options.workerPath) {
|
|
109
109
|
throw new Error('Please provide a worker path!');
|
|
110
110
|
}
|
|
111
|
-
|
|
112
|
-
// $FlowFixMe
|
|
113
111
|
this.localWorker = require(this.options.workerPath);
|
|
112
|
+
|
|
113
|
+
// @ts-expect-error TS2322
|
|
114
114
|
this.localWorkerInit = this.localWorker.childInit != null ? this.localWorker.childInit() : null;
|
|
115
115
|
this.run = this.createHandle('run');
|
|
116
116
|
|
|
@@ -127,9 +127,9 @@ class WorkerFarm extends _events().default {
|
|
|
127
127
|
}
|
|
128
128
|
workerApi = {
|
|
129
129
|
callMaster: async (request, awaitResponse = true) => {
|
|
130
|
-
// $FlowFixMe
|
|
131
130
|
let result = await this.processRequest({
|
|
132
131
|
...request,
|
|
132
|
+
// @ts-expect-error TS2322
|
|
133
133
|
awaitResponse
|
|
134
134
|
});
|
|
135
135
|
return (0, _buildCache().deserialize)((0, _buildCache().serialize)(result));
|
|
@@ -203,6 +203,7 @@ class WorkerFarm extends _events().default {
|
|
|
203
203
|
onError(error, worker) {
|
|
204
204
|
// Handle ipc errors
|
|
205
205
|
if (error.code === 'ERR_IPC_CHANNEL_CLOSED') {
|
|
206
|
+
// @ts-expect-error TS2322
|
|
206
207
|
return this.stopWorker(worker);
|
|
207
208
|
} else {
|
|
208
209
|
_logger().default.error(error, '@atlaspack/workers');
|
|
@@ -261,6 +262,7 @@ class WorkerFarm extends _events().default {
|
|
|
261
262
|
continue;
|
|
262
263
|
}
|
|
263
264
|
if (worker.calls.size < this.options.maxConcurrentCallsPerWorker) {
|
|
265
|
+
// @ts-expect-error TS2345
|
|
264
266
|
this.callWorker(worker, this.callQueue.shift());
|
|
265
267
|
}
|
|
266
268
|
}
|
|
@@ -287,7 +289,7 @@ class WorkerFarm extends _events().default {
|
|
|
287
289
|
var _this$handles$get;
|
|
288
290
|
mod = (0, _nullthrows().default)((_this$handles$get = this.handles.get(handleId)) === null || _this$handles$get === void 0 ? void 0 : _this$handles$get.fn);
|
|
289
291
|
} else if (location) {
|
|
290
|
-
//
|
|
292
|
+
// @ts-expect-error TS2339
|
|
291
293
|
if (process.browser) {
|
|
292
294
|
if (location === '@atlaspack/workers/bus') {
|
|
293
295
|
mod = bus;
|
|
@@ -295,7 +297,6 @@ class WorkerFarm extends _events().default {
|
|
|
295
297
|
throw new Error('No dynamic require possible: ' + location);
|
|
296
298
|
}
|
|
297
299
|
} else {
|
|
298
|
-
// $FlowFixMe this must be dynamic
|
|
299
300
|
mod = require(location);
|
|
300
301
|
}
|
|
301
302
|
} else {
|
|
@@ -316,6 +317,7 @@ class WorkerFarm extends _events().default {
|
|
|
316
317
|
let result;
|
|
317
318
|
if (method == null) {
|
|
318
319
|
try {
|
|
320
|
+
// @ts-expect-error TS2488
|
|
319
321
|
result = responseFromContent(await mod(...args));
|
|
320
322
|
} catch (e) {
|
|
321
323
|
result = errorResponseFromError(e);
|
|
@@ -326,7 +328,6 @@ class WorkerFarm extends _events().default {
|
|
|
326
328
|
mod = mod.default;
|
|
327
329
|
}
|
|
328
330
|
try {
|
|
329
|
-
// $FlowFixMe
|
|
330
331
|
result = responseFromContent(await mod[method](...args));
|
|
331
332
|
} catch (e) {
|
|
332
333
|
result = errorResponseFromError(e);
|
|
@@ -435,8 +436,11 @@ class WorkerFarm extends _events().default {
|
|
|
435
436
|
// If the reference was created with the isCacheable option set to false,
|
|
436
437
|
// serializedSharedReferences will contain `null` as the value.
|
|
437
438
|
if (cached !== null) {
|
|
439
|
+
// @ts-expect-error TS2345
|
|
438
440
|
this.serializedSharedReferences.set(ref, buf);
|
|
439
441
|
}
|
|
442
|
+
|
|
443
|
+
// @ts-expect-error TS2322
|
|
440
444
|
return buf;
|
|
441
445
|
}
|
|
442
446
|
async startProfile() {
|
|
@@ -481,6 +485,7 @@ class WorkerFarm extends _events().default {
|
|
|
481
485
|
let filename = `profile-${getTimeId()}.trace`;
|
|
482
486
|
let stream = trace.pipe(_fs().default.createWriteStream(filename));
|
|
483
487
|
for (let profile of profiles) {
|
|
488
|
+
// @ts-expect-error TS2345
|
|
484
489
|
trace.addCPUProfile(names.shift(), profile);
|
|
485
490
|
}
|
|
486
491
|
trace.flush();
|
|
@@ -489,6 +494,7 @@ class WorkerFarm extends _events().default {
|
|
|
489
494
|
});
|
|
490
495
|
_logger().default.info({
|
|
491
496
|
origin: '@atlaspack/workers',
|
|
497
|
+
// @ts-expect-error TS2345
|
|
492
498
|
message: (0, _diagnostic().md)`Wrote profile to ${filename}`
|
|
493
499
|
});
|
|
494
500
|
}
|
|
@@ -523,6 +529,7 @@ class WorkerFarm extends _events().default {
|
|
|
523
529
|
})));
|
|
524
530
|
_logger().default.info({
|
|
525
531
|
origin: '@atlaspack/workers',
|
|
532
|
+
// @ts-expect-error TS2345
|
|
526
533
|
message: (0, _diagnostic().md)`Wrote heap snapshots to the following paths:\n${snapshotPaths.join('\n')}`
|
|
527
534
|
});
|
|
528
535
|
} catch {
|
|
@@ -543,7 +550,10 @@ class WorkerFarm extends _events().default {
|
|
|
543
550
|
return _childState.child.workerApi;
|
|
544
551
|
}
|
|
545
552
|
static getConcurrentCallsPerWorker(defaultValue = DEFAULT_MAX_CONCURRENT_CALLS) {
|
|
546
|
-
return
|
|
553
|
+
return (
|
|
554
|
+
// @ts-expect-error TS2345
|
|
555
|
+
parseInt(process.env.ATLASPACK_MAX_CONCURRENT_CALLS, 10) || defaultValue
|
|
556
|
+
);
|
|
547
557
|
}
|
|
548
558
|
}
|
|
549
559
|
exports.default = WorkerFarm;
|
package/lib/backend.d.ts
ADDED
package/lib/backend.js
CHANGED
|
@@ -5,8 +5,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.detectBackend = detectBackend;
|
|
7
7
|
exports.getWorkerBackend = getWorkerBackend;
|
|
8
|
+
// flow-to-ts helpers
|
|
9
|
+
|
|
10
|
+
// /flow-to-ts helpers
|
|
11
|
+
|
|
8
12
|
function detectBackend() {
|
|
9
|
-
//
|
|
13
|
+
// @ts-expect-error TS2339
|
|
10
14
|
if (process.browser) return 'web';
|
|
11
15
|
switch (process.env.ATLASPACK_WORKER_BACKEND) {
|
|
12
16
|
case 'threads':
|
package/lib/bus.d.ts
ADDED
package/lib/bus.js
CHANGED
|
@@ -17,7 +17,7 @@ class Bus extends _events().default {
|
|
|
17
17
|
emit(event, ...args) {
|
|
18
18
|
if (_childState.child) {
|
|
19
19
|
_childState.child.workerApi.callMaster({
|
|
20
|
-
//
|
|
20
|
+
// @ts-expect-error TS2339
|
|
21
21
|
location: process.browser ? '@atlaspack/workers/bus' : __filename,
|
|
22
22
|
method: 'emit',
|
|
23
23
|
args: [event, ...args]
|