2020-05-12 15:30:13 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2020 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import * as childProcess from 'child_process';
|
2021-11-10 12:31:15 +00:00
|
|
|
import * as fs from 'fs';
|
|
|
|
import * as path from 'path';
|
|
|
|
import * as readline from 'readline';
|
|
|
|
import removeFolder from 'rimraf';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {promisify} from 'util';
|
|
|
|
import {assert} from '../common/assert.js';
|
|
|
|
import {Connection} from '../common/Connection.js';
|
|
|
|
import {debug} from '../common/Debug.js';
|
|
|
|
import {TimeoutError} from '../common/Errors.js';
|
2022-05-31 14:34:16 +00:00
|
|
|
import {
|
|
|
|
debugError,
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener,
|
2022-06-10 13:27:42 +00:00
|
|
|
isErrnoException,
|
2022-06-14 11:16:21 +00:00
|
|
|
isErrorLike,
|
|
|
|
PuppeteerEventListener,
|
|
|
|
removeEventListeners,
|
|
|
|
} from '../common/util.js';
|
2022-06-22 13:25:44 +00:00
|
|
|
import {Product} from '../common/Product.js';
|
|
|
|
import {NodeWebSocketTransport as WebSocketTransport} from '../node/NodeWebSocketTransport.js';
|
|
|
|
import {LaunchOptions} from './LaunchOptions.js';
|
|
|
|
import {PipeTransport} from './PipeTransport.js';
|
2020-05-12 15:30:13 +00:00
|
|
|
|
2020-06-25 10:54:00 +00:00
|
|
|
const removeFolderAsync = promisify(removeFolder);
|
2021-11-10 12:31:15 +00:00
|
|
|
const renameAsync = promisify(fs.rename);
|
|
|
|
const unlinkAsync = promisify(fs.unlink);
|
|
|
|
|
2020-05-12 15:30:13 +00:00
|
|
|
const debugLauncher = debug('puppeteer:launcher');
|
2021-11-10 12:31:15 +00:00
|
|
|
|
2020-06-15 13:02:00 +00:00
|
|
|
const PROCESS_ERROR_EXPLANATION = `Puppeteer was unable to kill the process which ran the browser binary.
|
|
|
|
This means that, on future Puppeteer launches, Puppeteer might not be able to launch the browser.
|
|
|
|
Please check your open processes and ensure that the browser processes that Puppeteer launched have been killed.
|
|
|
|
If you think this is a bug, please report it on the Puppeteer issue tracker.`;
|
2020-05-12 15:30:13 +00:00
|
|
|
|
|
|
|
export class BrowserRunner {
|
2022-06-13 09:16:25 +00:00
|
|
|
#product: Product;
|
|
|
|
#executablePath: string;
|
|
|
|
#processArguments: string[];
|
|
|
|
#userDataDir: string;
|
|
|
|
#isTempUserDataDir?: boolean;
|
|
|
|
#closed = true;
|
|
|
|
#listeners: PuppeteerEventListener[] = [];
|
|
|
|
#processClosing!: Promise<void>;
|
2020-05-12 15:30:13 +00:00
|
|
|
|
2022-05-31 14:34:16 +00:00
|
|
|
proc?: childProcess.ChildProcess;
|
|
|
|
connection?: Connection;
|
2020-05-12 15:30:13 +00:00
|
|
|
|
|
|
|
constructor(
|
2021-03-05 09:00:56 +00:00
|
|
|
product: Product,
|
2020-05-12 15:30:13 +00:00
|
|
|
executablePath: string,
|
|
|
|
processArguments: string[],
|
2021-11-10 12:31:15 +00:00
|
|
|
userDataDir: string,
|
|
|
|
isTempUserDataDir?: boolean
|
2020-05-12 15:30:13 +00:00
|
|
|
) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#product = product;
|
|
|
|
this.#executablePath = executablePath;
|
|
|
|
this.#processArguments = processArguments;
|
|
|
|
this.#userDataDir = userDataDir;
|
|
|
|
this.#isTempUserDataDir = isTempUserDataDir;
|
2020-05-12 15:30:13 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 13:02:00 +00:00
|
|
|
start(options: LaunchOptions): void {
|
2022-06-22 13:25:44 +00:00
|
|
|
const {handleSIGINT, handleSIGTERM, handleSIGHUP, dumpio, env, pipe} =
|
2021-05-12 14:48:30 +00:00
|
|
|
options;
|
2021-10-07 21:43:57 +00:00
|
|
|
let stdio: Array<'ignore' | 'pipe'>;
|
2020-05-12 15:30:13 +00:00
|
|
|
if (pipe) {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (dumpio) {
|
|
|
|
stdio = ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'];
|
|
|
|
} else {
|
|
|
|
stdio = ['ignore', 'ignore', 'ignore', 'pipe', 'pipe'];
|
|
|
|
}
|
2021-10-07 21:43:57 +00:00
|
|
|
} else {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (dumpio) {
|
|
|
|
stdio = ['pipe', 'pipe', 'pipe'];
|
|
|
|
} else {
|
|
|
|
stdio = ['pipe', 'ignore', 'pipe'];
|
|
|
|
}
|
2020-05-12 15:30:13 +00:00
|
|
|
}
|
|
|
|
assert(!this.proc, 'This process has previously been started.');
|
|
|
|
debugLauncher(
|
2022-06-13 09:16:25 +00:00
|
|
|
`Calling ${this.#executablePath} ${this.#processArguments.join(' ')}`
|
2020-05-12 15:30:13 +00:00
|
|
|
);
|
|
|
|
this.proc = childProcess.spawn(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#executablePath,
|
|
|
|
this.#processArguments,
|
2020-05-12 15:30:13 +00:00
|
|
|
{
|
2020-06-19 14:39:03 +00:00
|
|
|
// On non-windows platforms, `detached: true` makes child process a
|
|
|
|
// leader of a new process group, making it possible to kill child
|
|
|
|
// process tree with `.kill(-pid)` command. @see
|
|
|
|
// https://nodejs.org/api/child_process.html#child_process_options_detached
|
2020-05-12 15:30:13 +00:00
|
|
|
detached: process.platform !== 'win32',
|
|
|
|
env,
|
|
|
|
stdio,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if (dumpio) {
|
2022-05-31 14:34:16 +00:00
|
|
|
this.proc.stderr?.pipe(process.stderr);
|
|
|
|
this.proc.stdout?.pipe(process.stdout);
|
2020-05-12 15:30:13 +00:00
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#closed = false;
|
|
|
|
this.#processClosing = new Promise((fulfill, reject) => {
|
2022-05-31 14:34:16 +00:00
|
|
|
this.proc!.once('exit', async () => {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#closed = true;
|
2020-05-12 15:30:13 +00:00
|
|
|
// Cleanup as processes exit.
|
2022-06-13 09:16:25 +00:00
|
|
|
if (this.#isTempUserDataDir) {
|
2021-11-10 12:31:15 +00:00
|
|
|
try {
|
2022-06-13 09:16:25 +00:00
|
|
|
await removeFolderAsync(this.#userDataDir);
|
2021-11-10 12:31:15 +00:00
|
|
|
fulfill();
|
|
|
|
} catch (error) {
|
2022-02-18 08:47:14 +00:00
|
|
|
debugError(error);
|
2021-11-10 12:31:15 +00:00
|
|
|
reject(error);
|
|
|
|
}
|
|
|
|
} else {
|
2022-06-13 09:16:25 +00:00
|
|
|
if (this.#product === 'firefox') {
|
2021-11-10 12:31:15 +00:00
|
|
|
try {
|
|
|
|
// When an existing user profile has been used remove the user
|
|
|
|
// preferences file and restore possibly backuped preferences.
|
2022-06-13 09:16:25 +00:00
|
|
|
await unlinkAsync(path.join(this.#userDataDir, 'user.js'));
|
2021-11-10 12:31:15 +00:00
|
|
|
|
|
|
|
const prefsBackupPath = path.join(
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#userDataDir,
|
2021-11-10 12:31:15 +00:00
|
|
|
'prefs.js.puppeteer'
|
|
|
|
);
|
|
|
|
if (fs.existsSync(prefsBackupPath)) {
|
2022-06-13 09:16:25 +00:00
|
|
|
const prefsPath = path.join(this.#userDataDir, 'prefs.js');
|
2021-11-10 12:31:15 +00:00
|
|
|
await unlinkAsync(prefsPath);
|
|
|
|
await renameAsync(prefsBackupPath, prefsPath);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2022-02-18 08:47:14 +00:00
|
|
|
debugError(error);
|
2021-07-30 07:32:53 +00:00
|
|
|
reject(error);
|
2021-11-10 12:31:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-12 15:30:13 +00:00
|
|
|
fulfill();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2022-06-14 11:16:21 +00:00
|
|
|
this.#listeners = [addEventListener(process, 'exit', this.kill.bind(this))];
|
2022-06-14 11:55:35 +00:00
|
|
|
if (handleSIGINT) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#listeners.push(
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(process, 'SIGINT', () => {
|
2020-05-12 15:30:13 +00:00
|
|
|
this.kill();
|
|
|
|
process.exit(130);
|
|
|
|
})
|
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
|
|
|
if (handleSIGTERM) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#listeners.push(
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(process, 'SIGTERM', this.close.bind(this))
|
2020-05-12 15:30:13 +00:00
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
|
|
|
if (handleSIGHUP) {
|
2022-06-13 09:16:25 +00:00
|
|
|
this.#listeners.push(
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(process, 'SIGHUP', this.close.bind(this))
|
2020-05-12 15:30:13 +00:00
|
|
|
);
|
2022-06-14 11:55:35 +00:00
|
|
|
}
|
2020-05-12 15:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
close(): Promise<void> {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (this.#closed) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
2022-06-13 09:16:25 +00:00
|
|
|
if (this.#isTempUserDataDir) {
|
2020-05-12 15:30:13 +00:00
|
|
|
this.kill();
|
|
|
|
} else if (this.connection) {
|
|
|
|
// Attempt to close the browser gracefully
|
2022-06-22 13:25:44 +00:00
|
|
|
this.connection.send('Browser.close').catch(error => {
|
2020-05-12 15:30:13 +00:00
|
|
|
debugError(error);
|
|
|
|
this.kill();
|
|
|
|
});
|
|
|
|
}
|
2020-06-15 13:02:00 +00:00
|
|
|
// Cleanup this listener last, as that makes sure the full callback runs. If we
|
|
|
|
// perform this earlier, then the previous function calls would not happen.
|
2022-06-14 11:16:21 +00:00
|
|
|
removeEventListeners(this.#listeners);
|
2022-06-13 09:16:25 +00:00
|
|
|
return this.#processClosing;
|
2020-05-12 15:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
kill(): void {
|
2020-06-15 13:02:00 +00:00
|
|
|
// If the process failed to launch (for example if the browser executable path
|
|
|
|
// is invalid), then the process does not get a pid assigned. A call to
|
|
|
|
// `proc.kill` would error, as the `pid` to-be-killed can not be found.
|
2022-02-17 12:17:33 +00:00
|
|
|
if (this.proc && this.proc.pid && pidExists(this.proc.pid)) {
|
2022-05-31 14:34:16 +00:00
|
|
|
const proc = this.proc;
|
2020-06-15 13:02:00 +00:00
|
|
|
try {
|
2022-02-17 12:17:33 +00:00
|
|
|
if (process.platform === 'win32') {
|
2022-06-22 13:25:44 +00:00
|
|
|
childProcess.exec(`taskkill /pid ${this.proc.pid} /T /F`, error => {
|
2022-05-16 13:32:29 +00:00
|
|
|
if (error) {
|
|
|
|
// taskkill can fail to kill the process e.g. due to missing permissions.
|
|
|
|
// Let's kill the process via Node API. This delays killing of all child
|
2022-06-07 13:25:45 +00:00
|
|
|
// processes of `this.proc` until the main Node.js process dies.
|
2022-05-31 14:34:16 +00:00
|
|
|
proc.kill();
|
2022-05-16 13:32:29 +00:00
|
|
|
}
|
|
|
|
});
|
2022-02-17 12:17:33 +00:00
|
|
|
} else {
|
|
|
|
// on linux the process group can be killed with the group id prefixed with
|
|
|
|
// a minus sign. The process group id is the group leader's pid.
|
|
|
|
const processGroupId = -this.proc.pid;
|
2022-06-07 13:25:45 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
process.kill(processGroupId, 'SIGKILL');
|
|
|
|
} catch (error) {
|
|
|
|
// Killing the process group can fail due e.g. to missing permissions.
|
|
|
|
// Let's kill the process via Node API. This delays killing of all child
|
|
|
|
// processes of `this.proc` until the main Node.js process dies.
|
|
|
|
proc.kill('SIGKILL');
|
|
|
|
}
|
2022-02-17 12:17:33 +00:00
|
|
|
}
|
2020-06-15 13:02:00 +00:00
|
|
|
} catch (error) {
|
|
|
|
throw new Error(
|
2022-05-31 14:34:16 +00:00
|
|
|
`${PROCESS_ERROR_EXPLANATION}\nError cause: ${
|
2022-06-10 13:27:42 +00:00
|
|
|
isErrorLike(error) ? error.stack : error
|
2022-05-31 14:34:16 +00:00
|
|
|
}`
|
2020-06-15 13:02:00 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2021-11-15 11:52:09 +00:00
|
|
|
|
|
|
|
// Attempt to remove temporary profile directory to avoid littering.
|
|
|
|
try {
|
2022-06-13 09:16:25 +00:00
|
|
|
if (this.#isTempUserDataDir) {
|
|
|
|
removeFolder.sync(this.#userDataDir);
|
2021-11-15 11:52:09 +00:00
|
|
|
}
|
|
|
|
} catch (error) {}
|
|
|
|
|
2020-06-15 13:02:00 +00:00
|
|
|
// Cleanup this listener last, as that makes sure the full callback runs. If we
|
|
|
|
// perform this earlier, then the previous function calls would not happen.
|
2022-06-14 11:16:21 +00:00
|
|
|
removeEventListeners(this.#listeners);
|
2020-05-12 15:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async setupConnection(options: {
|
|
|
|
usePipe?: boolean;
|
|
|
|
timeout: number;
|
|
|
|
slowMo: number;
|
|
|
|
preferredRevision: string;
|
|
|
|
}): Promise<Connection> {
|
2022-05-31 14:34:16 +00:00
|
|
|
assert(this.proc, 'BrowserRunner not started.');
|
|
|
|
|
2022-06-22 13:25:44 +00:00
|
|
|
const {usePipe, timeout, slowMo, preferredRevision} = options;
|
2020-05-12 15:30:13 +00:00
|
|
|
if (!usePipe) {
|
|
|
|
const browserWSEndpoint = await waitForWSEndpoint(
|
|
|
|
this.proc,
|
|
|
|
timeout,
|
|
|
|
preferredRevision
|
|
|
|
);
|
|
|
|
const transport = await WebSocketTransport.create(browserWSEndpoint);
|
|
|
|
this.connection = new Connection(browserWSEndpoint, transport, slowMo);
|
|
|
|
} else {
|
2020-06-19 14:39:03 +00:00
|
|
|
// stdio was assigned during start(), and the 'pipe' option there adds the
|
|
|
|
// 4th and 5th items to stdio array
|
2022-06-22 13:25:44 +00:00
|
|
|
const {3: pipeWrite, 4: pipeRead} = this.proc.stdio;
|
2020-05-12 15:30:13 +00:00
|
|
|
const transport = new PipeTransport(
|
|
|
|
pipeWrite as NodeJS.WritableStream,
|
|
|
|
pipeRead as NodeJS.ReadableStream
|
|
|
|
);
|
|
|
|
this.connection = new Connection('', transport, slowMo);
|
|
|
|
}
|
|
|
|
return this.connection;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function waitForWSEndpoint(
|
|
|
|
browserProcess: childProcess.ChildProcess,
|
|
|
|
timeout: number,
|
|
|
|
preferredRevision: string
|
|
|
|
): Promise<string> {
|
2022-05-31 14:34:16 +00:00
|
|
|
assert(browserProcess.stderr, '`browserProcess` does not have stderr.');
|
|
|
|
const rl = readline.createInterface(browserProcess.stderr);
|
|
|
|
let stderr = '';
|
|
|
|
|
2020-05-12 15:30:13 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const listeners = [
|
2022-06-14 11:16:21 +00:00
|
|
|
addEventListener(rl, 'line', onLine),
|
2022-06-15 10:42:21 +00:00
|
|
|
addEventListener(rl, 'close', () => {
|
|
|
|
return onClose();
|
|
|
|
}),
|
|
|
|
addEventListener(browserProcess, 'exit', () => {
|
|
|
|
return onClose();
|
|
|
|
}),
|
2022-06-22 13:25:44 +00:00
|
|
|
addEventListener(browserProcess, 'error', error => {
|
2022-06-15 10:42:21 +00:00
|
|
|
return onClose(error);
|
|
|
|
}),
|
2020-05-12 15:30:13 +00:00
|
|
|
];
|
|
|
|
const timeoutId = timeout ? setTimeout(onTimeout, timeout) : 0;
|
|
|
|
|
|
|
|
function onClose(error?: Error): void {
|
|
|
|
cleanup();
|
|
|
|
reject(
|
|
|
|
new Error(
|
|
|
|
[
|
|
|
|
'Failed to launch the browser process!' +
|
|
|
|
(error ? ' ' + error.message : ''),
|
|
|
|
stderr,
|
|
|
|
'',
|
2020-06-15 15:34:16 +00:00
|
|
|
'TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md',
|
2020-05-12 15:30:13 +00:00
|
|
|
'',
|
|
|
|
].join('\n')
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onTimeout(): void {
|
|
|
|
cleanup();
|
|
|
|
reject(
|
|
|
|
new TimeoutError(
|
|
|
|
`Timed out after ${timeout} ms while trying to connect to the browser! Only Chrome at revision r${preferredRevision} is guaranteed to work.`
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onLine(line: string): void {
|
|
|
|
stderr += line + '\n';
|
|
|
|
const match = line.match(/^DevTools listening on (ws:\/\/.*)$/);
|
2022-06-14 11:55:35 +00:00
|
|
|
if (!match) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-12 15:30:13 +00:00
|
|
|
cleanup();
|
2022-05-31 14:34:16 +00:00
|
|
|
// The RegExp matches, so this will obviously exist.
|
|
|
|
resolve(match[1]!);
|
2020-05-12 15:30:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function cleanup(): void {
|
2022-06-14 11:55:35 +00:00
|
|
|
if (timeoutId) {
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
}
|
2022-06-14 11:16:21 +00:00
|
|
|
removeEventListeners(listeners);
|
2020-05-12 15:30:13 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2022-02-17 12:17:33 +00:00
|
|
|
|
|
|
|
function pidExists(pid: number): boolean {
|
|
|
|
try {
|
|
|
|
return process.kill(pid, 0);
|
|
|
|
} catch (error) {
|
2022-06-10 13:27:42 +00:00
|
|
|
if (isErrnoException(error)) {
|
|
|
|
if (error.code && error.code === 'ESRCH') {
|
2022-05-31 14:34:16 +00:00
|
|
|
return false;
|
|
|
|
}
|
2022-02-17 12:17:33 +00:00
|
|
|
}
|
2022-05-31 14:34:16 +00:00
|
|
|
throw error;
|
2022-02-17 12:17:33 +00:00
|
|
|
}
|
|
|
|
}
|