@parcel/workers 2.8.3 → 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,72 +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
- var _Profiler = _interopRequireDefault(require("./Profiler"));
61
-
43
+ function _profiler() {
44
+ const data = require("@parcel/profiler");
45
+ _profiler = function () {
46
+ return data;
47
+ };
48
+ return data;
49
+ }
62
50
  var _Handle2 = _interopRequireDefault(require("./Handle"));
63
-
64
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); }
65
-
66
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; }
67
-
68
53
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
69
-
70
54
  // The import of './Handle' should really be imported eagerly (with @babel/plugin-transform-modules-commonjs's lazy mode).
71
55
  const Handle = _Handle2.default;
72
-
73
56
  class Child {
74
57
  callQueue = [];
75
58
  maxConcurrentCalls = 10;
@@ -78,18 +61,21 @@ class Child {
78
61
  handles = new Map();
79
62
  sharedReferences = new Map();
80
63
  sharedReferencesByValue = new Map();
81
-
82
64
  constructor(ChildBackend) {
83
65
  this.child = new ChildBackend(m => {
84
66
  this.messageListener(m);
85
- }, () => this.handleEnd()); // Monitior all logging events inside this child process and forward to
86
- // the main process via the bus.
67
+ }, () => this.handleEnd());
87
68
 
69
+ // Monitior all logging events inside this child process and forward to
70
+ // the main process via the bus.
88
71
  this.loggerDisposable = _logger().default.onLog(event => {
89
72
  _bus.default.emit('logEvent', event);
90
73
  });
74
+ // .. and do the same for trace events
75
+ this.tracerDisposable = _profiler().tracer.onTrace(event => {
76
+ _bus.default.emit('traceEvent', event);
77
+ });
91
78
  }
92
-
93
79
  workerApi = {
94
80
  callMaster: (request, awaitResponse = true) => this.addCall(request, awaitResponse),
95
81
  createReverseHandle: fn => this.createReverseHandle(fn),
@@ -100,7 +86,6 @@ class Child {
100
86
  getSharedReference: ref => this.sharedReferences.get(ref),
101
87
  resolveSharedReference: value => this.sharedReferencesByValue.get(value)
102
88
  };
103
-
104
89
  messageListener(message) {
105
90
  if (message.type === 'response') {
106
91
  return this.handleResponse(message);
@@ -108,21 +93,17 @@ class Child {
108
93
  return this.handleRequest(message);
109
94
  }
110
95
  }
111
-
112
96
  send(data) {
113
97
  this.child.send(data);
114
98
  }
115
-
116
99
  async childInit(module, childId) {
117
100
  // $FlowFixMe this must be dynamic
118
101
  this.module = require(module);
119
102
  this.childId = childId;
120
-
121
103
  if (this.module.childInit != null) {
122
104
  await this.module.childInit();
123
105
  }
124
106
  }
125
-
126
107
  async handleRequest(data) {
127
108
  let {
128
109
  idx,
@@ -131,7 +112,6 @@ class Child {
131
112
  handle: handleId
132
113
  } = data;
133
114
  let child = (0, _nullthrows().default)(data.child);
134
-
135
115
  const responseFromContent = content => ({
136
116
  idx,
137
117
  child,
@@ -139,7 +119,6 @@ class Child {
139
119
  contentType: 'data',
140
120
  content
141
121
  });
142
-
143
122
  const errorResponseFromError = e => ({
144
123
  idx,
145
124
  child,
@@ -147,13 +126,10 @@ class Child {
147
126
  contentType: 'error',
148
127
  content: (0, _diagnostic().anyToDiagnostic)(e)
149
128
  });
150
-
151
129
  let result;
152
-
153
130
  if (handleId != null) {
154
131
  try {
155
132
  var _this$handles$get;
156
-
157
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);
158
134
  result = responseFromContent(fn(...args));
159
135
  } catch (e) {
@@ -162,20 +138,20 @@ class Child {
162
138
  } else if (method === 'childInit') {
163
139
  try {
164
140
  let [moduleName, childOptions] = args;
165
-
166
141
  if (childOptions.shouldPatchConsole) {
167
142
  (0, _logger().patchConsole)();
168
143
  } else {
169
144
  (0, _logger().unpatchConsole)();
170
145
  }
171
-
146
+ if (childOptions.shouldTrace) {
147
+ _profiler().tracer.enable();
148
+ }
172
149
  result = responseFromContent(await this.childInit(moduleName, child));
173
150
  } catch (e) {
174
151
  result = errorResponseFromError(e);
175
152
  }
176
153
  } else if (method === 'startProfile') {
177
- this.profiler = new _Profiler.default();
178
-
154
+ this.profiler = new (_profiler().SamplingProfiler)();
179
155
  try {
180
156
  result = responseFromContent(await this.profiler.startProfiling());
181
157
  } catch (e) {
@@ -191,14 +167,14 @@ class Child {
191
167
  } else if (method === 'takeHeapSnapshot') {
192
168
  try {
193
169
  let v8 = require('v8');
194
-
195
170
  result = responseFromContent(v8.writeHeapSnapshot('heap-' + args[0] + '-' + (this.childId ? 'worker' + this.childId : 'main') + '.heapsnapshot'));
196
171
  } catch (e) {
197
172
  result = errorResponseFromError(e);
198
173
  }
199
174
  } else if (method === 'createSharedReference') {
200
175
  let [ref, _value] = args;
201
- 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,
202
178
  // deserialize it.
203
179
  (0, _core().deserialize)(Buffer.from(_value)) : _value;
204
180
  this.sharedReferences.set(ref, value);
@@ -212,26 +188,24 @@ class Child {
212
188
  result = responseFromContent(null);
213
189
  } else {
214
190
  try {
215
- result = responseFromContent( // $FlowFixMe
191
+ result = responseFromContent(
192
+ // $FlowFixMe
216
193
  await this.module[method](this.workerApi, ...args));
217
194
  } catch (e) {
218
195
  result = errorResponseFromError(e);
219
196
  }
220
197
  }
221
-
222
198
  try {
223
199
  this.send(result);
224
200
  } catch (e) {
225
201
  result = this.send(errorResponseFromError(e));
226
202
  }
227
203
  }
228
-
229
204
  handleResponse(data) {
230
205
  let idx = (0, _nullthrows().default)(data.idx);
231
206
  let contentType = data.contentType;
232
207
  let content = data.content;
233
208
  let call = (0, _nullthrows().default)(this.responseQueue.get(idx));
234
-
235
209
  if (contentType === 'error') {
236
210
  (0, _assert().default)(typeof content !== 'string');
237
211
  call.reject(new (_diagnostic().default)({
@@ -240,18 +214,18 @@ class Child {
240
214
  } else {
241
215
  call.resolve(content);
242
216
  }
217
+ this.responseQueue.delete(idx);
243
218
 
244
- this.responseQueue.delete(idx); // Process the next call
245
-
219
+ // Process the next call
246
220
  this.processQueue();
247
- } // Keep in mind to make sure responses to these calls are JSON.Stringify safe
248
-
221
+ }
249
222
 
223
+ // Keep in mind to make sure responses to these calls are JSON.Stringify safe
250
224
  addCall(request, awaitResponse = true) {
251
225
  var _promise;
252
-
253
226
  // $FlowFixMe
254
- let call = { ...request,
227
+ let call = {
228
+ ...request,
255
229
  type: 'request',
256
230
  child: this.childId,
257
231
  // $FlowFixMe Added in Flow 0.121.0 upgrade in #4381
@@ -260,27 +234,22 @@ class Child {
260
234
  reject: () => {}
261
235
  };
262
236
  let promise;
263
-
264
237
  if (awaitResponse) {
265
238
  promise = new Promise((resolve, reject) => {
266
239
  call.resolve = resolve;
267
240
  call.reject = reject;
268
241
  });
269
242
  }
270
-
271
243
  this.callQueue.push(call);
272
244
  this.processQueue();
273
245
  return (_promise = promise) !== null && _promise !== void 0 ? _promise : Promise.resolve();
274
246
  }
275
-
276
247
  sendRequest(call) {
277
248
  let idx;
278
-
279
249
  if (call.awaitResponse) {
280
250
  idx = this.responseId++;
281
251
  this.responseQueue.set(idx, call);
282
252
  }
283
-
284
253
  this.send({
285
254
  idx,
286
255
  child: call.child,
@@ -292,21 +261,18 @@ class Child {
292
261
  awaitResponse: call.awaitResponse
293
262
  });
294
263
  }
295
-
296
264
  processQueue() {
297
265
  if (!this.callQueue.length) {
298
266
  return;
299
267
  }
300
-
301
268
  if (this.responseQueue.size < this.maxConcurrentCalls) {
302
269
  this.sendRequest(this.callQueue.shift());
303
270
  }
304
271
  }
305
-
306
272
  handleEnd() {
307
273
  this.loggerDisposable.dispose();
274
+ this.tracerDisposable.dispose();
308
275
  }
309
-
310
276
  createReverseHandle(fn) {
311
277
  let handle = new Handle({
312
278
  fn,
@@ -315,7 +281,5 @@ class Child {
315
281
  this.handles.set(handle.id, handle);
316
282
  return handle;
317
283
  }
318
-
319
284
  }
320
-
321
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,73 +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
-
35
+ function _profiler() {
36
+ const data = require("@parcel/profiler");
37
+ _profiler = function () {
38
+ return data;
39
+ };
40
+ return data;
41
+ }
44
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); }
45
-
46
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; }
47
-
48
44
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
49
-
50
45
  if (!_WorkerFarm.default.isWorker()) {
51
46
  // Forward all logger events originating from workers into the main process
52
47
  _bus.default.on('logEvent', e => {
53
48
  switch (e.level) {
54
49
  case 'info':
55
50
  _logger().default.info(e.diagnostics);
56
-
57
51
  break;
58
-
59
52
  case 'progress':
60
53
  (0, _assert().default)(typeof e.message === 'string');
61
-
62
54
  _logger().default.progress(e.message);
63
-
64
55
  break;
65
-
66
56
  case 'verbose':
67
57
  _logger().default.verbose(e.diagnostics);
68
-
69
58
  break;
70
-
71
59
  case 'warn':
72
60
  _logger().default.warn(e.diagnostics);
73
-
74
61
  break;
75
-
76
62
  case 'error':
77
63
  _logger().default.error(e.diagnostics);
78
-
79
64
  break;
80
-
81
65
  default:
82
66
  throw new Error('Unknown log level');
83
67
  }
84
68
  });
85
- }
86
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
+ }
87
75
  var _default = _WorkerFarm.default;
88
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;