2017-06-27 19:40:46 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2017 Google Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2017-07-27 23:16:37 +00:00
|
|
|
/** @type {?Map<string, boolean>} */
|
|
|
|
let apiCoverage = null;
|
2017-06-27 19:40:46 +00:00
|
|
|
class Helper {
|
|
|
|
/**
|
2017-07-27 19:23:41 +00:00
|
|
|
* @param {function()|string} fun
|
2017-06-27 19:40:46 +00:00
|
|
|
* @param {!Array<*>} args
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
static evaluationString(fun, ...args) {
|
2017-07-27 22:17:43 +00:00
|
|
|
if (Helper.isString(fun)) {
|
2017-07-27 19:23:41 +00:00
|
|
|
console.assert(args.length === 0, 'Cannot evaluate a string with arguments');
|
|
|
|
return fun;
|
|
|
|
}
|
2017-06-27 19:40:46 +00:00
|
|
|
return `(${fun})(${args.map(x => JSON.stringify(x)).join(',')})`;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} exceptionDetails
|
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-07-21 18:57:25 +00:00
|
|
|
static getExceptionMessage(exceptionDetails) {
|
|
|
|
if (exceptionDetails.exception)
|
|
|
|
return exceptionDetails.exception.description;
|
|
|
|
let message = exceptionDetails.text;
|
2017-06-27 19:40:46 +00:00
|
|
|
if (exceptionDetails.stackTrace) {
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const callframe of exceptionDetails.stackTrace.callFrames) {
|
|
|
|
const location = callframe.url + ':' + callframe.lineNumber + ':' + callframe.columnNumber;
|
|
|
|
const functionName = callframe.functionName || '<anonymous>';
|
2017-06-27 19:40:46 +00:00
|
|
|
message += `\n at ${functionName} (${location})`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return message;
|
|
|
|
}
|
2017-07-17 08:57:37 +00:00
|
|
|
|
|
|
|
/**
|
2017-08-09 23:14:00 +00:00
|
|
|
* @param {!Session} client
|
2017-07-17 08:57:37 +00:00
|
|
|
* @param {!Object} remoteObject
|
2017-07-25 04:43:54 +00:00
|
|
|
* @return {!Promise<!Object>}
|
2017-07-17 08:57:37 +00:00
|
|
|
*/
|
|
|
|
static async serializeRemoteObject(client, remoteObject) {
|
|
|
|
if (remoteObject.unserializableValue) {
|
|
|
|
switch (remoteObject.unserializableValue) {
|
|
|
|
case '-0':
|
|
|
|
return -0;
|
|
|
|
case 'NaN':
|
|
|
|
return NaN;
|
|
|
|
case 'Infinity':
|
|
|
|
return Infinity;
|
|
|
|
case '-Infinity':
|
|
|
|
return -Infinity;
|
|
|
|
default:
|
|
|
|
throw new Error('Unsupported unserializable value: ' + remoteObject.unserializableValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!remoteObject.objectId)
|
|
|
|
return remoteObject.value;
|
2017-08-16 00:35:59 +00:00
|
|
|
if (remoteObject.subtype === 'promise')
|
|
|
|
return remoteObject.description;
|
2017-07-18 07:03:52 +00:00
|
|
|
try {
|
2017-08-21 23:39:04 +00:00
|
|
|
const response = await client.send('Runtime.callFunctionOn', {
|
2017-07-18 07:03:52 +00:00
|
|
|
objectId: remoteObject.objectId,
|
|
|
|
functionDeclaration: 'function() { return this; }',
|
|
|
|
returnByValue: true,
|
|
|
|
});
|
|
|
|
return response.result.value;
|
|
|
|
} catch (e) {
|
|
|
|
// Return description for unserializable object, e.g. 'window'.
|
|
|
|
return remoteObject.description;
|
|
|
|
} finally {
|
2017-07-25 04:43:54 +00:00
|
|
|
Helper.releaseObject(client, remoteObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-08-09 23:14:00 +00:00
|
|
|
* @param {!Session} client
|
2017-07-25 04:43:54 +00:00
|
|
|
* @param {!Object} remoteObject
|
|
|
|
*/
|
|
|
|
static async releaseObject(client, remoteObject) {
|
|
|
|
if (!remoteObject.objectId)
|
|
|
|
return;
|
|
|
|
try {
|
|
|
|
await client.send('Runtime.releaseObject', {objectId: remoteObject.objectId});
|
|
|
|
} catch (e) {
|
|
|
|
// Exceptions might happen in case of a page been navigated or closed.
|
|
|
|
// Swallow these since they are harmless and we don't leak anything in this case.
|
2017-07-18 07:03:52 +00:00
|
|
|
}
|
2017-07-17 08:57:37 +00:00
|
|
|
}
|
2017-07-19 03:53:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} classType
|
|
|
|
*/
|
|
|
|
static tracePublicAPI(classType) {
|
|
|
|
let className = classType.prototype.constructor.name;
|
|
|
|
className = className.substring(0, 1).toLowerCase() + className.substring(1);
|
|
|
|
const debug = require('debug')(`puppeteer:${className}`);
|
2017-07-27 23:16:37 +00:00
|
|
|
if (!debug.enabled && !apiCoverage)
|
2017-07-19 03:53:00 +00:00
|
|
|
return;
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const methodName of Reflect.ownKeys(classType.prototype)) {
|
2017-07-19 03:53:00 +00:00
|
|
|
const method = Reflect.get(classType.prototype, methodName);
|
2017-07-26 18:48:40 +00:00
|
|
|
if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function')
|
2017-07-19 03:53:00 +00:00
|
|
|
continue;
|
2017-07-27 23:16:37 +00:00
|
|
|
if (apiCoverage)
|
|
|
|
apiCoverage.set(`${className}.${methodName}`, false);
|
2017-07-19 03:53:00 +00:00
|
|
|
Reflect.set(classType.prototype, methodName, function(...args) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const argsText = args.map(stringifyArgument).join(', ');
|
|
|
|
const callsite = `${className}.${methodName}(${argsText})`;
|
2017-07-27 23:16:37 +00:00
|
|
|
if (debug.enabled)
|
|
|
|
debug(callsite);
|
|
|
|
if (apiCoverage)
|
|
|
|
apiCoverage.set(`${className}.${methodName}`, true);
|
2017-07-19 03:53:00 +00:00
|
|
|
return method.call(this, ...args);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-28 18:45:05 +00:00
|
|
|
if (classType.Events) {
|
|
|
|
if (apiCoverage) {
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const event of Object.values(classType.Events))
|
2017-07-28 18:45:05 +00:00
|
|
|
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false);
|
|
|
|
}
|
|
|
|
const method = Reflect.get(classType.prototype, 'emit');
|
|
|
|
Reflect.set(classType.prototype, 'emit', function(event, ...args) {
|
2017-08-21 23:39:04 +00:00
|
|
|
const argsText = [JSON.stringify(event)].concat(args.map(stringifyArgument)).join(', ');
|
2017-08-03 22:20:31 +00:00
|
|
|
if (debug.enabled && this.listenerCount(event))
|
2017-07-28 18:45:05 +00:00
|
|
|
debug(`${className}.emit(${argsText})`);
|
|
|
|
if (apiCoverage && this.listenerCount(event))
|
|
|
|
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true);
|
|
|
|
return method.call(this, event, ...args);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-19 03:53:00 +00:00
|
|
|
/**
|
|
|
|
* @param {!Object} arg
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function stringifyArgument(arg) {
|
2017-07-29 20:02:56 +00:00
|
|
|
if (Helper.isString(arg) || Helper.isNumber(arg) || !arg)
|
|
|
|
return JSON.stringify(arg);
|
|
|
|
if (typeof arg === 'function') {
|
|
|
|
let text = arg.toString().split('\n').map(line => line.trim()).join('');
|
|
|
|
if (text.length > 20)
|
|
|
|
text = text.substring(0, 20) + '…';
|
|
|
|
return `"${text}"`;
|
2017-07-28 18:45:05 +00:00
|
|
|
}
|
2017-07-29 20:02:56 +00:00
|
|
|
const state = {};
|
|
|
|
const keys = Object.keys(arg);
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const key of keys) {
|
2017-07-29 20:02:56 +00:00
|
|
|
const value = arg[key];
|
|
|
|
if (Helper.isString(value) || Helper.isNumber(value))
|
|
|
|
state[key] = JSON.stringify(value);
|
|
|
|
}
|
|
|
|
const name = arg.constructor.name === 'Object' ? '' : arg.constructor.name;
|
|
|
|
return name + JSON.stringify(state);
|
2017-07-19 03:53:00 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-23 00:03:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!EventEmitter} emitter
|
|
|
|
* @param {string} eventName
|
|
|
|
* @param {function(?)} handler
|
|
|
|
* @return {{emitter: !EventEmitter, eventName: string, handler: function(?)}}
|
|
|
|
*/
|
|
|
|
static addEventListener(emitter, eventName, handler) {
|
|
|
|
emitter.on(eventName, handler);
|
|
|
|
return { emitter, eventName, handler };
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Array<{emitter: !EventEmitter, eventName: string, handler: function(?)}>}
|
|
|
|
*/
|
|
|
|
static removeEventListeners(listeners) {
|
2017-08-21 23:39:04 +00:00
|
|
|
for (const listener of listeners)
|
2017-07-23 00:03:58 +00:00
|
|
|
listener.emitter.removeListener(listener.eventName, listener.handler);
|
|
|
|
listeners.splice(0, listeners.length);
|
|
|
|
}
|
2017-07-27 23:16:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return {?Map<string, boolean>}
|
|
|
|
*/
|
|
|
|
static publicAPICoverage() {
|
|
|
|
return apiCoverage;
|
|
|
|
}
|
|
|
|
|
|
|
|
static recordPublicAPICoverage() {
|
|
|
|
apiCoverage = new Map();
|
|
|
|
}
|
2017-07-27 22:17:43 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} obj
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
static isString(obj) {
|
|
|
|
return typeof obj === 'string' || obj instanceof String;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} obj
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
static isNumber(obj) {
|
|
|
|
return typeof obj === 'number' || obj instanceof Number;
|
|
|
|
}
|
2017-06-27 19:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Helper;
|