source: trip-planner-front/node_modules/jest-worker/build/workers/NodeThreadsWorker.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 8.3 KB
Line 
1'use strict';
2
3Object.defineProperty(exports, '__esModule', {
4 value: true
5});
6exports.default = void 0;
7
8function path() {
9 const data = _interopRequireWildcard(require('path'));
10
11 path = function () {
12 return data;
13 };
14
15 return data;
16}
17
18function _stream() {
19 const data = require('stream');
20
21 _stream = function () {
22 return data;
23 };
24
25 return data;
26}
27
28function _worker_threads() {
29 const data = require('worker_threads');
30
31 _worker_threads = function () {
32 return data;
33 };
34
35 return data;
36}
37
38function _mergeStream() {
39 const data = _interopRequireDefault(require('merge-stream'));
40
41 _mergeStream = function () {
42 return data;
43 };
44
45 return data;
46}
47
48var _types = require('../types');
49
50function _interopRequireDefault(obj) {
51 return obj && obj.__esModule ? obj : {default: obj};
52}
53
54function _getRequireWildcardCache(nodeInterop) {
55 if (typeof WeakMap !== 'function') return null;
56 var cacheBabelInterop = new WeakMap();
57 var cacheNodeInterop = new WeakMap();
58 return (_getRequireWildcardCache = function (nodeInterop) {
59 return nodeInterop ? cacheNodeInterop : cacheBabelInterop;
60 })(nodeInterop);
61}
62
63function _interopRequireWildcard(obj, nodeInterop) {
64 if (!nodeInterop && obj && obj.__esModule) {
65 return obj;
66 }
67 if (obj === null || (typeof obj !== 'object' && typeof obj !== 'function')) {
68 return {default: obj};
69 }
70 var cache = _getRequireWildcardCache(nodeInterop);
71 if (cache && cache.has(obj)) {
72 return cache.get(obj);
73 }
74 var newObj = {};
75 var hasPropertyDescriptor =
76 Object.defineProperty && Object.getOwnPropertyDescriptor;
77 for (var key in obj) {
78 if (key !== 'default' && Object.prototype.hasOwnProperty.call(obj, key)) {
79 var desc = hasPropertyDescriptor
80 ? Object.getOwnPropertyDescriptor(obj, key)
81 : null;
82 if (desc && (desc.get || desc.set)) {
83 Object.defineProperty(newObj, key, desc);
84 } else {
85 newObj[key] = obj[key];
86 }
87 }
88 }
89 newObj.default = obj;
90 if (cache) {
91 cache.set(obj, newObj);
92 }
93 return newObj;
94}
95
96function _defineProperty(obj, key, value) {
97 if (key in obj) {
98 Object.defineProperty(obj, key, {
99 value: value,
100 enumerable: true,
101 configurable: true,
102 writable: true
103 });
104 } else {
105 obj[key] = value;
106 }
107 return obj;
108}
109
110class ExperimentalWorker {
111 constructor(options) {
112 _defineProperty(this, '_worker', void 0);
113
114 _defineProperty(this, '_options', void 0);
115
116 _defineProperty(this, '_request', void 0);
117
118 _defineProperty(this, '_retries', void 0);
119
120 _defineProperty(this, '_onProcessEnd', void 0);
121
122 _defineProperty(this, '_onCustomMessage', void 0);
123
124 _defineProperty(this, '_fakeStream', void 0);
125
126 _defineProperty(this, '_stdout', void 0);
127
128 _defineProperty(this, '_stderr', void 0);
129
130 _defineProperty(this, '_exitPromise', void 0);
131
132 _defineProperty(this, '_resolveExitPromise', void 0);
133
134 _defineProperty(this, '_forceExited', void 0);
135
136 this._options = options;
137 this._request = null;
138 this._fakeStream = null;
139 this._stdout = null;
140 this._stderr = null;
141 this._exitPromise = new Promise(resolve => {
142 this._resolveExitPromise = resolve;
143 });
144 this._forceExited = false;
145 this.initialize();
146 }
147
148 initialize() {
149 this._worker = new (_worker_threads().Worker)(
150 path().resolve(__dirname, './threadChild.js'),
151 {
152 eval: false,
153 resourceLimits: this._options.resourceLimits,
154 stderr: true,
155 stdout: true,
156 workerData: {
157 cwd: process.cwd(),
158 env: {
159 ...process.env,
160 JEST_WORKER_ID: String(this._options.workerId + 1) // 0-indexed workerId, 1-indexed JEST_WORKER_ID
161 },
162 // Suppress --debug / --inspect flags while preserving others (like --harmony).
163 execArgv: process.execArgv.filter(v => !/^--(debug|inspect)/.test(v)),
164 silent: true,
165 ...this._options.forkOptions
166 }
167 }
168 );
169
170 if (this._worker.stdout) {
171 if (!this._stdout) {
172 // We need to add a permanent stream to the merged stream to prevent it
173 // from ending when the subprocess stream ends
174 this._stdout = (0, _mergeStream().default)(this._getFakeStream());
175 }
176
177 this._stdout.add(this._worker.stdout);
178 }
179
180 if (this._worker.stderr) {
181 if (!this._stderr) {
182 // We need to add a permanent stream to the merged stream to prevent it
183 // from ending when the subprocess stream ends
184 this._stderr = (0, _mergeStream().default)(this._getFakeStream());
185 }
186
187 this._stderr.add(this._worker.stderr);
188 }
189
190 this._worker.on('message', this._onMessage.bind(this));
191
192 this._worker.on('exit', this._onExit.bind(this));
193
194 this._worker.postMessage([
195 _types.CHILD_MESSAGE_INITIALIZE,
196 false,
197 this._options.workerPath,
198 this._options.setupArgs
199 ]);
200
201 this._retries++; // If we exceeded the amount of retries, we will emulate an error reply
202 // coming from the child. This avoids code duplication related with cleaning
203 // the queue, and scheduling the next call.
204
205 if (this._retries > this._options.maxRetries) {
206 const error = new Error('Call retries were exceeded');
207
208 this._onMessage([
209 _types.PARENT_MESSAGE_CLIENT_ERROR,
210 error.name,
211 error.message,
212 error.stack,
213 {
214 type: 'WorkerError'
215 }
216 ]);
217 }
218 }
219
220 _shutdown() {
221 // End the permanent stream so the merged stream end too
222 if (this._fakeStream) {
223 this._fakeStream.end();
224
225 this._fakeStream = null;
226 }
227
228 this._resolveExitPromise();
229 }
230
231 _onMessage(response) {
232 let error;
233
234 switch (response[0]) {
235 case _types.PARENT_MESSAGE_OK:
236 this._onProcessEnd(null, response[1]);
237
238 break;
239
240 case _types.PARENT_MESSAGE_CLIENT_ERROR:
241 error = response[4];
242
243 if (error != null && typeof error === 'object') {
244 const extra = error; // @ts-expect-error: no index
245
246 const NativeCtor = global[response[1]];
247 const Ctor = typeof NativeCtor === 'function' ? NativeCtor : Error;
248 error = new Ctor(response[2]);
249 error.type = response[1];
250 error.stack = response[3];
251
252 for (const key in extra) {
253 // @ts-expect-error: no index
254 error[key] = extra[key];
255 }
256 }
257
258 this._onProcessEnd(error, null);
259
260 break;
261
262 case _types.PARENT_MESSAGE_SETUP_ERROR:
263 error = new Error('Error when calling setup: ' + response[2]); // @ts-expect-error: adding custom properties to errors.
264
265 error.type = response[1];
266 error.stack = response[3];
267
268 this._onProcessEnd(error, null);
269
270 break;
271
272 case _types.PARENT_MESSAGE_CUSTOM:
273 this._onCustomMessage(response[1]);
274
275 break;
276
277 default:
278 throw new TypeError('Unexpected response from worker: ' + response[0]);
279 }
280 }
281
282 _onExit(exitCode) {
283 if (exitCode !== 0 && !this._forceExited) {
284 this.initialize();
285
286 if (this._request) {
287 this._worker.postMessage(this._request);
288 }
289 } else {
290 this._shutdown();
291 }
292 }
293
294 waitForExit() {
295 return this._exitPromise;
296 }
297
298 forceExit() {
299 this._forceExited = true;
300
301 this._worker.terminate();
302 }
303
304 send(request, onProcessStart, onProcessEnd, onCustomMessage) {
305 onProcessStart(this);
306
307 this._onProcessEnd = (...args) => {
308 var _onProcessEnd;
309
310 // Clean the request to avoid sending past requests to workers that fail
311 // while waiting for a new request (timers, unhandled rejections...)
312 this._request = null;
313 const res =
314 (_onProcessEnd = onProcessEnd) === null || _onProcessEnd === void 0
315 ? void 0
316 : _onProcessEnd(...args); // Clean up the reference so related closures can be garbage collected.
317
318 onProcessEnd = null;
319 return res;
320 };
321
322 this._onCustomMessage = (...arg) => onCustomMessage(...arg);
323
324 this._request = request;
325 this._retries = 0;
326
327 this._worker.postMessage(request);
328 }
329
330 getWorkerId() {
331 return this._options.workerId;
332 }
333
334 getStdout() {
335 return this._stdout;
336 }
337
338 getStderr() {
339 return this._stderr;
340 }
341
342 _getFakeStream() {
343 if (!this._fakeStream) {
344 this._fakeStream = new (_stream().PassThrough)();
345 }
346
347 return this._fakeStream;
348 }
349}
350
351exports.default = ExperimentalWorker;
Note: See TracBrowser for help on using the repository browser.