@parcel/workers 2.0.0-nightly.130 → 2.0.0-nightly.1303

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,23 @@
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
+ shouldTrace?: boolean;
15
+ };
16
+
17
+ declare class WorkerFarm {
18
+ constructor(options: FarmOptions);
19
+
20
+ end(): Promise<void>;
21
+ }
22
+
23
+ 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/Worker.js CHANGED
@@ -5,53 +5,94 @@ 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
+ sentSharedReferences = new Set();
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];
64
+ let isArgWithParam = (arg === '-r' || arg === '--require') && filteredArgs[i + 1] === '@parcel/register' || arg === '--title';
50
65
 
51
- if ((arg === '-r' || arg === '--require') && filteredArgs[i + 1] === '@parcel/register') {
66
+ if (isArgWithParam) {
52
67
  filteredArgs.splice(i, 2);
53
68
  i--;
54
69
  }
70
+ } // Workaround for https://github.com/nodejs/node/issues/29117
71
+
72
+
73
+ if (process.env.NODE_OPTIONS) {
74
+ // arg parsing logic adapted from https://stackoverflow.com/a/46946420/2352201
75
+ let opts = [''];
76
+ let quote = false;
77
+
78
+ for (let c of (0, _nullthrows().default)(process.env.NODE_OPTIONS.match(/.|^$/g))) {
79
+ if (c === '"') {
80
+ quote = !quote;
81
+ } else if (!quote && c === ' ') {
82
+ opts.push('');
83
+ } else {
84
+ opts[opts.length - 1] += c.replace(/\\(.)/, '$1');
85
+ }
86
+ }
87
+
88
+ for (let i = 0; i < opts.length; i++) {
89
+ let opt = opts[i];
90
+
91
+ if (opt === '-r' || opt === '--require') {
92
+ filteredArgs.push(opt, opts[i + 1]);
93
+ i++;
94
+ }
95
+ }
55
96
  }
56
97
 
57
98
  let WorkerBackend = (0, _backend.getWorkerBackend)(this.options.backend);
@@ -66,17 +107,43 @@ class Worker extends _events.default {
66
107
  this.call({
67
108
  method: 'childInit',
68
109
  args: [forkModule, {
69
- patchConsole: !!this.options.patchConsole
110
+ shouldPatchConsole: !!this.options.shouldPatchConsole,
111
+ shouldTrace: !!this.options.shouldTrace
70
112
  }],
71
113
  retries: 0,
114
+ skipReadyCheck: true,
72
115
  resolve,
73
116
  reject
74
117
  });
75
118
  });
119
+ let sharedRefs = this.options.sharedReferences;
120
+ let refsShared = new Set(); // in case more refs are created while initial refs are sending
121
+
122
+ while (refsShared.size < sharedRefs.size) {
123
+ await Promise.all([...sharedRefs].filter(([ref]) => !refsShared.has(ref)).map(async ([ref, value]) => {
124
+ await this.sendSharedReference(ref, value);
125
+ refsShared.add(ref);
126
+ }));
127
+ }
128
+
76
129
  this.ready = true;
77
130
  this.emit('ready');
78
131
  }
79
132
 
133
+ sendSharedReference(ref, value) {
134
+ this.sentSharedReferences.add(ref);
135
+ return new Promise((resolve, reject) => {
136
+ this.call({
137
+ method: 'createSharedReference',
138
+ args: [ref, value],
139
+ resolve,
140
+ reject,
141
+ retries: 0,
142
+ skipReadyCheck: true
143
+ });
144
+ });
145
+ }
146
+
80
147
  send(data) {
81
148
  this.worker.send(data);
82
149
  }
@@ -88,14 +155,20 @@ class Worker extends _events.default {
88
155
 
89
156
  let idx = this.callId++;
90
157
  this.calls.set(idx, call);
91
- this.send({
158
+ let msg = {
92
159
  type: 'request',
93
160
  idx: idx,
94
161
  child: this.id,
95
162
  handle: call.handle,
96
163
  method: call.method,
97
164
  args: call.args
98
- });
165
+ };
166
+
167
+ if (this.ready || call.skipReadyCheck === true) {
168
+ this.send(msg);
169
+ } else {
170
+ this.once('ready', () => this.send(msg));
171
+ }
99
172
  }
100
173
 
101
174
  receive(message) {
@@ -120,7 +193,7 @@ class Worker extends _events.default {
120
193
  }
121
194
 
122
195
  if (message.contentType === 'error') {
123
- call.reject(new _diagnostic.default({
196
+ call.reject(new (_diagnostic().default)({
124
197
  diagnostic: message.content
125
198
  }));
126
199
  } else {