-
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 4 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,78 @@ | ||
/** | ||
* @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 from scripts on other domains. | ||
const results = artifacts.DateNowUse.errors.reduce((prev, err) => { | ||
if (url.parse(err.url).host === pageHost) { | ||
err.url = `${JSON.stringify(err)}`; | ||
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. you shouldn't modify the artifact itself here (and should pick a more descriptive property name once the formatter is changed) 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 |
||
prev.push(err); | ||
} | ||
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 |
||
}, []); | ||
|
||
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 |
---|---|---|
|
@@ -15,7 +15,10 @@ | |
<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}} | ||
</div> | ||
{{/each}} | ||
</details> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* @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 orig = Date.now; | ||
Date.now = function() { | ||
try { | ||
throw Error('__called Date.now()__'); | ||
} catch (e) { | ||
if (e.stack) { | ||
const split = e.stack.split('\n'); | ||
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. HOLD UP WAIT A MINUTE https://github.com/v8/v8/wiki/Stack-Trace-API#customizing-stack-traces ⬆️ ⬆️ ⬆️ ⬆️ ⬆️ ⬆️ ⬆️ ⬆️ 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 |
||
const m = split[split.length - 1].match(/(https?:\/\/.*):(\d+):(\d+)/); | ||
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. I tried out this date.now monkeypatch in the console, but it led to "Error: __called Date.now()__
at Error (native)
at Function.Date.now (<anonymous>:4:13)
at <anonymous>:1:6" The match() then returns null. I think you could make the regex testing here a bit more liberal. 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 with the ST API |
||
if (m) { | ||
window.__stackTraceErrors.push({url: m[1], line: m[2], col: m[3]}); | ||
} | ||
} | ||
} | ||
|
||
return orig(); | ||
}; | ||
} | ||
|
||
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(errors => { | ||
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. maybe something like 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 |
||
this.artifact.errors = errors; | ||
}, _ => { | ||
this.artifact = -1; | ||
return; | ||
}); | ||
} | ||
} | ||
|
||
module.exports = DateNowUse; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* 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: {errors: []}, | ||
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: { | ||
errors: [ | ||
{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: '2'} | ||
] | ||
}, | ||
URL: {finalUrl: URL}, | ||
}); | ||
assert.equal(auditResult.rawValue, false); | ||
assert.equal(auditResult.extendedInfo.value.length, 2); | ||
}); | ||
|
||
it('passes when Date.now() is only used on a different origin', () => { | ||
const auditResult = DateNowUseAudit.audit({ | ||
DateNowUse: { | ||
errors: [ | ||
{url: 'http://different.com/two', line: '2', col: '2'} | ||
] | ||
}, | ||
URL: {finalUrl: URL}, | ||
}); | ||
assert.equal(auditResult.rawValue, true); | ||
assert.equal(auditResult.extendedInfo.value.length, 0); | ||
}); | ||
}); |
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?