1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
|
/**
* 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';
import * as fs from 'fs';
import * as path from 'path';
import * as readline from 'readline';
import removeFolder from 'rimraf';
import {promisify} from 'util';
import {assert} from '../util/assert.js';
import {Connection} from '../common/Connection.js';
import {debug} from '../common/Debug.js';
import {TimeoutError} from '../common/Errors.js';
import {
debugError,
addEventListener,
PuppeteerEventListener,
removeEventListeners,
} from '../common/util.js';
import {isErrnoException, isErrorLike} from '../util/ErrorLike.js';
import {Product} from '../common/Product.js';
import {NodeWebSocketTransport as WebSocketTransport} from '../node/NodeWebSocketTransport.js';
import {LaunchOptions} from './LaunchOptions.js';
import {PipeTransport} from './PipeTransport.js';
const removeFolderAsync = promisify(removeFolder);
const renameAsync = promisify(fs.rename);
const unlinkAsync = promisify(fs.unlink);
const debugLauncher = debug('puppeteer:launcher');
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.`;
/**
* @internal
*/
export class BrowserRunner {
#product: Product;
#executablePath: string;
#processArguments: string[];
#userDataDir: string;
#isTempUserDataDir?: boolean;
#closed = true;
#listeners: PuppeteerEventListener[] = [];
#processClosing!: Promise<void>;
proc?: childProcess.ChildProcess;
connection?: Connection;
constructor(
product: Product,
executablePath: string,
processArguments: string[],
userDataDir: string,
isTempUserDataDir?: boolean
) {
this.#product = product;
this.#executablePath = executablePath;
this.#processArguments = processArguments;
this.#userDataDir = userDataDir;
this.#isTempUserDataDir = isTempUserDataDir;
}
start(options: LaunchOptions): void {
const {handleSIGINT, handleSIGTERM, handleSIGHUP, dumpio, env, pipe} =
options;
let stdio: Array<'ignore' | 'pipe'>;
if (pipe) {
if (dumpio) {
stdio = ['ignore', 'pipe', 'pipe', 'pipe', 'pipe'];
} else {
stdio = ['ignore', 'ignore', 'ignore', 'pipe', 'pipe'];
}
} else {
if (dumpio) {
stdio = ['pipe', 'pipe', 'pipe'];
} else {
stdio = ['pipe', 'ignore', 'pipe'];
}
}
assert(!this.proc, 'This process has previously been started.');
debugLauncher(
`Calling ${this.#executablePath} ${this.#processArguments.join(' ')}`
);
this.proc = childProcess.spawn(
this.#executablePath,
this.#processArguments,
{
// 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
detached: process.platform !== 'win32',
env,
stdio,
}
);
if (dumpio) {
this.proc.stderr?.pipe(process.stderr);
this.proc.stdout?.pipe(process.stdout);
}
this.#closed = false;
this.#processClosing = new Promise((fulfill, reject) => {
this.proc!.once('exit', async () => {
this.#closed = true;
// Cleanup as processes exit.
if (this.#isTempUserDataDir) {
try {
await removeFolderAsync(this.#userDataDir);
fulfill();
} catch (error) {
debugError(error);
reject(error);
}
} else {
if (this.#product === 'firefox') {
try {
// When an existing user profile has been used remove the user
// preferences file and restore possibly backuped preferences.
await unlinkAsync(path.join(this.#userDataDir, 'user.js'));
const prefsBackupPath = path.join(
this.#userDataDir,
'prefs.js.puppeteer'
);
if (fs.existsSync(prefsBackupPath)) {
const prefsPath = path.join(this.#userDataDir, 'prefs.js');
await unlinkAsync(prefsPath);
await renameAsync(prefsBackupPath, prefsPath);
}
} catch (error) {
debugError(error);
reject(error);
}
}
fulfill();
}
});
});
this.#listeners = [addEventListener(process, 'exit', this.kill.bind(this))];
if (handleSIGINT) {
this.#listeners.push(
addEventListener(process, 'SIGINT', () => {
this.kill();
process.exit(130);
})
);
}
if (handleSIGTERM) {
this.#listeners.push(
addEventListener(process, 'SIGTERM', this.close.bind(this))
);
}
if (handleSIGHUP) {
this.#listeners.push(
addEventListener(process, 'SIGHUP', this.close.bind(this))
);
}
}
close(): Promise<void> {
if (this.#closed) {
return Promise.resolve();
}
if (this.#isTempUserDataDir) {
this.kill();
} else if (this.connection) {
// Attempt to close the browser gracefully
this.connection.send('Browser.close').catch(error => {
debugError(error);
this.kill();
});
}
// 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.
removeEventListeners(this.#listeners);
return this.#processClosing;
}
kill(): void {
// 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.
if (this.proc && this.proc.pid && pidExists(this.proc.pid)) {
const proc = this.proc;
try {
if (process.platform === 'win32') {
childProcess.exec(`taskkill /pid ${this.proc.pid} /T /F`, error => {
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
// processes of `this.proc` until the main Node.js process dies.
proc.kill();
}
});
} 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;
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');
}
}
} catch (error) {
throw new Error(
`${PROCESS_ERROR_EXPLANATION}\nError cause: ${
isErrorLike(error) ? error.stack : error
}`
);
}
}
// Attempt to remove temporary profile directory to avoid littering.
try {
if (this.#isTempUserDataDir) {
removeFolder.sync(this.#userDataDir);
}
} catch (error) {}
// 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.
removeEventListeners(this.#listeners);
}
async setupConnection(options: {
usePipe?: boolean;
timeout: number;
slowMo: number;
preferredRevision: string;
}): Promise<Connection> {
assert(this.proc, 'BrowserRunner not started.');
const {usePipe, timeout, slowMo, preferredRevision} = options;
if (!usePipe) {
const browserWSEndpoint = await waitForWSEndpoint(
this.proc,
timeout,
preferredRevision
);
const transport = await WebSocketTransport.create(browserWSEndpoint);
this.connection = new Connection(browserWSEndpoint, transport, slowMo);
} else {
// stdio was assigned during start(), and the 'pipe' option there adds the
// 4th and 5th items to stdio array
const {3: pipeWrite, 4: pipeRead} = this.proc.stdio;
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> {
assert(browserProcess.stderr, '`browserProcess` does not have stderr.');
const rl = readline.createInterface(browserProcess.stderr);
let stderr = '';
return new Promise((resolve, reject) => {
const listeners = [
addEventListener(rl, 'line', onLine),
addEventListener(rl, 'close', () => {
return onClose();
}),
addEventListener(browserProcess, 'exit', () => {
return onClose();
}),
addEventListener(browserProcess, 'error', error => {
return onClose(error);
}),
];
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,
'',
'TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md',
'',
].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:\/\/.*)$/);
if (!match) {
return;
}
cleanup();
// The RegExp matches, so this will obviously exist.
resolve(match[1]!);
}
function cleanup(): void {
if (timeoutId) {
clearTimeout(timeoutId);
}
removeEventListeners(listeners);
}
});
}
function pidExists(pid: number): boolean {
try {
return process.kill(pid, 0);
} catch (error) {
if (isErrnoException(error)) {
if (error.code && error.code === 'ESRCH') {
return false;
}
}
throw error;
}
}
|