@parcel/workers 2.0.0-nightly.97 → 2.0.0-nightly.970

Sign up to get free protection for your applications and to get access to all the features.
package/index.d.ts ADDED
@@ -0,0 +1,22 @@
1
+ import {FilePath} from '@parcel/types';
2
+
3
+ type BackendType = 'process' | 'threads';
4
+
5
+ export type FarmOptions = {
6
+ maxConcurrentWorkers: number,
7
+ maxConcurrentCallsPerWorker: number,
8
+ forcedKillTime: number,
9
+ useLocalWorker: boolean,
10
+ warmWorkers: boolean,
11
+ workerPath?: FilePath,
12
+ backend: BackendType,
13
+ shouldPatchConsole?: boolean,
14
+ };
15
+
16
+ declare class WorkerFarm {
17
+ constructor(options: FarmOptions);
18
+
19
+ end(): Promise<void>;
20
+ }
21
+
22
+ export default WorkerFarm;
package/lib/Handle.js CHANGED
@@ -5,33 +5,32 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _core = require("@parcel/core");
8
+ function _core() {
9
+ const data = require("@parcel/core");
9
10
 
10
- var _childState = require("./childState");
11
+ _core = function () {
12
+ return data;
13
+ };
14
+
15
+ return data;
16
+ }
11
17
 
12
18
  var _package = _interopRequireDefault(require("../package.json"));
13
19
 
14
20
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
21
 
16
- 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; }
22
+ // $FlowFixMe
23
+ let HANDLE_ID = 0; // $FlowFixMe
17
24
 
18
- let HANDLE_ID = 0;
19
25
  const handleById = new Map();
20
26
 
21
27
  class Handle {
22
28
  constructor(opts) {
23
- _defineProperty(this, "id", void 0);
24
-
25
- _defineProperty(this, "childId", void 0);
26
-
27
- _defineProperty(this, "fn", void 0);
29
+ var _opts$id;
28
30
 
29
- _defineProperty(this, "workerApi", void 0);
30
-
31
- this.id = ++HANDLE_ID;
31
+ this.id = (_opts$id = opts.id) !== null && _opts$id !== void 0 ? _opts$id : ++HANDLE_ID;
32
32
  this.fn = opts.fn;
33
33
  this.childId = opts.childId;
34
- this.workerApi = opts.workerApi;
35
34
  handleById.set(this.id, this);
36
35
  }
37
36
 
@@ -47,37 +46,7 @@ class Handle {
47
46
  }
48
47
 
49
48
  static deserialize(opts) {
50
- return function (...args) {
51
- let workerApi;
52
-
53
- if (_childState.child) {
54
- workerApi = _childState.child.workerApi;
55
- } else {
56
- let handle = handleById.get(opts.id);
57
-
58
- if (!handle) {
59
- throw new Error('Corresponding Handle was not found. It may have been disposed.');
60
- }
61
-
62
- workerApi = handle.workerApi;
63
- }
64
-
65
- if (opts.childId != null && _childState.child) {
66
- throw new Error('Cannot call another child from a child');
67
- }
68
-
69
- if (opts.childId != null && workerApi.callChild) {
70
- return workerApi.callChild(opts.childId, {
71
- handle: opts.id,
72
- args
73
- });
74
- }
75
-
76
- return workerApi.callMaster({
77
- handle: opts.id,
78
- args
79
- }, true);
80
- };
49
+ return new Handle(opts);
81
50
  }
82
51
 
83
52
  } // Register the Handle as a serializable class so that it will properly be deserialized
@@ -85,4 +54,4 @@ class Handle {
85
54
 
86
55
 
87
56
  exports.default = Handle;
88
- (0, _core.registerSerializableClass)(`${_package.default.version}:Handle`, Handle);
57
+ (0, _core().registerSerializableClass)(`${_package.default.version}:Handle`, Handle);
package/lib/Profiler.js CHANGED
@@ -5,26 +5,36 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _assert = _interopRequireDefault(require("assert"));
8
+ function _assert() {
9
+ const data = _interopRequireDefault(require("assert"));
9
10
 
10
- var _diagnostic = _interopRequireDefault(require("@parcel/diagnostic"));
11
+ _assert = function () {
12
+ return data;
13
+ };
11
14
 
12
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
+ return data;
16
+ }
13
17
 
14
- 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; }
18
+ function _diagnostic() {
19
+ const data = _interopRequireDefault(require("@parcel/diagnostic"));
15
20
 
16
- class Profiler {
17
- constructor() {
18
- _defineProperty(this, "session", void 0);
19
- }
21
+ _diagnostic = function () {
22
+ return data;
23
+ };
20
24
 
25
+ return data;
26
+ }
27
+
28
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
29
+
30
+ class Profiler {
21
31
  startProfiling() {
22
32
  let inspector;
23
33
 
24
34
  try {
25
35
  inspector = require('inspector');
26
36
  } catch (err) {
27
- throw new _diagnostic.default({
37
+ throw new (_diagnostic().default)({
28
38
  diagnostic: {
29
39
  message: `The inspector module isn't available`,
30
40
  origin: '@parcel/workers',
@@ -41,7 +51,7 @@ class Profiler {
41
51
  }
42
52
 
43
53
  sendCommand(method, params) {
44
- (0, _assert.default)(this.session != null);
54
+ (0, _assert().default)(this.session != null);
45
55
  return new Promise((resolve, reject) => {
46
56
  this.session.post(method, params, (err, params) => {
47
57
  if (err == null) {
package/lib/Trace.js CHANGED
@@ -5,32 +5,32 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _chromeTraceEvent = require("chrome-trace-event");
8
+ function _chromeTraceEvent() {
9
+ const data = require("chrome-trace-event");
9
10
 
10
- 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; }
11
+ _chromeTraceEvent = function () {
12
+ return data;
13
+ };
14
+
15
+ return data;
16
+ }
11
17
 
12
18
  class Trace {
13
19
  constructor() {
14
- _defineProperty(this, "tracer", void 0);
15
-
16
- _defineProperty(this, "tid", void 0);
17
-
18
- _defineProperty(this, "eventId", void 0);
19
-
20
- this.tracer = new _chromeTraceEvent.Tracer();
20
+ this.tracer = new (_chromeTraceEvent().Tracer)();
21
21
  this.tid = 0;
22
22
  this.eventId = 0;
23
- this.init();
24
23
  }
25
24
 
26
25
  getEventId() {
27
26
  return this.eventId++;
28
27
  }
29
28
 
30
- init() {
29
+ init(ts) {
31
30
  this.tracer.instantEvent({
32
31
  name: 'TracingStartedInPage',
33
32
  id: this.getEventId(),
33
+ ts,
34
34
  cat: ['disabled-by-default-devtools.timeline'],
35
35
  args: {
36
36
  data: {
@@ -47,6 +47,7 @@ class Trace {
47
47
  this.tracer.instantEvent({
48
48
  name: 'TracingStartedInBrowser',
49
49
  id: this.getEventId(),
50
+ ts,
50
51
  cat: ['disabled-by-default-devtools.timeline'],
51
52
  args: {
52
53
  data: {
@@ -57,6 +58,10 @@ class Trace {
57
58
  }
58
59
 
59
60
  addCPUProfile(name, profile) {
61
+ if (this.eventId === 0) {
62
+ this.init(profile.startTime);
63
+ }
64
+
60
65
  const trace = this.tracer;
61
66
  const tid = this.tid;
62
67
  this.tid++;
package/lib/Worker.js CHANGED
@@ -5,45 +5,59 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.default = void 0;
7
7
 
8
- var _events = _interopRequireDefault(require("events"));
8
+ function _nullthrows() {
9
+ const data = _interopRequireDefault(require("nullthrows"));
9
10
 
10
- var _diagnostic = _interopRequireDefault(require("@parcel/diagnostic"));
11
+ _nullthrows = function () {
12
+ return data;
13
+ };
11
14
 
12
- var _backend = require("./backend");
13
-
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- 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; }
17
-
18
- let WORKER_ID = 0;
15
+ return data;
16
+ }
19
17
 
20
- class Worker extends _events.default {
21
- constructor(options) {
22
- super();
18
+ function _events() {
19
+ const data = _interopRequireDefault(require("events"));
23
20
 
24
- _defineProperty(this, "options", void 0);
21
+ _events = function () {
22
+ return data;
23
+ };
25
24
 
26
- _defineProperty(this, "worker", void 0);
25
+ return data;
26
+ }
27
27
 
28
- _defineProperty(this, "id", WORKER_ID++);
28
+ function _diagnostic() {
29
+ const data = _interopRequireDefault(require("@parcel/diagnostic"));
29
30
 
30
- _defineProperty(this, "calls", new Map());
31
+ _diagnostic = function () {
32
+ return data;
33
+ };
31
34
 
32
- _defineProperty(this, "exitCode", null);
35
+ return data;
36
+ }
33
37
 
34
- _defineProperty(this, "callId", 0);
38
+ var _backend = require("./backend");
35
39
 
36
- _defineProperty(this, "ready", false);
40
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
37
41
 
38
- _defineProperty(this, "stopped", false);
42
+ let WORKER_ID = 0;
39
43
 
40
- _defineProperty(this, "isStopping", false);
44
+ class Worker extends _events().default {
45
+ id = WORKER_ID++;
46
+ sharedReferences = new Map();
47
+ calls = new Map();
48
+ exitCode = null;
49
+ callId = 0;
50
+ ready = false;
51
+ stopped = false;
52
+ isStopping = false;
41
53
 
54
+ constructor(options) {
55
+ super();
42
56
  this.options = options;
43
57
  }
44
58
 
45
59
  async fork(forkModule) {
46
- let filteredArgs = process.execArgv.filter(v => !/^--(debug|inspect)/.test(v));
60
+ let filteredArgs = process.execArgv.filter(v => !/^--(debug|inspect|max-old-space-size=)/.test(v));
47
61
 
48
62
  for (let i = 0; i < filteredArgs.length; i++) {
49
63
  let arg = filteredArgs[i];
@@ -52,6 +66,32 @@ class Worker extends _events.default {
52
66
  filteredArgs.splice(i, 2);
53
67
  i--;
54
68
  }
69
+ } // Workaround for https://github.com/nodejs/node/issues/29117
70
+
71
+
72
+ if (process.env.NODE_OPTIONS) {
73
+ // arg parsing logic adapted from https://stackoverflow.com/a/46946420/2352201
74
+ let opts = [''];
75
+ let quote = false;
76
+
77
+ for (let c of (0, _nullthrows().default)(process.env.NODE_OPTIONS.match(/.|^$/g))) {
78
+ if (c === '"') {
79
+ quote = !quote;
80
+ } else if (!quote && c === ' ') {
81
+ opts.push('');
82
+ } else {
83
+ opts[opts.length - 1] += c.replace(/\\(.)/, '$1');
84
+ }
85
+ }
86
+
87
+ for (let i = 0; i < opts.length; i++) {
88
+ let opt = opts[i];
89
+
90
+ if (opt === '-r' || opt === '--require') {
91
+ filteredArgs.push(opt, opts[i + 1]);
92
+ i++;
93
+ }
94
+ }
55
95
  }
56
96
 
57
97
  let WorkerBackend = (0, _backend.getWorkerBackend)(this.options.backend);
@@ -66,17 +106,41 @@ class Worker extends _events.default {
66
106
  this.call({
67
107
  method: 'childInit',
68
108
  args: [forkModule, {
69
- patchConsole: !!this.options.patchConsole
109
+ shouldPatchConsole: !!this.options.shouldPatchConsole
70
110
  }],
71
111
  retries: 0,
112
+ skipReadyCheck: true,
72
113
  resolve,
73
114
  reject
74
115
  });
75
116
  });
117
+ let sharedRefs = this.options.sharedReferences;
118
+ let refsShared = new Set(); // in case more refs are created while initial refs are sending
119
+
120
+ while (refsShared.size < sharedRefs.size) {
121
+ await Promise.all([...sharedRefs].filter(([ref]) => !refsShared.has(ref)).map(async ([ref, value]) => {
122
+ await this.sendSharedReference(ref, value);
123
+ refsShared.add(ref);
124
+ }));
125
+ }
126
+
76
127
  this.ready = true;
77
128
  this.emit('ready');
78
129
  }
79
130
 
131
+ sendSharedReference(ref, value) {
132
+ return new Promise((resolve, reject) => {
133
+ this.call({
134
+ method: 'createSharedReference',
135
+ args: [ref, value],
136
+ resolve,
137
+ reject,
138
+ retries: 0,
139
+ skipReadyCheck: true
140
+ });
141
+ });
142
+ }
143
+
80
144
  send(data) {
81
145
  this.worker.send(data);
82
146
  }
@@ -88,14 +152,20 @@ class Worker extends _events.default {
88
152
 
89
153
  let idx = this.callId++;
90
154
  this.calls.set(idx, call);
91
- this.send({
155
+ let msg = {
92
156
  type: 'request',
93
157
  idx: idx,
94
158
  child: this.id,
95
159
  handle: call.handle,
96
160
  method: call.method,
97
161
  args: call.args
98
- });
162
+ };
163
+
164
+ if (this.ready || call.skipReadyCheck === true) {
165
+ this.send(msg);
166
+ } else {
167
+ this.once('ready', () => this.send(msg));
168
+ }
99
169
  }
100
170
 
101
171
  receive(message) {
@@ -120,7 +190,7 @@ class Worker extends _events.default {
120
190
  }
121
191
 
122
192
  if (message.contentType === 'error') {
123
- call.reject(new _diagnostic.default({
193
+ call.reject(new (_diagnostic().default)({
124
194
  diagnostic: message.content
125
195
  }));
126
196
  } else {