@atlaspack/workers 2.14.21-typescript-e99c742e9.0 → 2.14.21-typescript-5b4d3ad41.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/lib/Handle.d.ts +19 -0
- package/lib/Handle.js +42 -0
- package/lib/Worker.d.ts +40 -0
- package/lib/Worker.js +194 -0
- package/lib/WorkerFarm.d.ts +93 -0
- package/lib/WorkerFarm.js +563 -0
- package/lib/backend.d.ts +4 -0
- package/lib/backend.js +38 -0
- package/lib/bus.d.ts +6 -0
- package/lib/bus.js +31 -0
- package/lib/child.d.ts +43 -0
- package/lib/child.js +291 -0
- package/lib/childState.d.ts +3 -0
- package/lib/childState.js +14 -0
- package/lib/cpuCount.d.ts +2 -0
- package/lib/cpuCount.js +83 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +75 -0
- package/lib/process/ProcessChild.d.ts +9 -0
- package/lib/process/ProcessChild.js +63 -0
- package/lib/process/ProcessWorker.d.ts +16 -0
- package/lib/process/ProcessWorker.js +90 -0
- package/lib/threads/ThreadsChild.d.ts +8 -0
- package/lib/threads/ThreadsChild.js +52 -0
- package/lib/threads/ThreadsWorker.d.ts +15 -0
- package/lib/threads/ThreadsWorker.js +69 -0
- package/lib/types.d.ts +52 -0
- package/lib/types.js +1 -0
- package/lib/web/WebChild.d.ts +8 -0
- package/lib/web/WebChild.js +49 -0
- package/lib/web/WebWorker.d.ts +15 -0
- package/lib/web/WebWorker.js +101 -0
- package/package.json +8 -8
package/lib/child.js
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Child = void 0;
|
|
7
|
+
function _assert() {
|
|
8
|
+
const data = _interopRequireDefault(require("assert"));
|
|
9
|
+
_assert = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _nullthrows() {
|
|
15
|
+
const data = _interopRequireDefault(require("nullthrows"));
|
|
16
|
+
_nullthrows = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
function _buildCache() {
|
|
22
|
+
const data = require("@atlaspack/build-cache");
|
|
23
|
+
_buildCache = function () {
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
function _logger() {
|
|
29
|
+
const data = _interopRequireWildcard(require("@atlaspack/logger"));
|
|
30
|
+
_logger = function () {
|
|
31
|
+
return data;
|
|
32
|
+
};
|
|
33
|
+
return data;
|
|
34
|
+
}
|
|
35
|
+
function _diagnostic() {
|
|
36
|
+
const data = _interopRequireWildcard(require("@atlaspack/diagnostic"));
|
|
37
|
+
_diagnostic = function () {
|
|
38
|
+
return data;
|
|
39
|
+
};
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
var _bus = _interopRequireDefault(require("./bus"));
|
|
43
|
+
function _profiler() {
|
|
44
|
+
const data = require("@atlaspack/profiler");
|
|
45
|
+
_profiler = function () {
|
|
46
|
+
return data;
|
|
47
|
+
};
|
|
48
|
+
return data;
|
|
49
|
+
}
|
|
50
|
+
var _Handle2 = _interopRequireDefault(require("./Handle"));
|
|
51
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
52
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
53
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
54
|
+
// flow-to-ts helpers
|
|
55
|
+
|
|
56
|
+
// /flow-to-ts helpers
|
|
57
|
+
// The import of './Handle' should really be imported eagerly (with @babel/plugin-transform-modules-commonjs's lazy mode).
|
|
58
|
+
const Handle = _Handle2.default;
|
|
59
|
+
class Child {
|
|
60
|
+
callQueue = [];
|
|
61
|
+
maxConcurrentCalls = 10;
|
|
62
|
+
responseId = 0;
|
|
63
|
+
responseQueue = new Map();
|
|
64
|
+
// @ts-expect-error TS2749
|
|
65
|
+
handles = new Map();
|
|
66
|
+
sharedReferences = new Map();
|
|
67
|
+
sharedReferencesByValue = new Map();
|
|
68
|
+
constructor(ChildBackend) {
|
|
69
|
+
this.child = new ChildBackend(m => {
|
|
70
|
+
this.messageListener(m);
|
|
71
|
+
}, () => this.handleEnd());
|
|
72
|
+
|
|
73
|
+
// Monitior all logging events inside this child process and forward to
|
|
74
|
+
// the main process via the bus.
|
|
75
|
+
this.loggerDisposable = _logger().default.onLog(event => {
|
|
76
|
+
_bus.default.emit('logEvent', event);
|
|
77
|
+
});
|
|
78
|
+
// .. and do the same for trace events
|
|
79
|
+
this.tracerDisposable = _profiler().tracer.onTrace(event => {
|
|
80
|
+
_bus.default.emit('traceEvent', event);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
workerApi = {
|
|
84
|
+
callMaster: (request, awaitResponse = true) => this.addCall(request, awaitResponse),
|
|
85
|
+
// @ts-expect-error TS2749
|
|
86
|
+
createReverseHandle: fn => this.createReverseHandle(fn),
|
|
87
|
+
// @ts-expect-error TS2749
|
|
88
|
+
runHandle: (handle, args) => this.workerApi.callMaster({
|
|
89
|
+
handle: handle.id,
|
|
90
|
+
args
|
|
91
|
+
}, true),
|
|
92
|
+
getSharedReference: ref => this.sharedReferences.get(ref),
|
|
93
|
+
resolveSharedReference: value => this.sharedReferencesByValue.get(value)
|
|
94
|
+
};
|
|
95
|
+
messageListener(message) {
|
|
96
|
+
if (message.type === 'response') {
|
|
97
|
+
return this.handleResponse(message);
|
|
98
|
+
} else if (message.type === 'request') {
|
|
99
|
+
return this.handleRequest(message);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
send(data) {
|
|
103
|
+
this.child.send(data);
|
|
104
|
+
}
|
|
105
|
+
async childInit(module, childId) {
|
|
106
|
+
this.module = require(module);
|
|
107
|
+
this.childId = childId;
|
|
108
|
+
if (this.module.childInit != null) {
|
|
109
|
+
await this.module.childInit();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
async handleRequest(data) {
|
|
113
|
+
let {
|
|
114
|
+
idx,
|
|
115
|
+
method,
|
|
116
|
+
args,
|
|
117
|
+
handle: handleId
|
|
118
|
+
} = data;
|
|
119
|
+
let child = (0, _nullthrows().default)(data.child);
|
|
120
|
+
const responseFromContent = content => ({
|
|
121
|
+
idx,
|
|
122
|
+
child,
|
|
123
|
+
type: 'response',
|
|
124
|
+
contentType: 'data',
|
|
125
|
+
content
|
|
126
|
+
});
|
|
127
|
+
const errorResponseFromError = e => ({
|
|
128
|
+
idx,
|
|
129
|
+
child,
|
|
130
|
+
type: 'response',
|
|
131
|
+
contentType: 'error',
|
|
132
|
+
content: (0, _diagnostic().anyToDiagnostic)(e)
|
|
133
|
+
});
|
|
134
|
+
let result;
|
|
135
|
+
if (handleId != null) {
|
|
136
|
+
try {
|
|
137
|
+
var _this$handles$get;
|
|
138
|
+
let fn = (0, _nullthrows().default)((_this$handles$get = this.handles.get(handleId)) === null || _this$handles$get === void 0 ? void 0 : _this$handles$get.fn);
|
|
139
|
+
result = responseFromContent(fn(...args));
|
|
140
|
+
} catch (e) {
|
|
141
|
+
result = errorResponseFromError(e);
|
|
142
|
+
}
|
|
143
|
+
} else if (method === 'childInit') {
|
|
144
|
+
try {
|
|
145
|
+
let [moduleName, childOptions] = args;
|
|
146
|
+
if (childOptions.shouldPatchConsole) {
|
|
147
|
+
(0, _logger().patchConsole)();
|
|
148
|
+
} else {
|
|
149
|
+
(0, _logger().unpatchConsole)();
|
|
150
|
+
}
|
|
151
|
+
if (childOptions.shouldTrace) {
|
|
152
|
+
_profiler().tracer.enable();
|
|
153
|
+
}
|
|
154
|
+
result = responseFromContent(await this.childInit(moduleName, child));
|
|
155
|
+
} catch (e) {
|
|
156
|
+
result = errorResponseFromError(e);
|
|
157
|
+
}
|
|
158
|
+
} else if (method === 'startProfile') {
|
|
159
|
+
this.profiler = new (_profiler().SamplingProfiler)();
|
|
160
|
+
try {
|
|
161
|
+
result = responseFromContent(await this.profiler.startProfiling());
|
|
162
|
+
} catch (e) {
|
|
163
|
+
result = errorResponseFromError(e);
|
|
164
|
+
}
|
|
165
|
+
} else if (method === 'endProfile') {
|
|
166
|
+
try {
|
|
167
|
+
let res = this.profiler ? await this.profiler.stopProfiling() : null;
|
|
168
|
+
result = responseFromContent(res);
|
|
169
|
+
} catch (e) {
|
|
170
|
+
result = errorResponseFromError(e);
|
|
171
|
+
}
|
|
172
|
+
} else if (method === 'takeHeapSnapshot') {
|
|
173
|
+
try {
|
|
174
|
+
let v8 = require('v8');
|
|
175
|
+
result = responseFromContent(v8.writeHeapSnapshot('heap-' + args[0] + '-' + (this.childId ? 'worker' + this.childId : 'main') + '.heapsnapshot'));
|
|
176
|
+
} catch (e) {
|
|
177
|
+
result = errorResponseFromError(e);
|
|
178
|
+
}
|
|
179
|
+
} else if (method === 'createSharedReference') {
|
|
180
|
+
let [ref, _value] = args;
|
|
181
|
+
let value = _value instanceof ArrayBuffer ?
|
|
182
|
+
// In the case the value is pre-serialized as a buffer,
|
|
183
|
+
// deserialize it.
|
|
184
|
+
(0, _buildCache().deserialize)(Buffer.from(_value)) : _value;
|
|
185
|
+
this.sharedReferences.set(ref, value);
|
|
186
|
+
this.sharedReferencesByValue.set(value, ref);
|
|
187
|
+
result = responseFromContent(null);
|
|
188
|
+
} else if (method === 'deleteSharedReference') {
|
|
189
|
+
let ref = args[0];
|
|
190
|
+
let value = this.sharedReferences.get(ref);
|
|
191
|
+
this.sharedReferencesByValue.delete(value);
|
|
192
|
+
this.sharedReferences.delete(ref);
|
|
193
|
+
result = responseFromContent(null);
|
|
194
|
+
} else {
|
|
195
|
+
try {
|
|
196
|
+
result = responseFromContent(
|
|
197
|
+
// @ts-expect-error TS2538
|
|
198
|
+
await this.module[method](this.workerApi, ...args));
|
|
199
|
+
} catch (e) {
|
|
200
|
+
result = errorResponseFromError(e);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
try {
|
|
204
|
+
this.send(result);
|
|
205
|
+
} catch (e) {
|
|
206
|
+
result = this.send(errorResponseFromError(e));
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
handleResponse(data) {
|
|
210
|
+
let idx = (0, _nullthrows().default)(data.idx);
|
|
211
|
+
let contentType = data.contentType;
|
|
212
|
+
let content = data.content;
|
|
213
|
+
let call = (0, _nullthrows().default)(this.responseQueue.get(idx));
|
|
214
|
+
if (contentType === 'error') {
|
|
215
|
+
(0, _assert().default)(typeof content !== 'string');
|
|
216
|
+
call.reject(new (_diagnostic().default)({
|
|
217
|
+
diagnostic: content
|
|
218
|
+
}));
|
|
219
|
+
} else {
|
|
220
|
+
call.resolve(content);
|
|
221
|
+
}
|
|
222
|
+
this.responseQueue.delete(idx);
|
|
223
|
+
|
|
224
|
+
// Process the next call
|
|
225
|
+
this.processQueue();
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Keep in mind to make sure responses to these calls are JSON.Stringify safe
|
|
229
|
+
addCall(request, awaitResponse = true) {
|
|
230
|
+
let call = {
|
|
231
|
+
...request,
|
|
232
|
+
type: 'request',
|
|
233
|
+
child: this.childId,
|
|
234
|
+
// @ts-expect-error TS2322
|
|
235
|
+
awaitResponse,
|
|
236
|
+
resolve: () => {},
|
|
237
|
+
reject: () => {}
|
|
238
|
+
};
|
|
239
|
+
let promise;
|
|
240
|
+
if (awaitResponse) {
|
|
241
|
+
promise = new Promise((resolve, reject) => {
|
|
242
|
+
call.resolve = resolve;
|
|
243
|
+
call.reject = reject;
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
this.callQueue.push(call);
|
|
247
|
+
this.processQueue();
|
|
248
|
+
return promise ?? Promise.resolve();
|
|
249
|
+
}
|
|
250
|
+
sendRequest(call) {
|
|
251
|
+
let idx;
|
|
252
|
+
if (call.awaitResponse) {
|
|
253
|
+
idx = this.responseId++;
|
|
254
|
+
this.responseQueue.set(idx, call);
|
|
255
|
+
}
|
|
256
|
+
this.send({
|
|
257
|
+
idx,
|
|
258
|
+
child: call.child,
|
|
259
|
+
type: call.type,
|
|
260
|
+
location: call.location,
|
|
261
|
+
handle: call.handle,
|
|
262
|
+
method: call.method,
|
|
263
|
+
args: call.args,
|
|
264
|
+
awaitResponse: call.awaitResponse
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
processQueue() {
|
|
268
|
+
if (!this.callQueue.length) {
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
if (this.responseQueue.size < this.maxConcurrentCalls) {
|
|
272
|
+
// @ts-expect-error TS2345
|
|
273
|
+
this.sendRequest(this.callQueue.shift());
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
handleEnd() {
|
|
277
|
+
this.loggerDisposable.dispose();
|
|
278
|
+
this.tracerDisposable.dispose();
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// @ts-expect-error TS2749
|
|
282
|
+
createReverseHandle(fn) {
|
|
283
|
+
let handle = new Handle({
|
|
284
|
+
fn,
|
|
285
|
+
childId: this.childId
|
|
286
|
+
});
|
|
287
|
+
this.handles.set(handle.id, handle);
|
|
288
|
+
return handle;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
exports.Child = Child;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.child = void 0;
|
|
7
|
+
exports.setChild = setChild;
|
|
8
|
+
// This file is imported by both the WorkerFarm and child implementation.
|
|
9
|
+
// When a worker is inited, it sets the state in this file.
|
|
10
|
+
// This way, WorkerFarm can access the state without directly importing the child code.
|
|
11
|
+
let child = exports.child = null;
|
|
12
|
+
function setChild(c) {
|
|
13
|
+
exports.child = child = c;
|
|
14
|
+
}
|
package/lib/cpuCount.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = getCores;
|
|
7
|
+
exports.detectRealCores = detectRealCores;
|
|
8
|
+
function _os() {
|
|
9
|
+
const data = _interopRequireDefault(require("os"));
|
|
10
|
+
_os = function () {
|
|
11
|
+
return data;
|
|
12
|
+
};
|
|
13
|
+
return data;
|
|
14
|
+
}
|
|
15
|
+
function _child_process() {
|
|
16
|
+
const data = require("child_process");
|
|
17
|
+
_child_process = function () {
|
|
18
|
+
return data;
|
|
19
|
+
};
|
|
20
|
+
return data;
|
|
21
|
+
}
|
|
22
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
|
+
const exec = command => {
|
|
24
|
+
try {
|
|
25
|
+
let stdout = (0, _child_process().execSync)(command, {
|
|
26
|
+
encoding: 'utf8',
|
|
27
|
+
// This prevents the command from outputting to the console
|
|
28
|
+
stdio: [null, null, null]
|
|
29
|
+
});
|
|
30
|
+
return stdout.trim();
|
|
31
|
+
} catch (e) {
|
|
32
|
+
return '';
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
function detectRealCores() {
|
|
36
|
+
let platform = _os().default.platform();
|
|
37
|
+
let amount = 0;
|
|
38
|
+
if (platform === 'linux') {
|
|
39
|
+
amount = parseInt(exec('lscpu -p | egrep -v "^#" | sort -u -t, -k 2,4 | wc -l'), 10);
|
|
40
|
+
} else if (platform === 'darwin') {
|
|
41
|
+
amount = parseInt(exec('sysctl -n hw.physicalcpu_max'), 10);
|
|
42
|
+
} else if (platform === 'win32') {
|
|
43
|
+
const str = exec('wmic cpu get NumberOfCores').match(/\d+/g);
|
|
44
|
+
if (str !== null) {
|
|
45
|
+
amount = parseInt(str.filter(n => n !== '')[0], 10);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (!amount || amount <= 0) {
|
|
49
|
+
throw new Error('Could not detect cpu count!');
|
|
50
|
+
}
|
|
51
|
+
return amount;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// @ts-expect-error TS7034
|
|
55
|
+
let cores;
|
|
56
|
+
function getCores(bypassCache = false) {
|
|
57
|
+
// Do not re-run commands if we already have the count...
|
|
58
|
+
// @ts-expect-error TS7005
|
|
59
|
+
if (cores && !bypassCache) {
|
|
60
|
+
return cores;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// @ts-expect-error TS2339
|
|
64
|
+
if (process.browser) {
|
|
65
|
+
cores = navigator.hardwareConcurrency / 2;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// @ts-expect-error TS7005
|
|
69
|
+
if (!cores) {
|
|
70
|
+
try {
|
|
71
|
+
cores = detectRealCores();
|
|
72
|
+
} catch (e) {
|
|
73
|
+
// Guess the amount of real cores
|
|
74
|
+
cores = _os().default.cpus().filter((cpu, index) => !cpu.model.includes('Intel') || index % 2 === 1).length;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Another fallback
|
|
79
|
+
if (!cores) {
|
|
80
|
+
cores = 1;
|
|
81
|
+
}
|
|
82
|
+
return cores;
|
|
83
|
+
}
|
package/lib/index.d.ts
ADDED
package/lib/index.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "Handle", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _WorkerFarm.Handle;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "bus", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _bus.default;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
exports.default = void 0;
|
|
19
|
+
function _assert() {
|
|
20
|
+
const data = _interopRequireDefault(require("assert"));
|
|
21
|
+
_assert = function () {
|
|
22
|
+
return data;
|
|
23
|
+
};
|
|
24
|
+
return data;
|
|
25
|
+
}
|
|
26
|
+
var _WorkerFarm = _interopRequireWildcard(require("./WorkerFarm"));
|
|
27
|
+
function _logger() {
|
|
28
|
+
const data = _interopRequireDefault(require("@atlaspack/logger"));
|
|
29
|
+
_logger = function () {
|
|
30
|
+
return data;
|
|
31
|
+
};
|
|
32
|
+
return data;
|
|
33
|
+
}
|
|
34
|
+
var _bus = _interopRequireDefault(require("./bus"));
|
|
35
|
+
function _profiler() {
|
|
36
|
+
const data = require("@atlaspack/profiler");
|
|
37
|
+
_profiler = function () {
|
|
38
|
+
return data;
|
|
39
|
+
};
|
|
40
|
+
return data;
|
|
41
|
+
}
|
|
42
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
43
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
44
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
45
|
+
if (!_WorkerFarm.default.isWorker()) {
|
|
46
|
+
// Forward all logger events originating from workers into the main process
|
|
47
|
+
_bus.default.on('logEvent', e => {
|
|
48
|
+
switch (e.level) {
|
|
49
|
+
case 'info':
|
|
50
|
+
_logger().default.info(e.diagnostics);
|
|
51
|
+
break;
|
|
52
|
+
case 'progress':
|
|
53
|
+
(0, _assert().default)(typeof e.message === 'string');
|
|
54
|
+
_logger().default.progress(e.message);
|
|
55
|
+
break;
|
|
56
|
+
case 'verbose':
|
|
57
|
+
_logger().default.verbose(e.diagnostics);
|
|
58
|
+
break;
|
|
59
|
+
case 'warn':
|
|
60
|
+
_logger().default.warn(e.diagnostics);
|
|
61
|
+
break;
|
|
62
|
+
case 'error':
|
|
63
|
+
_logger().default.error(e.diagnostics);
|
|
64
|
+
break;
|
|
65
|
+
default:
|
|
66
|
+
throw new Error('Unknown log level');
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Forward all trace events originating from workers into the main process
|
|
71
|
+
_bus.default.on('traceEvent', e => {
|
|
72
|
+
_profiler().tracer.trace(e);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
var _default = exports.default = _WorkerFarm.default;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ChildImpl, MessageHandler, ExitHandler, WorkerMessage } from '../types';
|
|
2
|
+
export default class ProcessChild implements ChildImpl {
|
|
3
|
+
onMessage: MessageHandler;
|
|
4
|
+
onExit: ExitHandler;
|
|
5
|
+
constructor(onMessage: MessageHandler, onExit: ExitHandler);
|
|
6
|
+
handleMessage(data: string): void;
|
|
7
|
+
send(data: WorkerMessage): void;
|
|
8
|
+
stop(): void;
|
|
9
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
function _buildCache() {
|
|
8
|
+
const data = require("@atlaspack/build-cache");
|
|
9
|
+
_buildCache = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _nullthrows() {
|
|
15
|
+
const data = _interopRequireDefault(require("nullthrows"));
|
|
16
|
+
_nullthrows = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
var _child = require("../child");
|
|
22
|
+
var _childState = require("../childState");
|
|
23
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
24
|
+
// @ts-expect-error TS2420
|
|
25
|
+
class ProcessChild {
|
|
26
|
+
constructor(onMessage, onExit) {
|
|
27
|
+
if (!process.send) {
|
|
28
|
+
throw new Error('Only create ProcessChild instances in a worker!');
|
|
29
|
+
}
|
|
30
|
+
this.onMessage = onMessage;
|
|
31
|
+
this.onExit = onExit;
|
|
32
|
+
// @ts-expect-error TS2345
|
|
33
|
+
process.on('message', data => this.handleMessage(data));
|
|
34
|
+
}
|
|
35
|
+
handleMessage(data) {
|
|
36
|
+
if (data === 'die') {
|
|
37
|
+
return this.stop();
|
|
38
|
+
}
|
|
39
|
+
this.onMessage((0, _buildCache().deserialize)(Buffer.from(data, 'base64')));
|
|
40
|
+
}
|
|
41
|
+
send(data) {
|
|
42
|
+
let processSend = (0, _nullthrows().default)(process.send).bind(process);
|
|
43
|
+
// @ts-expect-error TS7006
|
|
44
|
+
processSend((0, _buildCache().serialize)(data).toString('base64'), err => {
|
|
45
|
+
if (err && err instanceof Error) {
|
|
46
|
+
// @ts-expect-error TS2339
|
|
47
|
+
if (err.code === 'ERR_IPC_CHANNEL_CLOSED') {
|
|
48
|
+
// IPC connection closed
|
|
49
|
+
// no need to keep the worker running if it can't send or receive data
|
|
50
|
+
return this.stop();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
stop() {
|
|
56
|
+
this.onExit(0);
|
|
57
|
+
process.exit();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// @ts-expect-error TS2345
|
|
62
|
+
exports.default = ProcessChild;
|
|
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
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = exports.WORKER_PATH = void 0;
|
|
7
|
+
function _child_process() {
|
|
8
|
+
const data = _interopRequireDefault(require("child_process"));
|
|
9
|
+
_child_process = function () {
|
|
10
|
+
return data;
|
|
11
|
+
};
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
function _path() {
|
|
15
|
+
const data = _interopRequireDefault(require("path"));
|
|
16
|
+
_path = function () {
|
|
17
|
+
return data;
|
|
18
|
+
};
|
|
19
|
+
return data;
|
|
20
|
+
}
|
|
21
|
+
function _buildCache() {
|
|
22
|
+
const data = require("@atlaspack/build-cache");
|
|
23
|
+
_buildCache = function () {
|
|
24
|
+
return data;
|
|
25
|
+
};
|
|
26
|
+
return data;
|
|
27
|
+
}
|
|
28
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
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
|
|
35
|
+
class ProcessWorker {
|
|
36
|
+
// @ts-expect-error TS2564
|
|
37
|
+
|
|
38
|
+
processQueue = true;
|
|
39
|
+
sendQueue = [];
|
|
40
|
+
constructor(execArgv, onMessage, onError, onExit) {
|
|
41
|
+
this.execArgv = execArgv;
|
|
42
|
+
this.onMessage = onMessage;
|
|
43
|
+
this.onError = onError;
|
|
44
|
+
this.onExit = onExit;
|
|
45
|
+
}
|
|
46
|
+
start() {
|
|
47
|
+
this.child = _child_process().default.fork(WORKER_PATH, process.argv, {
|
|
48
|
+
execArgv: this.execArgv,
|
|
49
|
+
env: process.env,
|
|
50
|
+
cwd: process.cwd()
|
|
51
|
+
});
|
|
52
|
+
this.child.on('message', data => {
|
|
53
|
+
this.onMessage((0, _buildCache().deserialize)(Buffer.from(data, 'base64')));
|
|
54
|
+
});
|
|
55
|
+
this.child.once('exit', this.onExit);
|
|
56
|
+
this.child.on('error', this.onError);
|
|
57
|
+
return Promise.resolve();
|
|
58
|
+
}
|
|
59
|
+
async stop() {
|
|
60
|
+
this.child.send('die');
|
|
61
|
+
let forceKill = setTimeout(() => this.child.kill('SIGINT'), 500);
|
|
62
|
+
await new Promise(resolve => {
|
|
63
|
+
this.child.once('exit', resolve);
|
|
64
|
+
});
|
|
65
|
+
clearTimeout(forceKill);
|
|
66
|
+
}
|
|
67
|
+
send(data) {
|
|
68
|
+
if (!this.processQueue) {
|
|
69
|
+
this.sendQueue.push(data);
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
let result = this.child.send((0, _buildCache().serialize)(data).toString('base64'), error => {
|
|
73
|
+
if (error && error instanceof Error) {
|
|
74
|
+
// Ignore this, the workerfarm handles child errors
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
this.processQueue = true;
|
|
78
|
+
if (this.sendQueue.length > 0) {
|
|
79
|
+
let queueCopy = this.sendQueue.slice(0);
|
|
80
|
+
this.sendQueue = [];
|
|
81
|
+
queueCopy.forEach(entry => this.send(entry));
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
if (!result || /^win/.test(process.platform)) {
|
|
85
|
+
// Queue is handling too much messages throttle it
|
|
86
|
+
this.processQueue = false;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
exports.default = ProcessWorker;
|
|
@@ -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
|
+
}
|