Skip to content

Commit 518149d

Browse files
committed
refactor(@angular-devkit/build-angular): use direct fs caching for font inlining
The `cacache` package was only minimally used within the font inlining post-build processing. The usage has now been replaced with direct filesystem access and key hashing to cache any font files. This not only lowers the overall dependency count but also provides a small performance improvement by removing the need to resolve, load, and evaluate additional JavaScript at build time.
1 parent e827c69 commit 518149d

File tree

5 files changed

+42
-25
lines changed

5 files changed

+42
-25
lines changed

package.json

-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
"@types/babel__core": "7.20.1",
9494
"@types/babel__template": "7.4.1",
9595
"@types/browserslist": "^4.15.0",
96-
"@types/cacache": "^15.0.0",
9796
"@types/express": "^4.16.0",
9897
"@types/http-proxy": "^1.17.4",
9998
"@types/ini": "^1.3.31",
@@ -129,7 +128,6 @@
129128
"bootstrap": "^4.0.0",
130129
"browserslist": "^4.21.5",
131130
"buffer": "6.0.3",
132-
"cacache": "17.1.3",
133131
"chokidar": "3.5.3",
134132
"copy-webpack-plugin": "11.0.0",
135133
"critters": "0.0.18",

packages/angular_devkit/build_angular/BUILD.bazel

-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ ts_library(
129129
"@npm//@types/babel__core",
130130
"@npm//@types/babel__template",
131131
"@npm//@types/browserslist",
132-
"@npm//@types/cacache",
133132
"@npm//@types/inquirer",
134133
"@npm//@types/karma",
135134
"@npm//@types/less",
@@ -145,7 +144,6 @@ ts_library(
145144
"@npm//babel-loader",
146145
"@npm//babel-plugin-istanbul",
147146
"@npm//browserslist",
148-
"@npm//cacache",
149147
"@npm//chokidar",
150148
"@npm//copy-webpack-plugin",
151149
"@npm//critters",

packages/angular_devkit/build_angular/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"babel-loader": "9.1.2",
2929
"babel-plugin-istanbul": "6.1.1",
3030
"browserslist": "^4.21.5",
31-
"cacache": "17.1.3",
3231
"chokidar": "3.5.3",
3332
"copy-webpack-plugin": "11.0.0",
3433
"critters": "0.0.18",

packages/angular_devkit/build_angular/src/utils/index-file/inline-fonts.ts

+42-13
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import * as cacache from 'cacache';
10-
import * as fs from 'fs';
11-
import * as https from 'https';
129
import proxyAgent from 'https-proxy-agent';
13-
import { join } from 'path';
14-
import { URL } from 'url';
10+
import { createHash } from 'node:crypto';
11+
import { readFile, rm, writeFile } from 'node:fs/promises';
12+
import * as https from 'node:https';
13+
import { join } from 'node:path';
14+
import { URL } from 'node:url';
1515
import { NormalizedCachedOptions } from '../normalize-cache';
1616
import { VERSION } from '../package-version';
1717
import { htmlRewritingStream } from './html-rewriting-stream';
@@ -34,6 +34,16 @@ const SUPPORTED_PROVIDERS: Record<string, FontProviderDetails> = {
3434
},
3535
};
3636

37+
/**
38+
* Hash algorithm used for cached files.
39+
*/
40+
const CONTENT_HASH_ALGORITHM = 'sha256';
41+
42+
/**
43+
* String length of the SHA-256 content hash stored in cached files.
44+
*/
45+
const CONTENT_HASH_LENGTH = 64;
46+
3747
export class InlineFontsProcessor {
3848
private readonly cachePath: string | undefined;
3949
constructor(private options: InlineFontsOptions) {
@@ -161,13 +171,29 @@ export class InlineFontsProcessor {
161171
}
162172

163173
private async getResponse(url: URL): Promise<string> {
164-
const key = `${VERSION}|${url}`;
165-
174+
let cacheFile;
166175
if (this.cachePath) {
167-
const entry = await cacache.get.info(this.cachePath, key);
168-
if (entry) {
169-
return fs.promises.readFile(entry.path, 'utf8');
170-
}
176+
const key = createHash(CONTENT_HASH_ALGORITHM).update(`${VERSION}|${url}`).digest('hex');
177+
cacheFile = join(this.cachePath, key);
178+
}
179+
180+
if (cacheFile) {
181+
try {
182+
const data = await readFile(cacheFile, 'utf8');
183+
// Check for valid content via stored hash
184+
if (data.length > CONTENT_HASH_LENGTH) {
185+
const storedHash = data.slice(0, CONTENT_HASH_LENGTH);
186+
const content = data.slice(CONTENT_HASH_LENGTH);
187+
const contentHash = createHash(CONTENT_HASH_ALGORITHM).update(content).digest('base64');
188+
if (storedHash === contentHash) {
189+
// Return valid content
190+
return content;
191+
} else {
192+
// Delete corrupted cache content
193+
await rm(cacheFile);
194+
}
195+
}
196+
} catch {}
171197
}
172198

173199
let agent: proxyAgent.HttpsProxyAgent | undefined;
@@ -214,8 +240,11 @@ export class InlineFontsProcessor {
214240
);
215241
});
216242

217-
if (this.cachePath) {
218-
await cacache.put(this.cachePath, key, data);
243+
if (cacheFile) {
244+
try {
245+
const dataHash = createHash(CONTENT_HASH_ALGORITHM).update(data).digest('hex');
246+
await writeFile(cacheFile, dataHash + data);
247+
} catch {}
219248
}
220249

221250
return data;

yarn.lock

-7
Original file line numberDiff line numberDiff line change
@@ -3020,13 +3020,6 @@
30203020
dependencies:
30213021
browserslist "*"
30223022

3023-
"@types/cacache@^15.0.0":
3024-
version "15.0.1"
3025-
resolved "https://registry.yarnpkg.com/@types/cacache/-/cacache-15.0.1.tgz#3d1943cc80ade160c9ae98bd5c1ebcc538f9cd57"
3026-
integrity sha512-JhL2GFJuHMx4RMg4z0XfXB4ZkKdyiOaOLpjoYMXcyKfrkF3IBXNZBj6/Peo9zX/7PPHyfI63NWVD589cI2YTzg==
3027-
dependencies:
3028-
"@types/node" "*"
3029-
30303023
"@types/connect-history-api-fallback@^1.3.5":
30313024
version "1.5.0"
30323025
resolved "https://registry.yarnpkg.com/@types/connect-history-api-fallback/-/connect-history-api-fallback-1.5.0.tgz#9fd20b3974bdc2bcd4ac6567e2e0f6885cb2cf41"

0 commit comments

Comments
 (0)