* chore: initial scaffolding * chore: implement interface and docs * chore: address code style review * fix: cleanup of utility process on shutdown * chore: simplify NodeBindings::CreateEnvironment * chore: rename disableLibraryValidation => allowLoadingUnsignedLibraries * chore: implement process.parentPort * chore(posix): implement stdio pipe interface * chore(win): implement stdio interface * chore: reenable SetNodeOptions for utility process * chore: add specs * chore: fix lint * fix: update kill API * fix: update process.parentPort API * fix: exit event * docs: update exit event * fix: tests on linux * chore: expand on some comments * fix: shutdown of pipe reader Avoid logging since it is always the case that reader end of pipe will terminate after the child process. * fix: remove exit code check for crash spec * fix: rm PR_SET_NO_NEW_PRIVS for unsandbox utility process * chore: fix incorrect rebase * fix: address review feedback * chore: rename utility_process -> utility * chore: update docs * chore: cleanup c++ implemantation * fix: leak in NodeServiceHost impl * chore: minor cleanup * chore: cleanup JS implementation * chore: flip default stdio to inherit * fix: some api improvements * Support cwd option * Remove path restriction for modulePath * Rewire impl for env support * fix: add tests for cwd and env option * chore: alt impl for reading stdio handles * chore: support message queuing * chore: fix lint * chore: new UtilityProcess => utilityProcess.fork * fix: support for uncaught exception exits * chore: remove process.execArgv as default * fix: windows build * fix: style changes * fix: docs and style changes * chore: update patches * spec: disable flaky test on win32 arm CI Co-authored-by: PatchUp <73610968+patchup[bot]@users.noreply.github.com>
5.2 KiB
utilityProcess
utilityProcess creates a child process with
Node.js and Message ports enabled. It provides the equivalent of child_process.fork API from Node.js
but instead uses Services API from Chromium to launch the child process.
Process: Main
Methods
utilityProcess.fork(modulePath[, args][, options])
modulePathstring - Path to the script that should run as entrypoint in the child process.argsstring[] (optional) - List of string arguments that will be available asprocess.argvin the child process.optionsObject (optional)envObject (optional) - Environment key-value pairs. Default isprocess.env.execArgvstring[] (optional) - List of string arguments passed to the executable.cwdstring (optional) - Current working directory of the child process.stdio(string[] | string) (optional) - Allows configuring the mode forstdoutandstderrof the child process. Default isinherit. String value can be one ofpipe,ignore,inherit, for more details on these values you can refer to stdio documentation from Node.js. Currently this option only supports configuringstdoutandstderrto eitherpipe,inheritorignore. Configuringstdinis not supported;stdinwill always be ignored. For example, the supported values will be processed as following:pipe: equivalent to ['ignore', 'pipe', 'pipe'] (the default)ignore: equivalent to 'ignore', 'ignore', 'ignore']inherit: equivalent to ['ignore', 'inherit', 'inherit']
serviceNamestring (optional) - Name of the process that will appear innameproperty ofchild-process-goneevent ofapp. Default isnode.mojom.NodeService.allowLoadingUnsignedLibrariesboolean (optional) macOS - With this flag, the utility process will be launched via theElectron Helper (Plugin).apphelper executable on macOS, which can be codesigned withcom.apple.security.cs.disable-library-validationandcom.apple.security.cs.allow-unsigned-executable-memoryentitlements. This will allow the utility process to load unsigned libraries. Unless you specifically need this capability, it is best to leave this disabled. Default isfalse.
Returns UtilityProcess
Class: UtilityProcess
Instances of the
UtilityProcessrepresent the Chromium spawned child process with Node.js integration.
UtilityProcess is an [EventEmitter][event-emitter].
Instance Methods
child.postMessage(message, [transfer])
messageanytransferMessagePortMain[] (optional)
Send a message to the child process, optionally transferring ownership of
zero or more [MessagePortMain][] objects.
For example:
// Main process
const { port1, port2 } = new MessageChannelMain()
const child = utilityProcess.fork(path.join(__dirname, 'test.js'))
child.postMessage({ message: 'hello' }, [port1])
// Child process
process.parentPort.once('message', (e) => {
const [port] = e.ports
// ...
})
child.kill()
Returns boolean
Terminates the process gracefully. On POSIX, it uses SIGTERM but will ensure the process is reaped on exit. This function returns true if the kill is successful, and false otherwise.
Instance Properties
child.pid
A Integer | undefined representing the process identifier (PID) of the child process.
If the child process fails to spawn due to errors, then the value is undefined. When
the child process exits, then the value is undefined after the exit event is emitted.
child.stdout
A NodeJS.ReadableStream | null that represents the child process's stdout.
If the child was spawned with options.stdio[1] set to anything other than 'pipe', then this will be null.
When the child process exits, then the value is null after the exit event is emitted.
// Main process
const { port1, port2 } = new MessageChannelMain()
const child = utilityProcess.fork(path.join(__dirname, 'test.js'))
child.stdout.on('data', (data) => {
console.log(`Received chunk ${data}`)
})
child.stderr
A NodeJS.ReadableStream | null that represents the child process's stderr.
If the child was spawned with options.stdio[2] set to anything other than 'pipe', then this will be null.
When the child process exits, then the value is null after the exit event is emitted.
Instance Events
Event: 'spawn'
Emitted once the child process has spawned successfully.
Event: 'exit'
Returns:
codenumber - Contains the exit code for the process obtained from waitpid on posix, or GetExitCodeProcess on windows.
Emitted after the child process ends.
Event: 'message'
Returns:
messageany
Emitted when the child process sends a message using process.parentPort.postMessage().