From 279cd4c9fb54cddaffbb00ce84a0024e1b3f9b8f Mon Sep 17 00:00:00 2001 From: Andrey Lushnikov Date: Tue, 11 Jul 2017 08:00:21 -0700 Subject: [PATCH] Remote Browser's remoteDebuggingPort option This patch remove remoteDebuggingPort option. Instead, browser is launched with '--remote-debugging-port=0' flag, letting browser to pick any port. The puppeteer reads the port number from the browser's stderr stream. This change cuts average browser start time from 300ms to 250ms on my machine. This happens since puppeteer doesn't have to probe network once every 100ms, waiting for the remote debugging server to instantiate. Fixes #21. --- docs/api.md | 1 - examples/custom-chromium-revision.js | 11 ++---- lib/Browser.js | 50 +++++++++++++--------------- phantom_shim/runner.js | 1 - 4 files changed, 27 insertions(+), 36 deletions(-) diff --git a/docs/api.md b/docs/api.md index d14161f47f4..c99c95df345 100644 --- a/docs/api.md +++ b/docs/api.md @@ -88,7 +88,6 @@ not necessarily result in launching browser; the instance will be launched when - `options` <[Object]> Set of configurable options to set on the browser. Can have the following fields: - `headless` <[boolean]> Wether to run chromium in headless mode. Defaults to `true`. - - `remoteDebuggingPort` <[number]> Specify a remote debugging port to open on chromium instance. Defaults to `9229`. - `executablePath` <[string]> Path to a chromium executable to run instead of bundled chromium. - `args` <[Array]<[string]>> Additional arguments to pass to the chromium instance. List of chromium flags could be found [here](http://peter.sh/experiments/chromium-command-line-switches/). diff --git a/examples/custom-chromium-revision.js b/examples/custom-chromium-revision.js index 1bff0bf9115..0b4fe4628dc 100644 --- a/examples/custom-chromium-revision.js +++ b/examples/custom-chromium-revision.js @@ -17,18 +17,13 @@ var Browser = require('../lib/Browser'); var Downloader = require('../utils/ChromiumDownloader'); -var revision = "464642"; +var revision = '483012'; console.log('Downloading custom chromium revision - ' + revision); Downloader.downloadRevision(Downloader.currentPlatform(), revision).then(async () => { console.log('Done.'); var executablePath = Downloader.revisionInfo(Downloader.currentPlatform(), revision).executablePath; - var browser1 = new Browser({ - remoteDebuggingPort: 9228, - executablePath, - }); - var browser2 = new Browser({ - remoteDebuggingPort: 9229, - }); + var browser1 = new Browser({ executablePath }); + var browser2 = new Browser(); var [version1, version2] = await Promise.all([ browser1.version(), browser2.version() diff --git a/lib/Browser.js b/lib/Browser.js index 0626b3d836c..2555b6a7657 100644 --- a/lib/Browser.js +++ b/lib/Browser.js @@ -15,13 +15,13 @@ */ let {Duplex} = require('stream'); -let http = require('http'); let path = require('path'); let removeRecursive = require('rimraf').sync; let Page = require('./Page'); let childProcess = require('child_process'); let Downloader = require('../utils/ChromiumDownloader'); let Connection = require('./Connection'); +let readline = require('readline'); let CHROME_PROFILE_PATH = path.resolve(__dirname, '..', '.dev_profile'); let browserId = 0; @@ -29,6 +29,7 @@ let browserId = 0; let DEFAULT_ARGS = [ '--disable-background-timer-throttling', '--no-first-run', + '--remote-debugging-port=0', ]; class Browser { @@ -39,12 +40,9 @@ class Browser { options = options || {}; ++browserId; this._userDataDir = CHROME_PROFILE_PATH + browserId; - this._remoteDebuggingPort = 9227; - if (typeof options.remoteDebuggingPort === 'number') - this._remoteDebuggingPort = options.remoteDebuggingPort; + this._remoteDebuggingPort = 0; this._chromeArguments = DEFAULT_ARGS.concat([ `--user-data-dir=${this._userDataDir}`, - `--remote-debugging-port=${this._remoteDebuggingPort}`, ]); if (typeof options.headless !== 'boolean' || options.headless) { this._chromeArguments.push(...[ @@ -122,7 +120,13 @@ class Browser { this._chromeProcess.stderr.pipe(this.stderr); this._chromeProcess.stdout.pipe(this.stdout); - await waitForChromeResponsive(this._remoteDebuggingPort, () => !this._terminated); + this._remoteDebuggingPort = await waitForRemoteDebuggingPort(this._chromeProcess); + // Failed to connect to browser. + if (this._remoteDebuggingPort === -1) { + this._chromeProcess.kill(); + throw new Error('Failed to connect to chrome!'); + } + if (this._terminated) throw new Error('Failed to launch chrome! ' + stderr); } @@ -136,30 +140,24 @@ class Browser { module.exports = Browser; -function waitForChromeResponsive(remoteDebuggingPort, shouldWaitCallback) { +function waitForRemoteDebuggingPort(chromeProcess) { + const rl = readline.createInterface({ input: chromeProcess.stderr }); let fulfill; let promise = new Promise(x => fulfill = x); - let options = { - method: 'GET', - host: 'localhost', - port: remoteDebuggingPort, - path: '/json/list' - }; - let probeTimeout = 100; - sendRequest(); + rl.on('line', onLine); + rl.once('close', () => fulfill(-1)); return promise; - function sendRequest() { - let req = http.request(options, res => { - fulfill(); - }); - req.on('error', e => { - if (shouldWaitCallback()) - setTimeout(sendRequest, probeTimeout); - else - fulfill(); - }); - req.end(); + /** + * @param {string} line + */ + function onLine(line) { + const match = line.match(/^DevTools listening on .*:([\d]+)$/); + if (!match) + return; + fulfill(Number.parseInt(match[1], 10)); + rl.removeListener('line', onLine); + rl.close(); } } diff --git a/phantom_shim/runner.js b/phantom_shim/runner.js index 9d35aec8429..550cfdf9b84 100755 --- a/phantom_shim/runner.js +++ b/phantom_shim/runner.js @@ -57,7 +57,6 @@ if (!fs.existsSync(scriptPath)) { } let browser = new Browser({ - remoteDebuggingPort: 9229, headless: argv.headless, args: ['--no-sandbox'] });