-
Notifications
You must be signed in to change notification settings - Fork 189
/
chrome-launcher.ts
454 lines (384 loc) · 14.1 KB
/
chrome-launcher.ts
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/**
* @license Copyright 2016 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.
*/
'use strict';
import * as childProcess from 'child_process';
import * as fs from 'fs';
import * as net from 'net';
import * as chromeFinder from './chrome-finder.js';
import {getRandomPort} from './random-port.js';
import {DEFAULT_FLAGS} from './flags.js';
import {makeTmpDir, defaults, delay, getPlatform, toWin32Path, InvalidUserDataDirectoryError, UnsupportedPlatformError, ChromeNotInstalledError} from './utils.js';
import {ChildProcess} from 'child_process';
import {spawn, spawnSync} from 'child_process';
import log from 'lighthouse-logger';
const isWsl = getPlatform() === 'wsl';
const isWindows = getPlatform() === 'win32';
const _SIGINT = 'SIGINT';
const _SIGINT_EXIT_CODE = 130;
const _SUPPORTED_PLATFORMS = new Set(['darwin', 'linux', 'win32', 'wsl']);
type SupportedPlatforms = 'darwin'|'linux'|'win32'|'wsl';
const instances = new Set<Launcher>();
type JSONLike =|{[property: string]: JSONLike}|readonly JSONLike[]|string|number|boolean|null;
export interface Options {
startingUrl?: string;
chromeFlags?: Array<string>;
prefs?: Record<string, JSONLike>;
port?: number;
portStrictMode?: boolean;
handleSIGINT?: boolean;
chromePath?: string;
userDataDir?: string|boolean;
logLevel?: 'verbose'|'info'|'error'|'warn'|'silent';
ignoreDefaultFlags?: boolean;
connectionPollInterval?: number;
maxConnectionRetries?: number;
envVars?: {[key: string]: string|undefined};
}
export interface LaunchedChrome {
pid: number;
port: number;
process: ChildProcess;
kill: () => void;
}
export interface ModuleOverrides {
fs?: typeof fs;
spawn?: typeof childProcess.spawn;
}
const sigintListener = () => {
killAll();
process.exit(_SIGINT_EXIT_CODE);
};
async function launch(opts: Options = {}): Promise<LaunchedChrome> {
opts.handleSIGINT = defaults(opts.handleSIGINT, true);
const instance = new Launcher(opts);
// Kill spawned Chrome process in case of ctrl-C.
if (opts.handleSIGINT && instances.size === 0) {
process.on(_SIGINT, sigintListener);
}
instances.add(instance);
await instance.launch();
const kill = () => {
instances.delete(instance);
if (instances.size === 0) {
process.removeListener(_SIGINT, sigintListener);
}
instance.kill();
};
return {pid: instance.pid!, port: instance.port!, kill, process: instance.chromeProcess!};
}
/** Returns Chrome installation path that chrome-launcher will launch by default. */
function getChromePath(): string {
const installation = Launcher.getFirstInstallation();
if (!installation) {
throw new ChromeNotInstalledError();
}
return installation;
}
function killAll(): Array<Error> {
let errors = [];
for (const instance of instances) {
try {
instance.kill();
// only delete if kill did not error
// this means erroring instances remain in the Set
instances.delete(instance);
} catch (err) {
errors.push(err);
}
}
return errors;
}
class Launcher {
private tmpDirandPidFileReady = false;
private pidFile: string;
private startingUrl: string;
private outFile?: number;
private errFile?: number;
private chromePath?: string;
private ignoreDefaultFlags?: boolean;
private chromeFlags: string[];
private prefs: Record<string, JSONLike>;
private requestedPort?: number;
private portStrictMode?: boolean;
private connectionPollInterval: number;
private maxConnectionRetries: number;
private fs: typeof fs;
private spawn: typeof childProcess.spawn;
private useDefaultProfile: boolean;
private envVars: {[key: string]: string|undefined};
chromeProcess?: childProcess.ChildProcess;
userDataDir?: string;
port?: number;
pid?: number;
constructor(private opts: Options = {}, moduleOverrides: ModuleOverrides = {}) {
this.fs = moduleOverrides.fs || fs;
this.spawn = moduleOverrides.spawn || spawn;
log.setLevel(defaults(this.opts.logLevel, 'silent'));
// choose the first one (default)
this.startingUrl = defaults(this.opts.startingUrl, 'about:blank');
this.chromeFlags = defaults(this.opts.chromeFlags, []);
this.prefs = defaults(this.opts.prefs, {});
this.requestedPort = defaults(this.opts.port, 0);
this.portStrictMode = opts.portStrictMode;
this.chromePath = this.opts.chromePath;
this.ignoreDefaultFlags = defaults(this.opts.ignoreDefaultFlags, false);
this.connectionPollInterval = defaults(this.opts.connectionPollInterval, 500);
this.maxConnectionRetries = defaults(this.opts.maxConnectionRetries, 50);
this.envVars = defaults(opts.envVars, Object.assign({}, process.env));
if (typeof this.opts.userDataDir === 'boolean') {
if (!this.opts.userDataDir) {
this.useDefaultProfile = true;
this.userDataDir = undefined;
} else {
throw new InvalidUserDataDirectoryError();
}
} else {
this.useDefaultProfile = false;
this.userDataDir = this.opts.userDataDir;
}
}
private get flags() {
const flags = this.ignoreDefaultFlags ? [] : DEFAULT_FLAGS.slice();
flags.push(`--remote-debugging-port=${this.port}`);
if (!this.ignoreDefaultFlags && getPlatform() === 'linux') {
flags.push('--disable-setuid-sandbox');
}
if (!this.useDefaultProfile) {
// Place Chrome profile in a custom location we'll rm -rf later
// If in WSL, we need to use the Windows format
flags.push(`--user-data-dir=${isWsl ? toWin32Path(this.userDataDir) : this.userDataDir}`);
}
if (process.env.HEADLESS) flags.push('--headless');
flags.push(...this.chromeFlags);
flags.push(this.startingUrl);
return flags;
}
static defaultFlags() {
return DEFAULT_FLAGS.slice();
}
/** Returns the highest priority chrome installation. */
static getFirstInstallation() {
if (getPlatform() === 'darwin') return chromeFinder.darwinFast();
return chromeFinder[getPlatform() as SupportedPlatforms]()[0];
}
/** Returns all available chrome installations in decreasing priority order. */
static getInstallations() {
return chromeFinder[getPlatform() as SupportedPlatforms]();
}
// Wrapper function to enable easy testing.
makeTmpDir() {
return makeTmpDir();
}
prepare() {
const platform = getPlatform() as SupportedPlatforms;
if (!_SUPPORTED_PLATFORMS.has(platform)) {
throw new UnsupportedPlatformError();
}
this.userDataDir = this.userDataDir || this.makeTmpDir();
this.outFile = this.fs.openSync(`${this.userDataDir}/chrome-out.log`, 'a');
this.errFile = this.fs.openSync(`${this.userDataDir}/chrome-err.log`, 'a');
this.setBrowserPrefs();
// fix for Node4
// you can't pass a fd to fs.writeFileSync
this.pidFile = `${this.userDataDir}/chrome.pid`;
log.verbose('ChromeLauncher', `created ${this.userDataDir}`);
this.tmpDirandPidFileReady = true;
}
private setBrowserPrefs() {
// don't set prefs if not defined
if (Object.keys(this.prefs).length === 0) {
return;
}
const profileDir = `${this.userDataDir}/Default`;
if (!this.fs.existsSync(profileDir)) {
this.fs.mkdirSync(profileDir, {recursive: true});
}
const preferenceFile = `${profileDir}/Preferences`;
try {
if (this.fs.existsSync(preferenceFile)) {
// overwrite existing file
const file = this.fs.readFileSync(preferenceFile, 'utf-8');
const content = JSON.parse(file);
this.fs.writeFileSync(preferenceFile, JSON.stringify({...content, ...this.prefs}), 'utf-8');
} else {
// create new Preference file
this.fs.writeFileSync(preferenceFile, JSON.stringify({...this.prefs}), 'utf-8');
}
} catch (err) {
log.log('ChromeLauncher', `Failed to set browser prefs: ${err.message}`);
}
}
async launch() {
if (this.requestedPort !== 0) {
this.port = this.requestedPort;
// If an explict port is passed first look for an open connection...
try {
await this.isDebuggerReady();
log.log(
'ChromeLauncher',
`Found existing Chrome already running using port ${this.port}, using that.`);
return;
} catch (err) {
if (this.portStrictMode) {
throw new Error(`found no Chrome at port ${this.requestedPort}`);
}
log.log(
'ChromeLauncher',
`No debugging port found on port ${this.port}, launching a new Chrome.`);
}
}
if (this.chromePath === undefined) {
const installation = Launcher.getFirstInstallation();
if (!installation) {
throw new ChromeNotInstalledError();
}
this.chromePath = installation;
}
if (!this.tmpDirandPidFileReady) {
this.prepare();
}
this.pid = await this.spawnProcess(this.chromePath);
return Promise.resolve();
}
private async spawnProcess(execPath: string) {
const spawnPromise = (async () => {
if (this.chromeProcess) {
log.log('ChromeLauncher', `Chrome already running with pid ${this.chromeProcess.pid}.`);
return this.chromeProcess.pid;
}
// If a zero value port is set, it means the launcher
// is responsible for generating the port number.
// We do this here so that we can know the port before
// we pass it into chrome.
if (this.requestedPort === 0) {
this.port = await getRandomPort();
}
log.verbose(
'ChromeLauncher', `Launching with command:\n"${execPath}" ${this.flags.join(' ')}`);
this.chromeProcess = this.spawn(execPath, this.flags, {
// 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',
stdio: ['ignore', this.outFile, this.errFile],
env: this.envVars
});
if (this.chromeProcess.pid) {
this.fs.writeFileSync(this.pidFile, this.chromeProcess.pid.toString());
}
log.verbose(
'ChromeLauncher',
`Chrome running with pid ${this.chromeProcess.pid} on port ${this.port}.`);
return this.chromeProcess.pid;
})();
const pid = await spawnPromise;
await this.waitUntilReady();
return pid;
}
private cleanup(client?: net.Socket) {
if (client) {
client.removeAllListeners();
client.end();
client.destroy();
client.unref();
}
}
// resolves if ready, rejects otherwise
private isDebuggerReady(): Promise<void> {
return new Promise((resolve, reject) => {
const client = net.createConnection(this.port!, '127.0.0.1');
client.once('error', err => {
this.cleanup(client);
reject(err);
});
client.once('connect', () => {
this.cleanup(client);
resolve();
});
});
}
// resolves when debugger is ready, rejects after 10 polls
waitUntilReady() {
const launcher = this;
return new Promise<void>((resolve, reject) => {
let retries = 0;
let waitStatus = 'Waiting for browser.';
const poll = () => {
if (retries === 0) {
log.log('ChromeLauncher', waitStatus);
}
retries++;
waitStatus += '..';
log.log('ChromeLauncher', waitStatus);
launcher.isDebuggerReady()
.then(() => {
log.log('ChromeLauncher', waitStatus + `${log.greenify(log.tick)}`);
resolve();
})
.catch(err => {
if (retries > launcher.maxConnectionRetries) {
log.error('ChromeLauncher', err.message);
const stderr =
this.fs.readFileSync(`${this.userDataDir}/chrome-err.log`, {encoding: 'utf-8'});
log.error(
'ChromeLauncher', `Logging contents of ${this.userDataDir}/chrome-err.log`);
log.error('ChromeLauncher', stderr);
return reject(err);
}
delay(launcher.connectionPollInterval).then(poll);
});
};
poll();
});
}
kill() {
if (!this.chromeProcess) {
return;
}
this.chromeProcess.on('close', () => {
delete this.chromeProcess;
this.destroyTmp();
});
log.log('ChromeLauncher', `Killing Chrome instance ${this.chromeProcess.pid}`);
try {
if (isWindows) {
// https://github.com/GoogleChrome/chrome-launcher/issues/266
const taskkillProc = spawnSync(
`taskkill /pid ${this.chromeProcess.pid} /T /F`, {shell: true, encoding: 'utf-8'});
const {stderr} = taskkillProc;
if (stderr) log.error('ChromeLauncher', `taskkill stderr`, stderr);
} else {
if (this.chromeProcess.pid) {
process.kill(-this.chromeProcess.pid, 'SIGKILL');
}
}
} catch (err) {
const message = `Chrome could not be killed ${err.message}`;
log.warn('ChromeLauncher', message);
}
this.destroyTmp();
}
destroyTmp() {
if (this.outFile) {
this.fs.closeSync(this.outFile);
delete this.outFile;
}
// Only clean up the tmp dir if we created it.
if (this.userDataDir === undefined || this.opts.userDataDir !== undefined) {
return;
}
if (this.errFile) {
this.fs.closeSync(this.errFile);
delete this.errFile;
}
// backwards support for node v12 + v14.14+
// https://nodejs.org/api/deprecations.html#DEP0147
const rmSync = this.fs.rmSync || this.fs.rmdirSync;
rmSync(this.userDataDir, {recursive: true, force: true, maxRetries: 10});
}
};
export default Launcher;
export {Launcher, launch, killAll, getChromePath};