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.
|
|
|
|
*/
|
2018-08-09 23:51:12 +00:00
|
|
|
const {TimeoutError} = require('./Errors');
|
2017-06-27 19:40:46 +00:00
|
|
|
|
2017-09-11 23:21:51 +00:00
|
|
|
const debugError = require('debug')(`puppeteer:error`);
|
2017-07-27 23:16:37 +00:00
|
|
|
/** @type {?Map<string, boolean>} */
|
|
|
|
let apiCoverage = null;
|
2018-09-05 21:59:14 +00:00
|
|
|
|
2018-09-19 20:54:58 +00:00
|
|
|
/**
|
|
|
|
* @param {!Object} classType
|
|
|
|
* @param {string=} publicName
|
|
|
|
*/
|
|
|
|
function traceAPICoverage(classType, publicName) {
|
|
|
|
if (!apiCoverage)
|
|
|
|
return;
|
|
|
|
|
|
|
|
let className = publicName || classType.prototype.constructor.name;
|
|
|
|
className = className.substring(0, 1).toLowerCase() + className.substring(1);
|
|
|
|
for (const methodName of Reflect.ownKeys(classType.prototype)) {
|
|
|
|
const method = Reflect.get(classType.prototype, methodName);
|
|
|
|
if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function')
|
|
|
|
continue;
|
|
|
|
apiCoverage.set(`${className}.${methodName}`, false);
|
|
|
|
Reflect.set(classType.prototype, methodName, function(...args) {
|
|
|
|
apiCoverage.set(`${className}.${methodName}`, true);
|
|
|
|
return method.call(this, ...args);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (classType.Events) {
|
|
|
|
for (const event of Object.values(classType.Events))
|
|
|
|
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false);
|
|
|
|
const method = Reflect.get(classType.prototype, 'emit');
|
|
|
|
Reflect.set(classType.prototype, 'emit', function(event, ...args) {
|
|
|
|
if (this.listenerCount(event))
|
|
|
|
apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true);
|
|
|
|
return method.call(this, event, ...args);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 19:40:46 +00:00
|
|
|
class Helper {
|
|
|
|
/**
|
2017-10-10 05:31:40 +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)) {
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(args.length === 0, 'Cannot evaluate a string with arguments');
|
2017-10-10 05:31:40 +00:00
|
|
|
return /** @type {string} */ (fun);
|
2017-07-27 19:23:41 +00:00
|
|
|
}
|
2017-09-11 23:59:38 +00:00
|
|
|
return `(${fun})(${args.map(serializeArgument).join(',')})`;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {*} arg
|
|
|
|
* @return {string}
|
|
|
|
*/
|
|
|
|
function serializeArgument(arg) {
|
|
|
|
if (Object.is(arg, undefined))
|
|
|
|
return 'undefined';
|
|
|
|
return JSON.stringify(arg);
|
|
|
|
}
|
2017-06-27 19:40:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-04-07 01:20:48 +00:00
|
|
|
* @param {!Protocol.Runtime.ExceptionDetails} exceptionDetails
|
2017-06-27 19:40:46 +00:00
|
|
|
* @return {string}
|
|
|
|
*/
|
2017-07-21 18:57:25 +00:00
|
|
|
static getExceptionMessage(exceptionDetails) {
|
|
|
|
if (exceptionDetails.exception)
|
2018-06-14 17:23:13 +00:00
|
|
|
return exceptionDetails.exception.description || exceptionDetails.exception.value;
|
2017-07-21 18:57:25 +00:00
|
|
|
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
|
|
|
|
|
|
|
/**
|
2018-04-07 01:20:48 +00:00
|
|
|
* @param {!Protocol.Runtime.RemoteObject} remoteObject
|
2017-10-10 05:31:40 +00:00
|
|
|
* @return {*}
|
2017-07-17 08:57:37 +00:00
|
|
|
*/
|
2017-10-06 22:35:02 +00:00
|
|
|
static valueFromRemoteObject(remoteObject) {
|
2018-05-31 23:53:51 +00:00
|
|
|
assert(!remoteObject.objectId, 'Cannot extract value when objectId is given');
|
2017-07-17 08:57:37 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2017-10-06 22:35:02 +00:00
|
|
|
return remoteObject.value;
|
|
|
|
}
|
|
|
|
|
2017-07-25 04:43:54 +00:00
|
|
|
/**
|
2018-01-11 03:33:22 +00:00
|
|
|
* @param {!Puppeteer.CDPSession} client
|
2018-04-07 01:20:48 +00:00
|
|
|
* @param {!Protocol.Runtime.RemoteObject} remoteObject
|
2017-07-25 04:43:54 +00:00
|
|
|
*/
|
|
|
|
static async releaseObject(client, remoteObject) {
|
|
|
|
if (!remoteObject.objectId)
|
|
|
|
return;
|
2017-09-11 23:21:51 +00:00
|
|
|
await client.send('Runtime.releaseObject', {objectId: remoteObject.objectId}).catch(error => {
|
2017-07-25 04:43:54 +00:00
|
|
|
// 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-09-11 23:21:51 +00:00
|
|
|
debugError(error);
|
|
|
|
});
|
2017-07-17 08:57:37 +00:00
|
|
|
}
|
2017-07-19 03:53:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!Object} classType
|
2018-04-04 21:06:21 +00:00
|
|
|
* @param {string=} publicName
|
2017-07-19 03:53:00 +00:00
|
|
|
*/
|
2018-04-04 21:06:21 +00:00
|
|
|
static tracePublicAPI(classType, publicName) {
|
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);
|
2018-09-19 20:54:58 +00:00
|
|
|
if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function' || method.constructor.name !== 'AsyncFunction')
|
2017-07-19 03:53:00 +00:00
|
|
|
continue;
|
|
|
|
Reflect.set(classType.prototype, methodName, function(...args) {
|
2018-09-19 20:54:58 +00:00
|
|
|
const syncStack = new Error();
|
|
|
|
return method.call(this, ...args).catch(e => {
|
|
|
|
const stack = syncStack.stack.substring(syncStack.stack.indexOf('\n') + 1);
|
|
|
|
const clientStack = stack.substring(stack.indexOf('\n'));
|
|
|
|
if (!e.stack.includes(clientStack))
|
|
|
|
e.stack += '\n -- ASYNC --\n' + stack;
|
|
|
|
throw e;
|
|
|
|
});
|
2017-07-19 03:53:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-19 20:54:58 +00:00
|
|
|
traceAPICoverage(classType, publicName);
|
2017-07-19 03:53:00 +00:00
|
|
|
}
|
2017-07-23 00:03:58 +00:00
|
|
|
|
|
|
|
/**
|
2017-10-10 05:31:40 +00:00
|
|
|
* @param {!NodeJS.EventEmitter} emitter
|
2018-09-14 18:44:54 +00:00
|
|
|
* @param {(string|symbol)} eventName
|
2019-01-11 06:56:39 +00:00
|
|
|
* @param {function(?):void} handler
|
2018-09-14 18:44:54 +00:00
|
|
|
* @return {{emitter: !NodeJS.EventEmitter, eventName: (string|symbol), handler: function(?)}}
|
2017-07-23 00:03:58 +00:00
|
|
|
*/
|
|
|
|
static addEventListener(emitter, eventName, handler) {
|
|
|
|
emitter.on(eventName, handler);
|
|
|
|
return { emitter, eventName, handler };
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-11 06:56:39 +00:00
|
|
|
* @param {!Array<{emitter: !NodeJS.EventEmitter, eventName: (string|symbol), handler: function(?):void}>} listeners
|
2017-07-23 00:03:58 +00:00
|
|
|
*/
|
|
|
|
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-10-11 07:55:48 +00:00
|
|
|
|
|
|
|
static promisify(nodeFunction) {
|
|
|
|
function promisified(...args) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
function callback(err, ...result) {
|
|
|
|
if (err)
|
|
|
|
return reject(err);
|
|
|
|
if (result.length === 1)
|
|
|
|
return resolve(result[0]);
|
|
|
|
return resolve(result);
|
|
|
|
}
|
|
|
|
nodeFunction.call(null, ...args, callback);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return promisified;
|
|
|
|
}
|
2018-07-12 21:36:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {!NodeJS.EventEmitter} emitter
|
|
|
|
* @param {string} eventName
|
|
|
|
* @param {function} predicate
|
|
|
|
* @return {!Promise}
|
|
|
|
*/
|
|
|
|
static waitForEvent(emitter, eventName, predicate, timeout) {
|
|
|
|
let eventTimeout, resolveCallback, rejectCallback;
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
resolveCallback = resolve;
|
|
|
|
rejectCallback = reject;
|
|
|
|
});
|
|
|
|
const listener = Helper.addEventListener(emitter, eventName, event => {
|
|
|
|
if (!predicate(event))
|
|
|
|
return;
|
|
|
|
cleanup();
|
|
|
|
resolveCallback(event);
|
|
|
|
});
|
|
|
|
if (timeout) {
|
|
|
|
eventTimeout = setTimeout(() => {
|
|
|
|
cleanup();
|
2018-08-09 23:51:12 +00:00
|
|
|
rejectCallback(new TimeoutError('Timeout exceeded while waiting for event'));
|
2018-07-12 21:36:31 +00:00
|
|
|
}, timeout);
|
|
|
|
}
|
|
|
|
function cleanup() {
|
|
|
|
Helper.removeEventListeners([listener]);
|
|
|
|
clearTimeout(eventTimeout);
|
|
|
|
}
|
|
|
|
return promise;
|
|
|
|
}
|
2018-10-09 21:16:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @template T
|
|
|
|
* @param {!Promise<T>} promise
|
|
|
|
* @param {string} taskName
|
|
|
|
* @param {number} timeout
|
|
|
|
* @return {!Promise<T>}
|
|
|
|
*/
|
|
|
|
static async waitWithTimeout(promise, taskName, timeout) {
|
|
|
|
let reject;
|
|
|
|
const timeoutError = new TimeoutError(`waiting for ${taskName} failed: timeout ${timeout}ms exceeded`);
|
|
|
|
const timeoutPromise = new Promise((resolve, x) => reject = x);
|
|
|
|
const timeoutTimer = setTimeout(() => reject(timeoutError), timeout);
|
|
|
|
try {
|
|
|
|
return await Promise.race([promise, timeoutPromise]);
|
|
|
|
} finally {
|
|
|
|
clearTimeout(timeoutTimer);
|
|
|
|
}
|
|
|
|
}
|
2017-06-27 19:40:46 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 23:53:51 +00:00
|
|
|
/**
|
|
|
|
* @param {*} value
|
|
|
|
* @param {string=} message
|
|
|
|
*/
|
|
|
|
function assert(value, message) {
|
|
|
|
if (!value)
|
|
|
|
throw new Error(message);
|
|
|
|
}
|
|
|
|
|
2017-09-11 23:21:51 +00:00
|
|
|
module.exports = {
|
|
|
|
helper: Helper,
|
2018-05-31 23:53:51 +00:00
|
|
|
assert,
|
2017-09-11 23:21:51 +00:00
|
|
|
debugError
|
|
|
|
};
|