methods.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import process from 'node:process';
  2. import {sendMessage} from './send.js';
  3. import {getOneMessage} from './get-one.js';
  4. import {getEachMessage} from './get-each.js';
  5. import {getCancelSignal} from './graceful.js';
  6. // Add promise-based IPC methods in current process
  7. export const addIpcMethods = (subprocess, {ipc}) => {
  8. Object.assign(subprocess, getIpcMethods(subprocess, false, ipc));
  9. };
  10. // Get promise-based IPC in the subprocess
  11. export const getIpcExport = () => {
  12. const anyProcess = process;
  13. const isSubprocess = true;
  14. const ipc = process.channel !== undefined;
  15. return {
  16. ...getIpcMethods(anyProcess, isSubprocess, ipc),
  17. getCancelSignal: getCancelSignal.bind(undefined, {
  18. anyProcess,
  19. channel: anyProcess.channel,
  20. isSubprocess,
  21. ipc,
  22. }),
  23. };
  24. };
  25. // Retrieve the `ipc` shared by both the current process and the subprocess
  26. const getIpcMethods = (anyProcess, isSubprocess, ipc) => ({
  27. sendMessage: sendMessage.bind(undefined, {
  28. anyProcess,
  29. channel: anyProcess.channel,
  30. isSubprocess,
  31. ipc,
  32. }),
  33. getOneMessage: getOneMessage.bind(undefined, {
  34. anyProcess,
  35. channel: anyProcess.channel,
  36. isSubprocess,
  37. ipc,
  38. }),
  39. getEachMessage: getEachMessage.bind(undefined, {
  40. anyProcess,
  41. channel: anyProcess.channel,
  42. isSubprocess,
  43. ipc,
  44. }),
  45. });