NodeThreadsWorker.js 7.6 KB

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