1 | 'use strict';
|
---|
2 |
|
---|
3 | Object.defineProperty(exports, '__esModule', {
|
---|
4 | value: true
|
---|
5 | });
|
---|
6 | exports.default = void 0;
|
---|
7 |
|
---|
8 | function path() {
|
---|
9 | const data = _interopRequireWildcard(require('path'));
|
---|
10 |
|
---|
11 | path = function () {
|
---|
12 | return data;
|
---|
13 | };
|
---|
14 |
|
---|
15 | return data;
|
---|
16 | }
|
---|
17 |
|
---|
18 | function _stream() {
|
---|
19 | const data = require('stream');
|
---|
20 |
|
---|
21 | _stream = function () {
|
---|
22 | return data;
|
---|
23 | };
|
---|
24 |
|
---|
25 | return data;
|
---|
26 | }
|
---|
27 |
|
---|
28 | function _worker_threads() {
|
---|
29 | const data = require('worker_threads');
|
---|
30 |
|
---|
31 | _worker_threads = function () {
|
---|
32 | return data;
|
---|
33 | };
|
---|
34 |
|
---|
35 | return data;
|
---|
36 | }
|
---|
37 |
|
---|
38 | function _mergeStream() {
|
---|
39 | const data = _interopRequireDefault(require('merge-stream'));
|
---|
40 |
|
---|
41 | _mergeStream = function () {
|
---|
42 | return data;
|
---|
43 | };
|
---|
44 |
|
---|
45 | return data;
|
---|
46 | }
|
---|
47 |
|
---|
48 | var _types = require('../types');
|
---|
49 |
|
---|
50 | function _interopRequireDefault(obj) {
|
---|
51 | return obj && obj.__esModule ? obj : {default: obj};
|
---|
52 | }
|
---|
53 |
|
---|
54 | function _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 |
|
---|
63 | function _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 |
|
---|
96 | function _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 |
|
---|
110 | class 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 | // @ts-expect-error: added in newer versions
|
---|
154 | resourceLimits: this._options.resourceLimits,
|
---|
155 | stderr: true,
|
---|
156 | stdout: true,
|
---|
157 | workerData: this._options.workerData,
|
---|
158 | ...this._options.forkOptions
|
---|
159 | }
|
---|
160 | );
|
---|
161 |
|
---|
162 | if (this._worker.stdout) {
|
---|
163 | if (!this._stdout) {
|
---|
164 | // We need to add a permanent stream to the merged stream to prevent it
|
---|
165 | // from ending when the subprocess stream ends
|
---|
166 | this._stdout = (0, _mergeStream().default)(this._getFakeStream());
|
---|
167 | }
|
---|
168 |
|
---|
169 | this._stdout.add(this._worker.stdout);
|
---|
170 | }
|
---|
171 |
|
---|
172 | if (this._worker.stderr) {
|
---|
173 | if (!this._stderr) {
|
---|
174 | // We need to add a permanent stream to the merged stream to prevent it
|
---|
175 | // from ending when the subprocess stream ends
|
---|
176 | this._stderr = (0, _mergeStream().default)(this._getFakeStream());
|
---|
177 | }
|
---|
178 |
|
---|
179 | this._stderr.add(this._worker.stderr);
|
---|
180 | }
|
---|
181 |
|
---|
182 | this._worker.on('message', this._onMessage.bind(this));
|
---|
183 |
|
---|
184 | this._worker.on('exit', this._onExit.bind(this));
|
---|
185 |
|
---|
186 | this._worker.postMessage([
|
---|
187 | _types.CHILD_MESSAGE_INITIALIZE,
|
---|
188 | false,
|
---|
189 | this._options.workerPath,
|
---|
190 | this._options.setupArgs,
|
---|
191 | String(this._options.workerId + 1) // 0-indexed workerId, 1-indexed JEST_WORKER_ID
|
---|
192 | ]);
|
---|
193 |
|
---|
194 | this._retries++; // If we exceeded the amount of retries, we will emulate an error reply
|
---|
195 | // coming from the child. This avoids code duplication related with cleaning
|
---|
196 | // the queue, and scheduling the next call.
|
---|
197 |
|
---|
198 | if (this._retries > this._options.maxRetries) {
|
---|
199 | const error = new Error('Call retries were exceeded');
|
---|
200 |
|
---|
201 | this._onMessage([
|
---|
202 | _types.PARENT_MESSAGE_CLIENT_ERROR,
|
---|
203 | error.name,
|
---|
204 | error.message,
|
---|
205 | error.stack,
|
---|
206 | {
|
---|
207 | type: 'WorkerError'
|
---|
208 | }
|
---|
209 | ]);
|
---|
210 | }
|
---|
211 | }
|
---|
212 |
|
---|
213 | _shutdown() {
|
---|
214 | // End the permanent stream so the merged stream end too
|
---|
215 | if (this._fakeStream) {
|
---|
216 | this._fakeStream.end();
|
---|
217 |
|
---|
218 | this._fakeStream = null;
|
---|
219 | }
|
---|
220 |
|
---|
221 | this._resolveExitPromise();
|
---|
222 | }
|
---|
223 |
|
---|
224 | _onMessage(response) {
|
---|
225 | let error;
|
---|
226 |
|
---|
227 | switch (response[0]) {
|
---|
228 | case _types.PARENT_MESSAGE_OK:
|
---|
229 | this._onProcessEnd(null, response[1]);
|
---|
230 |
|
---|
231 | break;
|
---|
232 |
|
---|
233 | case _types.PARENT_MESSAGE_CLIENT_ERROR:
|
---|
234 | error = response[4];
|
---|
235 |
|
---|
236 | if (error != null && typeof error === 'object') {
|
---|
237 | const extra = error; // @ts-expect-error: no index
|
---|
238 |
|
---|
239 | const NativeCtor = global[response[1]];
|
---|
240 | const Ctor = typeof NativeCtor === 'function' ? NativeCtor : Error;
|
---|
241 | error = new Ctor(response[2]);
|
---|
242 | error.type = response[1];
|
---|
243 | error.stack = response[3];
|
---|
244 |
|
---|
245 | for (const key in extra) {
|
---|
246 | // @ts-expect-error: no index
|
---|
247 | error[key] = extra[key];
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | this._onProcessEnd(error, null);
|
---|
252 |
|
---|
253 | break;
|
---|
254 |
|
---|
255 | case _types.PARENT_MESSAGE_SETUP_ERROR:
|
---|
256 | error = new Error('Error when calling setup: ' + response[2]); // @ts-expect-error: adding custom properties to errors.
|
---|
257 |
|
---|
258 | error.type = response[1];
|
---|
259 | error.stack = response[3];
|
---|
260 |
|
---|
261 | this._onProcessEnd(error, null);
|
---|
262 |
|
---|
263 | break;
|
---|
264 |
|
---|
265 | case _types.PARENT_MESSAGE_CUSTOM:
|
---|
266 | this._onCustomMessage(response[1]);
|
---|
267 |
|
---|
268 | break;
|
---|
269 |
|
---|
270 | default:
|
---|
271 | throw new TypeError('Unexpected response from worker: ' + response[0]);
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | _onExit(exitCode) {
|
---|
276 | if (exitCode !== 0 && !this._forceExited) {
|
---|
277 | this.initialize();
|
---|
278 |
|
---|
279 | if (this._request) {
|
---|
280 | this._worker.postMessage(this._request);
|
---|
281 | }
|
---|
282 | } else {
|
---|
283 | this._shutdown();
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | waitForExit() {
|
---|
288 | return this._exitPromise;
|
---|
289 | }
|
---|
290 |
|
---|
291 | forceExit() {
|
---|
292 | this._forceExited = true;
|
---|
293 |
|
---|
294 | this._worker.terminate();
|
---|
295 | }
|
---|
296 |
|
---|
297 | send(request, onProcessStart, onProcessEnd, onCustomMessage) {
|
---|
298 | onProcessStart(this);
|
---|
299 |
|
---|
300 | this._onProcessEnd = (...args) => {
|
---|
301 | var _onProcessEnd;
|
---|
302 |
|
---|
303 | // Clean the request to avoid sending past requests to workers that fail
|
---|
304 | // while waiting for a new request (timers, unhandled rejections...)
|
---|
305 | this._request = null;
|
---|
306 | const res =
|
---|
307 | (_onProcessEnd = onProcessEnd) === null || _onProcessEnd === void 0
|
---|
308 | ? void 0
|
---|
309 | : _onProcessEnd(...args); // Clean up the reference so related closures can be garbage collected.
|
---|
310 |
|
---|
311 | onProcessEnd = null;
|
---|
312 | return res;
|
---|
313 | };
|
---|
314 |
|
---|
315 | this._onCustomMessage = (...arg) => onCustomMessage(...arg);
|
---|
316 |
|
---|
317 | this._request = request;
|
---|
318 | this._retries = 0;
|
---|
319 |
|
---|
320 | this._worker.postMessage(request);
|
---|
321 | }
|
---|
322 |
|
---|
323 | getWorkerId() {
|
---|
324 | return this._options.workerId;
|
---|
325 | }
|
---|
326 |
|
---|
327 | getStdout() {
|
---|
328 | return this._stdout;
|
---|
329 | }
|
---|
330 |
|
---|
331 | getStderr() {
|
---|
332 | return this._stderr;
|
---|
333 | }
|
---|
334 |
|
---|
335 | _getFakeStream() {
|
---|
336 | if (!this._fakeStream) {
|
---|
337 | this._fakeStream = new (_stream().PassThrough)();
|
---|
338 | }
|
---|
339 |
|
---|
340 | return this._fakeStream;
|
---|
341 | }
|
---|
342 | }
|
---|
343 |
|
---|
344 | exports.default = ExperimentalWorker;
|
---|