@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/index.d.ts CHANGED
@@ -3,14 +3,15 @@ import {FilePath} from '@parcel/types';
3
3
  type BackendType = 'process' | 'threads';
4
4
 
5
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,
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;
14
15
  };
15
16
 
16
17
  declare class WorkerFarm {
@@ -19,4 +20,4 @@ declare class WorkerFarm {
19
20
  end(): Promise<void>;
20
21
  }
21
22
 
22
- export default WorkerFarm;
23
+ export default WorkerFarm;
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);
@@ -107,7 +86,8 @@ class Worker extends _events().default {
107
86
  this.call({
108
87
  method: 'childInit',
109
88
  args: [forkModule, {
110
- shouldPatchConsole: !!this.options.shouldPatchConsole
89
+ shouldPatchConsole: !!this.options.shouldPatchConsole,
90
+ shouldTrace: !!this.options.shouldTrace
111
91
  }],
112
92
  retries: 0,
113
93
  skipReadyCheck: true,
@@ -116,19 +96,17 @@ class Worker extends _events().default {
116
96
  });
117
97
  });
118
98
  let sharedRefs = this.options.sharedReferences;
119
- let refsShared = new Set(); // in case more refs are created while initial refs are sending
120
-
99
+ let refsShared = new Set();
100
+ // in case more refs are created while initial refs are sending
121
101
  while (refsShared.size < sharedRefs.size) {
122
102
  await Promise.all([...sharedRefs].filter(([ref]) => !refsShared.has(ref)).map(async ([ref, value]) => {
123
103
  await this.sendSharedReference(ref, value);
124
104
  refsShared.add(ref);
125
105
  }));
126
106
  }
127
-
128
107
  this.ready = true;
129
108
  this.emit('ready');
130
109
  }
131
-
132
110
  sendSharedReference(ref, value) {
133
111
  this.sentSharedReferences.add(ref);
134
112
  return new Promise((resolve, reject) => {
@@ -142,16 +120,13 @@ class Worker extends _events().default {
142
120
  });
143
121
  });
144
122
  }
145
-
146
123
  send(data) {
147
124
  this.worker.send(data);
148
125
  }
149
-
150
126
  call(call) {
151
127
  if (this.stopped || this.isStopping) {
152
128
  return;
153
129
  }
154
-
155
130
  let idx = this.callId++;
156
131
  this.calls.set(idx, call);
157
132
  let msg = {
@@ -162,35 +137,28 @@ class Worker extends _events().default {
162
137
  method: call.method,
163
138
  args: call.args
164
139
  };
165
-
166
140
  if (this.ready || call.skipReadyCheck === true) {
167
141
  this.send(msg);
168
142
  } else {
169
143
  this.once('ready', () => this.send(msg));
170
144
  }
171
145
  }
172
-
173
146
  receive(message) {
174
147
  if (this.stopped || this.isStopping) {
175
148
  return;
176
149
  }
177
-
178
150
  if (message.type === 'request') {
179
151
  this.emit('request', message);
180
152
  } else if (message.type === 'response') {
181
153
  let idx = message.idx;
182
-
183
154
  if (idx == null) {
184
155
  return;
185
156
  }
186
-
187
157
  let call = this.calls.get(idx);
188
-
189
158
  if (!call) {
190
159
  // Return for unknown calls, these might accur if a third party process uses workers
191
160
  return;
192
161
  }
193
-
194
162
  if (message.contentType === 'error') {
195
163
  call.reject(new (_diagnostic().default)({
196
164
  diagnostic: message.content
@@ -198,22 +166,17 @@ class Worker extends _events().default {
198
166
  } else {
199
167
  call.resolve(message.content);
200
168
  }
201
-
202
169
  this.calls.delete(idx);
203
170
  this.emit('response', message);
204
171
  }
205
172
  }
206
-
207
173
  async stop() {
208
174
  if (!this.stopped) {
209
175
  this.stopped = true;
210
-
211
176
  if (this.worker) {
212
177
  await this.worker.stop();
213
178
  }
214
179
  }
215
180
  }
216
-
217
181
  }
218
-
219
182
  exports.default = Worker;