UNPKG

3.77 kBJavaScriptView Raw
1'use strict';
2
3function _jestUtil() {
4 const data = require('jest-util');
5
6 _jestUtil = function () {
7 return data;
8 };
9
10 return data;
11}
12
13var _types = require('../types');
14
15/**
16 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
17 *
18 * This source code is licensed under the MIT license found in the
19 * LICENSE file in the root directory of this source tree.
20 */
21let file = null;
22let setupArgs = [];
23let initialized = false;
24/**
25 * This file is a small bootstrapper for workers. It sets up the communication
26 * between the worker and the parent process, interpreting parent messages and
27 * sending results back.
28 *
29 * The file loaded will be lazily initialized the first time any of the workers
30 * is called. This is done for optimal performance: if the farm is initialized,
31 * but no call is made to it, child Node processes will be consuming the least
32 * possible amount of memory.
33 *
34 * If an invalid message is detected, the child will exit (by throwing) with a
35 * non-zero exit code.
36 */
37
38const messageListener = request => {
39 switch (request[0]) {
40 case _types.CHILD_MESSAGE_INITIALIZE:
41 const init = request;
42 file = init[2];
43 setupArgs = request[3];
44 break;
45
46 case _types.CHILD_MESSAGE_CALL:
47 const call = request;
48 execMethod(call[2], call[3]);
49 break;
50
51 case _types.CHILD_MESSAGE_END:
52 end();
53 break;
54
55 case _types.CHILD_MESSAGE_MEM_USAGE:
56 reportMemoryUsage();
57 break;
58
59 default:
60 throw new TypeError(
61 `Unexpected request from parent process: ${request[0]}`
62 );
63 }
64};
65
66process.on('message', messageListener);
67
68function reportSuccess(result) {
69 if (!process || !process.send) {
70 throw new Error('Child can only be used on a forked process');
71 }
72
73 process.send([_types.PARENT_MESSAGE_OK, result]);
74}
75
76function reportClientError(error) {
77 return reportError(error, _types.PARENT_MESSAGE_CLIENT_ERROR);
78}
79
80function reportInitializeError(error) {
81 return reportError(error, _types.PARENT_MESSAGE_SETUP_ERROR);
82}
83
84function reportMemoryUsage() {
85 if (!process || !process.send) {
86 throw new Error('Child can only be used on a forked process');
87 }
88
89 const msg = [_types.PARENT_MESSAGE_MEM_USAGE, process.memoryUsage().heapUsed];
90 process.send(msg);
91}
92
93function reportError(error, type) {
94 if (!process || !process.send) {
95 throw new Error('Child can only be used on a forked process');
96 }
97
98 if (error == null) {
99 error = new Error('"null" or "undefined" thrown');
100 }
101
102 process.send([
103 type,
104 error.constructor && error.constructor.name,
105 error.message,
106 error.stack,
107 typeof error === 'object' ? {...error} : error
108 ]);
109}
110
111function end() {
112 const main = require(file);
113
114 if (!main.teardown) {
115 exitProcess();
116 return;
117 }
118
119 execFunction(main.teardown, main, [], exitProcess, exitProcess);
120}
121
122function exitProcess() {
123 // Clean up open handles so the process ideally exits gracefully
124 process.removeListener('message', messageListener);
125}
126
127function execMethod(method, args) {
128 const main = require(file);
129
130 let fn;
131
132 if (method === 'default') {
133 fn = main.__esModule ? main['default'] : main;
134 } else {
135 fn = main[method];
136 }
137
138 function execHelper() {
139 execFunction(fn, main, args, reportSuccess, reportClientError);
140 }
141
142 if (initialized || !main.setup) {
143 execHelper();
144 return;
145 }
146
147 initialized = true;
148 execFunction(main.setup, main, setupArgs, execHelper, reportInitializeError);
149}
150
151function execFunction(fn, ctx, args, onResult, onError) {
152 let result;
153
154 try {
155 result = fn.apply(ctx, args);
156 } catch (err) {
157 onError(err);
158 return;
159 }
160
161 if ((0, _jestUtil().isPromise)(result)) {
162 result.then(onResult, onError);
163 } else {
164 onResult(result);
165 }
166}