UNPKG

4.11 kBJavaScriptView Raw
1'use strict';
2
3function _worker_threads() {
4 const data = require('worker_threads');
5
6 _worker_threads = function () {
7 return data;
8 };
9
10 return data;
11}
12
13function _jestUtil() {
14 const data = require('jest-util');
15
16 _jestUtil = function () {
17 return data;
18 };
19
20 return data;
21}
22
23var _types = require('../types');
24
25/**
26 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
27 *
28 * This source code is licensed under the MIT license found in the
29 * LICENSE file in the root directory of this source tree.
30 */
31let file = null;
32let setupArgs = [];
33let initialized = false;
34/**
35 * This file is a small bootstrapper for workers. It sets up the communication
36 * between the worker and the parent process, interpreting parent messages and
37 * sending results back.
38 *
39 * The file loaded will be lazily initialized the first time any of the workers
40 * is called. This is done for optimal performance: if the farm is initialized,
41 * but no call is made to it, child Node processes will be consuming the least
42 * possible amount of memory.
43 *
44 * If an invalid message is detected, the child will exit (by throwing) with a
45 * non-zero exit code.
46 */
47
48const 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
79function 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
89function 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
97function reportClientError(error) {
98 return reportError(error, _types.PARENT_MESSAGE_CLIENT_ERROR);
99}
100
101function reportInitializeError(error) {
102 return reportError(error, _types.PARENT_MESSAGE_SETUP_ERROR);
103}
104
105function 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
123function 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
134function exitProcess() {
135 // Clean up open handles so the worker ideally exits gracefully
136 _worker_threads().parentPort.removeListener('message', messageListener);
137}
138
139function 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
163function 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}