From 0455283ed71b9b023fc7e396fad7f69b074b173c Mon Sep 17 00:00:00 2001 From: Frederick Morlock Date: Mon, 14 Aug 2017 17:19:53 -0400 Subject: [PATCH] Add hostname CLI flag and option for CriConnection (#2728) * fixes GoogleChrome/lighthouse#2727 hostname expressed as second parameter to avoid breaking existing implementations --- lighthouse-cli/cli-flags.ts | 6 ++++-- lighthouse-core/gather/connections/cri.js | 11 ++++++----- lighthouse-core/index.js | 2 +- readme.md | 1 + 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/lighthouse-cli/cli-flags.ts b/lighthouse-cli/cli-flags.ts index 3650a91bbf50..ed013cb50db0 100644 --- a/lighthouse-cli/cli-flags.ts +++ b/lighthouse-cli/cli-flags.ts @@ -14,7 +14,7 @@ import {GetValidOutputOptions, OutputMode} from './printer'; export interface Flags { port: number, chromeFlags: string, output: any, outputPath: string, interactive: boolean, saveArtifacts: boolean, saveAssets: boolean, view: boolean, maxWaitForLoad: number, - logLevel: string + logLevel: string, hostname: string } export function getFlags(manualArgv?: string) { @@ -54,7 +54,7 @@ export function getFlags(manualArgv?: string) { [ 'save-assets', 'save-artifacts', 'list-all-audits', 'list-trace-categories', 'additional-trace-categories', 'config-path', 'chrome-flags', 'perf', 'port', - 'max-wait-for-load' + 'hostname', 'max-wait-for-load' ], 'Configuration:') .describe({ @@ -77,6 +77,7 @@ export function getFlags(manualArgv?: string) { 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. `, 'perf': 'Use a performance-test-only configuration', + 'hostname': 'The hostname to use for the debugging protocol.', 'port': 'The port to use for the debugging protocol. Use 0 for a random port', 'max-wait-for-load': '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`, .default('disable-cpu-throttling', false) .default('output', GetValidOutputOptions()[OutputMode.domhtml]) .default('port', 0) + .default('hostname', 'localhost') .default('max-wait-for-load', Driver.MAX_WAIT_FOR_FULLY_LOADED) .check((argv: {listAllAudits?: boolean, listTraceCategories?: boolean, _: Array}) => { // Make sure lighthouse has been passed a url, or at least one of --list-all-audits diff --git a/lighthouse-core/gather/connections/cri.js b/lighthouse-core/gather/connections/cri.js index b5e674ef22ff..6d7403db19b1 100644 --- a/lighthouse-core/gather/connections/cri.js +++ b/lighthouse-core/gather/connections/cri.js @@ -10,19 +10,20 @@ const WebSocket = require('ws'); const http = require('http'); const log = require('lighthouse-logger'); -const hostname = 'localhost'; +const DEFAULT_HOSTNAME = 'localhost'; const CONNECT_TIMEOUT = 10000; const DEFAULT_PORT = 9222; class CriConnection extends Connection { /** * @param {number=} port Optional port number. Defaults to 9222; + * @param {string=} hostname Optional hostname. Defaults to localhost. * @constructor */ - constructor(port) { + constructor(port = DEFAULT_PORT, hostname = DEFAULT_HOSTNAME) { super(); - - this.port = port || DEFAULT_PORT; + this.port = port; + this.hostname = hostname; } /** @@ -77,7 +78,7 @@ class CriConnection extends Connection { _runJsonCommand(command) { return new Promise((resolve, reject) => { const request = http.get({ - hostname: hostname, + hostname: this.hostname, port: this.port, path: '/json/' + command }, response => { diff --git a/lighthouse-core/index.js b/lighthouse-core/index.js index a1f90a66e586..0f2b6607fabb 100644 --- a/lighthouse-core/index.js +++ b/lighthouse-core/index.js @@ -35,7 +35,7 @@ module.exports = function(url, flags = {}, configJSON) { // Use ConfigParser to generate a valid config file const config = new Config(configJSON, flags.configPath); - const connection = new ChromeProtocol(flags.port); + const connection = new ChromeProtocol(flags.port, flags.hostname); // kick off a lighthouse run return Runner.run(connection, {url, flags, config}) diff --git a/readme.md b/readme.md index 0984ea97fe77..c41070edb207 100644 --- a/readme.md +++ b/readme.md @@ -63,6 +63,7 @@ Configuration: [default: ""] --perf Use a performance-test-only configuration [boolean] --port The port to use for the debugging protocol. Use 0 for a random port [default: 9222] + --hostname The hostname to use for the debugging protocol. [default: localhost] --max-wait-for-load 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 [default: 25000]