forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chrome-launcher.ts
296 lines (248 loc) · 8.23 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
/**
* @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 chromeFinder from './chrome-finder';
import {getRandomPort} from './random-port';
import {DEFAULT_FLAGS} from './flags';
import {makeTmpDir, defaults, delay} from './utils';
import * as net from 'net';
const rimraf = require('rimraf');
const log = require('../lighthouse-core/lib/log');
const spawn = childProcess.spawn;
const execSync = childProcess.execSync;
const isWindows = process.platform === 'win32';
const _SIGINT = 'SIGINT';
const _SIGINT_EXIT_CODE = 130;
const _SUPPORTED_PLATFORMS = new Set(['darwin', 'linux', 'win32']);
type SupportedPlatforms = 'darwin'|'linux'|'win32';
export interface Options {
startingUrl?: string;
chromeFlags?: Array<string>;
port?: number;
handleSIGINT?: boolean;
chromePath?: string;
}
export interface LaunchedChrome {
pid: number;
port: number;
kill: () => Promise<{}>;
}
export 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) {
process.on(_SIGINT, async () => {
await instance.kill();
process.exit(_SIGINT_EXIT_CODE);
});
}
await instance.launch();
return {pid: instance.pid!, port: instance.port!, kill: async () => instance.kill()};
}
export class Launcher {
private tmpDirandPidFileReady = false;
private pollInterval: number = 500;
private pidFile: string;
private startingUrl: string;
private userDataDir: string;
private TMP_PROFILE_DIR: string;
private outFile?: number;
private errFile?: number;
private chromePath?: string;
private chromeFlags: string[];
private chrome?: childProcess.ChildProcess;
private requestedPort?: number;
port?: number;
pid?: number;
constructor(opts: Options = {}) {
// choose the first one (default)
this.startingUrl = defaults(opts.startingUrl, 'about:blank');
this.chromeFlags = defaults(opts.chromeFlags, []);
this.requestedPort = defaults(opts.port, 0);
this.userDataDir = defaults(opts.userDataDir, this.TMP_PROFILE_DIR);
this.chromePath = opts.chromePath;
}
private get flags() {
const flags = DEFAULT_FLAGS.concat([
`--remote-debugging-port=${this.port}`,
// Place Chrome profile in a custom location we'll rm -rf later
`--user-data-dir=${this.userDataDir}`
]);
if (process.platform === 'linux') {
flags.push('--disable-setuid-sandbox');
}
flags.push(...this.chromeFlags);
flags.push(this.startingUrl);
return flags;
}
private prepare() {
const platform = process.platform as SupportedPlatforms;
if (!_SUPPORTED_PLATFORMS.has(platform)) {
throw new Error(`Platform ${platform} is not supported`);
}
this.TMP_PROFILE_DIR = makeTmpDir();
this.outFile = fs.openSync(`${this.TMP_PROFILE_DIR}/chrome-out.log`, 'a');
this.errFile = fs.openSync(`${this.TMP_PROFILE_DIR}/chrome-err.log`, 'a');
// fix for Node4
// you can't pass a fd to fs.writeFileSync
this.pidFile = `${this.TMP_PROFILE_DIR}/chrome.pid`;
log.verbose('ChromeLauncher', `created ${this.TMP_PROFILE_DIR}`);
this.tmpDirandPidFileReady = true;
}
async launch() {
if (this.requestedPort !== 0) {
this.port = this.requestedPort;
// If an explict port is passed first look for an open connection...
try {
return await this.isDebuggerReady();
} catch (err) {
log.log(
'ChromeLauncher',
`No debugging port found on port ${this.port}, launching a new Chrome.`);
}
}
if (!this.tmpDirandPidFileReady) {
this.prepare();
}
if (this.chromePath === undefined) {
const installations = await chromeFinder[process.platform as SupportedPlatforms]();
if (installations.length === 0) {
throw new Error('No Chrome Installations Found');
}
this.chromePath = installations[0];
}
this.pid = await this.spawn(this.chromePath);
return Promise.resolve();
}
private async spawn(execPath: string) {
// Typescript is losing track of the return type without the explict typing.
const spawnPromise: Promise<number> = new Promise(async (resolve) => {
if (this.chrome) {
log.log('ChromeLauncher', `Chrome already running with pid ${this.chrome.pid}.`);
return resolve(this.chrome.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();
}
const chrome = spawn(
execPath, this.flags, {detached: true, stdio: ['ignore', this.outFile, this.errFile]});
this.chrome = chrome;
fs.writeFileSync(this.pidFile, chrome.pid.toString());
log.verbose('ChromeLauncher', `Chrome running with pid ${chrome.pid} on port ${this.port}.`);
resolve(chrome.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<{}> {
return new Promise((resolve, reject) => {
const client = net.createConnection(this.port!);
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
private waitUntilReady() {
const launcher = this;
return new Promise((resolve, reject) => {
let retries = 0;
let waitStatus = 'Waiting for browser.';
(function 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 > 10) {
return reject(err);
}
delay(launcher.pollInterval).then(poll);
});
})();
});
}
kill() {
return new Promise(resolve => {
if (this.chrome) {
this.chrome.on('close', () => {
this.destroyTmp().then(resolve);
});
log.log('ChromeLauncher', 'Killing all Chrome Instances');
try {
if (isWindows) {
execSync(`taskkill /pid ${this.chrome.pid} /T /F`);
} else {
process.kill(-this.chrome.pid);
}
} catch (err) {
log.warn('ChromeLauncher', `Chrome could not be killed ${err.message}`);
}
delete this.chrome;
} else {
// fail silently as we did not start chrome
resolve();
}
});
}
private destroyTmp() {
return new Promise(resolve => {
if (!this.TMP_PROFILE_DIR) {
return resolve();
}
log.verbose('ChromeLauncher', `Removing ${this.TMP_PROFILE_DIR}`);
if (this.outFile) {
fs.closeSync(this.outFile);
delete this.outFile;
}
if (this.errFile) {
fs.closeSync(this.errFile);
delete this.errFile;
}
rimraf(this.TMP_PROFILE_DIR, () => resolve());
});
}
};