Skip to content

Commit

Permalink
Reformat codebase into 2-spaces
Browse files Browse the repository at this point in the history
This patch:
- reformats codebase to use 2-spaces instead of 4. This will
  align the project with other codebases (e.g. DevTools and Lighthouse)
- enables eslint indentation checking

References puppeteer#19
  • Loading branch information
aslushnikov authored and pavelfeldman committed Jun 21, 2017
1 parent bbaf2f0 commit 448ac4c
Show file tree
Hide file tree
Showing 25 changed files with 3,680 additions and 2,766 deletions.
3 changes: 1 addition & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,11 @@ module.exports = {
"no-trailing-spaces": 2,
"linebreak-style": [ 2, "unix" ],

"indent": [2, 2, { "SwitchCase": 1, "CallExpression": {"arguments": 2}, "MemberExpression": 2 }],
/**
* Disabled, aspirational rules
*/

"indent": [0, 4, { "SwitchCase": 1, "CallExpression": {"arguments": 2}, "MemberExpression": 2 }],

// brace-style is disabled, as eslint cannot enforce 1tbs as default, but allman for functions
"brace-style": [0, "allman", { "allowSingleLine": true }],

Expand Down
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
*/

module.exports = {
Browser: require('./lib/Browser')
Browser: require('./lib/Browser')
};
28 changes: 13 additions & 15 deletions install.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,26 @@ var ProgressBar = require('progress');

// Do nothing if the revision is already downloaded.
if (Downloader.revisionInfo(Downloader.currentPlatform(), revision))
return;
return;

Downloader.downloadRevision(Downloader.currentPlatform(), revision, onProgress)
.catch(error => {
console.error('Download failed: ' + error.message);
});
.catch(error => console.error('Download failed: ' + error.message));

var progressBar = null;
function onProgress(bytesTotal, delta) {
if (!progressBar) {
progressBar = new ProgressBar(`Downloading Chromium - ${toMegabytes(bytesTotal)} [:bar] :percent :etas `, {
complete: '=',
incomplete: ' ',
width: 20,
total: bytesTotal,
});
}
progressBar.tick(delta);
if (!progressBar) {
progressBar = new ProgressBar(`Downloading Chromium - ${toMegabytes(bytesTotal)} [:bar] :percent :etas `, {
complete: '=',
incomplete: ' ',
width: 20,
total: bytesTotal,
});
}
progressBar.tick(delta);
}

function toMegabytes(bytes) {
var mb = bytes / 1024 / 1024;
return (Math.round(mb * 10) / 10) + ' Mb';
var mb = bytes / 1024 / 1024;
return (Math.round(mb * 10) / 10) + ' Mb';
}

210 changes: 105 additions & 105 deletions lib/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,125 +26,125 @@ var CHROME_PROFILE_PATH = path.resolve(__dirname, '..', '.dev_profile');
var browserId = 0;

var DEFAULT_ARGS = [
'--disable-background-timer-throttling',
'--no-first-run',
'--disable-background-timer-throttling',
'--no-first-run',
];

class Browser {
/**
* @param {(!Object|undefined)} options
*/
constructor(options) {
options = options || {};
++browserId;
this._userDataDir = CHROME_PROFILE_PATH + browserId;
this._remoteDebuggingPort = 9227;
if (typeof options.remoteDebuggingPort === 'number')
this._remoteDebuggingPort = options.remoteDebuggingPort;
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(...[
`--headless`,
`--disable-gpu`,
]);
}
if (typeof options.executablePath === 'string') {
this._chromeExecutable = options.executablePath;
} else {
var chromiumRevision = require('../package.json').puppeteer.chromium_revision;
var revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), chromiumRevision);
console.assert(revisionInfo, 'Chromium revision is not downloaded. Run npm install');
this._chromeExecutable = revisionInfo.executablePath;
}
if (Array.isArray(options.args))
this._chromeArguments.push(...options.args);
this._terminated = false;
this._chromeProcess = null;
/**
* @param {(!Object|undefined)} options
*/
constructor(options) {
options = options || {};
++browserId;
this._userDataDir = CHROME_PROFILE_PATH + browserId;
this._remoteDebuggingPort = 9227;
if (typeof options.remoteDebuggingPort === 'number')
this._remoteDebuggingPort = options.remoteDebuggingPort;
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(...[
`--headless`,
`--disable-gpu`,
]);
}

/**
* @return {!Promise<!Page>}
*/
async newPage() {
await this._ensureChromeIsRunning();
if (!this._chromeProcess || this._terminated)
throw new Error('ERROR: this chrome instance is not alive any more!');
var client = await Connection.create(this._remoteDebuggingPort);
var page = await Page.create(client);
return page;
if (typeof options.executablePath === 'string') {
this._chromeExecutable = options.executablePath;
} else {
var chromiumRevision = require('../package.json').puppeteer.chromium_revision;
var revisionInfo = Downloader.revisionInfo(Downloader.currentPlatform(), chromiumRevision);
console.assert(revisionInfo, 'Chromium revision is not downloaded. Run npm install');
this._chromeExecutable = revisionInfo.executablePath;
}
if (Array.isArray(options.args))
this._chromeArguments.push(...options.args);
this._terminated = false;
this._chromeProcess = null;
}

/**
* @param {!Page} page
*/
async closePage(page) {
if (!this._chromeProcess || this._terminated)
throw new Error('ERROR: this chrome instance is not running');
await page.close();
}
/**
* @return {!Promise<!Page>}
*/
async newPage() {
await this._ensureChromeIsRunning();
if (!this._chromeProcess || this._terminated)
throw new Error('ERROR: this chrome instance is not alive any more!');
var client = await Connection.create(this._remoteDebuggingPort);
var page = await Page.create(client);
return page;
}

/**
* @return {string}
*/
async version() {
await this._ensureChromeIsRunning();
var version = await Connection.version(this._remoteDebuggingPort);
return version.Browser;
}
/**
* @param {!Page} page
*/
async closePage(page) {
if (!this._chromeProcess || this._terminated)
throw new Error('ERROR: this chrome instance is not running');
await page.close();
}

async _ensureChromeIsRunning() {
if (this._chromeProcess)
return;
this._chromeProcess = childProcess.spawn(this._chromeExecutable, this._chromeArguments, {});
var stderr = '';
this._chromeProcess.stderr.on('data', data => stderr += data.toString('utf8'));
// Cleanup as processes exit.
process.on('exit', () => this._chromeProcess.kill());
this._chromeProcess.on('exit', () => {
this._terminated = true;
removeRecursive(this._userDataDir);
});
/**
* @return {string}
*/
async version() {
await this._ensureChromeIsRunning();
var version = await Connection.version(this._remoteDebuggingPort);
return version.Browser;
}

await waitForChromeResponsive(this._remoteDebuggingPort, () => !this._terminated);
if (this._terminated)
throw new Error('Failed to launch chrome! ' + stderr);
}
async _ensureChromeIsRunning() {
if (this._chromeProcess)
return;
this._chromeProcess = childProcess.spawn(this._chromeExecutable, this._chromeArguments, {});
var stderr = '';
this._chromeProcess.stderr.on('data', data => stderr += data.toString('utf8'));
// Cleanup as processes exit.
process.on('exit', () => this._chromeProcess.kill());
this._chromeProcess.on('exit', () => {
this._terminated = true;
removeRecursive(this._userDataDir);
});

close() {
if (!this._chromeProcess)
return;
this._chromeProcess.kill();
}
await waitForChromeResponsive(this._remoteDebuggingPort, () => !this._terminated);
if (this._terminated)
throw new Error('Failed to launch chrome! ' + stderr);
}

close() {
if (!this._chromeProcess)
return;
this._chromeProcess.kill();
}
}

module.exports = Browser;

function waitForChromeResponsive(remoteDebuggingPort, shouldWaitCallback) {
var fulfill;
var promise = new Promise(x => fulfill = x);
var options = {
method: 'GET',
host: 'localhost',
port: remoteDebuggingPort,
path: '/json/list'
};
var probeTimeout = 100;
sendRequest();
return promise;
var fulfill;
var promise = new Promise(x => fulfill = x);
var options = {
method: 'GET',
host: 'localhost',
port: remoteDebuggingPort,
path: '/json/list'
};
var probeTimeout = 100;
sendRequest();
return promise;

function sendRequest() {
var req = http.request(options, res => {
fulfill();
});
req.on('error', e => {
if (shouldWaitCallback())
setTimeout(sendRequest, probeTimeout);
else
fulfill();
});
req.end();
}
function sendRequest() {
var req = http.request(options, res => {
fulfill();
});
req.on('error', e => {
if (shouldWaitCallback())
setTimeout(sendRequest, probeTimeout);
else
fulfill();
});
req.end();
}
}
Loading

0 comments on commit 448ac4c

Please sign in to comment.