1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const {TimeoutError} = require('./Errors');
|
17 |
|
18 | const debugError = require('debug')(`puppeteer:error`);
|
19 |
|
20 | let apiCoverage = null;
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | function traceAPICoverage(classType, publicName) {
|
27 | if (!apiCoverage)
|
28 | return;
|
29 |
|
30 | let className = publicName || classType.prototype.constructor.name;
|
31 | className = className.substring(0, 1).toLowerCase() + className.substring(1);
|
32 | for (const methodName of Reflect.ownKeys(classType.prototype)) {
|
33 | const method = Reflect.get(classType.prototype, methodName);
|
34 | if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function')
|
35 | continue;
|
36 | apiCoverage.set(`${className}.${methodName}`, false);
|
37 | Reflect.set(classType.prototype, methodName, function(...args) {
|
38 | apiCoverage.set(`${className}.${methodName}`, true);
|
39 | return method.call(this, ...args);
|
40 | });
|
41 | }
|
42 |
|
43 | if (classType.Events) {
|
44 | for (const event of Object.values(classType.Events))
|
45 | apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, false);
|
46 | const method = Reflect.get(classType.prototype, 'emit');
|
47 | Reflect.set(classType.prototype, 'emit', function(event, ...args) {
|
48 | if (this.listenerCount(event))
|
49 | apiCoverage.set(`${className}.emit(${JSON.stringify(event)})`, true);
|
50 | return method.call(this, event, ...args);
|
51 | });
|
52 | }
|
53 | }
|
54 |
|
55 | class Helper {
|
56 | |
57 |
|
58 |
|
59 |
|
60 |
|
61 | static evaluationString(fun, ...args) {
|
62 | if (Helper.isString(fun)) {
|
63 | assert(args.length === 0, 'Cannot evaluate a string with arguments');
|
64 | return (fun);
|
65 | }
|
66 | return `(${fun})(${args.map(serializeArgument).join(',')})`;
|
67 |
|
68 | |
69 |
|
70 |
|
71 |
|
72 | function serializeArgument(arg) {
|
73 | if (Object.is(arg, undefined))
|
74 | return 'undefined';
|
75 | return JSON.stringify(arg);
|
76 | }
|
77 | }
|
78 |
|
79 | |
80 |
|
81 |
|
82 |
|
83 | static getExceptionMessage(exceptionDetails) {
|
84 | if (exceptionDetails.exception)
|
85 | return exceptionDetails.exception.description || exceptionDetails.exception.value;
|
86 | let message = exceptionDetails.text;
|
87 | if (exceptionDetails.stackTrace) {
|
88 | for (const callframe of exceptionDetails.stackTrace.callFrames) {
|
89 | const location = callframe.url + ':' + callframe.lineNumber + ':' + callframe.columnNumber;
|
90 | const functionName = callframe.functionName || '<anonymous>';
|
91 | message += `\n at ${functionName} (${location})`;
|
92 | }
|
93 | }
|
94 | return message;
|
95 | }
|
96 |
|
97 | |
98 |
|
99 |
|
100 |
|
101 | static valueFromRemoteObject(remoteObject) {
|
102 | assert(!remoteObject.objectId, 'Cannot extract value when objectId is given');
|
103 | if (remoteObject.unserializableValue) {
|
104 | switch (remoteObject.unserializableValue) {
|
105 | case '-0':
|
106 | return -0;
|
107 | case 'NaN':
|
108 | return NaN;
|
109 | case 'Infinity':
|
110 | return Infinity;
|
111 | case '-Infinity':
|
112 | return -Infinity;
|
113 | default:
|
114 | throw new Error('Unsupported unserializable value: ' + remoteObject.unserializableValue);
|
115 | }
|
116 | }
|
117 | return remoteObject.value;
|
118 | }
|
119 |
|
120 | |
121 |
|
122 |
|
123 |
|
124 | static async releaseObject(client, remoteObject) {
|
125 | if (!remoteObject.objectId)
|
126 | return;
|
127 | await client.send('Runtime.releaseObject', {objectId: remoteObject.objectId}).catch(error => {
|
128 |
|
129 |
|
130 | debugError(error);
|
131 | });
|
132 | }
|
133 |
|
134 | |
135 |
|
136 |
|
137 |
|
138 | static tracePublicAPI(classType, publicName) {
|
139 | for (const methodName of Reflect.ownKeys(classType.prototype)) {
|
140 | const method = Reflect.get(classType.prototype, methodName);
|
141 | if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function' || method.constructor.name !== 'AsyncFunction')
|
142 | continue;
|
143 | Reflect.set(classType.prototype, methodName, function(...args) {
|
144 | const syncStack = new Error();
|
145 | return method.call(this, ...args).catch(e => {
|
146 | const stack = syncStack.stack.substring(syncStack.stack.indexOf('\n') + 1);
|
147 | const clientStack = stack.substring(stack.indexOf('\n'));
|
148 | if (!e.stack.includes(clientStack))
|
149 | e.stack += '\n -- ASYNC --\n' + stack;
|
150 | throw e;
|
151 | });
|
152 | });
|
153 | }
|
154 |
|
155 | traceAPICoverage(classType, publicName);
|
156 | }
|
157 |
|
158 | |
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 | static addEventListener(emitter, eventName, handler) {
|
165 | emitter.on(eventName, handler);
|
166 | return { emitter, eventName, handler };
|
167 | }
|
168 |
|
169 | |
170 |
|
171 |
|
172 | static removeEventListeners(listeners) {
|
173 | for (const listener of listeners)
|
174 | listener.emitter.removeListener(listener.eventName, listener.handler);
|
175 | listeners.splice(0, listeners.length);
|
176 | }
|
177 |
|
178 | |
179 |
|
180 |
|
181 | static publicAPICoverage() {
|
182 | return apiCoverage;
|
183 | }
|
184 |
|
185 | static recordPublicAPICoverage() {
|
186 | apiCoverage = new Map();
|
187 | }
|
188 |
|
189 | |
190 |
|
191 |
|
192 |
|
193 | static isString(obj) {
|
194 | return typeof obj === 'string' || obj instanceof String;
|
195 | }
|
196 |
|
197 | |
198 |
|
199 |
|
200 |
|
201 | static isNumber(obj) {
|
202 | return typeof obj === 'number' || obj instanceof Number;
|
203 | }
|
204 |
|
205 | static promisify(nodeFunction) {
|
206 | function promisified(...args) {
|
207 | return new Promise((resolve, reject) => {
|
208 | function callback(err, ...result) {
|
209 | if (err)
|
210 | return reject(err);
|
211 | if (result.length === 1)
|
212 | return resolve(result[0]);
|
213 | return resolve(result);
|
214 | }
|
215 | nodeFunction.call(null, ...args, callback);
|
216 | });
|
217 | }
|
218 | return promisified;
|
219 | }
|
220 |
|
221 | |
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | static waitForEvent(emitter, eventName, predicate, timeout) {
|
228 | let eventTimeout, resolveCallback, rejectCallback;
|
229 | const promise = new Promise((resolve, reject) => {
|
230 | resolveCallback = resolve;
|
231 | rejectCallback = reject;
|
232 | });
|
233 | const listener = Helper.addEventListener(emitter, eventName, event => {
|
234 | if (!predicate(event))
|
235 | return;
|
236 | cleanup();
|
237 | resolveCallback(event);
|
238 | });
|
239 | if (timeout) {
|
240 | eventTimeout = setTimeout(() => {
|
241 | cleanup();
|
242 | rejectCallback(new TimeoutError('Timeout exceeded while waiting for event'));
|
243 | }, timeout);
|
244 | }
|
245 | function cleanup() {
|
246 | Helper.removeEventListeners([listener]);
|
247 | clearTimeout(eventTimeout);
|
248 | }
|
249 | return promise;
|
250 | }
|
251 |
|
252 | |
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 | static async waitWithTimeout(promise, taskName, timeout) {
|
260 | let reject;
|
261 | const timeoutError = new TimeoutError(`waiting for ${taskName} failed: timeout ${timeout}ms exceeded`);
|
262 | const timeoutPromise = new Promise((resolve, x) => reject = x);
|
263 | const timeoutTimer = setTimeout(() => reject(timeoutError), timeout);
|
264 | try {
|
265 | return await Promise.race([promise, timeoutPromise]);
|
266 | } finally {
|
267 | clearTimeout(timeoutTimer);
|
268 | }
|
269 | }
|
270 | }
|
271 |
|
272 |
|
273 |
|
274 |
|
275 |
|
276 | function assert(value, message) {
|
277 | if (!value)
|
278 | throw new Error(message);
|
279 | }
|
280 |
|
281 | module.exports = {
|
282 | helper: Helper,
|
283 | assert,
|
284 | debugError
|
285 | };
|