Skip to content

Commit 57731de

Browse files
gkalpakhansl
authored andcommitted
fix(@angular-devkit/build-angular): hash files as binary
For most files it doesn't make a difference, but for files that are not UTF-8 encoded (such as `*.ico`, `*.png`, etc) converting to string before hashing creates a different digest than what the ServiceWorker will generate (see [here][1]). This in turn causes the SW to think the config is wrong and enter a degraded state. This commit ensures that `ngsw.json` will contain hashes computed in the same way as the SW will compute them. [1]: https://github.com/angular/angular/blob/c8a1a14b87e5907458e8e87021e47f9796cb3257/packages/service-worker/worker/src/assets.ts#L418
1 parent 1fcb744 commit 57731de

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

packages/angular_devkit/build_angular/src/angular-cli-files/utilities/service-worker/index.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,15 @@ class CliFilesystem implements Filesystem {
3737
}
3838

3939
read(path: string): Promise<string> {
40-
return this._host.read(this._resolve(path))
41-
.toPromise()
40+
return this._readIntoBuffer(path)
4241
.then(content => virtualFs.fileBufferToString(content));
4342
}
4443

4544
hash(path: string): Promise<string> {
4645
const sha1 = crypto.createHash('sha1');
4746

48-
return this.read(path)
49-
.then(content => sha1.update(content))
47+
return this._readIntoBuffer(path)
48+
.then(content => sha1.update(Buffer.from(content)))
5049
.then(() => sha1.digest('hex'));
5150
}
5251

@@ -55,6 +54,11 @@ class CliFilesystem implements Filesystem {
5554
.toPromise();
5655
}
5756

57+
private _readIntoBuffer(path: string): Promise<virtualFs.FileBuffer> {
58+
return this._host.read(this._resolve(path))
59+
.toPromise();
60+
}
61+
5862
private _resolve(path: string): Path {
5963
return join(normalize(this.base), path);
6064
}

packages/angular_devkit/build_angular/test/browser/service-worker_spec_large.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ describe('Browser Builder', () => {
9696
],
9797
dataGroups: [],
9898
hashTable: {
99-
'/favicon.ico': '460fcbd48b20fcc32b184388606af1238c890dba',
99+
'/favicon.ico': '84161b857f5c547e3699ddfbffc6d8d737542e01',
100100
'/assets/folder-asset.txt': '617f202968a6a81050aa617c2e28e1dca11ce8d4',
101101
'/index.html': '3e659d6e536916b7d178d02a2e6e5492f868bf68',
102102
},
@@ -151,7 +151,7 @@ describe('Browser Builder', () => {
151151
],
152152
dataGroups: [],
153153
hashTable: {
154-
'/foo/bar/favicon.ico': '460fcbd48b20fcc32b184388606af1238c890dba',
154+
'/foo/bar/favicon.ico': '84161b857f5c547e3699ddfbffc6d8d737542e01',
155155
'/foo/bar/assets/folder-asset.txt': '617f202968a6a81050aa617c2e28e1dca11ce8d4',
156156
'/foo/bar/index.html': '5b53fa9e07e4111b8ef84613fb989a56fee502b0',
157157
},

0 commit comments

Comments
 (0)