Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
more feedback
  • Loading branch information
patrickhulce committed Nov 30, 2016
commit ca5091dfce9eb5a05cde24633b877782e51d3de1
4 changes: 3 additions & 1 deletion lighthouse-cli/test/fixtures/static-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const parseURL = require('url').parse;
function requestHandler(request, response) {
const filePath = parseURL(request.url).pathname;
const queryString = parseURL(request.url).search;
const absoluteFilePath = path.join(__dirname, filePath);
const absoluteFilePath = filePath === '/dobetterweb/promise_polyfill.js' ?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a comment here for why this is done?

I also feel like this is also going to be a long term resident in static-server, so a full if statement is probably better so it's a bit quicker mentally parse

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

path.join(__dirname, '../../../lighthouse-core/third_party/promise-polyfill/promise.js') :
path.join(__dirname, filePath);

fs.exists(absoluteFilePath, fsExistsCallback);

Expand Down
20 changes: 9 additions & 11 deletions lighthouse-core/gather/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,16 +145,15 @@ class Driver {
);

this.sendCommand('Runtime.evaluate', {
// We need to wrap the raw expression for several purposes
// 1. Ensure that the expression will be a native Promise and not a polyfill/non-Promise.
// 2. Ensure that errors captured in the Promise are converted into plain-old JS Objects
// so that they can be serialized properly b/c JSON.stringify(new Error('foo')) === '{}'
expression: `(function wrapInNativePromise() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this feels like it could be simplified, but are there reasons for everything? Why wrap in a promise constructor and try/catch and Promise.resolve() instead of doing a single promise wrapper?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should also update the function docs

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes each has a purpose

try/catch - for errors that happen outside of promises
Promise.resolve - to enable sync executions
new Promise - to ensure the promise returned is indeed a native promise + avoid inconsistent error handling between sync and async paths

but as I'm typing this just remembering that we opted for Promise.resolve().then( => ), will fix

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wanna add a comment to document this? we're definitely gonna be headscratching if we need to touch this code again. :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done :)

const __nativePromise = window.__nativePromise || Promise;
return new __nativePromise(function(resolve) {
const wrapError = ${wrapRuntimeEvalErrorInBrowser.toString()};
try {
__nativePromise.resolve(${asyncExpression}).then(resolve, wrapError);
} catch (e) {
wrapError(e);
}
});
return __nativePromise.resolve()
.then(_ => ${asyncExpression})
.catch(${wrapRuntimeEvalErrorInBrowser.toString()});
}())`,
includeCommandLineAPI: true,
awaitPromise: true,
Expand Down Expand Up @@ -737,16 +736,15 @@ function captureJSCallUsage(funcRef, set) {
* istanbul ignore next
*/
function wrapRuntimeEvalErrorInBrowser(err) {
/* global resolve */
err = err || new Error();
const fallbackMessage = typeof err === 'string' ? err : 'unknown error';

resolve({
return {
__failedInBrowser: true,
name: err.name || 'Error',
message: err.message || fallbackMessage,
stack: err.stack || (new Error()).stack,
});
};
}

module.exports = Driver;