feat: UtilityProcess API (#34980)
* 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>
This commit is contained in:
parent
44c40efecf
commit
da0fd286b4
59 changed files with 2700 additions and 54 deletions
|
@ -31,6 +31,7 @@ export const browserModuleList: ElectronInternal.ModuleEntry[] = [
|
|||
{ name: 'systemPreferences', loader: () => require('./system-preferences') },
|
||||
{ name: 'TouchBar', loader: () => require('./touch-bar') },
|
||||
{ name: 'Tray', loader: () => require('./tray') },
|
||||
{ name: 'utilityProcess', loader: () => require('./utility-process') },
|
||||
{ name: 'View', loader: () => require('./view') },
|
||||
{ name: 'webContents', loader: () => require('./web-contents') },
|
||||
{ name: 'WebContentsView', loader: () => require('./web-contents-view') },
|
||||
|
|
150
lib/browser/api/utility-process.ts
Normal file
150
lib/browser/api/utility-process.ts
Normal file
|
@ -0,0 +1,150 @@
|
|||
import { EventEmitter } from 'events';
|
||||
import { Duplex, PassThrough } from 'stream';
|
||||
import { Socket } from 'net';
|
||||
import { MessagePortMain } from '@electron/internal/browser/message-port-main';
|
||||
const { _fork } = process._linkedBinding('electron_browser_utility_process');
|
||||
|
||||
class ForkUtilityProcess extends EventEmitter {
|
||||
#handle: ElectronInternal.UtilityProcessWrapper | null;
|
||||
#stdout: Duplex | null = null;
|
||||
#stderr: Duplex | null = null;
|
||||
constructor (modulePath: string, args?: string[], options?: Electron.ForkOptions) {
|
||||
super();
|
||||
|
||||
if (!modulePath) {
|
||||
throw new Error('Missing UtilityProcess entry script.');
|
||||
}
|
||||
|
||||
if (args == null) {
|
||||
args = [];
|
||||
} else if (typeof args === 'object' && !Array.isArray(args)) {
|
||||
options = args;
|
||||
args = [];
|
||||
}
|
||||
|
||||
if (options == null) {
|
||||
options = {};
|
||||
} else {
|
||||
options = { ...options };
|
||||
}
|
||||
|
||||
if (!options) {
|
||||
throw new Error('Options cannot be undefined.');
|
||||
}
|
||||
|
||||
if (options.execArgv != null) {
|
||||
if (!Array.isArray(options.execArgv)) {
|
||||
throw new Error('execArgv must be an array of strings.');
|
||||
}
|
||||
}
|
||||
|
||||
if (options.serviceName != null) {
|
||||
if (typeof options.serviceName !== 'string') {
|
||||
throw new Error('serviceName must be a string.');
|
||||
}
|
||||
}
|
||||
|
||||
if (options.cwd != null) {
|
||||
if (typeof options.cwd !== 'string') {
|
||||
throw new Error('cwd path must be a string.');
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof options.stdio === 'string') {
|
||||
const stdio : Array<'pipe' | 'ignore' | 'inherit'> = [];
|
||||
switch (options.stdio) {
|
||||
case 'inherit':
|
||||
case 'ignore':
|
||||
stdio.push('ignore', options.stdio, options.stdio);
|
||||
break;
|
||||
case 'pipe':
|
||||
this.#stderr = new PassThrough();
|
||||
this.#stdout = new PassThrough();
|
||||
stdio.push('ignore', options.stdio, options.stdio);
|
||||
break;
|
||||
default:
|
||||
throw new Error('stdio must be of the following values: inherit, pipe, ignore');
|
||||
}
|
||||
options.stdio = stdio;
|
||||
} else if (Array.isArray(options.stdio)) {
|
||||
if (options.stdio.length >= 3) {
|
||||
if (options.stdio[0] !== 'ignore') {
|
||||
throw new Error('stdin value other than ignore is not supported.');
|
||||
}
|
||||
|
||||
if (options.stdio[1] === 'pipe') {
|
||||
this.#stdout = new PassThrough();
|
||||
} else if (options.stdio[1] !== 'ignore' && options.stdio[1] !== 'inherit') {
|
||||
throw new Error('stdout configuration must be of the following values: inherit, pipe, ignore');
|
||||
}
|
||||
|
||||
if (options.stdio[2] === 'pipe') {
|
||||
this.#stderr = new PassThrough();
|
||||
} else if (options.stdio[2] !== 'ignore' && options.stdio[2] !== 'inherit') {
|
||||
throw new Error('stderr configuration must be of the following values: inherit, pipe, ignore');
|
||||
}
|
||||
} else {
|
||||
throw new Error('configuration missing for stdin, stdout or stderr.');
|
||||
}
|
||||
}
|
||||
|
||||
this.#handle = _fork({ options, modulePath, args });
|
||||
this.#handle!.emit = (channel: string | symbol, ...args: any[]) => {
|
||||
if (channel === 'exit') {
|
||||
try {
|
||||
this.emit('exit', ...args);
|
||||
} finally {
|
||||
this.#handle = null;
|
||||
if (this.#stdout) {
|
||||
this.#stdout.removeAllListeners();
|
||||
this.#stdout = null;
|
||||
}
|
||||
if (this.#stderr) {
|
||||
this.#stderr.removeAllListeners();
|
||||
this.#stderr = null;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else if (channel === 'stdout' && this.#stdout) {
|
||||
new Socket({ fd: args[0], readable: true }).pipe(this.#stdout);
|
||||
return true;
|
||||
} else if (channel === 'stderr' && this.#stderr) {
|
||||
new Socket({ fd: args[0], readable: true }).pipe(this.#stderr);
|
||||
return true;
|
||||
} else {
|
||||
return this.emit(channel, ...args);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
get pid () {
|
||||
return this.#handle?.pid;
|
||||
}
|
||||
|
||||
get stdout () {
|
||||
return this.#stdout;
|
||||
}
|
||||
|
||||
get stderr () {
|
||||
return this.#stderr;
|
||||
}
|
||||
|
||||
postMessage (message: any, transfer?: MessagePortMain[]) {
|
||||
if (Array.isArray(transfer)) {
|
||||
transfer = transfer.map((o: any) => o instanceof MessagePortMain ? o._internalPort : o);
|
||||
return this.#handle?.postMessage(message, transfer);
|
||||
}
|
||||
return this.#handle?.postMessage(message);
|
||||
}
|
||||
|
||||
kill () : boolean {
|
||||
if (this.#handle === null) {
|
||||
return false;
|
||||
}
|
||||
return this.#handle.kill();
|
||||
}
|
||||
}
|
||||
|
||||
export function fork (modulePath: string, args?: string[], options?: Electron.ForkOptions) {
|
||||
return new ForkUtilityProcess(modulePath, args, options);
|
||||
}
|
|
@ -33,20 +33,29 @@ function wrap <T extends AnyFn> (func: T, wrapper: (fn: AnyFn) => T) {
|
|||
return wrapped;
|
||||
}
|
||||
|
||||
// process.nextTick and setImmediate make use of uv_check and uv_prepare to
|
||||
// run the callbacks, however since we only run uv loop on requests, the
|
||||
// callbacks wouldn't be called until something else activated the uv loop,
|
||||
// which would delay the callbacks for arbitrary long time. So we should
|
||||
// initiatively activate the uv loop once process.nextTick and setImmediate is
|
||||
// called.
|
||||
process.nextTick = wrapWithActivateUvLoop(process.nextTick);
|
||||
|
||||
global.setImmediate = timers.setImmediate = wrapWithActivateUvLoop(timers.setImmediate);
|
||||
global.clearImmediate = timers.clearImmediate;
|
||||
|
||||
// setTimeout needs to update the polling timeout of the event loop, when
|
||||
// called under Chromium's event loop the node's event loop won't get a chance
|
||||
// to update the timeout, so we have to force the node's event loop to
|
||||
// recalculate the timeout in browser process.
|
||||
// recalculate the timeout in the process.
|
||||
timers.setTimeout = wrapWithActivateUvLoop(timers.setTimeout);
|
||||
timers.setInterval = wrapWithActivateUvLoop(timers.setInterval);
|
||||
|
||||
// Only override the global setTimeout/setInterval impls in the browser process
|
||||
if (process.type === 'browser') {
|
||||
// Update the global version of the timer apis to use the above wrapper
|
||||
// only in the process that runs node event loop alongside chromium
|
||||
// event loop. We skip renderer with nodeIntegration here because node globals
|
||||
// are deleted in these processes, see renderer/init.js for reference.
|
||||
if (process.type === 'browser' ||
|
||||
process.type === 'utility') {
|
||||
global.setTimeout = timers.setTimeout;
|
||||
global.setInterval = timers.setInterval;
|
||||
}
|
||||
|
|
21
lib/utility/.eslintrc.json
Normal file
21
lib/utility/.eslintrc.json
Normal file
|
@ -0,0 +1,21 @@
|
|||
{
|
||||
"rules": {
|
||||
"no-restricted-imports": [
|
||||
"error",
|
||||
{
|
||||
"paths": [
|
||||
"electron",
|
||||
"electron/renderer"
|
||||
],
|
||||
"patterns": [
|
||||
"./*",
|
||||
"../*",
|
||||
"@electron/internal/isolated_renderer/*",
|
||||
"@electron/internal/renderer/*",
|
||||
"@electron/internal/sandboxed_worker/*",
|
||||
"@electron/internal/worker/*"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
6
lib/utility/api/exports/electron.ts
Normal file
6
lib/utility/api/exports/electron.ts
Normal file
|
@ -0,0 +1,6 @@
|
|||
import { defineProperties } from '@electron/internal/common/define-properties';
|
||||
import { utilityNodeModuleList } from '@electron/internal/utility/api/module-list';
|
||||
|
||||
module.exports = {};
|
||||
|
||||
defineProperties(module.exports, utilityNodeModuleList);
|
2
lib/utility/api/module-list.ts
Normal file
2
lib/utility/api/module-list.ts
Normal file
|
@ -0,0 +1,2 @@
|
|||
// Utility side modules, please sort alphabetically.
|
||||
export const utilityNodeModuleList: ElectronInternal.ModuleEntry[] = [];
|
38
lib/utility/init.ts
Normal file
38
lib/utility/init.ts
Normal file
|
@ -0,0 +1,38 @@
|
|||
import { ParentPort } from '@electron/internal/utility/parent-port';
|
||||
const Module = require('module');
|
||||
const v8Util = process._linkedBinding('electron_common_v8_util');
|
||||
|
||||
const entryScript: string = v8Util.getHiddenValue(process, '_serviceStartupScript');
|
||||
// We modified the original process.argv to let node.js load the init.js,
|
||||
// we need to restore it here.
|
||||
process.argv.splice(1, 1, entryScript);
|
||||
|
||||
// Clear search paths.
|
||||
require('../common/reset-search-paths');
|
||||
|
||||
// Import common settings.
|
||||
require('@electron/internal/common/init');
|
||||
|
||||
const parentPort: ParentPort = new ParentPort();
|
||||
Object.defineProperty(process, 'parentPort', {
|
||||
enumerable: true,
|
||||
writable: false,
|
||||
value: parentPort
|
||||
});
|
||||
|
||||
// Based on third_party/electron_node/lib/internal/worker/io.js
|
||||
parentPort.on('newListener', (name: string) => {
|
||||
if (name === 'message' && parentPort.listenerCount('message') === 0) {
|
||||
parentPort.start();
|
||||
}
|
||||
});
|
||||
|
||||
parentPort.on('removeListener', (name: string) => {
|
||||
if (name === 'message' && parentPort.listenerCount('message') === 0) {
|
||||
parentPort.pause();
|
||||
}
|
||||
});
|
||||
|
||||
// Finally load entry script.
|
||||
process._firstFileName = Module._resolveFilename(entryScript, null, false);
|
||||
Module._load(entryScript, Module, true);
|
30
lib/utility/parent-port.ts
Normal file
30
lib/utility/parent-port.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
import { EventEmitter } from 'events';
|
||||
import { MessagePortMain } from '@electron/internal/browser/message-port-main';
|
||||
const { createParentPort } = process._linkedBinding('electron_utility_parent_port');
|
||||
|
||||
export class ParentPort extends EventEmitter {
|
||||
#port: ParentPort
|
||||
constructor () {
|
||||
super();
|
||||
this.#port = createParentPort();
|
||||
this.#port.emit = (channel: string | symbol, event: { ports: any[] }) => {
|
||||
if (channel === 'message') {
|
||||
event = { ...event, ports: event.ports.map(p => new MessagePortMain(p)) };
|
||||
}
|
||||
this.emit(channel, event);
|
||||
return false;
|
||||
};
|
||||
}
|
||||
|
||||
start () : void {
|
||||
this.#port.start();
|
||||
}
|
||||
|
||||
pause () : void {
|
||||
this.#port.pause();
|
||||
}
|
||||
|
||||
postMessage (message: any) : void {
|
||||
this.#port.postMessage(message);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue