From a1fd612b4c3f74238e75b353b2bf901f128b7ca2 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Wed, 21 Dec 2016 20:29:54 +0100 Subject: [PATCH 1/6] Fix captureJSCallUsage in the face of Error polyfills. (error.stack is overwritten) V8 has the property prepareStackTrace on errors where you can format a stacktrace. This only works on native errors not on custom ones. --- lighthouse-core/gather/driver.js | 14 ++++++++------ lighthouse-core/gather/gather-runner.js | 3 ++- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/lighthouse-core/gather/driver.js b/lighthouse-core/gather/driver.js index 155a4b8c81ae..28fa946a33cc 100644 --- a/lighthouse-core/gather/driver.js +++ b/lighthouse-core/gather/driver.js @@ -701,7 +701,7 @@ class Driver { this.evaluateScriptOnLoad(` ${globalVarToPopulate} = new Set(); - (${funcName} = ${funcBody}(${funcName}, ${globalVarToPopulate}))`); + (${funcName} = ${funcBody}(${funcName}, ${globalVarToPopulate}, window.__nativeError))`); return collectUsage; } @@ -712,11 +712,13 @@ class Driver { * @param {function(...*): *} funcRef The function call to track. * @param {!Set} set An empty set to populate with stack traces. Should be * on the global object. + * @param {Error} nativeError The origal error object some libraries overwrite it. + * So they can take full control of errors. * @return {function(...*): *} A wrapper around the original function. */ -function captureJSCallUsage(funcRef, set) { +function captureJSCallUsage(funcRef, set, NativeError) { const originalFunc = funcRef; - const originalPrepareStackTrace = Error.prepareStackTrace; + const originalPrepareStackTrace = nativeError.prepareStackTrace; return function() { // Note: this function runs in the context of the page that is being audited. @@ -724,7 +726,7 @@ function captureJSCallUsage(funcRef, set) { const args = [...arguments]; // callee's arguments. // See v8's Stack Trace API https://github.com/v8/v8/wiki/Stack-Trace-API#customizing-stack-traces - Error.prepareStackTrace = function(error, structStackTrace) { + NativeError.prepareStackTrace = function(error, structStackTrace) { // First frame is the function we injected (the one that just threw). // Second, is the actual callsite of the funcRef we're after. const callFrame = structStackTrace[1]; @@ -747,12 +749,12 @@ function captureJSCallUsage(funcRef, set) { // would be unique. return {url, args, line, col, isEval}; // return value is e.stack }; - const e = new Error(`__called ${funcRef.name}__`); + const e = new NativeError(`__called ${funcRef.name}__`); set.add(JSON.stringify(e.stack)); // Restore prepareStackTrace so future errors use v8's formatter and not // our custom one. - Error.prepareStackTrace = originalPrepareStackTrace; + NativeError.prepareStackTrace = originalPrepareStackTrace; // eslint-disable-next-line no-invalid-this return originalFunc.apply(this, arguments); diff --git a/lighthouse-core/gather/gather-runner.js b/lighthouse-core/gather/gather-runner.js index ae7286670fdf..33977d0d4cd6 100644 --- a/lighthouse-core/gather/gather-runner.js +++ b/lighthouse-core/gather/gather-runner.js @@ -94,7 +94,8 @@ class GatherRunner { return driver.assertNoSameOriginServiceWorkerClients(options.url) .then(_ => driver.beginEmulation(options.flags)) .then(_ => driver.enableRuntimeEvents()) - .then(_ => driver.evaluateScriptOnLoad('window.__nativePromise = Promise;')) + .then(_ => driver.evaluateScriptOnLoad(`window.__nativePromise = Promise; + window.__nativeError = Error;`)) .then(_ => driver.cleanAndDisableBrowserCaches()) .then(_ => driver.clearDataForOrigin(options.url)); } From 01ec31bda4bc20999cd4a5a693daca7211750f94 Mon Sep 17 00:00:00 2001 From: Ward Peeters Date: Wed, 21 Dec 2016 21:08:54 +0100 Subject: [PATCH 2/6] Add dbw test --- lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html | 1 + 1 file changed, 1 insertion(+) diff --git a/lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html b/lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html index 2834f982da3c..aeb7269fcd24 100644 --- a/lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html +++ b/lighthouse-cli/test/fixtures/dobetterweb/dbw_tester.html @@ -88,6 +88,7 @@

Do better web tester page

internal link is ok +