From 734c752fea3877a9776856095add1df0f2dd93ef Mon Sep 17 00:00:00 2001 From: Dmitry Gozman Date: Thu, 16 Jan 2020 17:48:38 -0800 Subject: [PATCH] chore: rework log api calls functionality to not replace objects (#518) --- index.js | 2 +- src/dom.ts | 1 - src/frames.ts | 2 -- src/helper.ts | 36 ++++++++++++++---------------------- src/javascript.ts | 2 -- src/page.ts | 2 -- 6 files changed, 15 insertions(+), 30 deletions(-) diff --git a/index.js b/index.js index 644f5b919f..4d8e9ca4a4 100644 --- a/index.js +++ b/index.js @@ -20,7 +20,7 @@ const packageJson = require('./package.json'); for (const className in api) { if (typeof api[className] === 'function') - helper.installAsyncStackHooks(api[className]); + helper.installApiHooks(className, api[className]); } module.exports = { diff --git a/src/dom.ts b/src/dom.ts index b17933f90f..8f43d09032 100644 --- a/src/dom.ts +++ b/src/dom.ts @@ -123,7 +123,6 @@ export class ElementHandle extends js.JSHandle { super(context, remoteObject); this._context = context; this._page = context.frame._page; - return helper.logPublicApiCalls('handle', this); } asElement(): ElementHandle | null { diff --git a/src/frames.ts b/src/frames.ts index 3597961b95..fccb725512 100644 --- a/src/frames.ts +++ b/src/frames.ts @@ -303,8 +303,6 @@ export class Frame { if (this._parentFrame) this._parentFrame._childFrames.add(this); - - return helper.logPublicApiCalls('frame', this); } async goto(url: string, options?: GotoOptions): Promise { diff --git a/src/helper.ts b/src/helper.ts index 8644653c3f..062736bc11 100644 --- a/src/helper.ts +++ b/src/helper.ts @@ -41,12 +41,24 @@ class Helper { } } - static installAsyncStackHooks(classType: any) { + static installApiHooks(className: string, classType: any) { + const log = platform.debug('pw:api'); 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' || method.constructor.name !== 'AsyncFunction') + if (methodName === 'constructor' || typeof methodName !== 'string' || methodName.startsWith('_') || typeof method !== 'function') + continue; + const isAsync = method.constructor.name === 'AsyncFunction'; + if (!isAsync && !log.enabled) continue; Reflect.set(classType.prototype, methodName, function(this: any, ...args: any[]) { + if (log.enabled) { + if (args.length) + log(`${className}.${methodName} %o`, args); + else + log(`${className}.${methodName}`); + } + if (!isAsync) + return method.call(this, ...args); const syncStack: any = {}; Error.captureStackTrace(syncStack); return method.call(this, ...args).catch((e: any) => { @@ -60,26 +72,6 @@ class Helper { } } - static logPublicApiCalls(className: string, object: any): any { - const log = platform.debug('pw:api'); - if (!log.enabled) - return object; - return new Proxy(object, { - get(target, key: string) { - const value = target[key]; - if (typeof key === 'string' && key.startsWith('_') || typeof value !== 'function') - return value; - return function(this: any, ...args: any) { - if (args.length) - log(`${className}.${key} %o`, args); - else - log(`${className}.${key}`); - return value.apply(this, args); - }; - } - }); - } - static addEventListener( emitter: platform.EventEmitterType, eventName: (string | symbol), diff --git a/src/javascript.ts b/src/javascript.ts index 3b693e22dc..0b25f68607 100644 --- a/src/javascript.ts +++ b/src/javascript.ts @@ -16,7 +16,6 @@ import * as types from './types'; import * as dom from './dom'; -import { helper } from './helper'; export interface ExecutionContextDelegate { evaluate(context: ExecutionContext, returnByValue: boolean, pageFunction: string | Function, ...args: any[]): Promise; @@ -58,7 +57,6 @@ export class JSHandle { constructor(context: ExecutionContext, remoteObject: any) { this._context = context; this._remoteObject = remoteObject; - return helper.logPublicApiCalls('handle', this); } evaluate: types.EvaluateOn = (pageFunction, ...args) => { diff --git a/src/page.ts b/src/page.ts index 1cfe984a91..041dfc4677 100644 --- a/src/page.ts +++ b/src/page.ts @@ -139,7 +139,6 @@ export class Page extends platform.EventEmitter { if (delegate.pdf) this.pdf = delegate.pdf.bind(delegate); this.coverage = delegate.coverage(); - return helper.logPublicApiCalls('page', this); } _didClose() { @@ -539,7 +538,6 @@ export class Worker { this._url = url; this._executionContextCallback = () => {}; this._executionContextPromise = new Promise(x => this._executionContextCallback = x); - return helper.logPublicApiCalls('worker', this); } _createExecutionContext(delegate: js.ExecutionContextDelegate) {