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

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