NodeThreadsWorker.js 8.0 KB

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