@parcel/workers 2.8.4-nightly.0 → 2.9.0

Sign up to get free protection for your applications and to get access to all the features.
package/lib/child.js CHANGED
@@ -4,80 +4,55 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.Child = void 0;
7
-
8
7
  function _assert() {
9
8
  const data = _interopRequireDefault(require("assert"));
10
-
11
9
  _assert = function () {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  function _nullthrows() {
19
15
  const data = _interopRequireDefault(require("nullthrows"));
20
-
21
16
  _nullthrows = function () {
22
17
  return data;
23
18
  };
24
-
25
19
  return data;
26
20
  }
27
-
28
21
  function _logger() {
29
22
  const data = _interopRequireWildcard(require("@parcel/logger"));
30
-
31
23
  _logger = function () {
32
24
  return data;
33
25
  };
34
-
35
26
  return data;
36
27
  }
37
-
38
28
  function _diagnostic() {
39
29
  const data = _interopRequireWildcard(require("@parcel/diagnostic"));
40
-
41
30
  _diagnostic = function () {
42
31
  return data;
43
32
  };
44
-
45
33
  return data;
46
34
  }
47
-
48
35
  function _core() {
49
36
  const data = require("@parcel/core");
50
-
51
37
  _core = function () {
52
38
  return data;
53
39
  };
54
-
55
40
  return data;
56
41
  }
57
-
58
42
  var _bus = _interopRequireDefault(require("./bus"));
59
-
60
43
  function _profiler() {
61
44
  const data = require("@parcel/profiler");
62
-
63
45
  _profiler = function () {
64
46
  return data;
65
47
  };
66
-
67
48
  return data;
68
49
  }
69
-
70
50
  var _Handle2 = _interopRequireDefault(require("./Handle"));
71
-
72
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); }
73
-
74
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; }
75
-
76
53
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
77
-
78
54
  // The import of './Handle' should really be imported eagerly (with @babel/plugin-transform-modules-commonjs's lazy mode).
79
55
  const Handle = _Handle2.default;
80
-
81
56
  class Child {
82
57
  callQueue = [];
83
58
  maxConcurrentCalls = 10;
@@ -86,22 +61,21 @@ class Child {
86
61
  handles = new Map();
87
62
  sharedReferences = new Map();
88
63
  sharedReferencesByValue = new Map();
89
-
90
64
  constructor(ChildBackend) {
91
65
  this.child = new ChildBackend(m => {
92
66
  this.messageListener(m);
93
- }, () => this.handleEnd()); // Monitior all logging events inside this child process and forward to
94
- // the main process via the bus.
67
+ }, () => this.handleEnd());
95
68
 
69
+ // Monitior all logging events inside this child process and forward to
70
+ // the main process via the bus.
96
71
  this.loggerDisposable = _logger().default.onLog(event => {
97
72
  _bus.default.emit('logEvent', event);
98
- }); // .. and do the same for trace events
99
-
73
+ });
74
+ // .. and do the same for trace events
100
75
  this.tracerDisposable = _profiler().tracer.onTrace(event => {
101
76
  _bus.default.emit('traceEvent', event);
102
77
  });
103
78
  }
104
-
105
79
  workerApi = {
106
80
  callMaster: (request, awaitResponse = true) => this.addCall(request, awaitResponse),
107
81
  createReverseHandle: fn => this.createReverseHandle(fn),
@@ -112,7 +86,6 @@ class Child {
112
86
  getSharedReference: ref => this.sharedReferences.get(ref),
113
87
  resolveSharedReference: value => this.sharedReferencesByValue.get(value)
114
88
  };
115
-
116
89
  messageListener(message) {
117
90
  if (message.type === 'response') {
118
91
  return this.handleResponse(message);
@@ -120,21 +93,17 @@ class Child {
120
93
  return this.handleRequest(message);
121
94
  }
122
95
  }
123
-
124
96
  send(data) {
125
97
  this.child.send(data);
126
98
  }
127
-
128
99
  async childInit(module, childId) {
129
100
  // $FlowFixMe this must be dynamic
130
101
  this.module = require(module);
131
102
  this.childId = childId;
132
-
133
103
  if (this.module.childInit != null) {
134
104
  await this.module.childInit();
135
105
  }
136
106
  }
137
-
138
107
  async handleRequest(data) {
139
108
  let {
140
109
  idx,
@@ -143,7 +112,6 @@ class Child {
143
112
  handle: handleId
144
113
  } = data;
145
114
  let child = (0, _nullthrows().default)(data.child);
146
-
147
115
  const responseFromContent = content => ({
148
116
  idx,
149
117
  child,
@@ -151,7 +119,6 @@ class Child {
151
119
  contentType: 'data',
152
120
  content
153
121
  });
154
-
155
122
  const errorResponseFromError = e => ({
156
123
  idx,
157
124
  child,
@@ -159,13 +126,10 @@ class Child {
159
126
  contentType: 'error',
160
127
  content: (0, _diagnostic().anyToDiagnostic)(e)
161
128
  });
162
-
163
129
  let result;
164
-
165
130
  if (handleId != null) {
166
131
  try {
167
132
  var _this$handles$get;
168
-
169
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);
170
134
  result = responseFromContent(fn(...args));
171
135
  } catch (e) {
@@ -174,24 +138,20 @@ class Child {
174
138
  } else if (method === 'childInit') {
175
139
  try {
176
140
  let [moduleName, childOptions] = args;
177
-
178
141
  if (childOptions.shouldPatchConsole) {
179
142
  (0, _logger().patchConsole)();
180
143
  } else {
181
144
  (0, _logger().unpatchConsole)();
182
145
  }
183
-
184
146
  if (childOptions.shouldTrace) {
185
147
  _profiler().tracer.enable();
186
148
  }
187
-
188
149
  result = responseFromContent(await this.childInit(moduleName, child));
189
150
  } catch (e) {
190
151
  result = errorResponseFromError(e);
191
152
  }
192
153
  } else if (method === 'startProfile') {
193
154
  this.profiler = new (_profiler().SamplingProfiler)();
194
-
195
155
  try {
196
156
  result = responseFromContent(await this.profiler.startProfiling());
197
157
  } catch (e) {
@@ -207,14 +167,14 @@ class Child {
207
167
  } else if (method === 'takeHeapSnapshot') {
208
168
  try {
209
169
  let v8 = require('v8');
210
-
211
170
  result = responseFromContent(v8.writeHeapSnapshot('heap-' + args[0] + '-' + (this.childId ? 'worker' + this.childId : 'main') + '.heapsnapshot'));
212
171
  } catch (e) {
213
172
  result = errorResponseFromError(e);
214
173
  }
215
174
  } else if (method === 'createSharedReference') {
216
175
  let [ref, _value] = args;
217
- let value = _value instanceof ArrayBuffer ? // In the case the value is pre-serialized as a buffer,
176
+ let value = _value instanceof ArrayBuffer ?
177
+ // In the case the value is pre-serialized as a buffer,
218
178
  // deserialize it.
219
179
  (0, _core().deserialize)(Buffer.from(_value)) : _value;
220
180
  this.sharedReferences.set(ref, value);
@@ -228,26 +188,24 @@ class Child {
228
188
  result = responseFromContent(null);
229
189
  } else {
230
190
  try {
231
- result = responseFromContent( // $FlowFixMe
191
+ result = responseFromContent(
192
+ // $FlowFixMe
232
193
  await this.module[method](this.workerApi, ...args));
233
194
  } catch (e) {
234
195
  result = errorResponseFromError(e);
235
196
  }
236
197
  }
237
-
238
198
  try {
239
199
  this.send(result);
240
200
  } catch (e) {
241
201
  result = this.send(errorResponseFromError(e));
242
202
  }
243
203
  }
244
-
245
204
  handleResponse(data) {
246
205
  let idx = (0, _nullthrows().default)(data.idx);
247
206
  let contentType = data.contentType;
248
207
  let content = data.content;
249
208
  let call = (0, _nullthrows().default)(this.responseQueue.get(idx));
250
-
251
209
  if (contentType === 'error') {
252
210
  (0, _assert().default)(typeof content !== 'string');
253
211
  call.reject(new (_diagnostic().default)({
@@ -256,18 +214,18 @@ class Child {
256
214
  } else {
257
215
  call.resolve(content);
258
216
  }
217
+ this.responseQueue.delete(idx);
259
218
 
260
- this.responseQueue.delete(idx); // Process the next call
261
-
219
+ // Process the next call
262
220
  this.processQueue();
263
- } // Keep in mind to make sure responses to these calls are JSON.Stringify safe
264
-
221
+ }
265
222
 
223
+ // Keep in mind to make sure responses to these calls are JSON.Stringify safe
266
224
  addCall(request, awaitResponse = true) {
267
225
  var _promise;
268
-
269
226
  // $FlowFixMe
270
- let call = { ...request,
227
+ let call = {
228
+ ...request,
271
229
  type: 'request',
272
230
  child: this.childId,
273
231
  // $FlowFixMe Added in Flow 0.121.0 upgrade in #4381
@@ -276,27 +234,22 @@ class Child {
276
234
  reject: () => {}
277
235
  };
278
236
  let promise;
279
-
280
237
  if (awaitResponse) {
281
238
  promise = new Promise((resolve, reject) => {
282
239
  call.resolve = resolve;
283
240
  call.reject = reject;
284
241
  });
285
242
  }
286
-
287
243
  this.callQueue.push(call);
288
244
  this.processQueue();
289
245
  return (_promise = promise) !== null && _promise !== void 0 ? _promise : Promise.resolve();
290
246
  }
291
-
292
247
  sendRequest(call) {
293
248
  let idx;
294
-
295
249
  if (call.awaitResponse) {
296
250
  idx = this.responseId++;
297
251
  this.responseQueue.set(idx, call);
298
252
  }
299
-
300
253
  this.send({
301
254
  idx,
302
255
  child: call.child,
@@ -308,22 +261,18 @@ class Child {
308
261
  awaitResponse: call.awaitResponse
309
262
  });
310
263
  }
311
-
312
264
  processQueue() {
313
265
  if (!this.callQueue.length) {
314
266
  return;
315
267
  }
316
-
317
268
  if (this.responseQueue.size < this.maxConcurrentCalls) {
318
269
  this.sendRequest(this.callQueue.shift());
319
270
  }
320
271
  }
321
-
322
272
  handleEnd() {
323
273
  this.loggerDisposable.dispose();
324
274
  this.tracerDisposable.dispose();
325
275
  }
326
-
327
276
  createReverseHandle(fn) {
328
277
  let handle = new Handle({
329
278
  fn,
@@ -332,7 +281,5 @@ class Child {
332
281
  this.handles.set(handle.id, handle);
333
282
  return handle;
334
283
  }
335
-
336
284
  }
337
-
338
285
  exports.Child = Child;
package/lib/childState.js CHANGED
@@ -10,7 +10,6 @@ exports.setChild = setChild;
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
@@ -5,29 +5,21 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = getCores;
7
7
  exports.detectRealCores = detectRealCores;
8
-
9
8
  function _os() {
10
9
  const data = _interopRequireDefault(require("os"));
11
-
12
10
  _os = function () {
13
11
  return data;
14
12
  };
15
-
16
13
  return data;
17
14
  }
18
-
19
15
  function _child_process() {
20
16
  const data = require("child_process");
21
-
22
17
  _child_process = function () {
23
18
  return data;
24
19
  };
25
-
26
20
  return data;
27
21
  }
28
-
29
22
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
30
-
31
23
  const exec = command => {
32
24
  try {
33
25
  let stdout = (0, _child_process().execSync)(command, {
@@ -40,50 +32,40 @@ const exec = command => {
40
32
  return '';
41
33
  }
42
34
  };
43
-
44
35
  function detectRealCores() {
45
36
  let platform = _os().default.platform();
46
-
47
37
  let amount = 0;
48
-
49
38
  if (platform === 'linux') {
50
39
  amount = parseInt(exec('lscpu -p | egrep -v "^#" | sort -u -t, -k 2,4 | wc -l'), 10);
51
40
  } else if (platform === 'darwin') {
52
41
  amount = parseInt(exec('sysctl -n hw.physicalcpu_max'), 10);
53
42
  } else if (platform === 'win32') {
54
43
  const str = exec('wmic cpu get NumberOfCores').match(/\d+/g);
55
-
56
44
  if (str !== null) {
57
45
  amount = parseInt(str.filter(n => n !== '')[0], 10);
58
46
  }
59
47
  }
60
-
61
48
  if (!amount || amount <= 0) {
62
49
  throw new Error('Could not detect cpu count!');
63
50
  }
64
-
65
51
  return amount;
66
52
  }
67
-
68
53
  let cores;
69
-
70
54
  function getCores(bypassCache = false) {
71
55
  // Do not re-run commands if we already have the count...
72
56
  if (cores && !bypassCache) {
73
57
  return cores;
74
58
  }
75
-
76
59
  try {
77
60
  cores = detectRealCores();
78
61
  } catch (e) {
79
62
  // Guess the amount of real cores
80
63
  cores = _os().default.cpus().filter((cpu, index) => !cpu.model.includes('Intel') || index % 2 === 1).length;
81
- } // Another fallback
82
-
64
+ }
83
65
 
66
+ // Another fallback
84
67
  if (!cores) {
85
68
  cores = 1;
86
69
  }
87
-
88
70
  return cores;
89
71
  }
package/lib/index.js CHANGED
@@ -16,88 +16,61 @@ Object.defineProperty(exports, "bus", {
16
16
  }
17
17
  });
18
18
  exports.default = void 0;
19
-
20
19
  function _assert() {
21
20
  const data = _interopRequireDefault(require("assert"));
22
-
23
21
  _assert = function () {
24
22
  return data;
25
23
  };
26
-
27
24
  return data;
28
25
  }
29
-
30
26
  var _WorkerFarm = _interopRequireWildcard(require("./WorkerFarm"));
31
-
32
27
  function _logger() {
33
28
  const data = _interopRequireDefault(require("@parcel/logger"));
34
-
35
29
  _logger = function () {
36
30
  return data;
37
31
  };
38
-
39
32
  return data;
40
33
  }
41
-
42
34
  var _bus = _interopRequireDefault(require("./bus"));
43
-
44
35
  function _profiler() {
45
36
  const data = require("@parcel/profiler");
46
-
47
37
  _profiler = function () {
48
38
  return data;
49
39
  };
50
-
51
40
  return data;
52
41
  }
53
-
54
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); }
55
-
56
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; }
57
-
58
44
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
59
-
60
45
  if (!_WorkerFarm.default.isWorker()) {
61
46
  // Forward all logger events originating from workers into the main process
62
47
  _bus.default.on('logEvent', e => {
63
48
  switch (e.level) {
64
49
  case 'info':
65
50
  _logger().default.info(e.diagnostics);
66
-
67
51
  break;
68
-
69
52
  case 'progress':
70
53
  (0, _assert().default)(typeof e.message === 'string');
71
-
72
54
  _logger().default.progress(e.message);
73
-
74
55
  break;
75
-
76
56
  case 'verbose':
77
57
  _logger().default.verbose(e.diagnostics);
78
-
79
58
  break;
80
-
81
59
  case 'warn':
82
60
  _logger().default.warn(e.diagnostics);
83
-
84
61
  break;
85
-
86
62
  case 'error':
87
63
  _logger().default.error(e.diagnostics);
88
-
89
64
  break;
90
-
91
65
  default:
92
66
  throw new Error('Unknown log level');
93
67
  }
94
- }); // Forward all trace events originating from workers into the main process
95
-
68
+ });
96
69
 
70
+ // Forward all trace events originating from workers into the main process
97
71
  _bus.default.on('traceEvent', e => {
98
72
  _profiler().tracer.trace(e);
99
73
  });
100
74
  }
101
-
102
75
  var _default = _WorkerFarm.default;
103
76
  exports.default = _default;
@@ -4,52 +4,38 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  function _nullthrows() {
9
8
  const data = _interopRequireDefault(require("nullthrows"));
10
-
11
9
  _nullthrows = function () {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  var _childState = require("../childState");
19
-
20
15
  var _child = require("../child");
21
-
22
16
  function _core() {
23
17
  const data = require("@parcel/core");
24
-
25
18
  _core = function () {
26
19
  return data;
27
20
  };
28
-
29
21
  return data;
30
22
  }
31
-
32
23
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
33
-
34
24
  class ProcessChild {
35
25
  constructor(onMessage, onExit) {
36
26
  if (!process.send) {
37
27
  throw new Error('Only create ProcessChild instances in a worker!');
38
28
  }
39
-
40
29
  this.onMessage = onMessage;
41
30
  this.onExit = onExit;
42
31
  process.on('message', data => this.handleMessage(data));
43
32
  }
44
-
45
33
  handleMessage(data) {
46
34
  if (data === 'die') {
47
35
  return this.stop();
48
36
  }
49
-
50
37
  this.onMessage((0, _core().deserialize)(Buffer.from(data, 'base64')));
51
38
  }
52
-
53
39
  send(data) {
54
40
  let processSend = (0, _nullthrows().default)(process.send).bind(process);
55
41
  processSend((0, _core().serialize)(data).toString('base64'), err => {
@@ -63,13 +49,10 @@ class ProcessChild {
63
49
  }
64
50
  });
65
51
  }
66
-
67
52
  stop() {
68
53
  this.onExit(0);
69
54
  process.exit();
70
55
  }
71
-
72
56
  }
73
-
74
57
  exports.default = ProcessChild;
75
58
  (0, _childState.setChild)(new _child.Child(ProcessChild));
@@ -4,52 +4,38 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
7
  function _child_process() {
9
8
  const data = _interopRequireDefault(require("child_process"));
10
-
11
9
  _child_process = function () {
12
10
  return data;
13
11
  };
14
-
15
12
  return data;
16
13
  }
17
-
18
14
  function _path() {
19
15
  const data = _interopRequireDefault(require("path"));
20
-
21
16
  _path = function () {
22
17
  return data;
23
18
  };
24
-
25
19
  return data;
26
20
  }
27
-
28
21
  function _core() {
29
22
  const data = require("@parcel/core");
30
-
31
23
  _core = function () {
32
24
  return data;
33
25
  };
34
-
35
26
  return data;
36
27
  }
37
-
38
28
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
39
-
40
29
  const WORKER_PATH = _path().default.join(__dirname, 'ProcessChild.js');
41
-
42
30
  class ProcessWorker {
43
31
  processQueue = true;
44
32
  sendQueue = [];
45
-
46
33
  constructor(execArgv, onMessage, onError, onExit) {
47
34
  this.execArgv = execArgv;
48
35
  this.onMessage = onMessage;
49
36
  this.onError = onError;
50
37
  this.onExit = onExit;
51
38
  }
52
-
53
39
  start() {
54
40
  this.child = _child_process().default.fork(WORKER_PATH, process.argv, {
55
41
  execArgv: this.execArgv,
@@ -63,7 +49,6 @@ class ProcessWorker {
63
49
  this.child.on('error', this.onError);
64
50
  return Promise.resolve();
65
51
  }
66
-
67
52
  async stop() {
68
53
  this.child.send('die');
69
54
  let forceKill = setTimeout(() => this.child.kill('SIGINT'), 500);
@@ -72,34 +57,27 @@ class ProcessWorker {
72
57
  });
73
58
  clearTimeout(forceKill);
74
59
  }
75
-
76
60
  send(data) {
77
61
  if (!this.processQueue) {
78
62
  this.sendQueue.push(data);
79
63
  return;
80
64
  }
81
-
82
65
  let result = this.child.send((0, _core().serialize)(data).toString('base64'), error => {
83
66
  if (error && error instanceof Error) {
84
67
  // Ignore this, the workerfarm handles child errors
85
68
  return;
86
69
  }
87
-
88
70
  this.processQueue = true;
89
-
90
71
  if (this.sendQueue.length > 0) {
91
72
  let queueCopy = this.sendQueue.slice(0);
92
73
  this.sendQueue = [];
93
74
  queueCopy.forEach(entry => this.send(entry));
94
75
  }
95
76
  });
96
-
97
77
  if (!result || /^win/.test(process.platform)) {
98
78
  // Queue is handling too much messages throttle it
99
79
  this.processQueue = false;
100
80
  }
101
81
  }
102
-
103
82
  }
104
-
105
83
  exports.default = ProcessWorker;