mirror of
https://github.com/puppeteer/puppeteer
synced 2024-06-14 14:02:48 +00:00
Reformat codebase into 2-spaces
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 #19
This commit is contained in:
parent
bbaf2f091c
commit
448ac4ce64
@ -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 }],
|
||||
|
||||
|
2
index.js
2
index.js
@ -15,5 +15,5 @@
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
Browser: require('./lib/Browser')
|
||||
Browser: require('./lib/Browser')
|
||||
};
|
||||
|
28
install.js
28
install.js
@ -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
lib/Browser.js
210
lib/Browser.js
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -20,90 +20,90 @@ var http = require('http');
|
||||
const COMMAND_TIMEOUT = 10000;
|
||||
|
||||
class Connection extends EventEmitter {
|
||||
/**
|
||||
* @param {number} port
|
||||
* @param {string} pageId
|
||||
* @param {!WebSocket} ws
|
||||
*/
|
||||
constructor(port, pageId, ws) {
|
||||
super();
|
||||
this._port = port;
|
||||
this._pageId = pageId;
|
||||
this._lastId = 0;
|
||||
/** @type {!Map<number, {resolve: function(*), reject: function(*), method: string}>}*/
|
||||
this._callbacks = new Map();
|
||||
/**
|
||||
* @param {number} port
|
||||
* @param {string} pageId
|
||||
* @param {!WebSocket} ws
|
||||
*/
|
||||
constructor(port, pageId, ws) {
|
||||
super();
|
||||
this._port = port;
|
||||
this._pageId = pageId;
|
||||
this._lastId = 0;
|
||||
/** @type {!Map<number, {resolve: function(*), reject: function(*), method: string}>}*/
|
||||
this._callbacks = new Map();
|
||||
|
||||
this._ws = ws;
|
||||
this._ws.on('message', this._onMessage.bind(this));
|
||||
this._ws.on('close', this._onClose.bind(this));
|
||||
this._ws = ws;
|
||||
this._ws.on('message', this._onMessage.bind(this));
|
||||
this._ws.on('close', this._onClose.bind(this));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} method
|
||||
* @param {(!Object|undefined)} params
|
||||
* @return {!Promise<?Object>}
|
||||
*/
|
||||
send(method, params = {}) {
|
||||
var id = ++this._lastId;
|
||||
var message = JSON.stringify({id, method, params});
|
||||
this._ws.send(message);
|
||||
return new Promise((resolve, reject) => {
|
||||
this._callbacks.set(id, {resolve, reject, method});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} message
|
||||
*/
|
||||
_onMessage(message) {
|
||||
var object = JSON.parse(message);
|
||||
if (object.id && this._callbacks.has(object.id)) {
|
||||
var callback = this._callbacks.get(object.id);
|
||||
this._callbacks.delete(object.id);
|
||||
if (object.error)
|
||||
callback.reject(new Error(`Protocol error (${callback.method}): ${object.error.message}`));
|
||||
else
|
||||
callback.resolve(object.result);
|
||||
} else {
|
||||
console.assert(!object.id);
|
||||
this.emit(object.method, object.params);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} method
|
||||
* @param {(!Object|undefined)} params
|
||||
* @return {!Promise<?Object>}
|
||||
*/
|
||||
send(method, params = {}) {
|
||||
var id = ++this._lastId;
|
||||
var message = JSON.stringify({id, method, params});
|
||||
this._ws.send(message);
|
||||
return new Promise((resolve, reject) => {
|
||||
this._callbacks.set(id, {resolve, reject, method});
|
||||
});
|
||||
}
|
||||
_onClose() {
|
||||
this._ws.removeAllListeners();
|
||||
this._ws.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} message
|
||||
*/
|
||||
_onMessage(message) {
|
||||
var object = JSON.parse(message);
|
||||
if (object.id && this._callbacks.has(object.id)) {
|
||||
var callback = this._callbacks.get(object.id);
|
||||
this._callbacks.delete(object.id);
|
||||
if (object.error)
|
||||
callback.reject(new Error(`Protocol error (${callback.method}): ${object.error.message}`));
|
||||
else
|
||||
callback.resolve(object.result);
|
||||
} else {
|
||||
console.assert(!object.id);
|
||||
this.emit(object.method, object.params);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @return {!Promise}
|
||||
*/
|
||||
async dispose() {
|
||||
await runJsonCommand(this._port, `close/${this._pageId}`);
|
||||
}
|
||||
|
||||
_onClose() {
|
||||
this._ws.removeAllListeners();
|
||||
this._ws.close();
|
||||
}
|
||||
/**
|
||||
* @param {number} port
|
||||
* @return {!Promise<!Connection>}
|
||||
*/
|
||||
static async create(port) {
|
||||
var newTab = await runJsonCommand(port, 'new');
|
||||
var url = newTab.webSocketDebuggerUrl;
|
||||
|
||||
/**
|
||||
* @return {!Promise}
|
||||
*/
|
||||
async dispose() {
|
||||
await runJsonCommand(this._port, `close/${this._pageId}`);
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
var ws = new WebSocket(url, { perMessageDeflate: false });
|
||||
ws.on('open', () => resolve(new Connection(port, newTab.id, ws)));
|
||||
ws.on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} port
|
||||
* @return {!Promise<!Connection>}
|
||||
*/
|
||||
static async create(port) {
|
||||
var newTab = await runJsonCommand(port, 'new');
|
||||
var url = newTab.webSocketDebuggerUrl;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
var ws = new WebSocket(url, { perMessageDeflate: false });
|
||||
ws.on('open', () => resolve(new Connection(port, newTab.id, ws)));
|
||||
ws.on('error', reject);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} port
|
||||
* @return {!Promise<!Object>}
|
||||
*/
|
||||
static version(port) {
|
||||
return runJsonCommand(port, 'version');
|
||||
}
|
||||
/**
|
||||
* @param {number} port
|
||||
* @return {!Promise<!Object>}
|
||||
*/
|
||||
static version(port) {
|
||||
return runJsonCommand(port, 'version');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,41 +112,41 @@ class Connection extends EventEmitter {
|
||||
* @return {!Promise<!Object>}
|
||||
*/
|
||||
function runJsonCommand(port, command) {
|
||||
var request = http.get({
|
||||
hostname: 'localhost',
|
||||
port: port,
|
||||
path: '/json/' + command
|
||||
}, onResponse);
|
||||
request.setTimeout(COMMAND_TIMEOUT, onTimeout);
|
||||
var resolve, reject;
|
||||
return new Promise((res, rej) => { resolve = res; reject = rej; });
|
||||
var request = http.get({
|
||||
hostname: 'localhost',
|
||||
port: port,
|
||||
path: '/json/' + command
|
||||
}, onResponse);
|
||||
request.setTimeout(COMMAND_TIMEOUT, onTimeout);
|
||||
var resolve, reject;
|
||||
return new Promise((res, rej) => { resolve = res; reject = rej; });
|
||||
|
||||
function onResponse(response) {
|
||||
var data = '';
|
||||
response.on('data', chunk => data += chunk);
|
||||
response.on('end', () => {
|
||||
if (response.statusCode !== 200) {
|
||||
reject(new Error(`Protocol JSON API error (${command}), status: ${response.statusCode}`));
|
||||
return;
|
||||
}
|
||||
// In the case of 'close' & 'activate' Chromium returns a string rather than JSON: goo.gl/7v27xD
|
||||
if (data === 'Target is closing' || data === 'Target activated') {
|
||||
resolve({message: data});
|
||||
return;
|
||||
}
|
||||
try {
|
||||
resolve(JSON.parse(data));
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
function onResponse(response) {
|
||||
var data = '';
|
||||
response.on('data', chunk => data += chunk);
|
||||
response.on('end', () => {
|
||||
if (response.statusCode !== 200) {
|
||||
reject(new Error(`Protocol JSON API error (${command}), status: ${response.statusCode}`));
|
||||
return;
|
||||
}
|
||||
// In the case of 'close' & 'activate' Chromium returns a string rather than JSON: goo.gl/7v27xD
|
||||
if (data === 'Target is closing' || data === 'Target activated') {
|
||||
resolve({message: data});
|
||||
return;
|
||||
}
|
||||
try {
|
||||
resolve(JSON.parse(data));
|
||||
} catch (e) {
|
||||
reject(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function onTimeout() {
|
||||
request.abort();
|
||||
// Reject on error with code specifically indicating timeout in connection setup.
|
||||
reject(new Error('Timeout waiting for initial Debugger Protocol connection.'));
|
||||
}
|
||||
function onTimeout() {
|
||||
request.abort();
|
||||
// Reject on error with code specifically indicating timeout in connection setup.
|
||||
reject(new Error('Timeout waiting for initial Debugger Protocol connection.'));
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Connection;
|
||||
|
@ -15,55 +15,55 @@
|
||||
*/
|
||||
|
||||
class Dialog {
|
||||
/**
|
||||
* @param {!Connection} client
|
||||
* @param {!Dialog.Type} type
|
||||
* @param {string} message
|
||||
*/
|
||||
constructor(client, type, message) {
|
||||
this._client = client;
|
||||
this.type = type;
|
||||
this._message = message;
|
||||
this._handled = false;
|
||||
}
|
||||
/**
|
||||
* @param {!Connection} client
|
||||
* @param {!Dialog.Type} type
|
||||
* @param {string} message
|
||||
*/
|
||||
constructor(client, type, message) {
|
||||
this._client = client;
|
||||
this.type = type;
|
||||
this._message = message;
|
||||
this._handled = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
message() {
|
||||
return this._message;
|
||||
}
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
message() {
|
||||
return this._message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string=} promptText
|
||||
* @return {!Promise}
|
||||
*/
|
||||
async accept(promptText) {
|
||||
console.assert(!this._handled, 'Cannot accept dialog which is already handled!');
|
||||
this._handled = true;
|
||||
await this._client.send('Page.handleJavaScriptDialog', {
|
||||
accept: true,
|
||||
promptText: promptText
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @param {string=} promptText
|
||||
* @return {!Promise}
|
||||
*/
|
||||
async accept(promptText) {
|
||||
console.assert(!this._handled, 'Cannot accept dialog which is already handled!');
|
||||
this._handled = true;
|
||||
await this._client.send('Page.handleJavaScriptDialog', {
|
||||
accept: true,
|
||||
promptText: promptText
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!Promise}
|
||||
*/
|
||||
async dismiss() {
|
||||
console.assert(!this._handled, 'Cannot dismiss dialog which is already handled!');
|
||||
this._handled = true;
|
||||
await this._client.send('Page.handleJavaScriptDialog', {
|
||||
accept: false
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @return {!Promise}
|
||||
*/
|
||||
async dismiss() {
|
||||
console.assert(!this._handled, 'Cannot dismiss dialog which is already handled!');
|
||||
this._handled = true;
|
||||
await this._client.send('Page.handleJavaScriptDialog', {
|
||||
accept: false
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Dialog.Type = {
|
||||
Alert: 'alert',
|
||||
BeforeUnload: 'beforeunload',
|
||||
Confirm: 'confirm',
|
||||
Prompt: 'prompt'
|
||||
Alert: 'alert',
|
||||
BeforeUnload: 'beforeunload',
|
||||
Confirm: 'confirm',
|
||||
Prompt: 'prompt'
|
||||
};
|
||||
|
||||
module.exports = Dialog;
|
||||
|
@ -17,229 +17,229 @@
|
||||
var EventEmitter = require('events');
|
||||
|
||||
class FrameManager extends EventEmitter {
|
||||
/**
|
||||
* @param {!Connection} client
|
||||
* @return {!FrameManager}
|
||||
*/
|
||||
static async create(client) {
|
||||
var mainFramePayload = await client.send('Page.getResourceTree');
|
||||
return new FrameManager(client, mainFramePayload.frameTree);
|
||||
/**
|
||||
* @param {!Connection} client
|
||||
* @return {!FrameManager}
|
||||
*/
|
||||
static async create(client) {
|
||||
var mainFramePayload = await client.send('Page.getResourceTree');
|
||||
return new FrameManager(client, mainFramePayload.frameTree);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Connection} client
|
||||
* @param {!Object} frameTree
|
||||
*/
|
||||
constructor(client, frameTree) {
|
||||
super();
|
||||
this._client = client;
|
||||
/** @type {!Map<string, !Frame>} */
|
||||
this._frames = new Map();
|
||||
this._mainFrame = this._addFramesRecursively(null, frameTree);
|
||||
|
||||
this._client.on('Page.frameAttached', event => this._frameAttached(event.frameId, event.parentFrameId));
|
||||
this._client.on('Page.frameNavigated', event => this._frameNavigated(event.frame));
|
||||
this._client.on('Page.frameDetached', event => this._frameDetached(event.frameId));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!Frame}
|
||||
*/
|
||||
mainFrame() {
|
||||
return this._mainFrame;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!Array<!Frame>}
|
||||
*/
|
||||
frames() {
|
||||
return Array.from(this._frames.values());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} frameId
|
||||
* @param {?string} parentFrameId
|
||||
* @return {?Frame}
|
||||
*/
|
||||
_frameAttached(frameId, parentFrameId) {
|
||||
if (this._frames.has(frameId))
|
||||
return;
|
||||
|
||||
if (!parentFrameId) {
|
||||
// Navigation to the new backend process.
|
||||
this._navigateFrame(this._mainFrame, frameId, null);
|
||||
return;
|
||||
}
|
||||
var parentFrame = this._frames.get(parentFrameId);
|
||||
var frame = new Frame(parentFrame, frameId, null);
|
||||
this._frames.set(frame._id, frame);
|
||||
this.emit(FrameManager.Events.FrameAttached, frame);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Connection} client
|
||||
* @param {!Object} frameTree
|
||||
*/
|
||||
constructor(client, frameTree) {
|
||||
super();
|
||||
this._client = client;
|
||||
/** @type {!Map<string, !Frame>} */
|
||||
this._frames = new Map();
|
||||
this._mainFrame = this._addFramesRecursively(null, frameTree);
|
||||
|
||||
this._client.on('Page.frameAttached', event => this._frameAttached(event.frameId, event.parentFrameId));
|
||||
this._client.on('Page.frameNavigated', event => this._frameNavigated(event.frame));
|
||||
this._client.on('Page.frameDetached', event => this._frameDetached(event.frameId));
|
||||
/**
|
||||
* @param {!Object} framePayload
|
||||
*/
|
||||
_frameNavigated(framePayload) {
|
||||
var frame = this._frames.get(framePayload.id);
|
||||
if (!frame) {
|
||||
// Simulate missed "frameAttached" for a main frame navigation to the new backend process.
|
||||
console.assert(!framePayload.parentId, 'Main frame shouldn\'t have parent frame id.');
|
||||
frame = this._mainFrame;
|
||||
}
|
||||
this._navigateFrame(frame, framePayload.id, framePayload);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!Frame}
|
||||
*/
|
||||
mainFrame() {
|
||||
return this._mainFrame;
|
||||
}
|
||||
/**
|
||||
* @param {string} frameId
|
||||
*/
|
||||
_frameDetached(frameId) {
|
||||
var frame = this._frames.get(frameId);
|
||||
if (frame)
|
||||
this._removeFramesRecursively(frame);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!Array<!Frame>}
|
||||
*/
|
||||
frames() {
|
||||
return Array.from(this._frames.values());
|
||||
}
|
||||
/**
|
||||
* @param {!Frame} frame
|
||||
* @param {string} newFrameId
|
||||
* @param {?Object} newFramePayload
|
||||
*/
|
||||
_navigateFrame(frame, newFrameId, newFramePayload) {
|
||||
// Detach all child frames first.
|
||||
for (var child of frame.childFrames())
|
||||
this._removeFramesRecursively(child);
|
||||
this._frames.delete(frame._id, frame);
|
||||
frame._id = newFrameId;
|
||||
frame._adoptPayload(newFramePayload);
|
||||
this._frames.set(newFrameId, frame);
|
||||
this.emit(FrameManager.Events.FrameNavigated, frame);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} frameId
|
||||
* @param {?string} parentFrameId
|
||||
* @return {?Frame}
|
||||
*/
|
||||
_frameAttached(frameId, parentFrameId) {
|
||||
if (this._frames.has(frameId))
|
||||
return;
|
||||
/**
|
||||
* @param {?Frame} parentFrame
|
||||
* @param {!Object} frameTreePayload
|
||||
* @return {!Frame}
|
||||
*/
|
||||
_addFramesRecursively(parentFrame, frameTreePayload) {
|
||||
var framePayload = frameTreePayload.frame;
|
||||
var frame = new Frame(parentFrame, framePayload.id, framePayload);
|
||||
this._frames.set(frame._id, frame);
|
||||
|
||||
if (!parentFrameId) {
|
||||
// Navigation to the new backend process.
|
||||
this._navigateFrame(this._mainFrame, frameId, null);
|
||||
return;
|
||||
}
|
||||
var parentFrame = this._frames.get(parentFrameId);
|
||||
var frame = new Frame(parentFrame, frameId, null);
|
||||
this._frames.set(frame._id, frame);
|
||||
this.emit(FrameManager.Events.FrameAttached, frame);
|
||||
}
|
||||
for (var i = 0; frameTreePayload.childFrames && i < frameTreePayload.childFrames.length; ++i)
|
||||
this._addFramesRecursively(frame, frameTreePayload.childFrames[i]);
|
||||
return frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Object} framePayload
|
||||
*/
|
||||
_frameNavigated(framePayload) {
|
||||
var frame = this._frames.get(framePayload.id);
|
||||
if (!frame) {
|
||||
// Simulate missed "frameAttached" for a main frame navigation to the new backend process.
|
||||
console.assert(!framePayload.parentId, 'Main frame shouldn\'t have parent frame id.');
|
||||
frame = this._mainFrame;
|
||||
}
|
||||
this._navigateFrame(frame, framePayload.id, framePayload);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} frameId
|
||||
*/
|
||||
_frameDetached(frameId) {
|
||||
var frame = this._frames.get(frameId);
|
||||
if (frame)
|
||||
this._removeFramesRecursively(frame);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Frame} frame
|
||||
* @param {string} newFrameId
|
||||
* @param {?Object} newFramePayload
|
||||
*/
|
||||
_navigateFrame(frame, newFrameId, newFramePayload) {
|
||||
// Detach all child frames first.
|
||||
for (var child of frame.childFrames())
|
||||
this._removeFramesRecursively(child);
|
||||
this._frames.delete(frame._id, frame);
|
||||
frame._id = newFrameId;
|
||||
frame._adoptPayload(newFramePayload);
|
||||
this._frames.set(newFrameId, frame);
|
||||
this.emit(FrameManager.Events.FrameNavigated, frame);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {?Frame} parentFrame
|
||||
* @param {!Object} frameTreePayload
|
||||
* @return {!Frame}
|
||||
*/
|
||||
_addFramesRecursively(parentFrame, frameTreePayload) {
|
||||
var framePayload = frameTreePayload.frame;
|
||||
var frame = new Frame(parentFrame, framePayload.id, framePayload);
|
||||
this._frames.set(frame._id, frame);
|
||||
|
||||
for (var i = 0; frameTreePayload.childFrames && i < frameTreePayload.childFrames.length; ++i)
|
||||
this._addFramesRecursively(frame, frameTreePayload.childFrames[i]);
|
||||
return frame;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Frame} frame
|
||||
*/
|
||||
_removeFramesRecursively(frame) {
|
||||
for (var child of frame.childFrames())
|
||||
this._removeFramesRecursively(child);
|
||||
frame._detach();
|
||||
this._frames.delete(frame._id);
|
||||
this.emit(FrameManager.Events.FrameDetached, frame);
|
||||
}
|
||||
/**
|
||||
* @param {!Frame} frame
|
||||
*/
|
||||
_removeFramesRecursively(frame) {
|
||||
for (var child of frame.childFrames())
|
||||
this._removeFramesRecursively(child);
|
||||
frame._detach();
|
||||
this._frames.delete(frame._id);
|
||||
this.emit(FrameManager.Events.FrameDetached, frame);
|
||||
}
|
||||
}
|
||||
|
||||
/** @enum {string} */
|
||||
FrameManager.Events = {
|
||||
FrameAttached: 'frameattached',
|
||||
FrameNavigated: 'framenavigated',
|
||||
FrameDetached: 'framedetached'
|
||||
FrameAttached: 'frameattached',
|
||||
FrameNavigated: 'framenavigated',
|
||||
FrameDetached: 'framedetached'
|
||||
};
|
||||
|
||||
/**
|
||||
* @unrestricted
|
||||
*/
|
||||
class Frame {
|
||||
/**
|
||||
* @param {?Frame} parentFrame
|
||||
* @param {string} frameId
|
||||
* @param {?Object} payload
|
||||
*/
|
||||
constructor(parentFrame, frameId, payload) {
|
||||
this._parentFrame = parentFrame;
|
||||
this._url = '';
|
||||
this._id = frameId;
|
||||
this._adoptPayload(payload);
|
||||
/**
|
||||
* @param {?Frame} parentFrame
|
||||
* @param {string} frameId
|
||||
* @param {?Object} payload
|
||||
*/
|
||||
constructor(parentFrame, frameId, payload) {
|
||||
this._parentFrame = parentFrame;
|
||||
this._url = '';
|
||||
this._id = frameId;
|
||||
this._adoptPayload(payload);
|
||||
|
||||
/** @type {!Set<!Frame>} */
|
||||
this._childFrames = new Set();
|
||||
if (this._parentFrame)
|
||||
this._parentFrame._childFrames.add(this);
|
||||
}
|
||||
/** @type {!Set<!Frame>} */
|
||||
this._childFrames = new Set();
|
||||
if (this._parentFrame)
|
||||
this._parentFrame._childFrames.add(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
name() {
|
||||
return this._name || '';
|
||||
}
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
name() {
|
||||
return this._name || '';
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
url() {
|
||||
return this._url;
|
||||
}
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
url() {
|
||||
return this._url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
securityOrigin() {
|
||||
return this._securityOrigin;
|
||||
}
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
securityOrigin() {
|
||||
return this._securityOrigin;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {?Frame}
|
||||
*/
|
||||
parentFrame() {
|
||||
return this._parentFrame;
|
||||
}
|
||||
/**
|
||||
* @return {?Frame}
|
||||
*/
|
||||
parentFrame() {
|
||||
return this._parentFrame;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!Array.<!Frame>}
|
||||
*/
|
||||
childFrames() {
|
||||
return Array.from(this._childFrames);
|
||||
}
|
||||
/**
|
||||
* @return {!Array.<!Frame>}
|
||||
*/
|
||||
childFrames() {
|
||||
return Array.from(this._childFrames);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {boolean}
|
||||
*/
|
||||
isMainFrame() {
|
||||
return !this._detached && !this._parentFrame;
|
||||
}
|
||||
/**
|
||||
* @return {boolean}
|
||||
*/
|
||||
isMainFrame() {
|
||||
return !this._detached && !this._parentFrame;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {boolean}
|
||||
*/
|
||||
isDetached() {
|
||||
return this._detached;
|
||||
}
|
||||
/**
|
||||
* @return {boolean}
|
||||
*/
|
||||
isDetached() {
|
||||
return this._detached;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {?Object} framePayload
|
||||
*/
|
||||
_adoptPayload(framePayload) {
|
||||
framePayload = framePayload || {
|
||||
name: '',
|
||||
url: '',
|
||||
securityOrigin: '',
|
||||
mimeType: ''
|
||||
};
|
||||
this._name = framePayload.name;
|
||||
this._url = framePayload.url;
|
||||
this._securityOrigin = framePayload.securityOrigin;
|
||||
this._mimeType = framePayload.mimeType;
|
||||
}
|
||||
/**
|
||||
* @param {?Object} framePayload
|
||||
*/
|
||||
_adoptPayload(framePayload) {
|
||||
framePayload = framePayload || {
|
||||
name: '',
|
||||
url: '',
|
||||
securityOrigin: '',
|
||||
mimeType: ''
|
||||
};
|
||||
this._name = framePayload.name;
|
||||
this._url = framePayload.url;
|
||||
this._securityOrigin = framePayload.securityOrigin;
|
||||
this._mimeType = framePayload.mimeType;
|
||||
}
|
||||
|
||||
_detach() {
|
||||
this._detached = true;
|
||||
if (this._parentFrame)
|
||||
this._parentFrame._childFrames.delete(this);
|
||||
this._parentFrame = null;
|
||||
}
|
||||
_detach() {
|
||||
this._detached = true;
|
||||
if (this._parentFrame)
|
||||
this._parentFrame._childFrames.delete(this);
|
||||
this._parentFrame = null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = FrameManager;
|
||||
|
1080
lib/Page.js
1080
lib/Page.js
File diff suppressed because it is too large
Load Diff
186
lib/Request.js
186
lib/Request.js
@ -15,112 +15,112 @@
|
||||
*/
|
||||
|
||||
class Request {
|
||||
/**
|
||||
* @param {!Connection} client
|
||||
* @param {string} interceptionId
|
||||
* @param {!Object} payload
|
||||
*/
|
||||
constructor(client, interceptionId, payload) {
|
||||
this._client = client;
|
||||
this._interceptionId = interceptionId;
|
||||
this._url = payload.url;
|
||||
this._method = payload.method;
|
||||
this._headers = payload.headers;
|
||||
this._postData = payload.postData;
|
||||
/**
|
||||
* @param {!Connection} client
|
||||
* @param {string} interceptionId
|
||||
* @param {!Object} payload
|
||||
*/
|
||||
constructor(client, interceptionId, payload) {
|
||||
this._client = client;
|
||||
this._interceptionId = interceptionId;
|
||||
this._url = payload.url;
|
||||
this._method = payload.method;
|
||||
this._headers = payload.headers;
|
||||
this._postData = payload.postData;
|
||||
|
||||
this._urlOverride = undefined;
|
||||
this._methodOverride = undefined;
|
||||
this._postDataOverride = undefined;
|
||||
this._urlOverride = undefined;
|
||||
this._methodOverride = undefined;
|
||||
this._postDataOverride = undefined;
|
||||
|
||||
this._handled = false;
|
||||
}
|
||||
this._handled = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
url() {
|
||||
return this._urlOverride || this._url;
|
||||
}
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
url() {
|
||||
return this._urlOverride || this._url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
*/
|
||||
setUrl(url) {
|
||||
this._urlOverride = url;
|
||||
}
|
||||
/**
|
||||
* @param {string} url
|
||||
*/
|
||||
setUrl(url) {
|
||||
this._urlOverride = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
method() {
|
||||
return this._methodOverride || this._method;
|
||||
}
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
method() {
|
||||
return this._methodOverride || this._method;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} method
|
||||
*/
|
||||
setMethod(method) {
|
||||
this._methodOverride = method;
|
||||
}
|
||||
/**
|
||||
* @param {string} method
|
||||
*/
|
||||
setMethod(method) {
|
||||
this._methodOverride = method;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!Object}
|
||||
*/
|
||||
headers() {
|
||||
return Object.assign({}, this._headersOverride || this._headers);
|
||||
}
|
||||
/**
|
||||
* @return {!Object}
|
||||
*/
|
||||
headers() {
|
||||
return Object.assign({}, this._headersOverride || this._headers);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @param {string} value
|
||||
*/
|
||||
setHeader(key, value) {
|
||||
if (!this._headersOverride)
|
||||
this._headersOverride = Object.assign({}, this._headers);
|
||||
this._headersOverride[key] = value;
|
||||
}
|
||||
/**
|
||||
* @param {string} key
|
||||
* @param {string} value
|
||||
*/
|
||||
setHeader(key, value) {
|
||||
if (!this._headersOverride)
|
||||
this._headersOverride = Object.assign({}, this._headers);
|
||||
this._headersOverride[key] = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {(string|undefined)}
|
||||
*/
|
||||
postData() {
|
||||
return this._postDataOverride || this._postData;
|
||||
}
|
||||
/**
|
||||
* @return {(string|undefined)}
|
||||
*/
|
||||
postData() {
|
||||
return this._postDataOverride || this._postData;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {(string|undefined)}
|
||||
*/
|
||||
setPostData(data) {
|
||||
this._postDataOverride = data;
|
||||
}
|
||||
/**
|
||||
* @return {(string|undefined)}
|
||||
*/
|
||||
setPostData(data) {
|
||||
this._postDataOverride = data;
|
||||
}
|
||||
|
||||
abort() {
|
||||
console.assert(!this._handled, 'This request is already handled!');
|
||||
this._handled = true;
|
||||
this._client.send('Network.continueInterceptedRequest', {
|
||||
interceptionId: this._interceptionId,
|
||||
errorReason: 'Aborted'
|
||||
});
|
||||
}
|
||||
abort() {
|
||||
console.assert(!this._handled, 'This request is already handled!');
|
||||
this._handled = true;
|
||||
this._client.send('Network.continueInterceptedRequest', {
|
||||
interceptionId: this._interceptionId,
|
||||
errorReason: 'Aborted'
|
||||
});
|
||||
}
|
||||
|
||||
continue() {
|
||||
console.assert(!this._handled, 'This request is already handled!');
|
||||
this._handled = true;
|
||||
this._client.send('Network.continueInterceptedRequest', {
|
||||
interceptionId: this._interceptionId,
|
||||
url: this._urlOverride,
|
||||
method: this._methodOverride,
|
||||
postData: this._postDataOverride,
|
||||
headers: this._headersOverride
|
||||
});
|
||||
}
|
||||
continue() {
|
||||
console.assert(!this._handled, 'This request is already handled!');
|
||||
this._handled = true;
|
||||
this._client.send('Network.continueInterceptedRequest', {
|
||||
interceptionId: this._interceptionId,
|
||||
url: this._urlOverride,
|
||||
method: this._methodOverride,
|
||||
postData: this._postDataOverride,
|
||||
headers: this._headersOverride
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {boolean}
|
||||
*/
|
||||
handled() {
|
||||
return this._handled;
|
||||
}
|
||||
/**
|
||||
* @return {boolean}
|
||||
*/
|
||||
handled() {
|
||||
return this._handled;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Request;
|
||||
|
@ -15,21 +15,20 @@
|
||||
*/
|
||||
|
||||
module.exports = {
|
||||
|
||||
/**
|
||||
* @param {function()} fun
|
||||
* @param {!Array<*>} args
|
||||
* @param {boolean=} wrapInPromise
|
||||
* @param {string=} sourceURL
|
||||
* @return {string}
|
||||
*/
|
||||
evaluationString: function(fun, args, wrapInPromise, sourceURL) {
|
||||
var argsString = args.map(x => JSON.stringify(x)).join(',');
|
||||
var code = `(${fun.toString()})(${argsString})`;
|
||||
if (wrapInPromise)
|
||||
code = `Promise.resolve(${code})`;
|
||||
if (sourceURL)
|
||||
code += `\n//# sourceURL=${sourceURL}`;
|
||||
return code;
|
||||
}
|
||||
/**
|
||||
* @param {function()} fun
|
||||
* @param {!Array<*>} args
|
||||
* @param {boolean=} wrapInPromise
|
||||
* @param {string=} sourceURL
|
||||
* @return {string}
|
||||
*/
|
||||
evaluationString: function(fun, args, wrapInPromise, sourceURL) {
|
||||
var argsString = args.map(x => JSON.stringify(x)).join(',');
|
||||
var code = `(${fun.toString()})(${argsString})`;
|
||||
if (wrapInPromise)
|
||||
code = `Promise.resolve(${code})`;
|
||||
if (sourceURL)
|
||||
code += `\n//# sourceURL=${sourceURL}`;
|
||||
return code;
|
||||
}
|
||||
};
|
||||
|
914
package-lock.json
generated
914
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -21,355 +21,355 @@ var removeRecursive = require('rimraf').sync;
|
||||
var copyRecursive = deasync(require('ncp').ncp);
|
||||
|
||||
class FileSystem {
|
||||
constructor() {
|
||||
this.separator = path.sep;
|
||||
}
|
||||
constructor() {
|
||||
this.separator = path.sep;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
get workingDirectory() {
|
||||
return process.cwd();
|
||||
}
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
get workingDirectory() {
|
||||
return process.cwd();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} directoryPath
|
||||
*/
|
||||
changeWorkingDirectory(directoryPath) {
|
||||
try {
|
||||
process.chdir(directoryPath);
|
||||
return true;
|
||||
} catch (e){
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* @param {string} directoryPath
|
||||
*/
|
||||
changeWorkingDirectory(directoryPath) {
|
||||
try {
|
||||
process.chdir(directoryPath);
|
||||
return true;
|
||||
} catch (e){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} relativePath
|
||||
* @return {string}
|
||||
*/
|
||||
absolute(relativePath) {
|
||||
relativePath = path.normalize(relativePath);
|
||||
if (path.isAbsolute(relativePath))
|
||||
return relativePath;
|
||||
return path.resolve(path.join(process.cwd(), relativePath));
|
||||
}
|
||||
/**
|
||||
* @param {string} relativePath
|
||||
* @return {string}
|
||||
*/
|
||||
absolute(relativePath) {
|
||||
relativePath = path.normalize(relativePath);
|
||||
if (path.isAbsolute(relativePath))
|
||||
return relativePath;
|
||||
return path.resolve(path.join(process.cwd(), relativePath));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
exists(filePath) {
|
||||
return fs.existsSync(filePath);
|
||||
}
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
exists(filePath) {
|
||||
return fs.existsSync(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} fromPath
|
||||
* @param {string} toPath
|
||||
*/
|
||||
copy(fromPath, toPath) {
|
||||
var content = fs.readFileSync(fromPath);
|
||||
fs.writeFileSync(toPath, content);
|
||||
}
|
||||
/**
|
||||
* @param {string} fromPath
|
||||
* @param {string} toPath
|
||||
*/
|
||||
copy(fromPath, toPath) {
|
||||
var content = fs.readFileSync(fromPath);
|
||||
fs.writeFileSync(toPath, content);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} fromPath
|
||||
* @param {string} toPath
|
||||
*/
|
||||
move(fromPath, toPath) {
|
||||
var content = fs.readFileSync(fromPath);
|
||||
fs.writeFileSync(toPath, content);
|
||||
fs.unlinkSync(fromPath);
|
||||
}
|
||||
/**
|
||||
* @param {string} fromPath
|
||||
* @param {string} toPath
|
||||
*/
|
||||
move(fromPath, toPath) {
|
||||
var content = fs.readFileSync(fromPath);
|
||||
fs.writeFileSync(toPath, content);
|
||||
fs.unlinkSync(fromPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {number}
|
||||
*/
|
||||
size(filePath) {
|
||||
return fs.statSync(filePath).size;
|
||||
}
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {number}
|
||||
*/
|
||||
size(filePath) {
|
||||
return fs.statSync(filePath).size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
*/
|
||||
touch(filePath) {
|
||||
fs.closeSync(fs.openSync(filePath, 'a'));
|
||||
}
|
||||
/**
|
||||
* @param {string} filePath
|
||||
*/
|
||||
touch(filePath) {
|
||||
fs.closeSync(fs.openSync(filePath, 'a'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
*/
|
||||
remove(filePath) {
|
||||
fs.unlinkSync(filePath);
|
||||
}
|
||||
/**
|
||||
* @param {string} filePath
|
||||
*/
|
||||
remove(filePath) {
|
||||
fs.unlinkSync(filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
lastModified(filePath) {
|
||||
return fs.statSync(filePath).mtime;
|
||||
}
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
lastModified(filePath) {
|
||||
return fs.statSync(filePath).mtime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} dirPath
|
||||
* @return {boolean}
|
||||
*/
|
||||
makeDirectory(dirPath) {
|
||||
try {
|
||||
fs.mkdirSync(dirPath);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* @param {string} dirPath
|
||||
* @return {boolean}
|
||||
*/
|
||||
makeDirectory(dirPath) {
|
||||
try {
|
||||
fs.mkdirSync(dirPath);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} dirPath
|
||||
* @return {boolean}
|
||||
*/
|
||||
makeTree(dirPath) {
|
||||
return this.makeDirectory(dirPath);
|
||||
}
|
||||
/**
|
||||
* @param {string} dirPath
|
||||
* @return {boolean}
|
||||
*/
|
||||
makeTree(dirPath) {
|
||||
return this.makeDirectory(dirPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} dirPath
|
||||
*/
|
||||
removeTree(dirPath) {
|
||||
removeRecursive(dirPath);
|
||||
}
|
||||
/**
|
||||
* @param {string} dirPath
|
||||
*/
|
||||
removeTree(dirPath) {
|
||||
removeRecursive(dirPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} fromPath
|
||||
* @param {string} toPath
|
||||
*/
|
||||
copyTree(fromPath, toPath) {
|
||||
copyRecursive(fromPath, toPath);
|
||||
}
|
||||
/**
|
||||
* @param {string} fromPath
|
||||
* @param {string} toPath
|
||||
*/
|
||||
copyTree(fromPath, toPath) {
|
||||
copyRecursive(fromPath, toPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} dirPath
|
||||
* @return {!Array<string>}
|
||||
*/
|
||||
list(dirPath) {
|
||||
return fs.readdirSync(dirPath);
|
||||
}
|
||||
/**
|
||||
* @param {string} dirPath
|
||||
* @return {!Array<string>}
|
||||
*/
|
||||
list(dirPath) {
|
||||
return fs.readdirSync(dirPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} linkPath
|
||||
* @return {string}
|
||||
*/
|
||||
readLink(linkPath) {
|
||||
return fs.readlinkSync(linkPath);
|
||||
}
|
||||
/**
|
||||
* @param {string} linkPath
|
||||
* @return {string}
|
||||
*/
|
||||
readLink(linkPath) {
|
||||
return fs.readlinkSync(linkPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @param {Object} data
|
||||
* @param {string} mode
|
||||
*/
|
||||
write(filePath, data, mode) {
|
||||
var fd = new FileDescriptor(filePath, mode, 'utf8');
|
||||
fd.write(data);
|
||||
fd.close();
|
||||
}
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @param {Object} data
|
||||
* @param {string} mode
|
||||
*/
|
||||
write(filePath, data, mode) {
|
||||
var fd = new FileDescriptor(filePath, mode, 'utf8');
|
||||
fd.write(data);
|
||||
fd.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} somePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
isAbsolute(somePath) {
|
||||
return path.isAbsolute(somePath);
|
||||
}
|
||||
/**
|
||||
* @param {string} somePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
isAbsolute(somePath) {
|
||||
return path.isAbsolute(somePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
read(filePath) {
|
||||
return fs.readFileSync(filePath, 'utf8');
|
||||
}
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
read(filePath) {
|
||||
return fs.readFileSync(filePath, 'utf8');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
isFile(filePath) {
|
||||
return fs.existsSync(filePath) && fs.lstatSync(filePath).isFile();
|
||||
}
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
isFile(filePath) {
|
||||
return fs.existsSync(filePath) && fs.lstatSync(filePath).isFile();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} dirPath
|
||||
* @return {boolean}
|
||||
*/
|
||||
isDirectory(dirPath) {
|
||||
return fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory();
|
||||
}
|
||||
/**
|
||||
* @param {string} dirPath
|
||||
* @return {boolean}
|
||||
*/
|
||||
isDirectory(dirPath) {
|
||||
return fs.existsSync(dirPath) && fs.lstatSync(dirPath).isDirectory();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
isLink(filePath) {
|
||||
return fs.existsSync(filePath) && fs.lstatSync(filePath).isSymbolicLink();
|
||||
}
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
isLink(filePath) {
|
||||
return fs.existsSync(filePath) && fs.lstatSync(filePath).isSymbolicLink();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
isReadable(filePath) {
|
||||
try {
|
||||
fs.accessSync(filePath, fs.constants.R_OK);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
isReadable(filePath) {
|
||||
try {
|
||||
fs.accessSync(filePath, fs.constants.R_OK);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
isWritable(filePath) {
|
||||
try {
|
||||
fs.accessSync(filePath, fs.constants.W_OK);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
isWritable(filePath) {
|
||||
try {
|
||||
fs.accessSync(filePath, fs.constants.W_OK);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
isExecutable(filePath) {
|
||||
try {
|
||||
fs.accessSync(filePath, fs.constants.X_OK);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
isExecutable(filePath) {
|
||||
try {
|
||||
fs.accessSync(filePath, fs.constants.X_OK);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} somePath
|
||||
* @return {!Array<string>}
|
||||
*/
|
||||
split(somePath) {
|
||||
somePath = path.normalize(somePath);
|
||||
if (somePath.endsWith(path.sep))
|
||||
somePath = somePath.substring(0, somePath.length - path.sep.length);
|
||||
return somePath.split(path.sep);
|
||||
}
|
||||
/**
|
||||
* @param {string} somePath
|
||||
* @return {!Array<string>}
|
||||
*/
|
||||
split(somePath) {
|
||||
somePath = path.normalize(somePath);
|
||||
if (somePath.endsWith(path.sep))
|
||||
somePath = somePath.substring(0, somePath.length - path.sep.length);
|
||||
return somePath.split(path.sep);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} path1
|
||||
* @param {string} path2
|
||||
* @return {string}
|
||||
*/
|
||||
join(...args) {
|
||||
if (args[0] === '' && args.length > 1)
|
||||
args[0] = path.sep;
|
||||
args = args.filter(part => typeof part === 'string');
|
||||
return path.join.apply(path, args);
|
||||
}
|
||||
/**
|
||||
* @param {string} path1
|
||||
* @param {string} path2
|
||||
* @return {string}
|
||||
*/
|
||||
join(...args) {
|
||||
if (args[0] === '' && args.length > 1)
|
||||
args[0] = path.sep;
|
||||
args = args.filter(part => typeof part === 'string');
|
||||
return path.join.apply(path, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @param {(string|!Object)} option
|
||||
* @return {!FileDescriptor}
|
||||
*/
|
||||
open(filePath, option) {
|
||||
if (typeof option === 'string')
|
||||
return new FileDescriptor(filePath, option);
|
||||
return new FileDescriptor(filePath, option.mode);
|
||||
}
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @param {(string|!Object)} option
|
||||
* @return {!FileDescriptor}
|
||||
*/
|
||||
open(filePath, option) {
|
||||
if (typeof option === 'string')
|
||||
return new FileDescriptor(filePath, option);
|
||||
return new FileDescriptor(filePath, option.mode);
|
||||
}
|
||||
}
|
||||
|
||||
var fdwrite = deasync(fs.write);
|
||||
var fdread = deasync(fs.read);
|
||||
|
||||
class FileDescriptor {
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @param {string} mode
|
||||
*/
|
||||
constructor(filePath, mode) {
|
||||
this._position = 0;
|
||||
this._encoding = 'utf8';
|
||||
if (mode === 'rb') {
|
||||
this._mode = 'r';
|
||||
this._encoding = 'latin1';
|
||||
} else if (mode === 'wb' || mode === 'b') {
|
||||
this._mode = 'w';
|
||||
this._encoding = 'latin1';
|
||||
} else if (mode === 'rw+') {
|
||||
this._mode = 'a+';
|
||||
this._position = fs.existsSync(filePath) ? fs.statSync(filePath).size : 0;
|
||||
} else {
|
||||
this._mode = mode;
|
||||
}
|
||||
this._fd = fs.openSync(filePath, this._mode);
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @param {string} mode
|
||||
*/
|
||||
constructor(filePath, mode) {
|
||||
this._position = 0;
|
||||
this._encoding = 'utf8';
|
||||
if (mode === 'rb') {
|
||||
this._mode = 'r';
|
||||
this._encoding = 'latin1';
|
||||
} else if (mode === 'wb' || mode === 'b') {
|
||||
this._mode = 'w';
|
||||
this._encoding = 'latin1';
|
||||
} else if (mode === 'rw+') {
|
||||
this._mode = 'a+';
|
||||
this._position = fs.existsSync(filePath) ? fs.statSync(filePath).size : 0;
|
||||
} else {
|
||||
this._mode = mode;
|
||||
}
|
||||
this._fd = fs.openSync(filePath, this._mode);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} data
|
||||
*/
|
||||
write(data) {
|
||||
var buffer = Buffer.from(data, this._encoding);
|
||||
var written = fdwrite(this._fd, buffer, 0, buffer.length, this._position);
|
||||
this._position += written;
|
||||
}
|
||||
/**
|
||||
* @param {string} data
|
||||
*/
|
||||
write(data) {
|
||||
var buffer = Buffer.from(data, this._encoding);
|
||||
var written = fdwrite(this._fd, buffer, 0, buffer.length, this._position);
|
||||
this._position += written;
|
||||
}
|
||||
|
||||
getEncoding() {
|
||||
return 'UTF-8';
|
||||
}
|
||||
getEncoding() {
|
||||
return 'UTF-8';
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} data
|
||||
*/
|
||||
writeLine(data) {
|
||||
this.write(data + '\n');
|
||||
}
|
||||
/**
|
||||
* @param {string} data
|
||||
*/
|
||||
writeLine(data) {
|
||||
this.write(data + '\n');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number=} size
|
||||
* @return {string}
|
||||
*/
|
||||
read(size) {
|
||||
var position = this._position;
|
||||
if (!size) {
|
||||
size = fs.fstatSync(this._fd).size;
|
||||
position = 0;
|
||||
}
|
||||
var buffer = new Buffer(size);
|
||||
var bytesRead = fdread(this._fd, buffer, 0, size, position);
|
||||
this._position += bytesRead;
|
||||
return buffer.toString(this._encoding);
|
||||
/**
|
||||
* @param {number=} size
|
||||
* @return {string}
|
||||
*/
|
||||
read(size) {
|
||||
var position = this._position;
|
||||
if (!size) {
|
||||
size = fs.fstatSync(this._fd).size;
|
||||
position = 0;
|
||||
}
|
||||
var buffer = new Buffer(size);
|
||||
var bytesRead = fdread(this._fd, buffer, 0, size, position);
|
||||
this._position += bytesRead;
|
||||
return buffer.toString(this._encoding);
|
||||
}
|
||||
|
||||
flush() {
|
||||
// noop.
|
||||
}
|
||||
flush() {
|
||||
// noop.
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {number} position
|
||||
*/
|
||||
seek(position) {
|
||||
this._position = position;
|
||||
}
|
||||
/**
|
||||
* @param {number} position
|
||||
*/
|
||||
seek(position) {
|
||||
this._position = position;
|
||||
}
|
||||
|
||||
close() {
|
||||
fs.closeSync(this._fd);
|
||||
}
|
||||
close() {
|
||||
fs.closeSync(this._fd);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {boolean}
|
||||
*/
|
||||
atEnd() {
|
||||
}
|
||||
/**
|
||||
* @return {boolean}
|
||||
*/
|
||||
atEnd() {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = FileSystem;
|
||||
|
@ -24,116 +24,116 @@ var url = require('url');
|
||||
* @param {string} scriptPath
|
||||
*/
|
||||
module.exports.create = function(context, scriptPath) {
|
||||
var phantom = {
|
||||
page: {
|
||||
onConsoleMessage: null,
|
||||
},
|
||||
var phantom = {
|
||||
page: {
|
||||
onConsoleMessage: null,
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} relative
|
||||
* @param {string} base
|
||||
* @return {string}
|
||||
*/
|
||||
resolveRelativeUrl: function(relative, base) {
|
||||
return url.resolve(base, relative);
|
||||
},
|
||||
/**
|
||||
* @param {string} relative
|
||||
* @param {string} base
|
||||
* @return {string}
|
||||
*/
|
||||
resolveRelativeUrl: function(relative, base) {
|
||||
return url.resolve(base, relative);
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @return {string}
|
||||
*/
|
||||
fullyDecodeUrl: function(url) {
|
||||
return decodeURI(url);
|
||||
},
|
||||
/**
|
||||
* @param {string} url
|
||||
* @return {string}
|
||||
*/
|
||||
fullyDecodeUrl: function(url) {
|
||||
return decodeURI(url);
|
||||
},
|
||||
|
||||
libraryPath: path.dirname(scriptPath),
|
||||
libraryPath: path.dirname(scriptPath),
|
||||
|
||||
onError: null,
|
||||
onError: null,
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
get outputEncoding() {
|
||||
return 'UTF-8';
|
||||
},
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
get outputEncoding() {
|
||||
return 'UTF-8';
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
*/
|
||||
set outputEncoding(value) {
|
||||
throw new Error('Phantom.outputEncoding setter is not implemented');
|
||||
},
|
||||
/**
|
||||
* @param {string} value
|
||||
*/
|
||||
set outputEncoding(value) {
|
||||
throw new Error('Phantom.outputEncoding setter is not implemented');
|
||||
},
|
||||
|
||||
/**
|
||||
* @return {boolean}
|
||||
*/
|
||||
get cookiesEnabled() {
|
||||
return true;
|
||||
},
|
||||
/**
|
||||
* @return {boolean}
|
||||
*/
|
||||
get cookiesEnabled() {
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {boolean} value
|
||||
*/
|
||||
set cookiesEnabled(value) {
|
||||
throw new Error('Phantom.cookiesEnabled setter is not implemented');
|
||||
},
|
||||
/**
|
||||
* @param {boolean} value
|
||||
*/
|
||||
set cookiesEnabled(value) {
|
||||
throw new Error('Phantom.cookiesEnabled setter is not implemented');
|
||||
},
|
||||
|
||||
/**
|
||||
* @return {!{major: number, minor: number, patch: number}}
|
||||
*/
|
||||
get version() {
|
||||
var versionParts = require('../package.json').version.split('.');
|
||||
return {
|
||||
major: parseInt(versionParts[0], 10),
|
||||
minor: parseInt(versionParts[1], 10),
|
||||
patch: parseInt(versionParts[2], 10),
|
||||
};
|
||||
},
|
||||
/**
|
||||
* @return {!{major: number, minor: number, patch: number}}
|
||||
*/
|
||||
get version() {
|
||||
var versionParts = require('../package.json').version.split('.');
|
||||
return {
|
||||
major: parseInt(versionParts[0], 10),
|
||||
minor: parseInt(versionParts[1], 10),
|
||||
patch: parseInt(versionParts[2], 10),
|
||||
};
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {number=} code
|
||||
*/
|
||||
exit: function(code) {
|
||||
process.exit(code);
|
||||
},
|
||||
/**
|
||||
* @param {number=} code
|
||||
*/
|
||||
exit: function(code) {
|
||||
process.exit(code);
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
injectJs: function(filePath) {
|
||||
filePath = path.resolve(phantom.libraryPath, filePath);
|
||||
if (!fs.existsSync(filePath))
|
||||
return false;
|
||||
var code = fs.readFileSync(filePath, 'utf8');
|
||||
if (code.startsWith('#!'))
|
||||
code = code.substring(code.indexOf('\n'));
|
||||
vm.runInContext(code, context, {
|
||||
filename: filePath,
|
||||
displayErrors: true
|
||||
});
|
||||
return true;
|
||||
},
|
||||
/**
|
||||
* @param {string} filePath
|
||||
* @return {boolean}
|
||||
*/
|
||||
injectJs: function(filePath) {
|
||||
filePath = path.resolve(phantom.libraryPath, filePath);
|
||||
if (!fs.existsSync(filePath))
|
||||
return false;
|
||||
var code = fs.readFileSync(filePath, 'utf8');
|
||||
if (code.startsWith('#!'))
|
||||
code = code.substring(code.indexOf('\n'));
|
||||
vm.runInContext(code, context, {
|
||||
filename: filePath,
|
||||
displayErrors: true
|
||||
});
|
||||
return true;
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} moduleSource
|
||||
* @param {string} filename
|
||||
*/
|
||||
loadModule: function(moduleSource, filename) {
|
||||
var code = [
|
||||
'(function(require, exports, module) {\n',
|
||||
moduleSource,
|
||||
'\n}.call({},',
|
||||
'require.cache[\'' + filename + '\']._getRequire(),',
|
||||
'require.cache[\'' + filename + '\'].exports,',
|
||||
'require.cache[\'' + filename + '\']',
|
||||
'));'
|
||||
].join('');
|
||||
vm.runInContext(code, context, {
|
||||
filename: filename,
|
||||
displayErrors: true
|
||||
});
|
||||
},
|
||||
};
|
||||
return phantom;
|
||||
/**
|
||||
* @param {string} moduleSource
|
||||
* @param {string} filename
|
||||
*/
|
||||
loadModule: function(moduleSource, filename) {
|
||||
var code = [
|
||||
'(function(require, exports, module) {\n',
|
||||
moduleSource,
|
||||
'\n}.call({},',
|
||||
'require.cache[\'' + filename + '\']._getRequire(),',
|
||||
'require.cache[\'' + filename + '\'].exports,',
|
||||
'require.cache[\'' + filename + '\']',
|
||||
'));'
|
||||
].join('');
|
||||
vm.runInContext(code, context, {
|
||||
filename: filename,
|
||||
displayErrors: true
|
||||
});
|
||||
},
|
||||
};
|
||||
return phantom;
|
||||
};
|
||||
|
@ -19,100 +19,100 @@ var await = require('./utilities').await;
|
||||
var os = require('os');
|
||||
|
||||
class System {
|
||||
/**
|
||||
* @param {!Array<string>} args
|
||||
*/
|
||||
constructor(args) {
|
||||
this.args = args;
|
||||
this.env = {};
|
||||
Object.assign(this.env, process.env);
|
||||
this.stdin = new StandardInput(process.stdin);
|
||||
this.stdout = new StandardOutput(process.stdout);
|
||||
this.stderr = new StandardOutput(process.stderr);
|
||||
this.platform = 'phantomjs';
|
||||
this.pid = process.pid;
|
||||
this.isSSLSupported = false;
|
||||
this.os = {
|
||||
architecture: os.arch(),
|
||||
name: os.type(),
|
||||
version: os.release()
|
||||
};
|
||||
}
|
||||
/**
|
||||
* @param {!Array<string>} args
|
||||
*/
|
||||
constructor(args) {
|
||||
this.args = args;
|
||||
this.env = {};
|
||||
Object.assign(this.env, process.env);
|
||||
this.stdin = new StandardInput(process.stdin);
|
||||
this.stdout = new StandardOutput(process.stdout);
|
||||
this.stderr = new StandardOutput(process.stderr);
|
||||
this.platform = 'phantomjs';
|
||||
this.pid = process.pid;
|
||||
this.isSSLSupported = false;
|
||||
this.os = {
|
||||
architecture: os.arch(),
|
||||
name: os.type(),
|
||||
version: os.release()
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class StandardInput {
|
||||
/**
|
||||
* @param {!Readable} readableStream
|
||||
*/
|
||||
constructor(readableStream) {
|
||||
this._readline = readline.createInterface({
|
||||
input: readableStream
|
||||
});
|
||||
this._lines = [];
|
||||
this._closed = false;
|
||||
this._readline.on('line', line => this._lines.push(line));
|
||||
this._readline.on('close', () => this._closed = true);
|
||||
}
|
||||
/**
|
||||
* @param {!Readable} readableStream
|
||||
*/
|
||||
constructor(readableStream) {
|
||||
this._readline = readline.createInterface({
|
||||
input: readableStream
|
||||
});
|
||||
this._lines = [];
|
||||
this._closed = false;
|
||||
this._readline.on('line', line => this._lines.push(line));
|
||||
this._readline.on('close', () => this._closed = true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
readLine() {
|
||||
if (this._closed && !this._lines.length)
|
||||
return '';
|
||||
if (!this._lines.length) {
|
||||
var linePromise = new Promise(fulfill => this._readline.once('line', fulfill));
|
||||
await(linePromise);
|
||||
}
|
||||
return this._lines.shift();
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
readLine() {
|
||||
if (this._closed && !this._lines.length)
|
||||
return '';
|
||||
if (!this._lines.length) {
|
||||
var linePromise = new Promise(fulfill => this._readline.once('line', fulfill));
|
||||
await(linePromise);
|
||||
}
|
||||
return this._lines.shift();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
read() {
|
||||
if (!this._closed) {
|
||||
var closePromise = new Promise(fulfill => this._readline.once('close', fulfill));
|
||||
await(closePromise);
|
||||
}
|
||||
var text = this._lines.join('\n');
|
||||
this._lines = [];
|
||||
return text;
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
read() {
|
||||
if (!this._closed) {
|
||||
var closePromise = new Promise(fulfill => this._readline.once('close', fulfill));
|
||||
await(closePromise);
|
||||
}
|
||||
var text = this._lines.join('\n');
|
||||
this._lines = [];
|
||||
return text;
|
||||
}
|
||||
|
||||
close() {
|
||||
this._readline.close();
|
||||
}
|
||||
close() {
|
||||
this._readline.close();
|
||||
}
|
||||
}
|
||||
|
||||
class StandardOutput {
|
||||
/**
|
||||
* @param {!Writable} writableStream
|
||||
*/
|
||||
constructor(writableStream) {
|
||||
this._stream = writableStream;
|
||||
}
|
||||
/**
|
||||
* @param {!Writable} writableStream
|
||||
*/
|
||||
constructor(writableStream) {
|
||||
this._stream = writableStream;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} data
|
||||
*/
|
||||
write(data) {
|
||||
this._stream.write(data);
|
||||
}
|
||||
/**
|
||||
* @param {string} data
|
||||
*/
|
||||
write(data) {
|
||||
this._stream.write(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} data
|
||||
*/
|
||||
writeLine(data) {
|
||||
this._stream.write(data + '\n');
|
||||
}
|
||||
/**
|
||||
* @param {string} data
|
||||
*/
|
||||
writeLine(data) {
|
||||
this._stream.write(data + '\n');
|
||||
}
|
||||
|
||||
flush() {
|
||||
}
|
||||
flush() {
|
||||
}
|
||||
|
||||
close() {
|
||||
this._stream.end();
|
||||
}
|
||||
close() {
|
||||
this._stream.end();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = System;
|
||||
|
@ -23,319 +23,319 @@ var PageEvents = require('../lib/Page').Events;
|
||||
var noop = function() { };
|
||||
|
||||
class WebPage {
|
||||
/**
|
||||
* @param {!Browser} browser
|
||||
* @param {string} scriptPath
|
||||
* @param {!Object=} options
|
||||
*/
|
||||
constructor(browser, scriptPath, options) {
|
||||
this._page = await(browser.newPage());
|
||||
this.settings = new WebPageSettings(this._page);
|
||||
/**
|
||||
* @param {!Browser} browser
|
||||
* @param {string} scriptPath
|
||||
* @param {!Object=} options
|
||||
*/
|
||||
constructor(browser, scriptPath, options) {
|
||||
this._page = await(browser.newPage());
|
||||
this.settings = new WebPageSettings(this._page);
|
||||
|
||||
options = options || {};
|
||||
options.settings = options.settings || {};
|
||||
if (options.settings.userAgent)
|
||||
this.settings.userAgent = options.settings.userAgent;
|
||||
if (options.viewportSize)
|
||||
await(this._page.setViewportSize(options.viewportSize));
|
||||
options = options || {};
|
||||
options.settings = options.settings || {};
|
||||
if (options.settings.userAgent)
|
||||
this.settings.userAgent = options.settings.userAgent;
|
||||
if (options.viewportSize)
|
||||
await(this._page.setViewportSize(options.viewportSize));
|
||||
|
||||
this.clipRect = options.clipRect || {left: 0, top: 0, width: 0, height: 0};
|
||||
this.onConsoleMessage = null;
|
||||
this.onLoadFinished = null;
|
||||
this.onResourceError = null;
|
||||
this.onResourceReceived = null;
|
||||
this.onInitialized = null;
|
||||
this._deferEvaluate = false;
|
||||
this.clipRect = options.clipRect || {left: 0, top: 0, width: 0, height: 0};
|
||||
this.onConsoleMessage = null;
|
||||
this.onLoadFinished = null;
|
||||
this.onResourceError = null;
|
||||
this.onResourceReceived = null;
|
||||
this.onInitialized = null;
|
||||
this._deferEvaluate = false;
|
||||
|
||||
this.libraryPath = path.dirname(scriptPath);
|
||||
this.libraryPath = path.dirname(scriptPath);
|
||||
|
||||
this._onResourceRequestedCallback = undefined;
|
||||
this._onConfirmCallback = undefined;
|
||||
this._onAlertCallback = undefined;
|
||||
this._onError = noop;
|
||||
this._onResourceRequestedCallback = undefined;
|
||||
this._onConfirmCallback = undefined;
|
||||
this._onAlertCallback = undefined;
|
||||
this._onError = noop;
|
||||
|
||||
this._pageEvents = new AsyncEmitter(this._page);
|
||||
this._pageEvents.on(PageEvents.ResponseReceived, response => this._onResponseReceived(response));
|
||||
this._pageEvents.on(PageEvents.ResourceLoadingFailed, event => (this.onResourceError || noop).call(null, event));
|
||||
this._pageEvents.on(PageEvents.ConsoleMessage, msg => (this.onConsoleMessage || noop).call(null, msg));
|
||||
this._pageEvents.on(PageEvents.Confirm, message => this._onConfirm(message));
|
||||
this._pageEvents.on(PageEvents.Alert, message => this._onAlert(message));
|
||||
this._pageEvents.on(PageEvents.Dialog, dialog => this._onDialog(dialog));
|
||||
this._pageEvents.on(PageEvents.Error, error => (this._onError || noop).call(null, error.message, error.stack));
|
||||
}
|
||||
this._pageEvents = new AsyncEmitter(this._page);
|
||||
this._pageEvents.on(PageEvents.ResponseReceived, response => this._onResponseReceived(response));
|
||||
this._pageEvents.on(PageEvents.ResourceLoadingFailed, event => (this.onResourceError || noop).call(null, event));
|
||||
this._pageEvents.on(PageEvents.ConsoleMessage, msg => (this.onConsoleMessage || noop).call(null, msg));
|
||||
this._pageEvents.on(PageEvents.Confirm, message => this._onConfirm(message));
|
||||
this._pageEvents.on(PageEvents.Alert, message => this._onAlert(message));
|
||||
this._pageEvents.on(PageEvents.Dialog, dialog => this._onDialog(dialog));
|
||||
this._pageEvents.on(PageEvents.Error, error => (this._onError || noop).call(null, error.message, error.stack));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {?function(!Object, !Request)}
|
||||
*/
|
||||
get onResourceRequested() {
|
||||
return this._onResourceRequestedCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {?function(!Object, !Request)} callback
|
||||
*/
|
||||
set onResourceRequested(callback) {
|
||||
this._onResourceRequestedCallback = callback;
|
||||
this._page.setRequestInterceptor(callback ? resourceInterceptor : null);
|
||||
|
||||
/**
|
||||
* @return {?function(!Object, !Request)}
|
||||
* @param {!Request} request
|
||||
*/
|
||||
get onResourceRequested() {
|
||||
return this._onResourceRequestedCallback;
|
||||
function resourceInterceptor(request) {
|
||||
var requestData = {
|
||||
url: request.url(),
|
||||
headers: request.headers()
|
||||
};
|
||||
callback(requestData, request);
|
||||
if (!request.handled())
|
||||
request.continue();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {?function(!Object, !Request)} callback
|
||||
*/
|
||||
set onResourceRequested(callback) {
|
||||
this._onResourceRequestedCallback = callback;
|
||||
this._page.setRequestInterceptor(callback ? resourceInterceptor : null);
|
||||
|
||||
/**
|
||||
* @param {!Request} request
|
||||
*/
|
||||
function resourceInterceptor(request) {
|
||||
var requestData = {
|
||||
url: request.url(),
|
||||
headers: request.headers()
|
||||
};
|
||||
callback(requestData, request);
|
||||
if (!request.handled())
|
||||
request.continue();
|
||||
}
|
||||
_onResponseReceived(response) {
|
||||
if (!this.onResourceReceived)
|
||||
return;
|
||||
var headers = [];
|
||||
for (var key in response.headers) {
|
||||
headers.push({
|
||||
name: key,
|
||||
value: response.headers[key]
|
||||
});
|
||||
}
|
||||
response.headers = headers;
|
||||
this.onResourceReceived.call(null, response);
|
||||
}
|
||||
|
||||
_onResponseReceived(response) {
|
||||
if (!this.onResourceReceived)
|
||||
return;
|
||||
var headers = [];
|
||||
for (var key in response.headers) {
|
||||
headers.push({
|
||||
name: key,
|
||||
value: response.headers[key]
|
||||
});
|
||||
}
|
||||
response.headers = headers;
|
||||
this.onResourceReceived.call(null, response);
|
||||
/**
|
||||
* @param {string} url
|
||||
* @param {function()} callback
|
||||
*/
|
||||
includeJs(url, callback) {
|
||||
this._page.addScriptTag(url).then(callback);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!{width: number, height: number}}
|
||||
*/
|
||||
get viewportSize() {
|
||||
return this._page.viewportSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!Object}
|
||||
*/
|
||||
get customHeaders() {
|
||||
return this._page.extraHTTPHeaders();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Object} value
|
||||
*/
|
||||
set customHeaders(value) {
|
||||
await(this._page.setExtraHTTPHeaders(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
*/
|
||||
injectJs(filePath) {
|
||||
if (!fs.existsSync(filePath))
|
||||
filePath = path.resolve(this.libraryPath, filePath);
|
||||
if (!fs.existsSync(filePath))
|
||||
return false;
|
||||
await(this._page.injectFile(filePath));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
get plainText() {
|
||||
return await(this._page.plainText());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
get title() {
|
||||
return await(this._page.title());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {(function()|undefined)}
|
||||
*/
|
||||
get onError() {
|
||||
return this._onError;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {(function()|undefined)} handler
|
||||
*/
|
||||
set onError(handler) {
|
||||
if (typeof handler !== 'function')
|
||||
handler = undefined;
|
||||
this._onError = handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {(function()|undefined)}
|
||||
*/
|
||||
get onConfirm() {
|
||||
return this._onConfirmCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {function()} handler
|
||||
*/
|
||||
set onConfirm(handler) {
|
||||
if (typeof handler !== 'function')
|
||||
handler = undefined;
|
||||
this._onConfirmCallback = handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {(function()|undefined)}
|
||||
*/
|
||||
get onAlert() {
|
||||
return this._onAlertCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {function()} handler
|
||||
*/
|
||||
set onAlert(handler) {
|
||||
if (typeof handler !== 'function')
|
||||
handler = undefined;
|
||||
this._onAlertCallback = handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Dialog} dialog
|
||||
*/
|
||||
_onDialog(dialog) {
|
||||
if (dialog.type === 'alert' && this._onAlertCallback) {
|
||||
this._onAlertCallback.call(null, dialog.message());
|
||||
await(dialog.accept());
|
||||
} else if (dialog.type === 'confirm' && this._onConfirmCallback) {
|
||||
var result = this._onConfirmCallback.call(null, dialog.message());
|
||||
await(result ? dialog.accept() : dialog.dismiss());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} url
|
||||
* @param {function()} callback
|
||||
*/
|
||||
includeJs(url, callback) {
|
||||
this._page.addScriptTag(url).then(callback);
|
||||
}
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
get url() {
|
||||
return await(this._page.url());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!{width: number, height: number}}
|
||||
*/
|
||||
get viewportSize() {
|
||||
return this._page.viewportSize();
|
||||
}
|
||||
/**
|
||||
* @param {string} html
|
||||
*/
|
||||
set content(html) {
|
||||
await(this._page.setContent(html));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {!Object}
|
||||
*/
|
||||
get customHeaders() {
|
||||
return this._page.extraHTTPHeaders();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Object} value
|
||||
*/
|
||||
set customHeaders(value) {
|
||||
await(this._page.setExtraHTTPHeaders(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} filePath
|
||||
*/
|
||||
injectJs(filePath) {
|
||||
if (!fs.existsSync(filePath))
|
||||
filePath = path.resolve(this.libraryPath, filePath);
|
||||
if (!fs.existsSync(filePath))
|
||||
return false;
|
||||
await(this._page.injectFile(filePath));
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
get plainText() {
|
||||
return await(this._page.plainText());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
get title() {
|
||||
return await(this._page.title());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {(function()|undefined)}
|
||||
*/
|
||||
get onError() {
|
||||
return this._onError;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {(function()|undefined)} handler
|
||||
*/
|
||||
set onError(handler) {
|
||||
if (typeof handler !== 'function')
|
||||
handler = undefined;
|
||||
this._onError = handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {(function()|undefined)}
|
||||
*/
|
||||
get onConfirm() {
|
||||
return this._onConfirmCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {function()} handler
|
||||
*/
|
||||
set onConfirm(handler) {
|
||||
if (typeof handler !== 'function')
|
||||
handler = undefined;
|
||||
this._onConfirmCallback = handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {(function()|undefined)}
|
||||
*/
|
||||
get onAlert() {
|
||||
return this._onAlertCallback;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {function()} handler
|
||||
*/
|
||||
set onAlert(handler) {
|
||||
if (typeof handler !== 'function')
|
||||
handler = undefined;
|
||||
this._onAlertCallback = handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Dialog} dialog
|
||||
*/
|
||||
_onDialog(dialog) {
|
||||
if (dialog.type === 'alert' && this._onAlertCallback) {
|
||||
this._onAlertCallback.call(null, dialog.message());
|
||||
await(dialog.accept());
|
||||
} else if (dialog.type === 'confirm' && this._onConfirmCallback) {
|
||||
var result = this._onConfirmCallback.call(null, dialog.message());
|
||||
await(result ? dialog.accept() : dialog.dismiss());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
get url() {
|
||||
return await(this._page.url());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} html
|
||||
*/
|
||||
set content(html) {
|
||||
await(this._page.setContent(html));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} html
|
||||
* @param {function()=} callback
|
||||
*/
|
||||
open(url, callback) {
|
||||
console.assert(arguments.length <= 2, 'WebPage.open does not support METHOD and DATA arguments');
|
||||
this._deferEvaluate = true;
|
||||
if (typeof this.onInitialized === 'function')
|
||||
this.onInitialized();
|
||||
this._deferEvaluate = false;
|
||||
this._page.navigate(url).then(result => {
|
||||
var status = result ? 'success' : 'fail';
|
||||
if (!result) {
|
||||
this.onResourceError.call(null, {
|
||||
url,
|
||||
errorString: 'SSL handshake failed'
|
||||
});
|
||||
}
|
||||
if (this.onLoadFinished)
|
||||
this.onLoadFinished.call(null, status);
|
||||
if (callback)
|
||||
callback.call(null, status);
|
||||
/**
|
||||
* @param {string} html
|
||||
* @param {function()=} callback
|
||||
*/
|
||||
open(url, callback) {
|
||||
console.assert(arguments.length <= 2, 'WebPage.open does not support METHOD and DATA arguments');
|
||||
this._deferEvaluate = true;
|
||||
if (typeof this.onInitialized === 'function')
|
||||
this.onInitialized();
|
||||
this._deferEvaluate = false;
|
||||
this._page.navigate(url).then(result => {
|
||||
var status = result ? 'success' : 'fail';
|
||||
if (!result) {
|
||||
this.onResourceError.call(null, {
|
||||
url,
|
||||
errorString: 'SSL handshake failed'
|
||||
});
|
||||
}
|
||||
}
|
||||
if (this.onLoadFinished)
|
||||
this.onLoadFinished.call(null, status);
|
||||
if (callback)
|
||||
callback.call(null, status);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!{width: number, height: number}} options
|
||||
*/
|
||||
set viewportSize(options) {
|
||||
await(this._page.setViewportSize(options));
|
||||
}
|
||||
/**
|
||||
* @param {!{width: number, height: number}} options
|
||||
*/
|
||||
set viewportSize(options) {
|
||||
await(this._page.setViewportSize(options));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {function()} fun
|
||||
* @param {!Array<!Object>} args
|
||||
*/
|
||||
evaluate(fun, ...args) {
|
||||
if (this._deferEvaluate)
|
||||
return await(this._page.evaluateOnInitialized(fun, ...args));
|
||||
return await(this._page.evaluate(fun, ...args));
|
||||
}
|
||||
/**
|
||||
* @param {function()} fun
|
||||
* @param {!Array<!Object>} args
|
||||
*/
|
||||
evaluate(fun, ...args) {
|
||||
if (this._deferEvaluate)
|
||||
return await(this._page.evaluateOnInitialized(fun, ...args));
|
||||
return await(this._page.evaluate(fun, ...args));
|
||||
}
|
||||
|
||||
/**
|
||||
* {string} fileName
|
||||
*/
|
||||
render(fileName) {
|
||||
if (fileName.endsWith('pdf')) {
|
||||
var options = {};
|
||||
var paperSize = this.paperSize || {};
|
||||
options.margin = paperSize.margin;
|
||||
options.format = paperSize.format;
|
||||
options.landscape = paperSize.orientation === 'landscape';
|
||||
options.width = paperSize.width;
|
||||
options.height = paperSize.height;
|
||||
await(this._page.printToPDF(fileName, options));
|
||||
} else {
|
||||
var options = {};
|
||||
if (this.clipRect && (this.clipRect.left || this.clipRect.top || this.clipRect.width || this.clipRect.height)) {
|
||||
options.clip = {
|
||||
x: this.clipRect.left,
|
||||
y: this.clipRect.top,
|
||||
width: this.clipRect.width,
|
||||
height: this.clipRect.height
|
||||
};
|
||||
}
|
||||
options.path = fileName;
|
||||
await(this._page.screenshot(options));
|
||||
}
|
||||
/**
|
||||
* {string} fileName
|
||||
*/
|
||||
render(fileName) {
|
||||
if (fileName.endsWith('pdf')) {
|
||||
var options = {};
|
||||
var paperSize = this.paperSize || {};
|
||||
options.margin = paperSize.margin;
|
||||
options.format = paperSize.format;
|
||||
options.landscape = paperSize.orientation === 'landscape';
|
||||
options.width = paperSize.width;
|
||||
options.height = paperSize.height;
|
||||
await(this._page.printToPDF(fileName, options));
|
||||
} else {
|
||||
var options = {};
|
||||
if (this.clipRect && (this.clipRect.left || this.clipRect.top || this.clipRect.width || this.clipRect.height)) {
|
||||
options.clip = {
|
||||
x: this.clipRect.left,
|
||||
y: this.clipRect.top,
|
||||
width: this.clipRect.width,
|
||||
height: this.clipRect.height
|
||||
};
|
||||
}
|
||||
options.path = fileName;
|
||||
await(this._page.screenshot(options));
|
||||
}
|
||||
}
|
||||
|
||||
release() {
|
||||
this._page.close();
|
||||
}
|
||||
release() {
|
||||
this._page.close();
|
||||
}
|
||||
|
||||
close() {
|
||||
this._page.close();
|
||||
}
|
||||
close() {
|
||||
this._page.close();
|
||||
}
|
||||
}
|
||||
|
||||
class WebPageSettings {
|
||||
/**
|
||||
* @param {!Page} page
|
||||
*/
|
||||
constructor(page) {
|
||||
this._page = page;
|
||||
}
|
||||
/**
|
||||
* @param {!Page} page
|
||||
*/
|
||||
constructor(page) {
|
||||
this._page = page;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} value
|
||||
*/
|
||||
set userAgent(value) {
|
||||
await(this._page.setUserAgentOverride(value));
|
||||
}
|
||||
/**
|
||||
* @param {string} value
|
||||
*/
|
||||
set userAgent(value) {
|
||||
await(this._page.setUserAgentOverride(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
get userAgent() {
|
||||
return this._page.userAgentOverride();
|
||||
}
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
get userAgent() {
|
||||
return this._page.userAgentOverride();
|
||||
}
|
||||
}
|
||||
|
||||
// To prevent reenterability, eventemitters should emit events
|
||||
@ -349,29 +349,29 @@ class WebPageSettings {
|
||||
// This class is a wrapper around EventEmitter which re-emits events asynchronously,
|
||||
// helping to overcome the issue.
|
||||
class AsyncEmitter extends EventEmitter {
|
||||
/**
|
||||
* @param {!Page} page
|
||||
*/
|
||||
constructor(page) {
|
||||
super();
|
||||
this._page = page;
|
||||
this._symbol = Symbol('AsyncEmitter');
|
||||
this.on('newListener', this._onListenerAdded);
|
||||
this.on('removeListener', this._onListenerRemoved);
|
||||
}
|
||||
/**
|
||||
* @param {!Page} page
|
||||
*/
|
||||
constructor(page) {
|
||||
super();
|
||||
this._page = page;
|
||||
this._symbol = Symbol('AsyncEmitter');
|
||||
this.on('newListener', this._onListenerAdded);
|
||||
this.on('removeListener', this._onListenerRemoved);
|
||||
}
|
||||
|
||||
_onListenerAdded(event, listener) {
|
||||
// Async listener calls original listener on next tick.
|
||||
var asyncListener = (...args) => {
|
||||
process.nextTick(() => listener.apply(null, args));
|
||||
};
|
||||
listener[this._symbol] = asyncListener;
|
||||
this._page.on(event, asyncListener);
|
||||
}
|
||||
_onListenerAdded(event, listener) {
|
||||
// Async listener calls original listener on next tick.
|
||||
var asyncListener = (...args) => {
|
||||
process.nextTick(() => listener.apply(null, args));
|
||||
};
|
||||
listener[this._symbol] = asyncListener;
|
||||
this._page.on(event, asyncListener);
|
||||
}
|
||||
|
||||
_onListenerRemoved(event, listener) {
|
||||
this._page.removeListener(event, listener[this._symbol]);
|
||||
}
|
||||
_onListenerRemoved(event, listener) {
|
||||
this._page.removeListener(event, listener[this._symbol]);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = WebPage;
|
||||
|
@ -18,66 +18,66 @@ var http = require('http');
|
||||
var await = require('./utilities').await;
|
||||
|
||||
class WebServer {
|
||||
constructor() {
|
||||
this._server = http.createServer();
|
||||
this.objectName = 'WebServer';
|
||||
this.listenOnPort = this.listen;
|
||||
this.newRequest = function(req, res) { };
|
||||
Object.defineProperty(this, 'port', {
|
||||
get: () => {
|
||||
if (!this._server.listening)
|
||||
return '';
|
||||
return this._server.address().port + '';
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: false
|
||||
});
|
||||
}
|
||||
|
||||
close() {
|
||||
this._server.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {nubmer} port
|
||||
* @return {boolean}
|
||||
*/
|
||||
listen(port, callback) {
|
||||
if (this._server.listening)
|
||||
return false;
|
||||
this.newRequest = callback;
|
||||
this._server.listen(port);
|
||||
var errorPromise = new Promise(x => this._server.once('error', x));
|
||||
var successPromise = new Promise(x => this._server.once('listening', x));
|
||||
await(Promise.race([errorPromise, successPromise]));
|
||||
constructor() {
|
||||
this._server = http.createServer();
|
||||
this.objectName = 'WebServer';
|
||||
this.listenOnPort = this.listen;
|
||||
this.newRequest = function(req, res) { };
|
||||
Object.defineProperty(this, 'port', {
|
||||
get: () => {
|
||||
if (!this._server.listening)
|
||||
return false;
|
||||
return '';
|
||||
return this._server.address().port + '';
|
||||
},
|
||||
enumerable: true,
|
||||
configurable: false
|
||||
});
|
||||
}
|
||||
|
||||
this._server.on('request', (req, res) => {
|
||||
res.close = res.end.bind(res);
|
||||
var headers = res.getHeaders();
|
||||
res.headers = [];
|
||||
for (var key in headers) {
|
||||
res.headers.push({
|
||||
name: key,
|
||||
value: headers[key]
|
||||
});
|
||||
}
|
||||
res.header = res.getHeader;
|
||||
res.setHeaders = headers => {
|
||||
for (var key in headers)
|
||||
res.setHeader(key, headers[key]);
|
||||
};
|
||||
Object.defineProperty(res, 'statusCode', {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: res.statusCode
|
||||
});
|
||||
this.newRequest.call(null, req, res);
|
||||
close() {
|
||||
this._server.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {nubmer} port
|
||||
* @return {boolean}
|
||||
*/
|
||||
listen(port, callback) {
|
||||
if (this._server.listening)
|
||||
return false;
|
||||
this.newRequest = callback;
|
||||
this._server.listen(port);
|
||||
var errorPromise = new Promise(x => this._server.once('error', x));
|
||||
var successPromise = new Promise(x => this._server.once('listening', x));
|
||||
await(Promise.race([errorPromise, successPromise]));
|
||||
if (!this._server.listening)
|
||||
return false;
|
||||
|
||||
this._server.on('request', (req, res) => {
|
||||
res.close = res.end.bind(res);
|
||||
var headers = res.getHeaders();
|
||||
res.headers = [];
|
||||
for (var key in headers) {
|
||||
res.headers.push({
|
||||
name: key,
|
||||
value: headers[key]
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
res.header = res.getHeader;
|
||||
res.setHeaders = headers => {
|
||||
for (var key in headers)
|
||||
res.setHeader(key, headers[key]);
|
||||
};
|
||||
Object.defineProperty(res, 'statusCode', {
|
||||
enumerable: true,
|
||||
configurable: true,
|
||||
writable: true,
|
||||
value: res.statusCode
|
||||
});
|
||||
this.newRequest.call(null, req, res);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = WebServer;
|
||||
|
@ -27,39 +27,39 @@ var child_process = require('child_process');
|
||||
var Browser = require('..').Browser;
|
||||
var version = require('../package.json').version;
|
||||
var argv = require('minimist')(process.argv.slice(2), {
|
||||
alias: { v: 'version' },
|
||||
boolean: ['headless'],
|
||||
default: {'headless': true },
|
||||
alias: { v: 'version' },
|
||||
boolean: ['headless'],
|
||||
default: {'headless': true },
|
||||
});
|
||||
|
||||
if (argv.version) {
|
||||
console.log('Puppeteer v' + version);
|
||||
return;
|
||||
console.log('Puppeteer v' + version);
|
||||
return;
|
||||
}
|
||||
|
||||
if (argv['ssl-certificates-path']) {
|
||||
console.error('Flag --ssl-certificates-path is not supported.');
|
||||
process.exit(1);
|
||||
return;
|
||||
console.error('Flag --ssl-certificates-path is not supported.');
|
||||
process.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
var scriptArguments = argv._;
|
||||
if (!scriptArguments.length) {
|
||||
console.log(__filename.split('/').pop() + ' [scriptfile]');
|
||||
return;
|
||||
console.log(__filename.split('/').pop() + ' [scriptfile]');
|
||||
return;
|
||||
}
|
||||
|
||||
var scriptPath = path.resolve(process.cwd(), scriptArguments[0]);
|
||||
if (!fs.existsSync(scriptPath)) {
|
||||
console.error(`script not found: ${scriptPath}`);
|
||||
process.exit(1);
|
||||
return;
|
||||
console.error(`script not found: ${scriptPath}`);
|
||||
process.exit(1);
|
||||
return;
|
||||
}
|
||||
|
||||
var browser = new Browser({
|
||||
remoteDebuggingPort: 9229,
|
||||
headless: argv.headless,
|
||||
args: ['--no-sandbox']
|
||||
remoteDebuggingPort: 9229,
|
||||
headless: argv.headless,
|
||||
args: ['--no-sandbox']
|
||||
});
|
||||
|
||||
var context = createPhantomContext(browser, scriptPath, argv);
|
||||
@ -73,38 +73,38 @@ vm.runInContext(scriptContent, context);
|
||||
* @return {!Object}
|
||||
*/
|
||||
function createPhantomContext(browser, scriptPath, argv) {
|
||||
var context = {};
|
||||
context.setInterval = setInterval;
|
||||
context.setTimeout = setTimeout;
|
||||
context.clearInterval = clearInterval;
|
||||
context.clearTimeout = clearTimeout;
|
||||
var context = {};
|
||||
context.setInterval = setInterval;
|
||||
context.setTimeout = setTimeout;
|
||||
context.clearInterval = clearInterval;
|
||||
context.clearTimeout = clearTimeout;
|
||||
|
||||
context.phantom = Phantom.create(context, scriptPath);
|
||||
context.console = console;
|
||||
context.window = context;
|
||||
context.WebPage = options => new WebPage(browser, scriptPath, options);
|
||||
context.phantom = Phantom.create(context, scriptPath);
|
||||
context.console = console;
|
||||
context.window = context;
|
||||
context.WebPage = options => new WebPage(browser, scriptPath, options);
|
||||
|
||||
vm.createContext(context);
|
||||
vm.createContext(context);
|
||||
|
||||
var nativeExports = {
|
||||
fs: new FileSystem(),
|
||||
system: new System(argv._),
|
||||
webpage: {
|
||||
create: context.WebPage,
|
||||
},
|
||||
webserver: {
|
||||
create: () => new WebServer(),
|
||||
},
|
||||
cookiejar: {
|
||||
create: () => {},
|
||||
},
|
||||
child_process: child_process
|
||||
};
|
||||
var bootstrapPath = path.join(__dirname, '..', 'third_party', 'phantomjs', 'bootstrap.js');
|
||||
var bootstrapCode = fs.readFileSync(bootstrapPath, 'utf8');
|
||||
vm.runInContext(bootstrapCode, context, {
|
||||
filename: 'bootstrap.js'
|
||||
})(nativeExports);
|
||||
return context;
|
||||
var nativeExports = {
|
||||
fs: new FileSystem(),
|
||||
system: new System(argv._),
|
||||
webpage: {
|
||||
create: context.WebPage,
|
||||
},
|
||||
webserver: {
|
||||
create: () => new WebServer(),
|
||||
},
|
||||
cookiejar: {
|
||||
create: () => {},
|
||||
},
|
||||
child_process: child_process
|
||||
};
|
||||
var bootstrapPath = path.join(__dirname, '..', 'third_party', 'phantomjs', 'bootstrap.js');
|
||||
var bootstrapCode = fs.readFileSync(bootstrapPath, 'utf8');
|
||||
vm.runInContext(bootstrapCode, context, {
|
||||
filename: 'bootstrap.js'
|
||||
})(nativeExports);
|
||||
return context;
|
||||
}
|
||||
|
||||
|
@ -17,16 +17,16 @@
|
||||
var loopWhile = require('deasync').loopWhile;
|
||||
|
||||
module.exports = {
|
||||
await: function(promise) {
|
||||
var error;
|
||||
var result;
|
||||
var done = false;
|
||||
promise.then(r => result = r)
|
||||
.catch(err => error = err)
|
||||
.then(() => done = true);
|
||||
loopWhile(() => !done);
|
||||
if (error)
|
||||
throw error;
|
||||
return result;
|
||||
}
|
||||
await: function(promise) {
|
||||
var error;
|
||||
var result;
|
||||
var done = false;
|
||||
promise.then(r => result = r)
|
||||
.catch(err => error = err)
|
||||
.then(() => done = true);
|
||||
loopWhile(() => !done);
|
||||
if (error)
|
||||
throw error;
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
@ -21,35 +21,35 @@ var path = require('path');
|
||||
var mime = require('mime');
|
||||
|
||||
class StaticServer {
|
||||
/**
|
||||
* @param {string} dirPath
|
||||
* @param {number} port
|
||||
*/
|
||||
constructor(dirPath, port) {
|
||||
this._server = http.createServer(this._onRequest.bind(this));
|
||||
this._server.listen(port);
|
||||
this._dirPath = dirPath;
|
||||
}
|
||||
/**
|
||||
* @param {string} dirPath
|
||||
* @param {number} port
|
||||
*/
|
||||
constructor(dirPath, port) {
|
||||
this._server = http.createServer(this._onRequest.bind(this));
|
||||
this._server.listen(port);
|
||||
this._dirPath = dirPath;
|
||||
}
|
||||
|
||||
stop() {
|
||||
this._server.close();
|
||||
}
|
||||
stop() {
|
||||
this._server.close();
|
||||
}
|
||||
|
||||
_onRequest(request, response) {
|
||||
var pathName = url.parse(request.url).path;
|
||||
if (pathName === '/')
|
||||
pathName = '/index.html';
|
||||
pathName = path.join(this._dirPath, pathName.substring(1));
|
||||
fs.readFile(pathName, function(err, data) {
|
||||
if (err) {
|
||||
response.statusCode = 404;
|
||||
response.end(`File not found: ${pathName}`);
|
||||
return;
|
||||
}
|
||||
response.setHeader('Content-Type', mime.lookup(pathName));
|
||||
response.end(data);
|
||||
});
|
||||
}
|
||||
_onRequest(request, response) {
|
||||
var pathName = url.parse(request.url).path;
|
||||
if (pathName === '/')
|
||||
pathName = '/index.html';
|
||||
pathName = path.join(this._dirPath, pathName.substring(1));
|
||||
fs.readFile(pathName, function(err, data) {
|
||||
if (err) {
|
||||
response.statusCode = 404;
|
||||
response.end(`File not found: ${pathName}`);
|
||||
return;
|
||||
}
|
||||
response.setHeader('Content-Type', mime.lookup(pathName));
|
||||
response.end(data);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = StaticServer;
|
||||
|
@ -1,62 +1,62 @@
|
||||
var utils = module.exports = {
|
||||
/**
|
||||
* @param {!Page} page
|
||||
* @param {string} frameId
|
||||
* @param {string} url
|
||||
* @return {!Promise}
|
||||
*/
|
||||
attachFrame: async function(page, frameId, url) {
|
||||
await page.evaluate(attachFrame, frameId, url);
|
||||
/**
|
||||
* @param {!Page} page
|
||||
* @param {string} frameId
|
||||
* @param {string} url
|
||||
* @return {!Promise}
|
||||
*/
|
||||
attachFrame: async function(page, frameId, url) {
|
||||
await page.evaluate(attachFrame, frameId, url);
|
||||
|
||||
function attachFrame(frameId, url) {
|
||||
var frame = document.createElement('iframe');
|
||||
frame.src = url;
|
||||
frame.id = frameId;
|
||||
document.body.appendChild(frame);
|
||||
return new Promise(x => frame.onload = x);
|
||||
}
|
||||
},
|
||||
function attachFrame(frameId, url) {
|
||||
var frame = document.createElement('iframe');
|
||||
frame.src = url;
|
||||
frame.id = frameId;
|
||||
document.body.appendChild(frame);
|
||||
return new Promise(x => frame.onload = x);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {!Page} page
|
||||
* @param {string} frameId
|
||||
* @return {!Promise}
|
||||
*/
|
||||
detachFrame: async function(page, frameId) {
|
||||
await page.evaluate(detachFrame, frameId);
|
||||
/**
|
||||
* @param {!Page} page
|
||||
* @param {string} frameId
|
||||
* @return {!Promise}
|
||||
*/
|
||||
detachFrame: async function(page, frameId) {
|
||||
await page.evaluate(detachFrame, frameId);
|
||||
|
||||
function detachFrame(frameId) {
|
||||
var frame = document.getElementById(frameId);
|
||||
frame.remove();
|
||||
}
|
||||
},
|
||||
function detachFrame(frameId) {
|
||||
var frame = document.getElementById(frameId);
|
||||
frame.remove();
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {!Page} page
|
||||
* @param {string} frameId
|
||||
* @param {string} url
|
||||
* @return {!Promise}
|
||||
*/
|
||||
navigateFrame: async function(page, frameId, url) {
|
||||
await page.evaluate(navigateFrame, frameId, url);
|
||||
/**
|
||||
* @param {!Page} page
|
||||
* @param {string} frameId
|
||||
* @param {string} url
|
||||
* @return {!Promise}
|
||||
*/
|
||||
navigateFrame: async function(page, frameId, url) {
|
||||
await page.evaluate(navigateFrame, frameId, url);
|
||||
|
||||
function navigateFrame(frameId, url) {
|
||||
var frame = document.getElementById(frameId);
|
||||
frame.src = url;
|
||||
return new Promise(x => frame.onload = x);
|
||||
}
|
||||
},
|
||||
function navigateFrame(frameId, url) {
|
||||
var frame = document.getElementById(frameId);
|
||||
frame.src = url;
|
||||
return new Promise(x => frame.onload = x);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {!Frame} frame
|
||||
* @param {string=} indentation
|
||||
* @return {string}
|
||||
*/
|
||||
dumpFrames: function(frame, indentation) {
|
||||
indentation = indentation || '';
|
||||
var result = indentation + frame.url();
|
||||
for (var child of frame.childFrames())
|
||||
result += '\n' + utils.dumpFrames(child, ' ' + indentation);
|
||||
return result;
|
||||
},
|
||||
/**
|
||||
* @param {!Frame} frame
|
||||
* @param {string=} indentation
|
||||
* @return {string}
|
||||
*/
|
||||
dumpFrames: function(frame, indentation) {
|
||||
indentation = indentation || '';
|
||||
var result = indentation + frame.url();
|
||||
for (var child of frame.childFrames())
|
||||
result += '\n' + utils.dumpFrames(child, ' ' + indentation);
|
||||
return result;
|
||||
},
|
||||
};
|
||||
|
@ -25,19 +25,19 @@ var GOLDEN_DIR = path.join(__dirname, 'golden');
|
||||
var OUTPUT_DIR = path.join(__dirname, 'output');
|
||||
|
||||
module.exports = {
|
||||
addMatchers: function(jasmine) {
|
||||
jasmine.addMatchers(customMatchers);
|
||||
},
|
||||
addMatchers: function(jasmine) {
|
||||
jasmine.addMatchers(customMatchers);
|
||||
},
|
||||
|
||||
removeOutputDir: function() {
|
||||
if (fs.existsSync(OUTPUT_DIR))
|
||||
rm(OUTPUT_DIR);
|
||||
},
|
||||
removeOutputDir: function() {
|
||||
if (fs.existsSync(OUTPUT_DIR))
|
||||
rm(OUTPUT_DIR);
|
||||
},
|
||||
};
|
||||
|
||||
var GoldenComparators = {
|
||||
'image/png': compareImages,
|
||||
'text/plain': compareText
|
||||
'image/png': compareImages,
|
||||
'text/plain': compareText
|
||||
};
|
||||
|
||||
/**
|
||||
@ -46,19 +46,19 @@ var GoldenComparators = {
|
||||
* @return {?{diff: (!Object:undefined), errorMessage: (string|undefined)}}
|
||||
*/
|
||||
function compareImages(actualBuffer, expectedBuffer) {
|
||||
if (!actualBuffer || !(actualBuffer instanceof Buffer))
|
||||
return { errorMessage: 'Actual result should be Buffer.' };
|
||||
if (!actualBuffer || !(actualBuffer instanceof Buffer))
|
||||
return { errorMessage: 'Actual result should be Buffer.' };
|
||||
|
||||
var actual = PNG.sync.read(actualBuffer);
|
||||
var expected = PNG.sync.read(expectedBuffer);
|
||||
if (expected.width !== actual.width || expected.height !== actual.height) {
|
||||
return {
|
||||
errorMessage: `Sizes differ: expected image ${expected.width}px X ${expected.height}px, but got ${actual.width}px X ${actual.height}px. `
|
||||
};
|
||||
}
|
||||
var diff = new PNG({width: expected.width, height: expected.height});
|
||||
var count = pixelmatch(expected.data, actual.data, diff.data, expected.width, expected.height, {threshold: 0.1});
|
||||
return count > 0 ? { diff: PNG.sync.write(diff) } : null;
|
||||
var actual = PNG.sync.read(actualBuffer);
|
||||
var expected = PNG.sync.read(expectedBuffer);
|
||||
if (expected.width !== actual.width || expected.height !== actual.height) {
|
||||
return {
|
||||
errorMessage: `Sizes differ: expected image ${expected.width}px X ${expected.height}px, but got ${actual.width}px X ${actual.height}px. `
|
||||
};
|
||||
}
|
||||
var diff = new PNG({width: expected.width, height: expected.height});
|
||||
var count = pixelmatch(expected.data, actual.data, diff.data, expected.width, expected.height, {threshold: 0.1});
|
||||
return count > 0 ? { diff: PNG.sync.write(diff) } : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,80 +67,80 @@ function compareImages(actualBuffer, expectedBuffer) {
|
||||
* @return {?{diff: (!Object:undefined), errorMessage: (string|undefined)}}
|
||||
*/
|
||||
function compareText(actual, expectedBuffer) {
|
||||
if (typeof actual !== 'string')
|
||||
return { errorMessage: 'Actual result should be string' };
|
||||
var expected = expectedBuffer.toString('utf-8');
|
||||
if (expected === actual)
|
||||
return null;
|
||||
var diff = new Diff();
|
||||
var result = diff.main(expected, actual);
|
||||
diff.cleanupSemantic(result);
|
||||
var html = diff.prettyHtml(result);
|
||||
var diffStylePath = path.join(__dirname, 'diffstyle.css');
|
||||
html = `<link rel="stylesheet" href="file://${diffStylePath}">` + html;
|
||||
return {
|
||||
diff: html,
|
||||
diffExtension: '.html'
|
||||
};
|
||||
if (typeof actual !== 'string')
|
||||
return { errorMessage: 'Actual result should be string' };
|
||||
var expected = expectedBuffer.toString('utf-8');
|
||||
if (expected === actual)
|
||||
return null;
|
||||
var diff = new Diff();
|
||||
var result = diff.main(expected, actual);
|
||||
diff.cleanupSemantic(result);
|
||||
var html = diff.prettyHtml(result);
|
||||
var diffStylePath = path.join(__dirname, 'diffstyle.css');
|
||||
html = `<link rel="stylesheet" href="file://${diffStylePath}">` + html;
|
||||
return {
|
||||
diff: html,
|
||||
diffExtension: '.html'
|
||||
};
|
||||
}
|
||||
|
||||
var customMatchers = {
|
||||
toBeGolden: function(util, customEqualityTesters) {
|
||||
toBeGolden: function(util, customEqualityTesters) {
|
||||
return {
|
||||
/**
|
||||
* @param {?Object} actual
|
||||
* @param {string} goldenName
|
||||
* @return {!{pass: boolean, message: (undefined|string)}}
|
||||
*/
|
||||
compare: function(actual, goldenName) {
|
||||
var expectedPath = path.join(GOLDEN_DIR, goldenName);
|
||||
var actualPath = path.join(OUTPUT_DIR, goldenName);
|
||||
|
||||
var messageSuffix = 'Output is saved in "' + path.basename(OUTPUT_DIR + '" directory');
|
||||
|
||||
if (!fs.existsSync(expectedPath)) {
|
||||
ensureOutputDir();
|
||||
fs.writeFileSync(actualPath, actual);
|
||||
return {
|
||||
pass: false,
|
||||
message: goldenName + ' is missing in golden results. ' + messageSuffix
|
||||
};
|
||||
}
|
||||
var expected = fs.readFileSync(expectedPath);
|
||||
var comparator = GoldenComparators[mime.lookup(goldenName)];
|
||||
if (!comparator) {
|
||||
return {
|
||||
pass: false,
|
||||
message: 'Failed to find comparator with type ' + mime.lookup(goldenName) + ': ' + goldenName
|
||||
};
|
||||
}
|
||||
var result = comparator(actual, expected);
|
||||
if (!result)
|
||||
return { pass: true };
|
||||
ensureOutputDir();
|
||||
fs.writeFileSync(actualPath, actual);
|
||||
// Copy expected to the output/ folder for convenience.
|
||||
fs.writeFileSync(addSuffix(actualPath, '-expected'), expected);
|
||||
if (result.diff) {
|
||||
var diffPath = addSuffix(actualPath, '-diff', result.diffExtension);
|
||||
fs.writeFileSync(diffPath, result.diff);
|
||||
}
|
||||
|
||||
var message = goldenName + ' mismatch!';
|
||||
if (result.errorMessage)
|
||||
message += ' ' + result.errorMessage;
|
||||
return {
|
||||
/**
|
||||
* @param {?Object} actual
|
||||
* @param {string} goldenName
|
||||
* @return {!{pass: boolean, message: (undefined|string)}}
|
||||
*/
|
||||
compare: function(actual, goldenName) {
|
||||
var expectedPath = path.join(GOLDEN_DIR, goldenName);
|
||||
var actualPath = path.join(OUTPUT_DIR, goldenName);
|
||||
|
||||
var messageSuffix = 'Output is saved in "' + path.basename(OUTPUT_DIR + '" directory');
|
||||
|
||||
if (!fs.existsSync(expectedPath)) {
|
||||
ensureOutputDir();
|
||||
fs.writeFileSync(actualPath, actual);
|
||||
return {
|
||||
pass: false,
|
||||
message: goldenName + ' is missing in golden results. ' + messageSuffix
|
||||
};
|
||||
}
|
||||
var expected = fs.readFileSync(expectedPath);
|
||||
var comparator = GoldenComparators[mime.lookup(goldenName)];
|
||||
if (!comparator) {
|
||||
return {
|
||||
pass: false,
|
||||
message: 'Failed to find comparator with type ' + mime.lookup(goldenName) + ': ' + goldenName
|
||||
};
|
||||
}
|
||||
var result = comparator(actual, expected);
|
||||
if (!result)
|
||||
return { pass: true };
|
||||
ensureOutputDir();
|
||||
fs.writeFileSync(actualPath, actual);
|
||||
// Copy expected to the output/ folder for convenience.
|
||||
fs.writeFileSync(addSuffix(actualPath, '-expected'), expected);
|
||||
if (result.diff) {
|
||||
var diffPath = addSuffix(actualPath, '-diff', result.diffExtension);
|
||||
fs.writeFileSync(diffPath, result.diff);
|
||||
}
|
||||
|
||||
var message = goldenName + ' mismatch!';
|
||||
if (result.errorMessage)
|
||||
message += ' ' + result.errorMessage;
|
||||
return {
|
||||
pass: false,
|
||||
message: message + ' ' + messageSuffix
|
||||
};
|
||||
|
||||
function ensureOutputDir() {
|
||||
if (!fs.existsSync(OUTPUT_DIR))
|
||||
fs.mkdirSync(OUTPUT_DIR);
|
||||
}
|
||||
}
|
||||
pass: false,
|
||||
message: message + ' ' + messageSuffix
|
||||
};
|
||||
},
|
||||
|
||||
function ensureOutputDir() {
|
||||
if (!fs.existsSync(OUTPUT_DIR))
|
||||
fs.mkdirSync(OUTPUT_DIR);
|
||||
}
|
||||
}
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
@ -150,8 +150,8 @@ var customMatchers = {
|
||||
* @return {string}
|
||||
*/
|
||||
function addSuffix(filePath, suffix, customExtension) {
|
||||
var dirname = path.dirname(filePath);
|
||||
var ext = path.extname(filePath);
|
||||
var name = path.basename(filePath, ext);
|
||||
return path.join(dirname, name + suffix + (customExtension || ext));
|
||||
var dirname = path.dirname(filePath);
|
||||
var ext = path.extname(filePath);
|
||||
var name = path.basename(filePath, ext);
|
||||
return path.join(dirname, name + suffix + (customExtension || ext));
|
||||
}
|
||||
|
568
test/test.js
568
test/test.js
@ -26,308 +26,308 @@ var EMPTY_PAGE = STATIC_PREFIX + '/empty.html';
|
||||
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10 * 1000;
|
||||
|
||||
describe('Puppeteer', function() {
|
||||
var browser;
|
||||
var staticServer;
|
||||
var page;
|
||||
var browser;
|
||||
var staticServer;
|
||||
var page;
|
||||
|
||||
beforeAll(function() {
|
||||
browser = new Browser({args: ['--no-sandbox']});
|
||||
staticServer = new StaticServer(path.join(__dirname, 'assets'), PORT);
|
||||
GoldenUtils.removeOutputDir();
|
||||
});
|
||||
beforeAll(function() {
|
||||
browser = new Browser({args: ['--no-sandbox']});
|
||||
staticServer = new StaticServer(path.join(__dirname, 'assets'), PORT);
|
||||
GoldenUtils.removeOutputDir();
|
||||
});
|
||||
|
||||
afterAll(function() {
|
||||
browser.close();
|
||||
staticServer.stop();
|
||||
});
|
||||
afterAll(function() {
|
||||
browser.close();
|
||||
staticServer.stop();
|
||||
});
|
||||
|
||||
beforeEach(SX(async function() {
|
||||
page = await browser.newPage();
|
||||
GoldenUtils.addMatchers(jasmine);
|
||||
beforeEach(SX(async function() {
|
||||
page = await browser.newPage();
|
||||
GoldenUtils.addMatchers(jasmine);
|
||||
}));
|
||||
|
||||
afterEach(function() {
|
||||
page.close();
|
||||
});
|
||||
|
||||
describe('Page.evaluate', function() {
|
||||
it('should work', SX(async function() {
|
||||
var result = await page.evaluate(() => 7 * 3);
|
||||
expect(result).toBe(21);
|
||||
}));
|
||||
|
||||
afterEach(function() {
|
||||
page.close();
|
||||
});
|
||||
|
||||
describe('Page.evaluate', function() {
|
||||
it('should work', SX(async function() {
|
||||
var result = await page.evaluate(() => 7 * 3);
|
||||
expect(result).toBe(21);
|
||||
}));
|
||||
it('should await promise', SX(async function() {
|
||||
var result = await page.evaluate(() => Promise.resolve(8 * 7));
|
||||
expect(result).toBe(56);
|
||||
}));
|
||||
it('should work from-inside inPageCallback', SX(async function() {
|
||||
// Setup inpage callback, which calls Page.evaluate
|
||||
await page.setInPageCallback('callController', async function(a, b) {
|
||||
return await page.evaluate((a, b) => a * b, a, b);
|
||||
});
|
||||
var result = await page.evaluate(async function() {
|
||||
return await callController(9, 3);
|
||||
});
|
||||
expect(result).toBe(27);
|
||||
}));
|
||||
it('should reject promise with exception', SX(async function() {
|
||||
var error = null;
|
||||
try {
|
||||
await page.evaluate(() => not.existing.object.property);
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
expect(error).toBeTruthy();
|
||||
expect(error.message).toContain('not is not defined');
|
||||
}));
|
||||
});
|
||||
|
||||
it('Page Events: ConsoleMessage', SX(async function() {
|
||||
var msgs = [];
|
||||
page.on('consolemessage', msg => msgs.push(msg));
|
||||
await page.evaluate(() => console.log('Message!'));
|
||||
expect(msgs).toEqual(['Message!']);
|
||||
it('should await promise', SX(async function() {
|
||||
var result = await page.evaluate(() => Promise.resolve(8 * 7));
|
||||
expect(result).toBe(56);
|
||||
}));
|
||||
it('should work from-inside inPageCallback', SX(async function() {
|
||||
// Setup inpage callback, which calls Page.evaluate
|
||||
await page.setInPageCallback('callController', async function(a, b) {
|
||||
return await page.evaluate((a, b) => a * b, a, b);
|
||||
});
|
||||
var result = await page.evaluate(async function() {
|
||||
return await callController(9, 3);
|
||||
});
|
||||
expect(result).toBe(27);
|
||||
}));
|
||||
it('should reject promise with exception', SX(async function() {
|
||||
var error = null;
|
||||
try {
|
||||
await page.evaluate(() => not.existing.object.property);
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
expect(error).toBeTruthy();
|
||||
expect(error.message).toContain('not is not defined');
|
||||
}));
|
||||
});
|
||||
|
||||
describe('Page.navigate', function() {
|
||||
it('should fail when navigating to bad url', SX(async function() {
|
||||
var success = await page.navigate('asdfasdf');
|
||||
expect(success).toBe(false);
|
||||
}));
|
||||
it('should succeed when navigating to good url', SX(async function() {
|
||||
var success = await page.navigate(EMPTY_PAGE);
|
||||
expect(success).toBe(true);
|
||||
}));
|
||||
it('Page Events: ConsoleMessage', SX(async function() {
|
||||
var msgs = [];
|
||||
page.on('consolemessage', msg => msgs.push(msg));
|
||||
await page.evaluate(() => console.log('Message!'));
|
||||
expect(msgs).toEqual(['Message!']);
|
||||
}));
|
||||
|
||||
describe('Page.navigate', function() {
|
||||
it('should fail when navigating to bad url', SX(async function() {
|
||||
var success = await page.navigate('asdfasdf');
|
||||
expect(success).toBe(false);
|
||||
}));
|
||||
it('should succeed when navigating to good url', SX(async function() {
|
||||
var success = await page.navigate(EMPTY_PAGE);
|
||||
expect(success).toBe(true);
|
||||
}));
|
||||
});
|
||||
|
||||
describe('Page.setInPageCallback', function() {
|
||||
it('should work', SX(async function() {
|
||||
await page.setInPageCallback('callController', function(a, b) {
|
||||
return a * b;
|
||||
});
|
||||
var result = await page.evaluate(async function() {
|
||||
return await callController(9, 4);
|
||||
});
|
||||
expect(result).toBe(36);
|
||||
}));
|
||||
it('should survive navigation', SX(async function() {
|
||||
await page.setInPageCallback('callController', function(a, b) {
|
||||
return a * b;
|
||||
});
|
||||
|
||||
await page.navigate(EMPTY_PAGE);
|
||||
var result = await page.evaluate(async function() {
|
||||
return await callController(9, 4);
|
||||
});
|
||||
expect(result).toBe(36);
|
||||
}));
|
||||
it('should await returned promise', SX(async function() {
|
||||
await page.setInPageCallback('callController', function(a, b) {
|
||||
return Promise.resolve(a * b);
|
||||
});
|
||||
|
||||
var result = await page.evaluate(async function() {
|
||||
return await callController(3, 5);
|
||||
});
|
||||
expect(result).toBe(15);
|
||||
}));
|
||||
});
|
||||
|
||||
describe('Page.setRequestInterceptor', function() {
|
||||
it('should intercept', SX(async function() {
|
||||
page.setRequestInterceptor(request => {
|
||||
expect(request.url()).toContain('empty.html');
|
||||
expect(request.headers()['User-Agent']).toBeTruthy();
|
||||
expect(request.method()).toBe('GET');
|
||||
expect(request.postData()).toBe(undefined);
|
||||
request.continue();
|
||||
});
|
||||
var success = await page.navigate(EMPTY_PAGE);
|
||||
expect(success).toBe(true);
|
||||
}));
|
||||
it('should show extraHTTPHeaders', SX(async function() {
|
||||
await page.setExtraHTTPHeaders({
|
||||
foo: 'bar'
|
||||
});
|
||||
page.setRequestInterceptor(request => {
|
||||
expect(request.headers()['foo']).toBe('bar');
|
||||
request.continue();
|
||||
});
|
||||
var success = await page.navigate(EMPTY_PAGE);
|
||||
expect(success).toBe(true);
|
||||
}));
|
||||
it('should be abortable', SX(async function() {
|
||||
page.setRequestInterceptor(request => {
|
||||
if (request.url().endsWith('.css'))
|
||||
request.abort();
|
||||
else
|
||||
request.continue();
|
||||
});
|
||||
var failedResources = 0;
|
||||
page.on('resourceloadingfailed', event => ++failedResources);
|
||||
var success = await page.navigate(STATIC_PREFIX + '/one-style.html');
|
||||
expect(success).toBe(true);
|
||||
expect(failedResources).toBe(1);
|
||||
}));
|
||||
});
|
||||
|
||||
describe('Page.Events.Dialog', function() {
|
||||
it('should fire', function(done) {
|
||||
page.on('dialog', dialog => {
|
||||
expect(dialog.type).toBe('alert');
|
||||
expect(dialog.message()).toBe('yo');
|
||||
done();
|
||||
});
|
||||
page.evaluate(() => alert('yo'));
|
||||
});
|
||||
// TODO Enable this when crbug.com/718235 is fixed.
|
||||
xit('should allow accepting prompts', SX(async function(done) {
|
||||
page.on('dialog', dialog => {
|
||||
expect(dialog.type).toBe('prompt');
|
||||
expect(dialog.message()).toBe('question?');
|
||||
dialog.accept('answer!');
|
||||
});
|
||||
var result = await page.evaluate(() => prompt('question?'));
|
||||
expect(result).toBe('answer!');
|
||||
}));
|
||||
});
|
||||
|
||||
describe('Page.setInPageCallback', function() {
|
||||
it('should work', SX(async function() {
|
||||
await page.setInPageCallback('callController', function(a, b) {
|
||||
return a * b;
|
||||
});
|
||||
var result = await page.evaluate(async function() {
|
||||
return await callController(9, 4);
|
||||
});
|
||||
expect(result).toBe(36);
|
||||
}));
|
||||
it('should survive navigation', SX(async function() {
|
||||
await page.setInPageCallback('callController', function(a, b) {
|
||||
return a * b;
|
||||
});
|
||||
|
||||
await page.navigate(EMPTY_PAGE);
|
||||
var result = await page.evaluate(async function() {
|
||||
return await callController(9, 4);
|
||||
});
|
||||
expect(result).toBe(36);
|
||||
}));
|
||||
it('should await returned promise', SX(async function() {
|
||||
await page.setInPageCallback('callController', function(a, b) {
|
||||
return Promise.resolve(a * b);
|
||||
});
|
||||
|
||||
var result = await page.evaluate(async function() {
|
||||
return await callController(3, 5);
|
||||
});
|
||||
expect(result).toBe(15);
|
||||
}));
|
||||
describe('Page.Events.Error', function() {
|
||||
it('should fire', function(done) {
|
||||
page.on('error', error => {
|
||||
expect(error.message).toContain('Fancy');
|
||||
done();
|
||||
});
|
||||
page.navigate(STATIC_PREFIX + '/error.html');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Page.setRequestInterceptor', function() {
|
||||
it('should intercept', SX(async function() {
|
||||
page.setRequestInterceptor(request => {
|
||||
expect(request.url()).toContain('empty.html');
|
||||
expect(request.headers()['User-Agent']).toBeTruthy();
|
||||
expect(request.method()).toBe('GET');
|
||||
expect(request.postData()).toBe(undefined);
|
||||
request.continue();
|
||||
});
|
||||
var success = await page.navigate(EMPTY_PAGE);
|
||||
expect(success).toBe(true);
|
||||
describe('Page.screenshot', function() {
|
||||
it('should work', SX(async function() {
|
||||
await page.setViewportSize({width: 500, height: 500});
|
||||
await page.navigate(STATIC_PREFIX + '/grid.html');
|
||||
var screenshot = await page.screenshot();
|
||||
expect(screenshot).toBeGolden('screenshot-sanity.png');
|
||||
}));
|
||||
it('should clip rect', SX(async function() {
|
||||
await page.setViewportSize({width: 500, height: 500});
|
||||
await page.navigate(STATIC_PREFIX + '/grid.html');
|
||||
var screenshot = await page.screenshot({
|
||||
clip: {
|
||||
x: 50,
|
||||
y: 100,
|
||||
width: 150,
|
||||
height: 100
|
||||
}
|
||||
});
|
||||
expect(screenshot).toBeGolden('screenshot-clip-rect.png');
|
||||
}));
|
||||
it('should work for offscreen clip', SX(async function() {
|
||||
await page.setViewportSize({width: 500, height: 500});
|
||||
await page.navigate(STATIC_PREFIX + '/grid.html');
|
||||
var screenshot = await page.screenshot({
|
||||
clip: {
|
||||
x: 50,
|
||||
y: 600,
|
||||
width: 100,
|
||||
height: 100
|
||||
}
|
||||
});
|
||||
expect(screenshot).toBeGolden('screenshot-offscreen-clip.png');
|
||||
}));
|
||||
it('should run in parallel', SX(async function() {
|
||||
await page.setViewportSize({width: 500, height: 500});
|
||||
await page.navigate(STATIC_PREFIX + '/grid.html');
|
||||
var promises = [];
|
||||
for (var i = 0; i < 3; ++i) {
|
||||
promises.push(page.screenshot({
|
||||
clip: {
|
||||
x: 50 * i,
|
||||
y: 0,
|
||||
width: 50,
|
||||
height: 50
|
||||
}
|
||||
}));
|
||||
it('should show extraHTTPHeaders', SX(async function() {
|
||||
await page.setExtraHTTPHeaders({
|
||||
foo: 'bar'
|
||||
});
|
||||
page.setRequestInterceptor(request => {
|
||||
expect(request.headers()['foo']).toBe('bar');
|
||||
request.continue();
|
||||
});
|
||||
var success = await page.navigate(EMPTY_PAGE);
|
||||
expect(success).toBe(true);
|
||||
}));
|
||||
it('should be abortable', SX(async function() {
|
||||
page.setRequestInterceptor(request => {
|
||||
if (request.url().endsWith('.css'))
|
||||
request.abort();
|
||||
else
|
||||
request.continue();
|
||||
});
|
||||
var failedResources = 0;
|
||||
page.on('resourceloadingfailed', event => ++failedResources);
|
||||
var success = await page.navigate(STATIC_PREFIX + '/one-style.html');
|
||||
expect(success).toBe(true);
|
||||
expect(failedResources).toBe(1);
|
||||
}));
|
||||
});
|
||||
}
|
||||
var screenshot = await promises[1];
|
||||
expect(screenshot).toBeGolden('screenshot-parallel-calls.png');
|
||||
}));
|
||||
it('should take fullPage screenshots', SX(async function() {
|
||||
await page.setViewportSize({width: 500, height: 500});
|
||||
await page.navigate(STATIC_PREFIX + '/grid.html');
|
||||
var screenshot = await page.screenshot({
|
||||
fullPage: true
|
||||
});
|
||||
expect(screenshot).toBeGolden('screenshot-grid-fullpage.png');
|
||||
}));
|
||||
});
|
||||
|
||||
describe('Page.Events.Dialog', function() {
|
||||
it('should fire', function(done) {
|
||||
page.on('dialog', dialog => {
|
||||
expect(dialog.type).toBe('alert');
|
||||
expect(dialog.message()).toBe('yo');
|
||||
done();
|
||||
});
|
||||
page.evaluate(() => alert('yo'));
|
||||
});
|
||||
// TODO Enable this when crbug.com/718235 is fixed.
|
||||
xit('should allow accepting prompts', SX(async function(done) {
|
||||
page.on('dialog', dialog => {
|
||||
expect(dialog.type).toBe('prompt');
|
||||
expect(dialog.message()).toBe('question?');
|
||||
dialog.accept('answer!');
|
||||
});
|
||||
var result = await page.evaluate(() => prompt('question?'));
|
||||
expect(result).toBe('answer!');
|
||||
}));
|
||||
});
|
||||
describe('Frame Management', function() {
|
||||
var FrameUtils = require('./frame-utils');
|
||||
it('should handle nested frames', SX(async function() {
|
||||
await page.navigate(STATIC_PREFIX + '/frames/nested-frames.html');
|
||||
expect(FrameUtils.dumpFrames(page.mainFrame())).toBeGolden('nested-frames.txt');
|
||||
}));
|
||||
it('should send events when frames are manipulated dynamically', SX(async function() {
|
||||
await page.navigate(EMPTY_PAGE);
|
||||
// validate frameattached events
|
||||
var attachedFrames = [];
|
||||
page.on('frameattached', frame => attachedFrames.push(frame));
|
||||
await FrameUtils.attachFrame(page, 'frame1', './assets/frame.html');
|
||||
expect(attachedFrames.length).toBe(1);
|
||||
expect(attachedFrames[0].url()).toContain('/assets/frame.html');
|
||||
|
||||
describe('Page.Events.Error', function() {
|
||||
it('should fire', function(done) {
|
||||
page.on('error', error => {
|
||||
expect(error.message).toContain('Fancy');
|
||||
done();
|
||||
});
|
||||
page.navigate(STATIC_PREFIX + '/error.html');
|
||||
});
|
||||
});
|
||||
// validate framenavigated events
|
||||
var navigatedFrames = [];
|
||||
page.on('framenavigated', frame => navigatedFrames.push(frame));
|
||||
await FrameUtils.navigateFrame(page, 'frame1', './empty.html');
|
||||
expect(navigatedFrames.length).toBe(1);
|
||||
expect(navigatedFrames[0].url()).toContain('/empty.html');
|
||||
|
||||
describe('Page.screenshot', function() {
|
||||
it('should work', SX(async function() {
|
||||
await page.setViewportSize({width: 500, height: 500});
|
||||
await page.navigate(STATIC_PREFIX + '/grid.html');
|
||||
var screenshot = await page.screenshot();
|
||||
expect(screenshot).toBeGolden('screenshot-sanity.png');
|
||||
}));
|
||||
it('should clip rect', SX(async function() {
|
||||
await page.setViewportSize({width: 500, height: 500});
|
||||
await page.navigate(STATIC_PREFIX + '/grid.html');
|
||||
var screenshot = await page.screenshot({
|
||||
clip: {
|
||||
x: 50,
|
||||
y: 100,
|
||||
width: 150,
|
||||
height: 100
|
||||
}
|
||||
});
|
||||
expect(screenshot).toBeGolden('screenshot-clip-rect.png');
|
||||
}));
|
||||
it('should work for offscreen clip', SX(async function() {
|
||||
await page.setViewportSize({width: 500, height: 500});
|
||||
await page.navigate(STATIC_PREFIX + '/grid.html');
|
||||
var screenshot = await page.screenshot({
|
||||
clip: {
|
||||
x: 50,
|
||||
y: 600,
|
||||
width: 100,
|
||||
height: 100
|
||||
}
|
||||
});
|
||||
expect(screenshot).toBeGolden('screenshot-offscreen-clip.png');
|
||||
}));
|
||||
it('should run in parallel', SX(async function() {
|
||||
await page.setViewportSize({width: 500, height: 500});
|
||||
await page.navigate(STATIC_PREFIX + '/grid.html');
|
||||
var promises = [];
|
||||
for (var i = 0; i < 3; ++i) {
|
||||
promises.push(page.screenshot({
|
||||
clip: {
|
||||
x: 50 * i,
|
||||
y: 0,
|
||||
width: 50,
|
||||
height: 50
|
||||
}
|
||||
}));
|
||||
}
|
||||
var screenshot = await promises[1];
|
||||
expect(screenshot).toBeGolden('screenshot-parallel-calls.png');
|
||||
}));
|
||||
it('should take fullPage screenshots', SX(async function() {
|
||||
await page.setViewportSize({width: 500, height: 500});
|
||||
await page.navigate(STATIC_PREFIX + '/grid.html');
|
||||
var screenshot = await page.screenshot({
|
||||
fullPage: true
|
||||
});
|
||||
expect(screenshot).toBeGolden('screenshot-grid-fullpage.png');
|
||||
}));
|
||||
});
|
||||
// validate framedetached events
|
||||
var detachedFrames = [];
|
||||
page.on('framedetached', frame => detachedFrames.push(frame));
|
||||
await FrameUtils.detachFrame(page, 'frame1');
|
||||
expect(detachedFrames.length).toBe(1);
|
||||
expect(detachedFrames[0].isDetached()).toBe(true);
|
||||
}));
|
||||
it('should persist mainFrame on cross-process navigation', SX(async function() {
|
||||
await page.navigate(EMPTY_PAGE);
|
||||
var mainFrame = page.mainFrame();
|
||||
await page.navigate('http://127.0.0.1:' + PORT + '/empty.html');
|
||||
expect(page.mainFrame() === mainFrame).toBeTruthy();
|
||||
}));
|
||||
it('should not send attach/detach events for main frame', SX(async function() {
|
||||
var hasEvents = false;
|
||||
page.on('frameattached', frame => hasEvents = true);
|
||||
page.on('framedetached', frame => hasEvents = true);
|
||||
await page.navigate(EMPTY_PAGE);
|
||||
expect(hasEvents).toBe(false);
|
||||
}));
|
||||
it('should detach child frames on navigation', SX(async function() {
|
||||
var attachedFrames = [];
|
||||
var detachedFrames = [];
|
||||
var navigatedFrames = [];
|
||||
page.on('frameattached', frame => attachedFrames.push(frame));
|
||||
page.on('framedetached', frame => detachedFrames.push(frame));
|
||||
page.on('framenavigated', frame => navigatedFrames.push(frame));
|
||||
await page.navigate(STATIC_PREFIX + '/frames/nested-frames.html');
|
||||
expect(attachedFrames.length).toBe(4);
|
||||
expect(detachedFrames.length).toBe(0);
|
||||
expect(navigatedFrames.length).toBe(5);
|
||||
|
||||
describe('Frame Management', function() {
|
||||
var FrameUtils = require('./frame-utils');
|
||||
it('should handle nested frames', SX(async function() {
|
||||
await page.navigate(STATIC_PREFIX + '/frames/nested-frames.html');
|
||||
expect(FrameUtils.dumpFrames(page.mainFrame())).toBeGolden('nested-frames.txt');
|
||||
}));
|
||||
it('should send events when frames are manipulated dynamically', SX(async function() {
|
||||
await page.navigate(EMPTY_PAGE);
|
||||
// validate frameattached events
|
||||
var attachedFrames = [];
|
||||
page.on('frameattached', frame => attachedFrames.push(frame));
|
||||
await FrameUtils.attachFrame(page, 'frame1', './assets/frame.html');
|
||||
expect(attachedFrames.length).toBe(1);
|
||||
expect(attachedFrames[0].url()).toContain('/assets/frame.html');
|
||||
|
||||
// validate framenavigated events
|
||||
var navigatedFrames = [];
|
||||
page.on('framenavigated', frame => navigatedFrames.push(frame));
|
||||
await FrameUtils.navigateFrame(page, 'frame1', './empty.html');
|
||||
expect(navigatedFrames.length).toBe(1);
|
||||
expect(navigatedFrames[0].url()).toContain('/empty.html');
|
||||
|
||||
// validate framedetached events
|
||||
var detachedFrames = [];
|
||||
page.on('framedetached', frame => detachedFrames.push(frame));
|
||||
await FrameUtils.detachFrame(page, 'frame1');
|
||||
expect(detachedFrames.length).toBe(1);
|
||||
expect(detachedFrames[0].isDetached()).toBe(true);
|
||||
}));
|
||||
it('should persist mainFrame on cross-process navigation', SX(async function() {
|
||||
await page.navigate(EMPTY_PAGE);
|
||||
var mainFrame = page.mainFrame();
|
||||
await page.navigate('http://127.0.0.1:' + PORT + '/empty.html');
|
||||
expect(page.mainFrame() === mainFrame).toBeTruthy();
|
||||
}));
|
||||
it('should not send attach/detach events for main frame', SX(async function() {
|
||||
var hasEvents = false;
|
||||
page.on('frameattached', frame => hasEvents = true);
|
||||
page.on('framedetached', frame => hasEvents = true);
|
||||
await page.navigate(EMPTY_PAGE);
|
||||
expect(hasEvents).toBe(false);
|
||||
}));
|
||||
it('should detach child frames on navigation', SX(async function() {
|
||||
var attachedFrames = [];
|
||||
var detachedFrames = [];
|
||||
var navigatedFrames = [];
|
||||
page.on('frameattached', frame => attachedFrames.push(frame));
|
||||
page.on('framedetached', frame => detachedFrames.push(frame));
|
||||
page.on('framenavigated', frame => navigatedFrames.push(frame));
|
||||
await page.navigate(STATIC_PREFIX + '/frames/nested-frames.html');
|
||||
expect(attachedFrames.length).toBe(4);
|
||||
expect(detachedFrames.length).toBe(0);
|
||||
expect(navigatedFrames.length).toBe(5);
|
||||
|
||||
var attachedFrames = [];
|
||||
var detachedFrames = [];
|
||||
var navigatedFrames = [];
|
||||
await page.navigate(EMPTY_PAGE);
|
||||
expect(attachedFrames.length).toBe(0);
|
||||
expect(detachedFrames.length).toBe(4);
|
||||
expect(navigatedFrames.length).toBe(1);
|
||||
}));
|
||||
});
|
||||
var attachedFrames = [];
|
||||
var detachedFrames = [];
|
||||
var navigatedFrames = [];
|
||||
await page.navigate(EMPTY_PAGE);
|
||||
expect(attachedFrames.length).toBe(0);
|
||||
expect(detachedFrames.length).toBe(4);
|
||||
expect(navigatedFrames.length).toBe(1);
|
||||
}));
|
||||
});
|
||||
});
|
||||
|
||||
// Since Jasmine doesn't like async functions, they should be wrapped
|
||||
// in a SX function.
|
||||
function SX(fun) {
|
||||
return done => Promise.resolve(fun()).then(done).catch(done.fail);
|
||||
return done => Promise.resolve(fun()).then(done).catch(done.fail);
|
||||
}
|
||||
|
@ -25,107 +25,107 @@ var URL = require('url');
|
||||
var CHROMIUM_PATH = path.join(__dirname, '..', '.local-chromium');
|
||||
|
||||
var downloadURLs = {
|
||||
linux: 'https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/%d/chrome-linux.zip',
|
||||
mac: 'https://storage.googleapis.com/chromium-browser-snapshots/Mac/%d/chrome-mac.zip',
|
||||
win32: 'https://storage.googleapis.com/chromium-browser-snapshots/Win/%d/chrome-win32.zip',
|
||||
win64: 'https://storage.googleapis.com/chromium-browser-snapshots/Win_x64/%d/chrome-win32.zip',
|
||||
linux: 'https://storage.googleapis.com/chromium-browser-snapshots/Linux_x64/%d/chrome-linux.zip',
|
||||
mac: 'https://storage.googleapis.com/chromium-browser-snapshots/Mac/%d/chrome-mac.zip',
|
||||
win32: 'https://storage.googleapis.com/chromium-browser-snapshots/Win/%d/chrome-win32.zip',
|
||||
win64: 'https://storage.googleapis.com/chromium-browser-snapshots/Win_x64/%d/chrome-win32.zip',
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
* @return {!Array<string>}
|
||||
*/
|
||||
supportedPlatforms: function() {
|
||||
return Object.keys(downloadURLs);
|
||||
},
|
||||
/**
|
||||
* @return {!Array<string>}
|
||||
*/
|
||||
supportedPlatforms: function() {
|
||||
return Object.keys(downloadURLs);
|
||||
},
|
||||
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
currentPlatform: function() {
|
||||
var platform = os.platform();
|
||||
if (platform === 'darwin')
|
||||
return 'mac';
|
||||
if (platform === 'linux')
|
||||
return 'linux';
|
||||
if (platform === 'win32')
|
||||
return os.arch() === 'x64' ? 'win64' : 'win32';
|
||||
return '';
|
||||
},
|
||||
/**
|
||||
* @return {string}
|
||||
*/
|
||||
currentPlatform: function() {
|
||||
var platform = os.platform();
|
||||
if (platform === 'darwin')
|
||||
return 'mac';
|
||||
if (platform === 'linux')
|
||||
return 'linux';
|
||||
if (platform === 'win32')
|
||||
return os.arch() === 'x64' ? 'win64' : 'win32';
|
||||
return '';
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} platform
|
||||
* @param {string} revision
|
||||
* @return {!Promise<boolean>}
|
||||
*/
|
||||
canDownloadRevision: function(platform, revision) {
|
||||
console.assert(downloadURLs[platform], 'Unknown platform: ' + platform);
|
||||
var url = URL.parse(util.format(downloadURLs[platform], revision));
|
||||
var options = {
|
||||
method: 'HEAD',
|
||||
host: url.host,
|
||||
path: url.pathname,
|
||||
};
|
||||
var resolve;
|
||||
var promise = new Promise(x => resolve = x);
|
||||
var request = https.request(options, response => {
|
||||
resolve(response.statusCode === 200);
|
||||
});
|
||||
request.on('error', error => {
|
||||
console.error(error);
|
||||
resolve(false);
|
||||
});
|
||||
request.end();
|
||||
return promise;
|
||||
},
|
||||
/**
|
||||
* @param {string} platform
|
||||
* @param {string} revision
|
||||
* @return {!Promise<boolean>}
|
||||
*/
|
||||
canDownloadRevision: function(platform, revision) {
|
||||
console.assert(downloadURLs[platform], 'Unknown platform: ' + platform);
|
||||
var url = URL.parse(util.format(downloadURLs[platform], revision));
|
||||
var options = {
|
||||
method: 'HEAD',
|
||||
host: url.host,
|
||||
path: url.pathname,
|
||||
};
|
||||
var resolve;
|
||||
var promise = new Promise(x => resolve = x);
|
||||
var request = https.request(options, response => {
|
||||
resolve(response.statusCode === 200);
|
||||
});
|
||||
request.on('error', error => {
|
||||
console.error(error);
|
||||
resolve(false);
|
||||
});
|
||||
request.end();
|
||||
return promise;
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} platform
|
||||
* @param {string} revision
|
||||
* @param {?function(number, number)} progressCallback
|
||||
* @return {!Promise}
|
||||
*/
|
||||
downloadRevision: async function(platform, revision, progressCallback) {
|
||||
var url = downloadURLs[platform];
|
||||
console.assert(url, `Unsupported platform: ${platform}`);
|
||||
url = util.format(url, revision);
|
||||
var zipPath = path.join(CHROMIUM_PATH, `download-${platform}-${revision}.zip`);
|
||||
var folderPath = getFolderPath(platform, revision);
|
||||
if (fs.existsSync(folderPath))
|
||||
return;
|
||||
try {
|
||||
if (!fs.existsSync(CHROMIUM_PATH))
|
||||
fs.mkdirSync(CHROMIUM_PATH);
|
||||
await downloadFile(url, zipPath, progressCallback);
|
||||
await extractZip(zipPath, folderPath);
|
||||
} finally {
|
||||
if (fs.existsSync(zipPath))
|
||||
fs.unlinkSync(zipPath);
|
||||
}
|
||||
},
|
||||
/**
|
||||
* @param {string} platform
|
||||
* @param {string} revision
|
||||
* @param {?function(number, number)} progressCallback
|
||||
* @return {!Promise}
|
||||
*/
|
||||
downloadRevision: async function(platform, revision, progressCallback) {
|
||||
var url = downloadURLs[platform];
|
||||
console.assert(url, `Unsupported platform: ${platform}`);
|
||||
url = util.format(url, revision);
|
||||
var zipPath = path.join(CHROMIUM_PATH, `download-${platform}-${revision}.zip`);
|
||||
var folderPath = getFolderPath(platform, revision);
|
||||
if (fs.existsSync(folderPath))
|
||||
return;
|
||||
try {
|
||||
if (!fs.existsSync(CHROMIUM_PATH))
|
||||
fs.mkdirSync(CHROMIUM_PATH);
|
||||
await downloadFile(url, zipPath, progressCallback);
|
||||
await extractZip(zipPath, folderPath);
|
||||
} finally {
|
||||
if (fs.existsSync(zipPath))
|
||||
fs.unlinkSync(zipPath);
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {string} platform
|
||||
* @param {string} revision
|
||||
* @return {?{executablePath: string}}
|
||||
*/
|
||||
revisionInfo: function(platform, revision) {
|
||||
var folderPath = getFolderPath(platform, revision);
|
||||
if (!fs.existsSync(folderPath))
|
||||
return null;
|
||||
var executablePath = '';
|
||||
if (platform === 'mac')
|
||||
executablePath = path.join(folderPath, 'chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium');
|
||||
else if (platform === 'linux')
|
||||
executablePath = path.join(folderPath, 'chrome-linux', 'chrome');
|
||||
else if (platform === 'win32' || platform === 'win64')
|
||||
executablePath = path.join(folderPath, 'chrome-win32', 'chrome.exe');
|
||||
else
|
||||
throw 'Unsupported platfrom: ' + platfrom;
|
||||
return {
|
||||
executablePath: executablePath
|
||||
};
|
||||
},
|
||||
/**
|
||||
* @param {string} platform
|
||||
* @param {string} revision
|
||||
* @return {?{executablePath: string}}
|
||||
*/
|
||||
revisionInfo: function(platform, revision) {
|
||||
var folderPath = getFolderPath(platform, revision);
|
||||
if (!fs.existsSync(folderPath))
|
||||
return null;
|
||||
var executablePath = '';
|
||||
if (platform === 'mac')
|
||||
executablePath = path.join(folderPath, 'chrome-mac', 'Chromium.app', 'Contents', 'MacOS', 'Chromium');
|
||||
else if (platform === 'linux')
|
||||
executablePath = path.join(folderPath, 'chrome-linux', 'chrome');
|
||||
else if (platform === 'win32' || platform === 'win64')
|
||||
executablePath = path.join(folderPath, 'chrome-win32', 'chrome.exe');
|
||||
else
|
||||
throw 'Unsupported platfrom: ' + platfrom;
|
||||
return {
|
||||
executablePath: executablePath
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
@ -134,7 +134,7 @@ module.exports = {
|
||||
* @return {string}
|
||||
*/
|
||||
function getFolderPath(platform, revision) {
|
||||
return path.join(CHROMIUM_PATH, platform + '-' + revision);
|
||||
return path.join(CHROMIUM_PATH, platform + '-' + revision);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -144,30 +144,30 @@ function getFolderPath(platform, revision) {
|
||||
* @return {!Promise}
|
||||
*/
|
||||
function downloadFile(url, destinationPath, progressCallback) {
|
||||
var fulfill, reject;
|
||||
var promise = new Promise((x, y) => { fulfill = x; reject = y; });
|
||||
var request = https.get(url, response => {
|
||||
if (response.statusCode !== 200) {
|
||||
var error = new Error(`Download failed: server returned code ${response.statusCode}. URL: ${url}`);
|
||||
// consume response data to free up memory
|
||||
response.resume();
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
var file = fs.createWriteStream(destinationPath);
|
||||
file.on('finish', () => fulfill());
|
||||
file.on('error', error => reject(error));
|
||||
response.pipe(file);
|
||||
var totalBytes = parseInt(response.headers['content-length'], 10);
|
||||
if (progressCallback)
|
||||
response.on('data', onData.bind(null, totalBytes));
|
||||
});
|
||||
request.on('error', error => reject(error));
|
||||
return promise;
|
||||
|
||||
function onData(totalBytes, chunk) {
|
||||
progressCallback(totalBytes, chunk.length);
|
||||
var fulfill, reject;
|
||||
var promise = new Promise((x, y) => { fulfill = x; reject = y; });
|
||||
var request = https.get(url, response => {
|
||||
if (response.statusCode !== 200) {
|
||||
var error = new Error(`Download failed: server returned code ${response.statusCode}. URL: ${url}`);
|
||||
// consume response data to free up memory
|
||||
response.resume();
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
var file = fs.createWriteStream(destinationPath);
|
||||
file.on('finish', () => fulfill());
|
||||
file.on('error', error => reject(error));
|
||||
response.pipe(file);
|
||||
var totalBytes = parseInt(response.headers['content-length'], 10);
|
||||
if (progressCallback)
|
||||
response.on('data', onData.bind(null, totalBytes));
|
||||
});
|
||||
request.on('error', error => reject(error));
|
||||
return promise;
|
||||
|
||||
function onData(totalBytes, chunk) {
|
||||
progressCallback(totalBytes, chunk.length);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -176,5 +176,5 @@ function downloadFile(url, destinationPath, progressCallback) {
|
||||
* @return {!Promise<?Error>}
|
||||
*/
|
||||
function extractZip(zipPath, folderPath) {
|
||||
return new Promise(fulfill => extract(zipPath, {dir: folderPath}, fulfill));
|
||||
return new Promise(fulfill => extract(zipPath, {dir: folderPath}, fulfill));
|
||||
}
|
||||
|
@ -20,43 +20,43 @@ var https = require('https');
|
||||
var OMAHA_PROXY = 'https://omahaproxy.appspot.com/all.json';
|
||||
|
||||
var colors = {
|
||||
reset: '\x1b[0m',
|
||||
red: '\x1b[31m',
|
||||
green: '\x1b[32m',
|
||||
yellow: '\x1b[33m'
|
||||
reset: '\x1b[0m',
|
||||
red: '\x1b[31m',
|
||||
green: '\x1b[32m',
|
||||
yellow: '\x1b[33m'
|
||||
};
|
||||
|
||||
class Table {
|
||||
/**
|
||||
* @param {!Array<number>} columnWidths
|
||||
*/
|
||||
constructor(columnWidths) {
|
||||
this.widths = columnWidths;
|
||||
}
|
||||
/**
|
||||
* @param {!Array<number>} columnWidths
|
||||
*/
|
||||
constructor(columnWidths) {
|
||||
this.widths = columnWidths;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {!Array<string>} values
|
||||
*/
|
||||
drawRow(values) {
|
||||
console.assert(values.length === this.widths.length);
|
||||
var row = '';
|
||||
for (var i = 0; i < values.length; ++i)
|
||||
row += padCenter(values[i], this.widths[i]);
|
||||
console.log(row);
|
||||
}
|
||||
/**
|
||||
* @param {!Array<string>} values
|
||||
*/
|
||||
drawRow(values) {
|
||||
console.assert(values.length === this.widths.length);
|
||||
var row = '';
|
||||
for (var i = 0; i < values.length; ++i)
|
||||
row += padCenter(values[i], this.widths[i]);
|
||||
console.log(row);
|
||||
}
|
||||
}
|
||||
|
||||
if (process.argv.length === 2) {
|
||||
checkOmahaProxyAvailability();
|
||||
return;
|
||||
checkOmahaProxyAvailability();
|
||||
return;
|
||||
}
|
||||
if (process.argv.length !== 4) {
|
||||
console.log(`
|
||||
console.log(`
|
||||
Usage: node check_revisions.js [fromRevision] [toRevision]
|
||||
|
||||
This script checks availability of different prebuild chromium revisions.
|
||||
Running command without arguments will check against omahaproxy revisions.`);
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
var fromRevision = parseInt(process.argv[2], 10);
|
||||
@ -67,27 +67,27 @@ checkRangeAvailability(fromRevision, toRevision);
|
||||
* @return {!Promise}
|
||||
*/
|
||||
async function checkOmahaProxyAvailability() {
|
||||
console.log('Fetching revisions from ' + OMAHA_PROXY);
|
||||
var platforms = await loadJSON(OMAHA_PROXY);
|
||||
if (!platforms) {
|
||||
console.error('ERROR: failed to fetch chromium revisions from omahaproxy.');
|
||||
return;
|
||||
}
|
||||
var table = new Table([27, 7, 7, 7, 7]);
|
||||
table.drawRow([''].concat(Downloader.supportedPlatforms()));
|
||||
for (var platform of platforms) {
|
||||
// Trust only to the main platforms.
|
||||
if (platform.os !== 'mac' && platform.os !== 'win' && platform.os !== 'win64' && platform.os !== 'linux')
|
||||
continue;
|
||||
var osName = platform.os === 'win' ? 'win32' : platform.os;
|
||||
for (var version of platform.versions) {
|
||||
if (version.channel !== 'dev' && version.channel !== 'beta' && version.channel !== 'canary' && version.channel !== 'stable')
|
||||
continue;
|
||||
var revisionName = padLeft('[' + osName + ' ' + version.channel + ']', 15);
|
||||
var revision = parseInt(version.branch_base_position, 10);
|
||||
await checkAndDrawRevisionAvailability(table, revisionName, revision);
|
||||
}
|
||||
console.log('Fetching revisions from ' + OMAHA_PROXY);
|
||||
var platforms = await loadJSON(OMAHA_PROXY);
|
||||
if (!platforms) {
|
||||
console.error('ERROR: failed to fetch chromium revisions from omahaproxy.');
|
||||
return;
|
||||
}
|
||||
var table = new Table([27, 7, 7, 7, 7]);
|
||||
table.drawRow([''].concat(Downloader.supportedPlatforms()));
|
||||
for (var platform of platforms) {
|
||||
// Trust only to the main platforms.
|
||||
if (platform.os !== 'mac' && platform.os !== 'win' && platform.os !== 'win64' && platform.os !== 'linux')
|
||||
continue;
|
||||
var osName = platform.os === 'win' ? 'win32' : platform.os;
|
||||
for (var version of platform.versions) {
|
||||
if (version.channel !== 'dev' && version.channel !== 'beta' && version.channel !== 'canary' && version.channel !== 'stable')
|
||||
continue;
|
||||
var revisionName = padLeft('[' + osName + ' ' + version.channel + ']', 15);
|
||||
var revision = parseInt(version.branch_base_position, 10);
|
||||
await checkAndDrawRevisionAvailability(table, revisionName, revision);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@ -96,11 +96,11 @@ async function checkOmahaProxyAvailability() {
|
||||
* @return {!Promise}
|
||||
*/
|
||||
async function checkRangeAvailability(fromRevision, toRevision) {
|
||||
var table = new Table([10, 7, 7, 7, 7]);
|
||||
table.drawRow([''].concat(Downloader.supportedPlatforms()));
|
||||
var inc = fromRevision < toRevision ? 1 : -1;
|
||||
for (var revision = fromRevision; revision !== toRevision; revision += inc)
|
||||
await checkAndDrawRevisionAvailability(table, '', revision);
|
||||
var table = new Table([10, 7, 7, 7, 7]);
|
||||
table.drawRow([''].concat(Downloader.supportedPlatforms()));
|
||||
var inc = fromRevision < toRevision ? 1 : -1;
|
||||
for (var revision = fromRevision; revision !== toRevision; revision += inc)
|
||||
await checkAndDrawRevisionAvailability(table, '', revision);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -110,18 +110,18 @@ async function checkRangeAvailability(fromRevision, toRevision) {
|
||||
* @return {!Promise}
|
||||
*/
|
||||
async function checkAndDrawRevisionAvailability(table, name, revision) {
|
||||
var promises = [];
|
||||
for (var platform of Downloader.supportedPlatforms())
|
||||
promises.push(Downloader.canDownloadRevision(platform, revision));
|
||||
var availability = await Promise.all(promises);
|
||||
var allAvailable = availability.every(e => !!e);
|
||||
var values = [name + ' ' + (allAvailable ? colors.green + revision + colors.reset : revision)];
|
||||
for (var i = 0; i < availability.length; ++i) {
|
||||
var decoration = availability[i] ? '+' : '-';
|
||||
var color = availability[i] ? colors.green : colors.red;
|
||||
values.push(color + decoration + colors.reset);
|
||||
}
|
||||
table.drawRow(values);
|
||||
var promises = [];
|
||||
for (var platform of Downloader.supportedPlatforms())
|
||||
promises.push(Downloader.canDownloadRevision(platform, revision));
|
||||
var availability = await Promise.all(promises);
|
||||
var allAvailable = availability.every(e => !!e);
|
||||
var values = [name + ' ' + (allAvailable ? colors.green + revision + colors.reset : revision)];
|
||||
for (var i = 0; i < availability.length; ++i) {
|
||||
var decoration = availability[i] ? '+' : '-';
|
||||
var color = availability[i] ? colors.green : colors.red;
|
||||
values.push(color + decoration + colors.reset);
|
||||
}
|
||||
table.drawRow(values);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -129,26 +129,26 @@ async function checkAndDrawRevisionAvailability(table, name, revision) {
|
||||
* @return {!Promise<?Object>}
|
||||
*/
|
||||
function loadJSON(url) {
|
||||
var resolve;
|
||||
var promise = new Promise(x => resolve = x);
|
||||
https.get(url, response => {
|
||||
if (response.statusCode !== 200) {
|
||||
resolve(null);
|
||||
return;
|
||||
}
|
||||
var body = '';
|
||||
response.on('data', function(chunk){
|
||||
body += chunk;
|
||||
});
|
||||
response.on('end', function(){
|
||||
var json = JSON.parse(body);
|
||||
resolve(json);
|
||||
});
|
||||
}).on('error', function(e){
|
||||
console.error('Error fetching json: ' + e);
|
||||
resolve(null);
|
||||
var resolve;
|
||||
var promise = new Promise(x => resolve = x);
|
||||
https.get(url, response => {
|
||||
if (response.statusCode !== 200) {
|
||||
resolve(null);
|
||||
return;
|
||||
}
|
||||
var body = '';
|
||||
response.on('data', function(chunk){
|
||||
body += chunk;
|
||||
});
|
||||
return promise;
|
||||
response.on('end', function(){
|
||||
var json = JSON.parse(body);
|
||||
resolve(json);
|
||||
});
|
||||
}).on('error', function(e){
|
||||
console.error('Error fetching json: ' + e);
|
||||
resolve(null);
|
||||
});
|
||||
return promise;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -156,7 +156,7 @@ function loadJSON(url) {
|
||||
* @return {string}
|
||||
*/
|
||||
function spaceString(size) {
|
||||
return new Array(size).fill(' ').join('');
|
||||
return new Array(size).fill(' ').join('');
|
||||
}
|
||||
|
||||
/**
|
||||
@ -164,11 +164,11 @@ function spaceString(size) {
|
||||
* @return {string}
|
||||
*/
|
||||
function filterOutColors(text) {
|
||||
for (var colorName in colors) {
|
||||
var color = colors[colorName];
|
||||
text = text.replace(color, '');
|
||||
}
|
||||
return text;
|
||||
for (var colorName in colors) {
|
||||
var color = colors[colorName];
|
||||
text = text.replace(color, '');
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -177,8 +177,8 @@ function filterOutColors(text) {
|
||||
* @return {string}
|
||||
*/
|
||||
function padLeft(text, length) {
|
||||
var printableCharacters = filterOutColors(text);
|
||||
return printableCharacters.length >= length ? text : spaceString(length - text.length) + text;
|
||||
var printableCharacters = filterOutColors(text);
|
||||
return printableCharacters.length >= length ? text : spaceString(length - text.length) + text;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -187,10 +187,10 @@ function padLeft(text, length) {
|
||||
* @return {string}
|
||||
*/
|
||||
function padCenter(text, length) {
|
||||
var printableCharacters = filterOutColors(text);
|
||||
if (printableCharacters.length >= length)
|
||||
return text;
|
||||
var left = Math.floor((length - printableCharacters.length) / 2);
|
||||
var right = Math.ceil((length - printableCharacters.length) / 2);
|
||||
return spaceString(left) + text + spaceString(right);
|
||||
var printableCharacters = filterOutColors(text);
|
||||
if (printableCharacters.length >= length)
|
||||
return text;
|
||||
var left = Math.floor((length - printableCharacters.length) / 2);
|
||||
var right = Math.ceil((length - printableCharacters.length) / 2);
|
||||
return spaceString(left) + text + spaceString(right);
|
||||
}
|
||||
|
@ -753,6 +753,10 @@ table@^4.0.1:
|
||||
slice-ansi "0.0.4"
|
||||
string-width "^2.0.0"
|
||||
|
||||
text-diff@^1.0.1:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/text-diff/-/text-diff-1.0.1.tgz#6c105905435e337857375c9d2f6ca63e453ff565"
|
||||
|
||||
text-table@~0.2.0:
|
||||
version "0.2.0"
|
||||
resolved "https://registry.yarnpkg.com/text-table/-/text-table-0.2.0.tgz#7f5ee823ae805207c00af2df4a84ec3fcfa570b4"
|
||||
|
Loading…
Reference in New Issue
Block a user