Skip to content

Commit 0455283

Browse files
FrederickGeek8paulirish
authored andcommitted
Add hostname CLI flag and option for CriConnection (#2728)
* fixes #2727 hostname expressed as second parameter to avoid breaking existing implementations
1 parent e72483b commit 0455283

File tree

4 files changed

+12
-8
lines changed

4 files changed

+12
-8
lines changed

lighthouse-cli/cli-flags.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {GetValidOutputOptions, OutputMode} from './printer';
1414
export interface Flags {
1515
port: number, chromeFlags: string, output: any, outputPath: string, interactive: boolean,
1616
saveArtifacts: boolean, saveAssets: boolean, view: boolean, maxWaitForLoad: number,
17-
logLevel: string
17+
logLevel: string, hostname: string
1818
}
1919

2020
export function getFlags(manualArgv?: string) {
@@ -54,7 +54,7 @@ export function getFlags(manualArgv?: string) {
5454
[
5555
'save-assets', 'save-artifacts', 'list-all-audits', 'list-trace-categories',
5656
'additional-trace-categories', 'config-path', 'chrome-flags', 'perf', 'port',
57-
'max-wait-for-load'
57+
'hostname', 'max-wait-for-load'
5858
],
5959
'Configuration:')
6060
.describe({
@@ -77,6 +77,7 @@ export function getFlags(manualArgv?: string) {
7777
CHROME_PATH: Explicit path of intended Chrome binary. If set must point to an executable of a build of Chromium version 54.0 or later. By default, any detected Chrome Canary or Chrome (stable) will be launched.
7878
`,
7979
'perf': 'Use a performance-test-only configuration',
80+
'hostname': 'The hostname to use for the debugging protocol.',
8081
'port': 'The port to use for the debugging protocol. Use 0 for a random port',
8182
'max-wait-for-load':
8283
'The timeout (in milliseconds) to wait before the page is considered done loading and the run should continue. WARNING: Very high values can lead to large traces and instability',
@@ -107,6 +108,7 @@ Example: --output-path=./lighthouse-results.html`,
107108
.default('disable-cpu-throttling', false)
108109
.default('output', GetValidOutputOptions()[OutputMode.domhtml])
109110
.default('port', 0)
111+
.default('hostname', 'localhost')
110112
.default('max-wait-for-load', Driver.MAX_WAIT_FOR_FULLY_LOADED)
111113
.check((argv: {listAllAudits?: boolean, listTraceCategories?: boolean, _: Array<any>}) => {
112114
// Make sure lighthouse has been passed a url, or at least one of --list-all-audits

lighthouse-core/gather/connections/cri.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,20 @@ const WebSocket = require('ws');
1010
const http = require('http');
1111
const log = require('lighthouse-logger');
1212

13-
const hostname = 'localhost';
13+
const DEFAULT_HOSTNAME = 'localhost';
1414
const CONNECT_TIMEOUT = 10000;
1515
const DEFAULT_PORT = 9222;
1616

1717
class CriConnection extends Connection {
1818
/**
1919
* @param {number=} port Optional port number. Defaults to 9222;
20+
* @param {string=} hostname Optional hostname. Defaults to localhost.
2021
* @constructor
2122
*/
22-
constructor(port) {
23+
constructor(port = DEFAULT_PORT, hostname = DEFAULT_HOSTNAME) {
2324
super();
24-
25-
this.port = port || DEFAULT_PORT;
25+
this.port = port;
26+
this.hostname = hostname;
2627
}
2728

2829
/**
@@ -77,7 +78,7 @@ class CriConnection extends Connection {
7778
_runJsonCommand(command) {
7879
return new Promise((resolve, reject) => {
7980
const request = http.get({
80-
hostname: hostname,
81+
hostname: this.hostname,
8182
port: this.port,
8283
path: '/json/' + command
8384
}, response => {

lighthouse-core/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ module.exports = function(url, flags = {}, configJSON) {
3535
// Use ConfigParser to generate a valid config file
3636
const config = new Config(configJSON, flags.configPath);
3737

38-
const connection = new ChromeProtocol(flags.port);
38+
const connection = new ChromeProtocol(flags.port, flags.hostname);
3939

4040
// kick off a lighthouse run
4141
return Runner.run(connection, {url, flags, config})

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ Configuration:
6363
[default: ""]
6464
--perf Use a performance-test-only configuration [boolean]
6565
--port The port to use for the debugging protocol. Use 0 for a random port [default: 9222]
66+
--hostname The hostname to use for the debugging protocol. [default: localhost]
6667
--max-wait-for-load The timeout (in milliseconds) to wait before the page is considered done loading and the run should continue.
6768
WARNING: Very high values can lead to large traces and instability [default: 25000]
6869

0 commit comments

Comments
 (0)