1 | 'use strict';
|
2 |
|
3 | function _worker_threads() {
|
4 | const data = require('worker_threads');
|
5 |
|
6 | _worker_threads = function () {
|
7 | return data;
|
8 | };
|
9 |
|
10 | return data;
|
11 | }
|
12 |
|
13 | function _jestUtil() {
|
14 | const data = require('jest-util');
|
15 |
|
16 | _jestUtil = function () {
|
17 | return data;
|
18 | };
|
19 |
|
20 | return data;
|
21 | }
|
22 |
|
23 | var _types = require('../types');
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | let file = null;
|
32 | let setupArgs = [];
|
33 | let initialized = false;
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | const messageListener = request => {
|
49 | switch (request[0]) {
|
50 | case _types.CHILD_MESSAGE_INITIALIZE:
|
51 | const init = request;
|
52 | file = init[2];
|
53 | setupArgs = request[3];
|
54 | process.env.JEST_WORKER_ID = request[4];
|
55 | break;
|
56 |
|
57 | case _types.CHILD_MESSAGE_CALL:
|
58 | const call = request;
|
59 | execMethod(call[2], call[3]);
|
60 | break;
|
61 |
|
62 | case _types.CHILD_MESSAGE_END:
|
63 | end();
|
64 | break;
|
65 |
|
66 | case _types.CHILD_MESSAGE_MEM_USAGE:
|
67 | reportMemoryUsage();
|
68 | break;
|
69 |
|
70 | default:
|
71 | throw new TypeError(
|
72 | `Unexpected request from parent process: ${request[0]}`
|
73 | );
|
74 | }
|
75 | };
|
76 |
|
77 | _worker_threads().parentPort.on('message', messageListener);
|
78 |
|
79 | function reportMemoryUsage() {
|
80 | if (_worker_threads().isMainThread) {
|
81 | throw new Error('Child can only be used on a forked process');
|
82 | }
|
83 |
|
84 | const msg = [_types.PARENT_MESSAGE_MEM_USAGE, process.memoryUsage().heapUsed];
|
85 |
|
86 | _worker_threads().parentPort.postMessage(msg);
|
87 | }
|
88 |
|
89 | function reportSuccess(result) {
|
90 | if (_worker_threads().isMainThread) {
|
91 | throw new Error('Child can only be used on a forked process');
|
92 | }
|
93 |
|
94 | _worker_threads().parentPort.postMessage([_types.PARENT_MESSAGE_OK, result]);
|
95 | }
|
96 |
|
97 | function reportClientError(error) {
|
98 | return reportError(error, _types.PARENT_MESSAGE_CLIENT_ERROR);
|
99 | }
|
100 |
|
101 | function reportInitializeError(error) {
|
102 | return reportError(error, _types.PARENT_MESSAGE_SETUP_ERROR);
|
103 | }
|
104 |
|
105 | function reportError(error, type) {
|
106 | if (_worker_threads().isMainThread) {
|
107 | throw new Error('Child can only be used on a forked process');
|
108 | }
|
109 |
|
110 | if (error == null) {
|
111 | error = new Error('"null" or "undefined" thrown');
|
112 | }
|
113 |
|
114 | _worker_threads().parentPort.postMessage([
|
115 | type,
|
116 | error.constructor && error.constructor.name,
|
117 | error.message,
|
118 | error.stack,
|
119 | typeof error === 'object' ? {...error} : error
|
120 | ]);
|
121 | }
|
122 |
|
123 | function end() {
|
124 | const main = require(file);
|
125 |
|
126 | if (!main.teardown) {
|
127 | exitProcess();
|
128 | return;
|
129 | }
|
130 |
|
131 | execFunction(main.teardown, main, [], exitProcess, exitProcess);
|
132 | }
|
133 |
|
134 | function exitProcess() {
|
135 |
|
136 | _worker_threads().parentPort.removeListener('message', messageListener);
|
137 | }
|
138 |
|
139 | function execMethod(method, args) {
|
140 | const main = require(file);
|
141 |
|
142 | let fn;
|
143 |
|
144 | if (method === 'default') {
|
145 | fn = main.__esModule ? main['default'] : main;
|
146 | } else {
|
147 | fn = main[method];
|
148 | }
|
149 |
|
150 | function execHelper() {
|
151 | execFunction(fn, main, args, reportSuccess, reportClientError);
|
152 | }
|
153 |
|
154 | if (initialized || !main.setup) {
|
155 | execHelper();
|
156 | return;
|
157 | }
|
158 |
|
159 | initialized = true;
|
160 | execFunction(main.setup, main, setupArgs, execHelper, reportInitializeError);
|
161 | }
|
162 |
|
163 | function execFunction(fn, ctx, args, onResult, onError) {
|
164 | let result;
|
165 |
|
166 | try {
|
167 | result = fn.apply(ctx, args);
|
168 | } catch (err) {
|
169 | onError(err);
|
170 | return;
|
171 | }
|
172 |
|
173 | if ((0, _jestUtil().isPromise)(result)) {
|
174 | result.then(onResult, onError);
|
175 | } else {
|
176 | onResult(result);
|
177 | }
|
178 | }
|