NodeThreadsWorker.js 6.7 KB

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