rpc-worker.js 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. Object.defineProperty(exports, "__esModule", { value: true });
  22. exports.getRpcWorkerData = exports.createRpcWorker = void 0;
  23. const child_process = __importStar(require("child_process"));
  24. const process = __importStar(require("process"));
  25. const wrap_rpc_1 = require("./wrap-rpc");
  26. const WORKER_DATA_ENV_KEY = 'WORKER_DATA';
  27. function createRpcWorker(modulePath, data, memoryLimit) {
  28. const options = {
  29. env: Object.assign(Object.assign({}, process.env), { [WORKER_DATA_ENV_KEY]: JSON.stringify(data || {}) }),
  30. stdio: ['inherit', 'inherit', 'inherit', 'ipc'],
  31. serialization: 'advanced',
  32. };
  33. if (memoryLimit) {
  34. options.execArgv = [`--max-old-space-size=${memoryLimit}`];
  35. }
  36. let childProcess;
  37. let remoteMethod;
  38. const worker = {
  39. connect() {
  40. if (childProcess && !childProcess.connected) {
  41. childProcess.kill('SIGTERM');
  42. childProcess = undefined;
  43. remoteMethod = undefined;
  44. }
  45. if (!(childProcess === null || childProcess === void 0 ? void 0 : childProcess.connected)) {
  46. childProcess = child_process.fork(modulePath, options);
  47. remoteMethod = (0, wrap_rpc_1.wrapRpc)(childProcess);
  48. }
  49. },
  50. terminate() {
  51. if (childProcess) {
  52. childProcess.kill('SIGTERM');
  53. childProcess = undefined;
  54. remoteMethod = undefined;
  55. }
  56. },
  57. get connected() {
  58. return Boolean(childProcess === null || childProcess === void 0 ? void 0 : childProcess.connected);
  59. },
  60. get process() {
  61. return childProcess;
  62. },
  63. };
  64. return Object.assign((...args) => {
  65. if (!worker.connected) {
  66. // try to auto-connect
  67. worker.connect();
  68. }
  69. if (!remoteMethod) {
  70. return Promise.reject('Worker is not connected - cannot perform RPC.');
  71. }
  72. return remoteMethod(...args);
  73. }, worker);
  74. }
  75. exports.createRpcWorker = createRpcWorker;
  76. function getRpcWorkerData() {
  77. return JSON.parse(process.env[WORKER_DATA_ENV_KEY] || '{}');
  78. }
  79. exports.getRpcWorkerData = getRpcWorkerData;