-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DBW: add audit for Date.now() usage #707
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/** | ||
* @license | ||
* Copyright 2016 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. | ||
*/ | ||
|
||
/** | ||
* @fileoverview Audit a page to see if it's using Date.now() (instead of a | ||
* newer API like performance.now()). | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const url = require('url'); | ||
const Audit = require('../audit'); | ||
const Formatter = require('../../formatters/formatter'); | ||
|
||
class NoDateNowAudit extends Audit { | ||
|
||
/** | ||
* @return {!AuditMeta} | ||
*/ | ||
static get meta() { | ||
return { | ||
category: 'JavaScript', | ||
name: 'no-datenow', | ||
description: 'Site does not use Date.now() in its own scripts', | ||
helpText: 'Consider using <a href="https://developer.mozilla.org/en-US/docs/Web/API/Performance/now" target="_blank">performance.now()</a>, which as better precision than <code>Date.now()</code> and always increases at a constant rate, independent of the system clock.', | ||
requiredArtifacts: ['URL', 'DateNowUse'] | ||
}; | ||
} | ||
|
||
/** | ||
* @param {!Artifacts} artifacts | ||
* @return {!AuditResult} | ||
*/ | ||
static audit(artifacts) { | ||
if (typeof artifacts.DateNowUse === 'undefined' || | ||
artifacts.DateNowUse === -1) { | ||
return NoDateNowAudit.generateAuditResult({ | ||
rawValue: -1, | ||
debugString: 'DateNowUse gatherer did not run' | ||
}); | ||
} | ||
|
||
const pageHost = url.parse(artifacts.URL.finalUrl).host; | ||
// Filter out Date.now() usage if script was on another host or an error with | ||
// the same url:line:col combo has already been seen. | ||
const results = artifacts.DateNowUse.dateNowUses.reduce((prev, err) => { | ||
const jsonStr = JSON.stringify(err); | ||
if (url.parse(err.url).host === pageHost && prev.indexOf(jsonStr) === -1) { | ||
prev.push(jsonStr); | ||
} | ||
return prev; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you want to dedupe these so if a function calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
}, []).map(err => { | ||
err = JSON.parse(err); | ||
err.misc = `(line: ${err.line}, col: ${err.col})`; | ||
return err; | ||
}); | ||
|
||
return NoDateNowAudit.generateAuditResult({ | ||
rawValue: results.length === 0, | ||
extendedInfo: { | ||
formatter: Formatter.SUPPORTED_FORMATS.URLLIST, | ||
value: results | ||
} | ||
}); | ||
} | ||
} | ||
|
||
module.exports = NoDateNowAudit; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,8 @@ | |
.http-resource__url { | ||
margin-right: 8px; | ||
} | ||
.http-resource__protocol { | ||
.http-resource__protocol, | ||
.http-resource__misc { | ||
color: #999; | ||
} | ||
</style> | ||
|
@@ -15,7 +16,13 @@ | |
<summary>URLs</summary> | ||
{{#each this}} | ||
<div class="http-resource"> | ||
<span class="http-resource__url">{{this.url}}</span><span class="http-resource__protocol">({{this.protocol}})</span> | ||
<span class="http-resource__url">{{this.url}}</span> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't overload There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
{{#if this.protocol}} | ||
<span class="http-resource__protocol">({{this.protocol}})</span> | ||
{{/if}} | ||
{{#if this.misc}} | ||
<span class="http-resource__misc">{{this.misc}}</span> | ||
{{/if}} | ||
</div> | ||
{{/each}} | ||
</details> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* @license | ||
* Copyright 2016 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. | ||
*/ | ||
|
||
/** | ||
* @fileoverview Tests whether the page is using Date.now(). | ||
*/ | ||
|
||
/* global window, __returnResults */ | ||
|
||
'use strict'; | ||
|
||
const Gatherer = require('../gatherer'); | ||
|
||
function patchDateNow() { | ||
window.__stackTraceErrors = []; | ||
// Override Date.now() so we know when if it's called. | ||
const origDateNow = Date.now; | ||
const originalPrepareStackTrace = Error.prepareStackTrace; | ||
Date.now = function() { | ||
// See v8's Stack Trace API https://github.com/v8/v8/wiki/Stack-Trace-API#customizing-stack-traces | ||
Error.prepareStackTrace = function(error, structStackTrace) { | ||
const lastCallFrame = structStackTrace[structStackTrace.length - 1]; | ||
const file = lastCallFrame.getFileName(); | ||
const line = lastCallFrame.getLineNumber(); | ||
const col = lastCallFrame.getColumnNumber(); | ||
window.__stackTraceErrors.push({url: file, line, col}); | ||
return {url: file, line, col}; // return value populates e.stack. | ||
}; | ||
Error.captureStackTrace(new Error('__called Date.now()__')); | ||
// Remove custom formatter so future results use v8's formatter. | ||
Error.prepareStackTrace = originalPrepareStackTrace; | ||
return origDateNow(); | ||
}; | ||
} | ||
|
||
function collectDateNowUsage() { | ||
__returnResults(window.__stackTraceErrors); | ||
} | ||
|
||
class DateNowUse extends Gatherer { | ||
|
||
beforePass(options) { | ||
return options.driver.evaluateScriptOnLoad(`(${patchDateNow.toString()}())`); | ||
} | ||
|
||
afterPass(options) { | ||
return options.driver.evaluateAsync(`(${collectDateNowUsage.toString()}())`) | ||
.then(dateNowUses => { | ||
this.artifact.dateNowUses = dateNowUses; | ||
}, _ => { | ||
this.artifact = -1; | ||
return; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = DateNowUse; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* Copyright 2016 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. | ||
*/ | ||
'use strict'; | ||
|
||
const DateNowUseAudit = require('../../../audits/dobetterweb/no-datenow.js'); | ||
const assert = require('assert'); | ||
|
||
const URL = 'https://example.com'; | ||
|
||
/* eslint-env mocha */ | ||
|
||
describe('Page does not use Date.now()', () => { | ||
it('fails when no input present', () => { | ||
const auditResult = DateNowUseAudit.audit({}); | ||
assert.equal(auditResult.rawValue, -1); | ||
assert.ok(auditResult.debugString); | ||
}); | ||
|
||
it('passes when Date.now() is not used', () => { | ||
const auditResult = DateNowUseAudit.audit({ | ||
DateNowUse: {dateNowUses: []}, | ||
URL: {finalUrl: URL}, | ||
}); | ||
assert.equal(auditResult.rawValue, true); | ||
assert.equal(auditResult.extendedInfo.value.length, 0); | ||
}); | ||
|
||
it('passes when Date.now() is used on a different origin', () => { | ||
const auditResult = DateNowUseAudit.audit({ | ||
DateNowUse: { | ||
dateNowUses: [ | ||
{url: 'http://different.com/two', line: 2, col: 2}, | ||
{url: 'http://example2.com/two', line: 2, col: 22} | ||
] | ||
}, | ||
URL: {finalUrl: URL}, | ||
}); | ||
assert.equal(auditResult.rawValue, true); | ||
assert.equal(auditResult.extendedInfo.value.length, 0); | ||
}); | ||
|
||
it('fails when Date.now() is used on the origin', () => { | ||
const auditResult = DateNowUseAudit.audit({ | ||
DateNowUse: { | ||
dateNowUses: [ | ||
{url: 'http://example.com/one', line: 1, col: 1}, | ||
{url: 'http://example.com/two', line: 10, col: 1}, | ||
{url: 'http://example2.com/two', line: 2, col: 22} | ||
] | ||
}, | ||
URL: {finalUrl: URL}, | ||
}); | ||
assert.equal(auditResult.rawValue, false); | ||
assert.equal(auditResult.extendedInfo.value.length, 2); | ||
}); | ||
|
||
it('same file:line:col usage is deduped', () => { | ||
const auditResult = DateNowUseAudit.audit({ | ||
DateNowUse: { | ||
dateNowUses: [ | ||
{url: 'http://example.com/dupe', line: 1, col: 1}, | ||
{url: 'http://example.com/dupe', line: 1, col: 1} | ||
] | ||
}, | ||
URL: {finalUrl: URL}, | ||
}); | ||
assert.equal(auditResult.rawValue, false); | ||
assert.equal(auditResult.extendedInfo.value.length, 1); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, this does seem like one of those tests people are going to be annoyed about if they need
Date.now()
for an actual timestamp since the epoch or ifperformance.now()
overhead isn't worth itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@brendankenny not annoyed, but if not Date.now(), what should be use if what you need is not performance? Maybe lighthouse should show recommendations besides just the warning/error?