-
Notifications
You must be signed in to change notification settings - Fork 9.6k
core(lightwallet): add path to budget.json #9453
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
Merged
brendankenny
merged 12 commits into
GoogleChrome:master
from
khempenius:lighthouse_path
Aug 16, 2019
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
aefec6d
Add to budget.json
khempenius 61b0058
Code cleanup re: budget.json path property
khempenius f57d4e1
Test that budget is not mutated
khempenius 21bcebf
Revert fixtures
khempenius 8a5c32d
Revert fixtures
khempenius c9f0cae
Merge branch 'master' of https://github.com/GoogleChrome/lighthouse i…
khempenius 5bf19fc
Merge branch 'master' of https://github.com/GoogleChrome/lighthouse i…
khempenius 154f1a7
Add sample_v2.json
khempenius a9b1b09
Fix ts error
khempenius 4e13de1
Fix ts error
khempenius 88c218a
lightwallet path: code cleanup
khempenius f5f5b7d
Update tests
khempenius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,8 @@ | |
| */ | ||
| 'use strict'; | ||
|
|
||
| const URL = require('../lib/url-shim.js'); | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * @param {unknown} arr | ||
| * @return {arr is Array<Record<string, unknown>>} | ||
|
|
@@ -96,6 +98,97 @@ class Budget { | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Validates that path is properly formed. Verifies the quantity and location | ||
| * of the two robot.txt regex characters: $, * | ||
| * @param {unknown} val | ||
| * @return {string} | ||
| */ | ||
| static validatePath(val) { | ||
| /* | ||
| * If no path is specified the budget is assumed to apply to all pages. This makes | ||
| * budget.json backwards compatible with earlier API versions that did not include path. | ||
| */ | ||
|
|
||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if ((val === undefined) || (val === null)) { | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return '/'; | ||
| } | ||
| const path = /** @type {string} */ (val); | ||
| const hasLeadingSlash = path.startsWith('/'); | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const validWildcardQuantity = (path.match(/\*/g) || []).length <= 1; | ||
| const validDollarSignQuantity = (path.match(/\$/g) || []).length <= 1; | ||
| const validDollarSignPlacement = !path.includes('$') || path.endsWith('$'); | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| const isValid = hasLeadingSlash && validWildcardQuantity | ||
| && validDollarSignQuantity && validDollarSignPlacement; | ||
|
|
||
| if (!isValid) { | ||
| throw new Error(`Invalid path ${path}. ` + | ||
| `'Path' should be specified using the 'robots.txt' format.\n` + | ||
| `Learn more about the 'robots.txt' format here:\n` + | ||
| `https://developers.google.com/search/reference/robots_txt#url-matching-based-on-path-values`); | ||
| } | ||
| return path; | ||
| } | ||
|
|
||
| /** | ||
| * Determines whether a URL matches against a robots.txt-style "path". | ||
| * @param {string} url | ||
| * @param {string} pattern | ||
| * @return {boolean} | ||
| */ | ||
| static urlMatchesPattern(url, pattern) { | ||
|
||
| /* | ||
| * Pattern should use the robots.txt format. E.g. "/*-article.html" or "/". Reference: | ||
| * https://developers.google.com/search/reference/robots_txt#url-matching-based-on-path-values | ||
khempenius marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
|
|
||
| const urlObj = new URL(url); | ||
| const urlPath = urlObj.pathname + urlObj.search; | ||
|
|
||
| const hasWildcard = pattern.includes('*'); | ||
| const hasDollarSign = pattern.includes('$'); | ||
|
|
||
| /** | ||
| * There are 4 different cases of path strings. | ||
| * Paths should have already been validated with #validatePath. | ||
| * | ||
| * Case #1: No special characters | ||
| * Example: "/cat" | ||
| * Behavior: URL should start with given pattern. | ||
| */ | ||
| if (!hasWildcard && !hasDollarSign) { | ||
| return urlPath.startsWith(pattern); | ||
| /** | ||
| * Case #2: $ only | ||
| * Example: "/js$" | ||
| * Behavior: URL should be identical to pattern. | ||
| */ | ||
| } else if (!hasWildcard && hasDollarSign) { | ||
| return urlPath === pattern.slice(0, -1); | ||
| /** | ||
| * Case #3: * only | ||
| * Example: "/vendor*chunk" | ||
| * Behavior: URL should start with the string pattern that comes before the wildcard | ||
| * & later in the string contain the string pattern that comes after the wildcard. | ||
| */ | ||
| } else if (hasWildcard && !hasDollarSign) { | ||
| const [beforeWildcard, afterWildcard] = pattern.split('*'); | ||
| const remainingUrl = urlPath.slice(beforeWildcard.length); | ||
| return urlPath.startsWith(beforeWildcard) && remainingUrl.includes(afterWildcard); | ||
| /** | ||
| * Case #4: $ and * | ||
| * Example: "/vendor*chunk.js$" | ||
| * Behavior: URL should start with the string pattern that comes before the wildcard | ||
| * & end with the string pattern that comes after the wildcard. | ||
| */ | ||
| } else if (hasWildcard && hasDollarSign) { | ||
| const [beforeWildcard, afterWildcard] = pattern.split('*'); | ||
| return urlPath.startsWith(beforeWildcard) && urlPath.endsWith(afterWildcard.slice(0, -1)); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * @param {Record<string, unknown>} timingBudget | ||
| * @return {LH.Budget.TimingBudget} | ||
|
|
@@ -147,9 +240,11 @@ class Budget { | |
| /** @type {LH.Budget} */ | ||
| const budget = {}; | ||
|
|
||
| const {resourceSizes, resourceCounts, timings, ...invalidRest} = b; | ||
| const {path, resourceSizes, resourceCounts, timings, ...invalidRest} = b; | ||
| Budget.assertNoExcessProperties(invalidRest, 'Budget'); | ||
|
|
||
| budget.path = Budget.validatePath(path); | ||
|
|
||
| if (isArrayOfUnknownObjects(resourceSizes)) { | ||
| budget.resourceSizes = resourceSizes.map(Budget.validateResourceBudget); | ||
| Budget.assertNoDuplicateStrings(budget.resourceSizes.map(r => r.resourceType), | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.