refactor(service-worker): sort manifest `url`/`hashTable` entries (#23586)

This makes it easier to quickly check whether a specific file ended up
in the manifest, for example when debugging.

PR Close #23586
This commit is contained in:
George Kalpakas 2018-04-28 02:18:35 +03:00 committed by Igor Minar
parent e0ed59e55f
commit f1e4a153f0
2 changed files with 22 additions and 16 deletions

View File

@ -27,13 +27,15 @@ export class Generator {
constructor(readonly fs: Filesystem, private baseHref: string) {}
async process(config: Config): Promise<Object> {
const hashTable = {};
const unorderedHashTable = {};
const assetGroups = await this.processAssetGroups(config, unorderedHashTable);
return {
configVersion: 1,
appData: config.appData,
index: joinUrls(this.baseHref, config.index),
assetGroups: await this.processAssetGroups(config, hashTable),
dataGroups: this.processDataGroups(config), hashTable,
index: joinUrls(this.baseHref, config.index), assetGroups,
dataGroups: this.processDataGroups(config),
hashTable: withOrderedKeys(unorderedHashTable),
navigationUrls: processNavigationUrls(this.baseHref, config.navigationUrls),
};
}
@ -45,16 +47,17 @@ export class Generator {
const fileMatcher = globListToMatcher(group.resources.files || []);
const versionedMatcher = globListToMatcher(group.resources.versionedFiles || []);
const allFiles = (await this.fs.list('/'));
const versionedFiles = allFiles.filter(versionedMatcher).filter(file => !seenMap.has(file));
versionedFiles.forEach(file => seenMap.add(file));
const allFiles = await this.fs.list('/');
const plainFiles = allFiles.filter(fileMatcher).filter(file => !seenMap.has(file));
plainFiles.forEach(file => seenMap.add(file));
const versionedFiles = allFiles.filter(versionedMatcher).filter(file => !seenMap.has(file));
versionedFiles.forEach(file => seenMap.add(file));
// Add the hashes.
await[...versionedFiles, ...plainFiles].reduce(async(previous, file) => {
const matchedFiles = [...plainFiles, ...versionedFiles].sort();
await matchedFiles.reduce(async(previous, file) => {
await previous;
const hash = await this.fs.hash(file);
hashTable[joinUrls(this.baseHref, file)] = hash;
@ -64,10 +67,7 @@ export class Generator {
name: group.name,
installMode: group.installMode || 'prefetch',
updateMode: group.updateMode || group.installMode || 'prefetch',
urls: ([] as string[])
.concat(plainFiles)
.concat(versionedFiles)
.map(url => joinUrls(this.baseHref, url)),
urls: matchedFiles.map(url => joinUrls(this.baseHref, url)),
patterns: (group.resources.urls || []).map(url => urlToRegex(url, this.baseHref)),
};
}));
@ -141,3 +141,9 @@ function joinUrls(a: string, b: string): string {
}
return a + b;
}
function withOrderedKeys<T extends{[key: string]: any}>(unorderedObj: T): T {
const orderedObj = {} as T;
Object.keys(unorderedObj).sort().forEach(key => orderedObj[key] = unorderedObj[key]);
return orderedObj;
}

View File

@ -74,8 +74,8 @@ import {MockFilesystem} from '../testing/mock';
installMode: 'prefetch',
updateMode: 'prefetch',
urls: [
'/test/index.html',
'/test/foo/test.html',
'/test/index.html',
'/test/test.txt',
],
patterns: [
@ -102,9 +102,9 @@ import {MockFilesystem} from '../testing/mock';
{positive: false, regex: '^http:\\/\\/example\\.com\\/excluded$'},
],
hashTable: {
'/test/test.txt': '18f6f8eb7b1c23d2bb61bff028b83d867a9e4643',
'/test/foo/test.html': '18f6f8eb7b1c23d2bb61bff028b83d867a9e4643',
'/test/index.html': 'a54d88e06612d820bc3be72877c74f257b561b19',
'/test/foo/test.html': '18f6f8eb7b1c23d2bb61bff028b83d867a9e4643'
'/test/test.txt': '18f6f8eb7b1c23d2bb61bff028b83d867a9e4643'
}
});
done();