* Spawns a new Node.js process + fork. * @param {string|URL} modulePath * @param {string[]} [args] * @param {{ * cwd?: string | URL; * detached?: boolean; * env?: Record ; * execPath?: string; * execArgv?: string[]; * gid?: number; * serialization?: string; *
(modulePath, args = [], options)
| 123 | * @returns {ChildProcess} |
| 124 | */ |
| 125 | function fork(modulePath, args = [], options) { |
| 126 | modulePath = getValidatedPath(modulePath, 'modulePath'); |
| 127 | |
| 128 | // Get options and args arguments. |
| 129 | let execArgv; |
| 130 | |
| 131 | if (args == null) { |
| 132 | args = []; |
| 133 | } else if (typeof args === 'object' && !ArrayIsArray(args)) { |
| 134 | options = args; |
| 135 | args = []; |
| 136 | } else { |
| 137 | validateArray(args, 'args'); |
| 138 | } |
| 139 | |
| 140 | if (options != null) { |
| 141 | validateObject(options, 'options'); |
| 142 | } |
| 143 | options = { __proto__: null, ...options, shell: false }; |
| 144 | options.execPath ||= process.execPath; |
| 145 | validateArgumentNullCheck(options.execPath, 'options.execPath'); |
| 146 | |
| 147 | // Prepare arguments for fork: |
| 148 | execArgv = options.execArgv || process.execArgv; |
| 149 | validateArgumentsNullCheck(execArgv, 'options.execArgv'); |
| 150 | |
| 151 | if (execArgv === process.execArgv && process._eval != null) { |
| 152 | const index = ArrayPrototypeLastIndexOf(execArgv, process._eval); |
| 153 | if (index > 0) { |
| 154 | // Remove the -e switch to avoid fork bombing ourselves. |
| 155 | execArgv = ArrayPrototypeSlice(execArgv); |
| 156 | ArrayPrototypeSplice(execArgv, index - 1, 2); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | args = [...execArgv, modulePath, ...args]; |
| 161 | |
| 162 | if (typeof options.stdio === 'string') { |
| 163 | options.stdio = stdioStringToArray(options.stdio, 'ipc'); |
| 164 | } else if (!ArrayIsArray(options.stdio)) { |
| 165 | // Use a separate fd=3 for the IPC channel. Inherit stdin, stdout, |
| 166 | // and stderr from the parent if silent isn't set. |
| 167 | options.stdio = stdioStringToArray( |
| 168 | options.silent ? 'pipe' : 'inherit', |
| 169 | 'ipc'); |
| 170 | } else if (!ArrayPrototypeIncludes(options.stdio, 'ipc')) { |
| 171 | throw new ERR_CHILD_PROCESS_IPC_REQUIRED('options.stdio'); |
| 172 | } |
| 173 | |
| 174 | return spawn(options.execPath, args, options); |
| 175 | } |
| 176 | |
| 177 | function _forkChild(fd, serializationMode) { |
| 178 | // set process.send() |
no test coverage detected
searching dependent graphs…