NodeThreadsWorker.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _stream() {
  7. const data = require('stream');
  8. _stream = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _worker_threads() {
  14. const data = require('worker_threads');
  15. _worker_threads = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _mergeStream() {
  21. const data = _interopRequireDefault(require('merge-stream'));
  22. _mergeStream = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. var _types = require('../types');
  28. function _interopRequireDefault(obj) {
  29. return obj && obj.__esModule ? obj : {default: obj};
  30. }
  31. /**
  32. * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
  33. *
  34. * This source code is licensed under the MIT license found in the
  35. * LICENSE file in the root directory of this source tree.
  36. */
  37. class ExperimentalWorker {
  38. _worker;
  39. _options;
  40. _request;
  41. _retries;
  42. _onProcessEnd;
  43. _onCustomMessage;
  44. _fakeStream;
  45. _stdout;
  46. _stderr;
  47. _exitPromise;
  48. _resolveExitPromise;
  49. _forceExited;
  50. constructor(options) {
  51. this._options = options;
  52. this._request = null;
  53. this._fakeStream = null;
  54. this._stdout = null;
  55. this._stderr = null;
  56. this._exitPromise = new Promise(resolve => {
  57. this._resolveExitPromise = resolve;
  58. });
  59. this._forceExited = false;
  60. this.initialize();
  61. }
  62. initialize() {
  63. this._worker = new (_worker_threads().Worker)(
  64. require.resolve('./threadChild'),
  65. {
  66. eval: false,
  67. resourceLimits: this._options.resourceLimits,
  68. stderr: true,
  69. stdout: true,
  70. workerData: this._options.workerData,
  71. ...this._options.forkOptions
  72. }
  73. );
  74. if (this._worker.stdout) {
  75. if (!this._stdout) {
  76. // We need to add a permanent stream to the merged stream to prevent it
  77. // from ending when the subprocess stream ends
  78. this._stdout = (0, _mergeStream().default)(this._getFakeStream());
  79. }
  80. this._stdout.add(this._worker.stdout);
  81. }
  82. if (this._worker.stderr) {
  83. if (!this._stderr) {
  84. // We need to add a permanent stream to the merged stream to prevent it
  85. // from ending when the subprocess stream ends
  86. this._stderr = (0, _mergeStream().default)(this._getFakeStream());
  87. }
  88. this._stderr.add(this._worker.stderr);
  89. }
  90. this._worker.on('message', this._onMessage.bind(this));
  91. this._worker.on('exit', this._onExit.bind(this));
  92. this._worker.postMessage([
  93. _types.CHILD_MESSAGE_INITIALIZE,
  94. false,
  95. this._options.workerPath,
  96. this._options.setupArgs,
  97. String(this._options.workerId + 1) // 0-indexed workerId, 1-indexed JEST_WORKER_ID
  98. ]);
  99. this._retries++; // If we exceeded the amount of retries, we will emulate an error reply
  100. // coming from the child. This avoids code duplication related with cleaning
  101. // the queue, and scheduling the next call.
  102. if (this._retries > this._options.maxRetries) {
  103. const error = new Error('Call retries were exceeded');
  104. this._onMessage([
  105. _types.PARENT_MESSAGE_CLIENT_ERROR,
  106. error.name,
  107. error.message,
  108. error.stack,
  109. {
  110. type: 'WorkerError'
  111. }
  112. ]);
  113. }
  114. }
  115. _shutdown() {
  116. // End the permanent stream so the merged stream end too
  117. if (this._fakeStream) {
  118. this._fakeStream.end();
  119. this._fakeStream = null;
  120. }
  121. this._resolveExitPromise();
  122. }
  123. _onMessage(response) {
  124. let error;
  125. switch (response[0]) {
  126. case _types.PARENT_MESSAGE_OK:
  127. this._onProcessEnd(null, response[1]);
  128. break;
  129. case _types.PARENT_MESSAGE_CLIENT_ERROR:
  130. error = response[4];
  131. if (error != null && typeof error === 'object') {
  132. const extra = error; // @ts-expect-error: no index
  133. const NativeCtor = globalThis[response[1]];
  134. const Ctor = typeof NativeCtor === 'function' ? NativeCtor : Error;
  135. error = new Ctor(response[2]);
  136. error.type = response[1];
  137. error.stack = response[3];
  138. for (const key in extra) {
  139. // @ts-expect-error: no index
  140. error[key] = extra[key];
  141. }
  142. }
  143. this._onProcessEnd(error, null);
  144. break;
  145. case _types.PARENT_MESSAGE_SETUP_ERROR:
  146. error = new Error(`Error when calling setup: ${response[2]}`); // @ts-expect-error: adding custom properties to errors.
  147. error.type = response[1];
  148. error.stack = response[3];
  149. this._onProcessEnd(error, null);
  150. break;
  151. case _types.PARENT_MESSAGE_CUSTOM:
  152. this._onCustomMessage(response[1]);
  153. break;
  154. default:
  155. throw new TypeError(`Unexpected response from worker: ${response[0]}`);
  156. }
  157. }
  158. _onExit(exitCode) {
  159. if (exitCode !== 0 && !this._forceExited) {
  160. this.initialize();
  161. if (this._request) {
  162. this._worker.postMessage(this._request);
  163. }
  164. } else {
  165. this._shutdown();
  166. }
  167. }
  168. waitForExit() {
  169. return this._exitPromise;
  170. }
  171. forceExit() {
  172. this._forceExited = true;
  173. this._worker.terminate();
  174. }
  175. send(request, onProcessStart, onProcessEnd, onCustomMessage) {
  176. onProcessStart(this);
  177. this._onProcessEnd = (...args) => {
  178. var _onProcessEnd;
  179. // Clean the request to avoid sending past requests to workers that fail
  180. // while waiting for a new request (timers, unhandled rejections...)
  181. this._request = null;
  182. const res =
  183. (_onProcessEnd = onProcessEnd) === null || _onProcessEnd === void 0
  184. ? void 0
  185. : _onProcessEnd(...args); // Clean up the reference so related closures can be garbage collected.
  186. onProcessEnd = null;
  187. return res;
  188. };
  189. this._onCustomMessage = (...arg) => onCustomMessage(...arg);
  190. this._request = request;
  191. this._retries = 0;
  192. this._worker.postMessage(request);
  193. }
  194. getWorkerId() {
  195. return this._options.workerId;
  196. }
  197. getStdout() {
  198. return this._stdout;
  199. }
  200. getStderr() {
  201. return this._stderr;
  202. }
  203. _getFakeStream() {
  204. if (!this._fakeStream) {
  205. this._fakeStream = new (_stream().PassThrough)();
  206. }
  207. return this._fakeStream;
  208. }
  209. }
  210. exports.default = ExperimentalWorker;