Node updated. Some todos.

This commit is contained in:
Norm Rasmussen
2024-09-23 20:52:09 -04:00
parent 8bfaca8375
commit f25622067f
2041 changed files with 124145 additions and 110445 deletions

View File

@ -104,19 +104,69 @@ export function computeSystemExecutablePath(options: SystemOptions): string {
* @public
*/
export interface LaunchOptions {
/**
* Absolute path to the browser's executable.
*/
executablePath: string;
/**
* Configures stdio streams to open two additional streams for automation over
* those streams instead of WebSocket.
*
* @defaultValue `false`.
*/
pipe?: boolean;
/**
* If true, forwards the browser's process stdout and stderr to the Node's
* process stdout and stderr.
*
* @defaultValue `false`.
*/
dumpio?: boolean;
/**
* Additional arguments to pass to the executable when launching.
*/
args?: string[];
/**
* Environment variables to set for the browser process.
*/
env?: Record<string, string | undefined>;
/**
* Handles SIGINT in the Node process and tries to kill the browser process.
*
* @defaultValue `true`.
*/
handleSIGINT?: boolean;
/**
* Handles SIGTERM in the Node process and tries to gracefully close the browser
* process.
*
* @defaultValue `true`.
*/
handleSIGTERM?: boolean;
/**
* Handles SIGHUP in the Node process and tries to gracefully close the browser process.
*
* @defaultValue `true`.
*/
handleSIGHUP?: boolean;
/**
* Whether to spawn process in the {@link https://nodejs.org/api/child_process.html#optionsdetached | detached}
* mode.
*
* @defaultValue `true` except on Windows.
*/
detached?: boolean;
/**
* A callback to run after the browser process exits or before the process
* will be closed via the {@link Process.close} call (including when handling
* signals). The callback is only run once.
*/
onExit?: () => Promise<void>;
}
/**
* Launches a browser process according to {@link LaunchOptions}.
*
* @public
*/
export function launch(opts: LaunchOptions): Process {
@ -135,6 +185,59 @@ export const CDP_WEBSOCKET_ENDPOINT_REGEX =
export const WEBDRIVER_BIDI_WEBSOCKET_ENDPOINT_REGEX =
/^WebDriver BiDi listening on (ws:\/\/.*)$/;
type EventHandler = (...args: any[]) => void;
const processListeners = new Map<string, EventHandler[]>();
const dispatchers = {
exit: (...args: any[]) => {
processListeners.get('exit')?.forEach(handler => {
return handler(...args);
});
},
SIGINT: (...args: any[]) => {
processListeners.get('SIGINT')?.forEach(handler => {
return handler(...args);
});
},
SIGHUP: (...args: any[]) => {
processListeners.get('SIGHUP')?.forEach(handler => {
return handler(...args);
});
},
SIGTERM: (...args: any[]) => {
processListeners.get('SIGTERM')?.forEach(handler => {
return handler(...args);
});
},
};
function subscribeToProcessEvent(
event: 'exit' | 'SIGINT' | 'SIGHUP' | 'SIGTERM',
handler: EventHandler
): void {
const listeners = processListeners.get(event) || [];
if (listeners.length === 0) {
process.on(event, dispatchers[event]);
}
listeners.push(handler);
processListeners.set(event, listeners);
}
function unsubscribeFromProcessEvent(
event: 'exit' | 'SIGINT' | 'SIGHUP' | 'SIGTERM',
handler: EventHandler
): void {
const listeners = processListeners.get(event) || [];
const existingListenerIdx = listeners.indexOf(handler);
if (existingListenerIdx === -1) {
return;
}
listeners.splice(existingListenerIdx, 1);
processListeners.set(event, listeners);
if (listeners.length === 0) {
process.off(event, dispatchers[event]);
}
}
/**
* @public
*/
@ -201,15 +304,15 @@ export class Process {
this.#browserProcess.stderr?.pipe(process.stderr);
this.#browserProcess.stdout?.pipe(process.stdout);
}
process.on('exit', this.#onDriverProcessExit);
subscribeToProcessEvent('exit', this.#onDriverProcessExit);
if (opts.handleSIGINT) {
process.on('SIGINT', this.#onDriverProcessSignal);
subscribeToProcessEvent('SIGINT', this.#onDriverProcessSignal);
}
if (opts.handleSIGTERM) {
process.on('SIGTERM', this.#onDriverProcessSignal);
subscribeToProcessEvent('SIGTERM', this.#onDriverProcessSignal);
}
if (opts.handleSIGHUP) {
process.on('SIGHUP', this.#onDriverProcessSignal);
subscribeToProcessEvent('SIGHUP', this.#onDriverProcessSignal);
}
if (opts.onExit) {
this.#onExitHook = opts.onExit;
@ -262,10 +365,10 @@ export class Process {
}
#clearListeners(): void {
process.off('exit', this.#onDriverProcessExit);
process.off('SIGINT', this.#onDriverProcessSignal);
process.off('SIGTERM', this.#onDriverProcessSignal);
process.off('SIGHUP', this.#onDriverProcessSignal);
unsubscribeFromProcessEvent('exit', this.#onDriverProcessExit);
unsubscribeFromProcessEvent('SIGINT', this.#onDriverProcessSignal);
unsubscribeFromProcessEvent('SIGTERM', this.#onDriverProcessSignal);
unsubscribeFromProcessEvent('SIGHUP', this.#onDriverProcessSignal);
}
#onDriverProcessExit = (_code: number) => {