ipc-input.js
This file
Path
lib/ipc/ipc-input.js
55 lines · 1.5 KiB · JavaScript
Last changed by
Fix the `strict` and `ipcInput` options with the `serialization` optionSindre Sorhusbefore adoption9842f1a
History of this fileTake it
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import {serialize} from 'node:v8';import {sendMessage} from './send.js';
// Validate the `ipcInput` optionexport const validateIpcInputOption = ({ipcInput, ipc, serialization}) => { if (ipcInput === undefined) { return; }
if (!ipc) { throw new Error('The `ipcInput` option cannot be set unless the `ipc` option is `true`.'); }
// An invalid `serialization` option is reported by `node:child_process` when spawning, like when `ipcInput` is not used validateIpcInput[serialization]?.(ipcInput);};
const validateAdvancedInput = ipcInput => { try { serialize(ipcInput); } catch (error) { throw new Error('The `ipcInput` option is not serializable with a structured clone.', {cause: error}); }};
const validateJsonInput = ipcInput => { try { // `JSON.stringify()` returns `undefined` instead of throwing for values it cannot represent, such as functions and symbols if (JSON.stringify(ipcInput) === undefined) { throw new TypeError('The value cannot be represented as JSON.'); } } catch (error) { throw new Error('The `ipcInput` option is not serializable with JSON.', {cause: error}); }};
const validateIpcInput = { __proto__: null, advanced: validateAdvancedInput, json: validateJsonInput,};
// When the `ipcInput` option is set, it is sent as an initial IPC message to the subprocessexport const sendIpcInput = async (subprocess, ipcInput, ipc) => { if (ipcInput === undefined) { return; }
await sendMessage({ anyProcess: subprocess, channel: subprocess.channel, isSubprocess: false, ipc, }, ipcInput);};