main-base.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. /**
  2. * Wrapper for sandboxing.
  3. *
  4. */
  5. import { ChildProcessor } from './child-processor';
  6. import { ParentCommand, ChildCommand } from '../enums';
  7. import { errorToJSON, toString } from '../utils';
  8. export default (send, receiver) => {
  9. const childProcessor = new ChildProcessor(send, receiver);
  10. receiver === null || receiver === void 0 ? void 0 : receiver.on('message', async (msg) => {
  11. try {
  12. switch (msg.cmd) {
  13. case ChildCommand.Init:
  14. await childProcessor.init(msg.value);
  15. break;
  16. case ChildCommand.Start:
  17. await childProcessor.start(msg.job, msg === null || msg === void 0 ? void 0 : msg.token);
  18. break;
  19. case ChildCommand.Stop:
  20. break;
  21. case ChildCommand.Cancel:
  22. childProcessor.cancel(msg.value);
  23. break;
  24. }
  25. }
  26. catch (err) {
  27. console.error('Error handling child message');
  28. }
  29. });
  30. process.on('SIGTERM', () => childProcessor.waitForCurrentJobAndExit());
  31. process.on('SIGINT', () => childProcessor.waitForCurrentJobAndExit());
  32. process.on('uncaughtException', async (err) => {
  33. if (typeof err !== 'object') {
  34. err = new Error(toString(err));
  35. }
  36. await send({
  37. cmd: ParentCommand.Failed,
  38. value: errorToJSON(err),
  39. });
  40. // An uncaughException leaves this process in a potentially undetermined state so
  41. // we must exit
  42. process.exit();
  43. });
  44. };
  45. //# sourceMappingURL=main-base.js.map