ipc-input.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import {serialize} from 'node:v8';
  2. // Validate the `ipcInput` option
  3. export const validateIpcInputOption = ({ipcInput, ipc, serialization}) => {
  4. if (ipcInput === undefined) {
  5. return;
  6. }
  7. if (!ipc) {
  8. throw new Error('The `ipcInput` option cannot be set unless the `ipc` option is `true`.');
  9. }
  10. validateIpcInput[serialization](ipcInput);
  11. };
  12. const validateAdvancedInput = ipcInput => {
  13. try {
  14. serialize(ipcInput);
  15. } catch (error) {
  16. throw new Error('The `ipcInput` option is not serializable with a structured clone.', {cause: error});
  17. }
  18. };
  19. const validateJsonInput = ipcInput => {
  20. try {
  21. JSON.stringify(ipcInput);
  22. } catch (error) {
  23. throw new Error('The `ipcInput` option is not serializable with JSON.', {cause: error});
  24. }
  25. };
  26. const validateIpcInput = {
  27. advanced: validateAdvancedInput,
  28. json: validateJsonInput,
  29. };
  30. // When the `ipcInput` option is set, it is sent as an initial IPC message to the subprocess
  31. export const sendIpcInput = async (subprocess, ipcInput) => {
  32. if (ipcInput === undefined) {
  33. return;
  34. }
  35. await subprocess.sendMessage(ipcInput);
  36. };