Skip to content

Commit

Permalink
fix(utils): more aggressive non-deterministic replacements
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Nov 8, 2019
1 parent eaffaf2 commit c4a2c40
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
16 changes: 12 additions & 4 deletions packages/utils/src/audit-diff-finder.js
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,18 @@ function synthesizeItemKeyDiffs(diffs, baseItems, compareItems) {

/** @param {string} s */
function replaceNondeterministicStrings(s) {
return s
.replace(/\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/gi, 'UUID')
.replace(/:[0-9]{3,5}\//, ':PORT/')
.replace(/\.[0-9a-f]{8}\.(js|css|woff|html|png|jpeg|jpg|svg)/, '.HASH.$1');
return (
s
// YouTube Embeds
.replace(/www-embed-player-[0-9a-z]+/i, 'www-embed-player')
.replace(/player_ias-[0-9a-z]+/i, 'player_ias')
// UUIDs
.replace(/\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/gi, 'UUID')
// localhost Ports
.replace(/:[0-9]{3,5}\//, ':PORT/')
// Hash components embedded in filenames
.replace(/(\.|-)[0-9a-f]{8}\.(js|css|woff|html|png|jpeg|jpg|svg)/i, '$1HASH.$2')
);
}

/** @param {Record<string, any>} item @return {string} */
Expand Down
12 changes: 11 additions & 1 deletion packages/utils/test/audit-diff-finder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,19 @@ describe('#replaceNondeterministicStrings', () => {
);
});

it('should replace YouTube embeds', () => {
expect(
replaceNondeterministicStrings('/yts/jsbin/www-embed-player-vfl7uF46t/www-embed-player.js')
).toEqual('/yts/jsbin/www-embed-player/www-embed-player.js');
expect(replaceNondeterministicStrings('/yts/jsbin/player_ias-vflyrg3IP/en_US/base.js')).toEqual(
'/yts/jsbin/player_ias/en_US/base.js'
);
});

it('should replace hash parts', () => {
expect(replaceNondeterministicStrings('foo.12345678.js')).toEqual('foo.HASH.js');
expect(replaceNondeterministicStrings('foo.abcdef12.js')).toEqual('foo.HASH.js');
expect(replaceNondeterministicStrings('foo.abcdef12.woff2')).toEqual('foo.HASH.woff2');
expect(replaceNondeterministicStrings('foo-abcdef12.css')).toEqual('foo-HASH.css');
});

it('should replace ports', () => {
Expand Down

0 comments on commit c4a2c40

Please sign in to comment.