@parcel/workers 2.0.0-nightly.135 → 2.0.0-nightly.1351

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
@@ -4,85 +4,43 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
- var _core = require("@parcel/core");
9
-
10
- var _childState = require("./childState");
11
-
7
+ function _core() {
8
+ const data = require("@parcel/core");
9
+ _core = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
12
14
  var _package = _interopRequireDefault(require("../package.json"));
13
-
14
15
  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
-
16
+ // $FlowFixMe
18
17
  let HANDLE_ID = 0;
19
- const handleById = new Map();
18
+ // $FlowFixMe
20
19
 
20
+ const handleById = new Map();
21
21
  class Handle {
22
22
  constructor(opts) {
23
- _defineProperty(this, "id", void 0);
24
-
25
- _defineProperty(this, "childId", void 0);
26
-
27
- _defineProperty(this, "fn", void 0);
28
-
29
- _defineProperty(this, "workerApi", void 0);
30
-
31
- this.id = ++HANDLE_ID;
23
+ var _opts$id;
24
+ this.id = (_opts$id = opts.id) !== null && _opts$id !== void 0 ? _opts$id : ++HANDLE_ID;
32
25
  this.fn = opts.fn;
33
26
  this.childId = opts.childId;
34
- this.workerApi = opts.workerApi;
35
27
  handleById.set(this.id, this);
36
28
  }
37
-
38
29
  dispose() {
39
30
  handleById.delete(this.id);
40
31
  }
41
-
42
32
  serialize() {
43
33
  return {
44
34
  id: this.id,
45
35
  childId: this.childId
46
36
  };
47
37
  }
48
-
49
38
  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
- };
39
+ return new Handle(opts);
81
40
  }
41
+ }
82
42
 
83
- } // Register the Handle as a serializable class so that it will properly be deserialized
43
+ // Register the Handle as a serializable class so that it will properly be deserialized
84
44
  // by anything that uses WorkerFarm.
85
-
86
-
87
45
  exports.default = Handle;
88
- (0, _core.registerSerializableClass)(`${_package.default.version}:Handle`, Handle);
46
+ (0, _core().registerSerializableClass)(`${_package.default.version}:Handle`, Handle);
package/lib/Worker.js CHANGED
@@ -4,56 +4,76 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.default = void 0;
7
-
8
- var _events = _interopRequireDefault(require("events"));
9
-
10
- var _diagnostic = _interopRequireDefault(require("@parcel/diagnostic"));
11
-
7
+ function _nullthrows() {
8
+ const data = _interopRequireDefault(require("nullthrows"));
9
+ _nullthrows = function () {
10
+ return data;
11
+ };
12
+ return data;
13
+ }
14
+ function _events() {
15
+ const data = _interopRequireDefault(require("events"));
16
+ _events = function () {
17
+ return data;
18
+ };
19
+ return data;
20
+ }
21
+ function _diagnostic() {
22
+ const data = _interopRequireDefault(require("@parcel/diagnostic"));
23
+ _diagnostic = function () {
24
+ return data;
25
+ };
26
+ return data;
27
+ }
12
28
  var _backend = require("./backend");
13
-
14
29
  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
30
  let WORKER_ID = 0;
19
-
20
- class Worker extends _events.default {
31
+ class Worker extends _events().default {
32
+ id = WORKER_ID++;
33
+ sentSharedReferences = new Set();
34
+ calls = new Map();
35
+ exitCode = null;
36
+ callId = 0;
37
+ ready = false;
38
+ stopped = false;
39
+ isStopping = false;
21
40
  constructor(options) {
22
41
  super();
23
-
24
- _defineProperty(this, "options", void 0);
25
-
26
- _defineProperty(this, "worker", void 0);
27
-
28
- _defineProperty(this, "id", WORKER_ID++);
29
-
30
- _defineProperty(this, "calls", new Map());
31
-
32
- _defineProperty(this, "exitCode", null);
33
-
34
- _defineProperty(this, "callId", 0);
35
-
36
- _defineProperty(this, "ready", false);
37
-
38
- _defineProperty(this, "stopped", false);
39
-
40
- _defineProperty(this, "isStopping", false);
41
-
42
42
  this.options = options;
43
43
  }
44
-
45
44
  async fork(forkModule) {
46
- let filteredArgs = process.execArgv.filter(v => !/^--(debug|inspect)/.test(v));
47
-
45
+ let filteredArgs = process.execArgv.filter(v => !/^--(debug|inspect|max-old-space-size=)/.test(v));
48
46
  for (let i = 0; i < filteredArgs.length; i++) {
49
47
  let arg = filteredArgs[i];
50
-
51
- if ((arg === '-r' || arg === '--require') && filteredArgs[i + 1] === '@parcel/register') {
48
+ let isArgWithParam = (arg === '-r' || arg === '--require') && filteredArgs[i + 1] === '@parcel/register' || arg === '--title';
49
+ if (isArgWithParam) {
52
50
  filteredArgs.splice(i, 2);
53
51
  i--;
54
52
  }
55
53
  }
56
54
 
55
+ // Workaround for https://github.com/nodejs/node/issues/29117
56
+ if (process.env.NODE_OPTIONS) {
57
+ // arg parsing logic adapted from https://stackoverflow.com/a/46946420/2352201
58
+ let opts = [''];
59
+ let quote = false;
60
+ for (let c of (0, _nullthrows().default)(process.env.NODE_OPTIONS.match(/.|^$/g))) {
61
+ if (c === '"') {
62
+ quote = !quote;
63
+ } else if (!quote && c === ' ') {
64
+ opts.push('');
65
+ } else {
66
+ opts[opts.length - 1] += c.replace(/\\(.)/, '$1');
67
+ }
68
+ }
69
+ for (let i = 0; i < opts.length; i++) {
70
+ let opt = opts[i];
71
+ if (opt === '-r' || opt === '--require') {
72
+ filteredArgs.push(opt, opts[i + 1]);
73
+ i++;
74
+ }
75
+ }
76
+ }
57
77
  let WorkerBackend = (0, _backend.getWorkerBackend)(this.options.backend);
58
78
  this.worker = new WorkerBackend(filteredArgs, data => this.receive(data), err => {
59
79
  this.emit('error', err);
@@ -66,82 +86,97 @@ class Worker extends _events.default {
66
86
  this.call({
67
87
  method: 'childInit',
68
88
  args: [forkModule, {
69
- patchConsole: !!this.options.patchConsole
89
+ shouldPatchConsole: !!this.options.shouldPatchConsole,
90
+ shouldTrace: !!this.options.shouldTrace
70
91
  }],
71
92
  retries: 0,
93
+ skipReadyCheck: true,
72
94
  resolve,
73
95
  reject
74
96
  });
75
97
  });
98
+ let sharedRefs = this.options.sharedReferences;
99
+ let refsShared = new Set();
100
+ // in case more refs are created while initial refs are sending
101
+ while (refsShared.size < sharedRefs.size) {
102
+ await Promise.all([...sharedRefs].filter(([ref]) => !refsShared.has(ref)).map(async ([ref, value]) => {
103
+ await this.sendSharedReference(ref, value);
104
+ refsShared.add(ref);
105
+ }));
106
+ }
76
107
  this.ready = true;
77
108
  this.emit('ready');
78
109
  }
79
-
110
+ sendSharedReference(ref, value) {
111
+ this.sentSharedReferences.add(ref);
112
+ return new Promise((resolve, reject) => {
113
+ this.call({
114
+ method: 'createSharedReference',
115
+ args: [ref, value],
116
+ resolve,
117
+ reject,
118
+ retries: 0,
119
+ skipReadyCheck: true
120
+ });
121
+ });
122
+ }
80
123
  send(data) {
81
124
  this.worker.send(data);
82
125
  }
83
-
84
126
  call(call) {
85
127
  if (this.stopped || this.isStopping) {
86
128
  return;
87
129
  }
88
-
89
130
  let idx = this.callId++;
90
131
  this.calls.set(idx, call);
91
- this.send({
132
+ let msg = {
92
133
  type: 'request',
93
134
  idx: idx,
94
135
  child: this.id,
95
136
  handle: call.handle,
96
137
  method: call.method,
97
138
  args: call.args
98
- });
139
+ };
140
+ if (this.ready || call.skipReadyCheck === true) {
141
+ this.send(msg);
142
+ } else {
143
+ this.once('ready', () => this.send(msg));
144
+ }
99
145
  }
100
-
101
146
  receive(message) {
102
147
  if (this.stopped || this.isStopping) {
103
148
  return;
104
149
  }
105
-
106
150
  if (message.type === 'request') {
107
151
  this.emit('request', message);
108
152
  } else if (message.type === 'response') {
109
153
  let idx = message.idx;
110
-
111
154
  if (idx == null) {
112
155
  return;
113
156
  }
114
-
115
157
  let call = this.calls.get(idx);
116
-
117
158
  if (!call) {
118
159
  // Return for unknown calls, these might accur if a third party process uses workers
119
160
  return;
120
161
  }
121
-
122
162
  if (message.contentType === 'error') {
123
- call.reject(new _diagnostic.default({
163
+ call.reject(new (_diagnostic().default)({
124
164
  diagnostic: message.content
125
165
  }));
126
166
  } else {
127
167
  call.resolve(message.content);
128
168
  }
129
-
130
169
  this.calls.delete(idx);
131
170
  this.emit('response', message);
132
171
  }
133
172
  }
134
-
135
173
  async stop() {
136
174
  if (!this.stopped) {
137
175
  this.stopped = true;
138
-
139
176
  if (this.worker) {
140
177
  await this.worker.stop();
141
178
  }
142
179
  }
143
180
  }
144
-
145
181
  }
146
-
147
182
  exports.default = Worker;