@parcel/workers 2.0.0-nightly.142 → 2.0.0-nightly.1421
Sign up to get free protection for your applications and to get access to all the features.
- package/index.d.ts +23 -0
- package/lib/Handle.js +16 -58
- package/lib/Worker.js +88 -53
- package/lib/WorkerFarm.js +240 -188
- package/lib/backend.js +0 -6
- package/lib/bus.js +8 -10
- package/lib/child.js +128 -114
- package/lib/childState.js +1 -2
- package/lib/cpuCount.js +25 -22
- package/lib/index.js +34 -30
- package/lib/process/ProcessChild.js +18 -24
- package/lib/process/ProcessWorker.js +27 -38
- package/lib/threads/ThreadsChild.js +26 -28
- package/lib/threads/ThreadsWorker.js +25 -31
- package/package.json +19 -8
- package/src/Handle.js +10 -39
- package/src/Worker.js +82 -15
- package/src/WorkerFarm.js +228 -55
- package/src/bus.js +1 -1
- package/src/child.js +83 -24
- package/src/cpuCount.js +9 -4
- package/src/index.js +8 -2
- package/src/process/ProcessChild.js +2 -1
- package/src/process/ProcessWorker.js +1 -1
- package/src/threads/ThreadsWorker.js +2 -2
- package/test/cpuCount.test.js +1 -1
- package/test/integration/workerfarm/console.js +1 -1
- package/test/integration/workerfarm/logging.js +1 -1
- package/test/integration/workerfarm/reverse-handle.js +2 -2
- package/test/workerfarm.js +5 -5
- package/lib/Profiler.js +0 -70
- package/lib/Trace.js +0 -126
- package/src/Profiler.js +0 -93
- package/src/Trace.js +0 -121
package/lib/backend.js
CHANGED
@@ -5,31 +5,25 @@ Object.defineProperty(exports, "__esModule", {
|
|
5
5
|
});
|
6
6
|
exports.detectBackend = detectBackend;
|
7
7
|
exports.getWorkerBackend = getWorkerBackend;
|
8
|
-
|
9
8
|
function detectBackend() {
|
10
9
|
switch (process.env.PARCEL_WORKER_BACKEND) {
|
11
10
|
case 'threads':
|
12
11
|
case 'process':
|
13
12
|
return process.env.PARCEL_WORKER_BACKEND;
|
14
13
|
}
|
15
|
-
|
16
14
|
try {
|
17
15
|
require('worker_threads');
|
18
|
-
|
19
16
|
return 'threads';
|
20
17
|
} catch (err) {
|
21
18
|
return 'process';
|
22
19
|
}
|
23
20
|
}
|
24
|
-
|
25
21
|
function getWorkerBackend(backend) {
|
26
22
|
switch (backend) {
|
27
23
|
case 'threads':
|
28
24
|
return require('./threads/ThreadsWorker').default;
|
29
|
-
|
30
25
|
case 'process':
|
31
26
|
return require('./process/ProcessWorker').default;
|
32
|
-
|
33
27
|
default:
|
34
28
|
throw new Error(`Invalid backend: ${backend}`);
|
35
29
|
}
|
package/lib/bus.js
CHANGED
@@ -4,14 +4,16 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.default = void 0;
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
function _events() {
|
8
|
+
const data = _interopRequireDefault(require("events"));
|
9
|
+
_events = function () {
|
10
|
+
return data;
|
11
|
+
};
|
12
|
+
return data;
|
13
|
+
}
|
10
14
|
var _childState = require("./childState");
|
11
|
-
|
12
15
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
13
|
-
|
14
|
-
class Bus extends _events.default {
|
16
|
+
class Bus extends _events().default {
|
15
17
|
emit(event, ...args) {
|
16
18
|
if (_childState.child) {
|
17
19
|
_childState.child.workerApi.callMaster({
|
@@ -19,15 +21,11 @@ class Bus extends _events.default {
|
|
19
21
|
method: 'emit',
|
20
22
|
args: [event, ...args]
|
21
23
|
}, false);
|
22
|
-
|
23
24
|
return true;
|
24
25
|
} else {
|
25
26
|
return super.emit(event, ...args);
|
26
27
|
}
|
27
28
|
}
|
28
|
-
|
29
29
|
}
|
30
|
-
|
31
30
|
var _default = new Bus();
|
32
|
-
|
33
31
|
exports.default = _default;
|
package/lib/child.js
CHANGED
@@ -4,76 +4,88 @@ Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
5
5
|
});
|
6
6
|
exports.Child = void 0;
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
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 _logger() {
|
22
|
+
const data = _interopRequireWildcard(require("@parcel/logger"));
|
23
|
+
_logger = function () {
|
24
|
+
return data;
|
25
|
+
};
|
26
|
+
return data;
|
27
|
+
}
|
28
|
+
function _diagnostic() {
|
29
|
+
const data = _interopRequireWildcard(require("@parcel/diagnostic"));
|
30
|
+
_diagnostic = function () {
|
31
|
+
return data;
|
32
|
+
};
|
33
|
+
return data;
|
34
|
+
}
|
35
|
+
function _core() {
|
36
|
+
const data = require("@parcel/core");
|
37
|
+
_core = function () {
|
38
|
+
return data;
|
39
|
+
};
|
40
|
+
return data;
|
41
|
+
}
|
16
42
|
var _bus = _interopRequireDefault(require("./bus"));
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
43
|
+
function _profiler() {
|
44
|
+
const data = require("@parcel/profiler");
|
45
|
+
_profiler = function () {
|
46
|
+
return data;
|
47
|
+
};
|
48
|
+
return data;
|
49
|
+
}
|
50
|
+
var _Handle2 = _interopRequireDefault(require("./Handle"));
|
51
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
52
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
26
53
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
31
|
-
|
32
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
33
|
-
|
54
|
+
// The import of './Handle' should really be imported eagerly (with @babel/plugin-transform-modules-commonjs's lazy mode).
|
55
|
+
const Handle = _Handle2.default;
|
34
56
|
class Child {
|
57
|
+
callQueue = [];
|
58
|
+
maxConcurrentCalls = 10;
|
59
|
+
responseId = 0;
|
60
|
+
responseQueue = new Map();
|
61
|
+
handles = new Map();
|
62
|
+
sharedReferences = new Map();
|
63
|
+
sharedReferencesByValue = new Map();
|
35
64
|
constructor(ChildBackend) {
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
_defineProperty(this, "maxConcurrentCalls", 10);
|
41
|
-
|
42
|
-
_defineProperty(this, "module", void 0);
|
43
|
-
|
44
|
-
_defineProperty(this, "responseId", 0);
|
45
|
-
|
46
|
-
_defineProperty(this, "responseQueue", new Map());
|
47
|
-
|
48
|
-
_defineProperty(this, "loggerDisposable", void 0);
|
49
|
-
|
50
|
-
_defineProperty(this, "child", void 0);
|
51
|
-
|
52
|
-
_defineProperty(this, "profiler", void 0);
|
65
|
+
this.child = new ChildBackend(m => {
|
66
|
+
this.messageListener(m);
|
67
|
+
}, () => this.handleEnd());
|
53
68
|
|
54
|
-
|
55
|
-
|
56
|
-
_defineProperty(this, "handles", new Map());
|
57
|
-
|
58
|
-
_defineProperty(this, "sharedReferences", new Map());
|
59
|
-
|
60
|
-
_defineProperty(this, "sharedReferencesByValue", new Map());
|
61
|
-
|
62
|
-
_defineProperty(this, "workerApi", {
|
63
|
-
callMaster: (request, awaitResponse = true) => this.addCall(request, awaitResponse),
|
64
|
-
createReverseHandle: fn => this.createReverseHandle(fn),
|
65
|
-
getSharedReference: ref => this.sharedReferences.get(ref),
|
66
|
-
resolveSharedReference: value => this.sharedReferencesByValue.get(value)
|
67
|
-
});
|
68
|
-
|
69
|
-
this.child = new ChildBackend(this.messageListener.bind(this), this.handleEnd.bind(this)); // Monitior all logging events inside this child process and forward to
|
69
|
+
// Monitior all logging events inside this child process and forward to
|
70
70
|
// the main process via the bus.
|
71
|
-
|
72
|
-
this.loggerDisposable = _logger.default.onLog(event => {
|
71
|
+
this.loggerDisposable = _logger().default.onLog(event => {
|
73
72
|
_bus.default.emit('logEvent', event);
|
74
73
|
});
|
74
|
+
// .. and do the same for trace events
|
75
|
+
this.tracerDisposable = _profiler().tracer.onTrace(event => {
|
76
|
+
_bus.default.emit('traceEvent', event);
|
77
|
+
});
|
75
78
|
}
|
76
|
-
|
79
|
+
workerApi = {
|
80
|
+
callMaster: (request, awaitResponse = true) => this.addCall(request, awaitResponse),
|
81
|
+
createReverseHandle: fn => this.createReverseHandle(fn),
|
82
|
+
runHandle: (handle, args) => this.workerApi.callMaster({
|
83
|
+
handle: handle.id,
|
84
|
+
args
|
85
|
+
}, true),
|
86
|
+
getSharedReference: ref => this.sharedReferences.get(ref),
|
87
|
+
resolveSharedReference: value => this.sharedReferencesByValue.get(value)
|
88
|
+
};
|
77
89
|
messageListener(message) {
|
78
90
|
if (message.type === 'response') {
|
79
91
|
return this.handleResponse(message);
|
@@ -81,17 +93,17 @@ class Child {
|
|
81
93
|
return this.handleRequest(message);
|
82
94
|
}
|
83
95
|
}
|
84
|
-
|
85
96
|
send(data) {
|
86
97
|
this.child.send(data);
|
87
98
|
}
|
88
|
-
|
89
|
-
childInit(module, childId) {
|
99
|
+
async childInit(module, childId) {
|
90
100
|
// $FlowFixMe this must be dynamic
|
91
101
|
this.module = require(module);
|
92
102
|
this.childId = childId;
|
103
|
+
if (this.module.childInit != null) {
|
104
|
+
await this.module.childInit();
|
105
|
+
}
|
93
106
|
}
|
94
|
-
|
95
107
|
async handleRequest(data) {
|
96
108
|
let {
|
97
109
|
idx,
|
@@ -99,8 +111,7 @@ class Child {
|
|
99
111
|
args,
|
100
112
|
handle: handleId
|
101
113
|
} = data;
|
102
|
-
let child = (0, _nullthrows.default)(data.child);
|
103
|
-
|
114
|
+
let child = (0, _nullthrows().default)(data.child);
|
104
115
|
const responseFromContent = content => ({
|
105
116
|
idx,
|
106
117
|
child,
|
@@ -108,20 +119,18 @@ class Child {
|
|
108
119
|
contentType: 'data',
|
109
120
|
content
|
110
121
|
});
|
111
|
-
|
112
122
|
const errorResponseFromError = e => ({
|
113
123
|
idx,
|
114
124
|
child,
|
115
125
|
type: 'response',
|
116
126
|
contentType: 'error',
|
117
|
-
content: (0, _diagnostic.anyToDiagnostic)(e)
|
127
|
+
content: (0, _diagnostic().anyToDiagnostic)(e)
|
118
128
|
});
|
119
|
-
|
120
129
|
let result;
|
121
|
-
|
122
130
|
if (handleId != null) {
|
123
131
|
try {
|
124
|
-
|
132
|
+
var _this$handles$get;
|
133
|
+
let fn = (0, _nullthrows().default)((_this$handles$get = this.handles.get(handleId)) === null || _this$handles$get === void 0 ? void 0 : _this$handles$get.fn);
|
125
134
|
result = responseFromContent(fn(...args));
|
126
135
|
} catch (e) {
|
127
136
|
result = errorResponseFromError(e);
|
@@ -129,22 +138,22 @@ class Child {
|
|
129
138
|
} else if (method === 'childInit') {
|
130
139
|
try {
|
131
140
|
let [moduleName, childOptions] = args;
|
132
|
-
|
133
|
-
|
134
|
-
(0, _logger.patchConsole)();
|
141
|
+
if (childOptions.shouldPatchConsole) {
|
142
|
+
(0, _logger().patchConsole)();
|
135
143
|
} else {
|
136
|
-
(0, _logger.unpatchConsole)();
|
144
|
+
(0, _logger().unpatchConsole)();
|
137
145
|
}
|
138
|
-
|
139
|
-
|
146
|
+
if (childOptions.shouldTrace) {
|
147
|
+
_profiler().tracer.enable();
|
148
|
+
}
|
149
|
+
result = responseFromContent(await this.childInit(moduleName, child));
|
140
150
|
} catch (e) {
|
141
151
|
result = errorResponseFromError(e);
|
142
152
|
}
|
143
153
|
} else if (method === 'startProfile') {
|
144
|
-
this.profiler = new
|
145
|
-
|
154
|
+
this.profiler = new (_profiler().SamplingProfiler)();
|
146
155
|
try {
|
147
|
-
result = responseFromContent(
|
156
|
+
result = responseFromContent(await this.profiler.startProfiling());
|
148
157
|
} catch (e) {
|
149
158
|
result = errorResponseFromError(e);
|
150
159
|
}
|
@@ -155,81 +164,92 @@ class Child {
|
|
155
164
|
} catch (e) {
|
156
165
|
result = errorResponseFromError(e);
|
157
166
|
}
|
167
|
+
} else if (method === 'takeHeapSnapshot') {
|
168
|
+
try {
|
169
|
+
let v8 = require('v8');
|
170
|
+
result = responseFromContent(v8.writeHeapSnapshot('heap-' + args[0] + '-' + (this.childId ? 'worker' + this.childId : 'main') + '.heapsnapshot'));
|
171
|
+
} catch (e) {
|
172
|
+
result = errorResponseFromError(e);
|
173
|
+
}
|
158
174
|
} else if (method === 'createSharedReference') {
|
159
|
-
let [ref,
|
175
|
+
let [ref, _value] = args;
|
176
|
+
let value = _value instanceof ArrayBuffer ?
|
177
|
+
// In the case the value is pre-serialized as a buffer,
|
178
|
+
// deserialize it.
|
179
|
+
(0, _core().deserialize)(Buffer.from(_value)) : _value;
|
160
180
|
this.sharedReferences.set(ref, value);
|
161
181
|
this.sharedReferencesByValue.set(value, ref);
|
162
182
|
result = responseFromContent(null);
|
163
183
|
} else if (method === 'deleteSharedReference') {
|
164
|
-
|
184
|
+
let ref = args[0];
|
185
|
+
let value = this.sharedReferences.get(ref);
|
186
|
+
this.sharedReferencesByValue.delete(value);
|
187
|
+
this.sharedReferences.delete(ref);
|
165
188
|
result = responseFromContent(null);
|
166
189
|
} else {
|
167
190
|
try {
|
168
|
-
result = responseFromContent(
|
169
|
-
|
191
|
+
result = responseFromContent(
|
192
|
+
// $FlowFixMe
|
193
|
+
await this.module[method](this.workerApi, ...args));
|
170
194
|
} catch (e) {
|
171
195
|
result = errorResponseFromError(e);
|
172
196
|
}
|
173
197
|
}
|
174
|
-
|
175
|
-
|
198
|
+
try {
|
199
|
+
this.send(result);
|
200
|
+
} catch (e) {
|
201
|
+
result = this.send(errorResponseFromError(e));
|
202
|
+
}
|
176
203
|
}
|
177
|
-
|
178
204
|
handleResponse(data) {
|
179
|
-
let idx = (0, _nullthrows.default)(data.idx);
|
205
|
+
let idx = (0, _nullthrows().default)(data.idx);
|
180
206
|
let contentType = data.contentType;
|
181
207
|
let content = data.content;
|
182
|
-
let call = (0, _nullthrows.default)(this.responseQueue.get(idx));
|
183
|
-
|
208
|
+
let call = (0, _nullthrows().default)(this.responseQueue.get(idx));
|
184
209
|
if (contentType === 'error') {
|
185
|
-
(0, _assert.default)(typeof content !== 'string');
|
186
|
-
call.reject(new _diagnostic.default({
|
210
|
+
(0, _assert().default)(typeof content !== 'string');
|
211
|
+
call.reject(new (_diagnostic().default)({
|
187
212
|
diagnostic: content
|
188
213
|
}));
|
189
214
|
} else {
|
190
215
|
call.resolve(content);
|
191
216
|
}
|
217
|
+
this.responseQueue.delete(idx);
|
192
218
|
|
193
|
-
|
194
|
-
|
219
|
+
// Process the next call
|
195
220
|
this.processQueue();
|
196
|
-
}
|
197
|
-
|
221
|
+
}
|
198
222
|
|
223
|
+
// Keep in mind to make sure responses to these calls are JSON.Stringify safe
|
199
224
|
addCall(request, awaitResponse = true) {
|
200
225
|
var _promise;
|
201
|
-
|
202
226
|
// $FlowFixMe
|
203
|
-
let call =
|
227
|
+
let call = {
|
228
|
+
...request,
|
204
229
|
type: 'request',
|
205
230
|
child: this.childId,
|
231
|
+
// $FlowFixMe Added in Flow 0.121.0 upgrade in #4381
|
206
232
|
awaitResponse,
|
207
233
|
resolve: () => {},
|
208
234
|
reject: () => {}
|
209
|
-
}
|
210
|
-
|
235
|
+
};
|
211
236
|
let promise;
|
212
|
-
|
213
237
|
if (awaitResponse) {
|
214
238
|
promise = new Promise((resolve, reject) => {
|
215
239
|
call.resolve = resolve;
|
216
240
|
call.reject = reject;
|
217
241
|
});
|
218
242
|
}
|
219
|
-
|
220
243
|
this.callQueue.push(call);
|
221
244
|
this.processQueue();
|
222
245
|
return (_promise = promise) !== null && _promise !== void 0 ? _promise : Promise.resolve();
|
223
246
|
}
|
224
|
-
|
225
247
|
sendRequest(call) {
|
226
248
|
let idx;
|
227
|
-
|
228
249
|
if (call.awaitResponse) {
|
229
250
|
idx = this.responseId++;
|
230
251
|
this.responseQueue.set(idx, call);
|
231
252
|
}
|
232
|
-
|
233
253
|
this.send({
|
234
254
|
idx,
|
235
255
|
child: call.child,
|
@@ -241,31 +261,25 @@ class Child {
|
|
241
261
|
awaitResponse: call.awaitResponse
|
242
262
|
});
|
243
263
|
}
|
244
|
-
|
245
264
|
processQueue() {
|
246
265
|
if (!this.callQueue.length) {
|
247
266
|
return;
|
248
267
|
}
|
249
|
-
|
250
268
|
if (this.responseQueue.size < this.maxConcurrentCalls) {
|
251
269
|
this.sendRequest(this.callQueue.shift());
|
252
270
|
}
|
253
271
|
}
|
254
|
-
|
255
272
|
handleEnd() {
|
256
273
|
this.loggerDisposable.dispose();
|
274
|
+
this.tracerDisposable.dispose();
|
257
275
|
}
|
258
|
-
|
259
276
|
createReverseHandle(fn) {
|
260
|
-
let handle = new
|
277
|
+
let handle = new Handle({
|
261
278
|
fn,
|
262
|
-
workerApi: this.workerApi,
|
263
279
|
childId: this.childId
|
264
280
|
});
|
265
281
|
this.handles.set(handle.id, handle);
|
266
282
|
return handle;
|
267
283
|
}
|
268
|
-
|
269
284
|
}
|
270
|
-
|
271
285
|
exports.Child = Child;
|
package/lib/childState.js
CHANGED
@@ -3,14 +3,13 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.setChild = setChild;
|
7
6
|
exports.child = void 0;
|
7
|
+
exports.setChild = setChild;
|
8
8
|
// This file is imported by both the WorkerFarm and child implementation.
|
9
9
|
// When a worker is inited, it sets the state in this file.
|
10
10
|
// This way, WorkerFarm can access the state without directly importing the child code.
|
11
11
|
let child = null;
|
12
12
|
exports.child = child;
|
13
|
-
|
14
13
|
function setChild(c) {
|
15
14
|
exports.child = child = c;
|
16
15
|
}
|
package/lib/cpuCount.js
CHANGED
@@ -3,18 +3,26 @@
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
4
4
|
value: true
|
5
5
|
});
|
6
|
-
exports.detectRealCores = detectRealCores;
|
7
6
|
exports.default = getCores;
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
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
|
+
}
|
13
22
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
14
|
-
|
15
23
|
const exec = command => {
|
16
24
|
try {
|
17
|
-
let stdout = (0, _child_process.execSync)(command, {
|
25
|
+
let stdout = (0, _child_process().execSync)(command, {
|
18
26
|
encoding: 'utf8',
|
19
27
|
// This prevents the command from outputting to the console
|
20
28
|
stdio: [null, null, null]
|
@@ -24,45 +32,40 @@ const exec = command => {
|
|
24
32
|
return '';
|
25
33
|
}
|
26
34
|
};
|
27
|
-
|
28
35
|
function detectRealCores() {
|
29
|
-
let platform = _os.default.platform();
|
30
|
-
|
36
|
+
let platform = _os().default.platform();
|
31
37
|
let amount = 0;
|
32
|
-
|
33
38
|
if (platform === 'linux') {
|
34
39
|
amount = parseInt(exec('lscpu -p | egrep -v "^#" | sort -u -t, -k 2,4 | wc -l'), 10);
|
35
40
|
} else if (platform === 'darwin') {
|
36
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
|
+
}
|
37
47
|
}
|
38
|
-
|
39
48
|
if (!amount || amount <= 0) {
|
40
49
|
throw new Error('Could not detect cpu count!');
|
41
50
|
}
|
42
|
-
|
43
51
|
return amount;
|
44
52
|
}
|
45
|
-
|
46
53
|
let cores;
|
47
|
-
|
48
54
|
function getCores(bypassCache = false) {
|
49
55
|
// Do not re-run commands if we already have the count...
|
50
|
-
// $FlowFixMe
|
51
56
|
if (cores && !bypassCache) {
|
52
57
|
return cores;
|
53
58
|
}
|
54
|
-
|
55
59
|
try {
|
56
60
|
cores = detectRealCores();
|
57
61
|
} catch (e) {
|
58
62
|
// Guess the amount of real cores
|
59
|
-
cores = _os.default.cpus().filter((cpu, index) => !cpu.model.includes('Intel') || index % 2 === 1).length;
|
60
|
-
}
|
61
|
-
|
63
|
+
cores = _os().default.cpus().filter((cpu, index) => !cpu.model.includes('Intel') || index % 2 === 1).length;
|
64
|
+
}
|
62
65
|
|
66
|
+
// Another fallback
|
63
67
|
if (!cores) {
|
64
68
|
cores = 1;
|
65
69
|
}
|
66
|
-
|
67
70
|
return cores;
|
68
71
|
}
|
package/lib/index.js
CHANGED
@@ -16,57 +16,61 @@ Object.defineProperty(exports, "bus", {
|
|
16
16
|
}
|
17
17
|
});
|
18
18
|
exports.default = void 0;
|
19
|
-
|
20
|
-
|
21
|
-
|
19
|
+
function _assert() {
|
20
|
+
const data = _interopRequireDefault(require("assert"));
|
21
|
+
_assert = function () {
|
22
|
+
return data;
|
23
|
+
};
|
24
|
+
return data;
|
25
|
+
}
|
22
26
|
var _WorkerFarm = _interopRequireWildcard(require("./WorkerFarm"));
|
23
|
-
|
24
|
-
|
25
|
-
|
27
|
+
function _logger() {
|
28
|
+
const data = _interopRequireDefault(require("@parcel/logger"));
|
29
|
+
_logger = function () {
|
30
|
+
return data;
|
31
|
+
};
|
32
|
+
return data;
|
33
|
+
}
|
26
34
|
var _bus = _interopRequireDefault(require("./bus"));
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
35
|
+
function _profiler() {
|
36
|
+
const data = require("@parcel/profiler");
|
37
|
+
_profiler = function () {
|
38
|
+
return data;
|
39
|
+
};
|
40
|
+
return data;
|
41
|
+
}
|
42
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
43
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
32
44
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
33
|
-
|
34
45
|
if (!_WorkerFarm.default.isWorker()) {
|
35
46
|
// Forward all logger events originating from workers into the main process
|
36
47
|
_bus.default.on('logEvent', e => {
|
37
48
|
switch (e.level) {
|
38
49
|
case 'info':
|
39
|
-
_logger.default.info(e.diagnostics);
|
40
|
-
|
50
|
+
_logger().default.info(e.diagnostics);
|
41
51
|
break;
|
42
|
-
|
43
52
|
case 'progress':
|
44
|
-
(0, _assert.default)(typeof e.message === 'string');
|
45
|
-
|
46
|
-
_logger.default.progress(e.message);
|
47
|
-
|
53
|
+
(0, _assert().default)(typeof e.message === 'string');
|
54
|
+
_logger().default.progress(e.message);
|
48
55
|
break;
|
49
|
-
|
50
56
|
case 'verbose':
|
51
|
-
_logger.default.verbose(e.diagnostics);
|
52
|
-
|
57
|
+
_logger().default.verbose(e.diagnostics);
|
53
58
|
break;
|
54
|
-
|
55
59
|
case 'warn':
|
56
|
-
_logger.default.warn(e.diagnostics);
|
57
|
-
|
60
|
+
_logger().default.warn(e.diagnostics);
|
58
61
|
break;
|
59
|
-
|
60
62
|
case 'error':
|
61
|
-
_logger.default.error(e.diagnostics);
|
62
|
-
|
63
|
+
_logger().default.error(e.diagnostics);
|
63
64
|
break;
|
64
|
-
|
65
65
|
default:
|
66
66
|
throw new Error('Unknown log level');
|
67
67
|
}
|
68
68
|
});
|
69
|
-
}
|
70
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
|
+
}
|
71
75
|
var _default = _WorkerFarm.default;
|
72
76
|
exports.default = _default;
|