Skip to content

Commit 67595eb

Browse files
nagahara-naokialan-agius4
authored andcommitted
fix(@angular/build): restore font stylesheet caching
Create the font cache directory before writing and validate cached stylesheets using the same hexadecimal hash format used when saving them. Add regression tests for cache reuse and failure handling. (cherry picked from commit 4673cec)
1 parent 0ee493e commit 67595eb

2 files changed

Lines changed: 96 additions & 3 deletions

File tree

‎packages/angular/build/src/utils/index-file/inline-fonts.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import { createHash } from 'node:crypto';
10-
import { readFile, rm, writeFile } from 'node:fs/promises';
10+
import { mkdir, readFile, rm, writeFile } from 'node:fs/promises';
1111
import { type Agent, get as httpsGet } from 'node:https';
1212
import { join } from 'node:path';
1313
import { NormalizedCachedOptions } from '../normalize-cache';
@@ -181,7 +181,7 @@ export class InlineFontsProcessor {
181181
if (data.length > CONTENT_HASH_LENGTH) {
182182
const storedHash = data.slice(0, CONTENT_HASH_LENGTH);
183183
const content = data.slice(CONTENT_HASH_LENGTH);
184-
const contentHash = createHash(CONTENT_HASH_ALGORITHM).update(content).digest('base64');
184+
const contentHash = createHash(CONTENT_HASH_ALGORITHM).update(content).digest('hex');
185185
if (storedHash === contentHash) {
186186
// Return valid content
187187
return content;
@@ -253,8 +253,9 @@ export class InlineFontsProcessor {
253253
);
254254
});
255255

256-
if (cacheFile) {
256+
if (cacheFile && this.cachePath) {
257257
try {
258+
await mkdir(this.cachePath, { recursive: true });
258259
const dataHash = createHash(CONTENT_HASH_ALGORITHM).update(data).digest('hex');
259260
await writeFile(cacheFile, dataHash + data);
260261
} catch {}

‎packages/angular/build/src/utils/index-file/inline-fonts_spec.ts‎

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,101 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9+
import assert from 'node:assert';
10+
import { createHash } from 'node:crypto';
11+
import { EventEmitter } from 'node:events';
12+
import { existsSync } from 'node:fs';
13+
import { mkdir, mkdtemp, readFile, readdir, rm, writeFile } from 'node:fs/promises';
14+
import type { ClientRequest, IncomingMessage } from 'node:http';
15+
import https from 'node:https';
16+
import { join } from 'node:path';
17+
import { Readable } from 'node:stream';
918
import { InlineFontsProcessor } from './inline-fonts';
1019

1120
describe('InlineFontsProcessor', () => {
21+
describe('disk cache', () => {
22+
const url = 'https://fonts.googleapis.com/css2?family=Roboto';
23+
const content = '@font-face { font-family: Roboto; src: url(roboto.woff2); }';
24+
let tempRoot: string;
25+
let cache: { enabled: boolean; path: string; basePath: string };
26+
let cacheDirectory: string;
27+
let cacheFile: string;
28+
let getSpy: jasmine.Spy<typeof https.get>;
29+
30+
beforeEach(async () => {
31+
const baseTmpDir = process.env['TEST_TMPDIR'];
32+
assert(baseTmpDir, 'TEST_TMPDIR is not set');
33+
tempRoot = await mkdtemp(join(baseTmpDir, 'angular-cli-font-cache-test-'));
34+
cache = { enabled: true, path: join(tempRoot, 'cache'), basePath: tempRoot };
35+
cacheDirectory = join(cache.path, 'angular-build-fonts');
36+
cacheFile = join(cacheDirectory, createHash('sha256').update(url).digest('hex'));
37+
38+
getSpy = spyOn(https, 'get').and.callFake((...args: unknown[]) => {
39+
const callback = args.at(-1);
40+
assert(typeof callback === 'function');
41+
const response = Readable.from([content]) as IncomingMessage;
42+
response.statusCode = 200;
43+
callback(response);
44+
45+
return new EventEmitter() as ClientRequest;
46+
});
47+
});
48+
49+
afterEach(async () => {
50+
if (tempRoot) {
51+
await rm(tempRoot, { recursive: true, force: true });
52+
}
53+
});
54+
55+
it('should create the cache directory and save the stylesheet', async () => {
56+
const processor = new InlineFontsProcessor({ cache });
57+
58+
expect(await processor.processURL(url)).toBe(content);
59+
expect(await readdir(cacheDirectory)).toHaveSize(1);
60+
expect(await readFile(cacheFile, 'utf8')).toBe(
61+
createHash('sha256').update(content).digest('hex') + content,
62+
);
63+
});
64+
65+
it('should reuse the stylesheet across processor instances without another request', async () => {
66+
await mkdir(cacheDirectory, { recursive: true });
67+
expect(await new InlineFontsProcessor({ cache }).processURL(url)).toBe(content);
68+
getSpy.calls.reset();
69+
getSpy.and.throwError('Unexpected font request');
70+
71+
expect(await new InlineFontsProcessor({ cache }).processURL(url)).toBe(content);
72+
expect(getSpy).not.toHaveBeenCalled();
73+
expect(existsSync(cacheFile)).toBeTrue();
74+
});
75+
76+
it('should replace corrupted cache content with a downloaded stylesheet', async () => {
77+
await mkdir(cacheDirectory, { recursive: true });
78+
await writeFile(cacheFile, '0'.repeat(64) + content);
79+
80+
expect(await new InlineFontsProcessor({ cache }).processURL(url)).toBe(content);
81+
expect(getSpy).toHaveBeenCalledTimes(1);
82+
expect(await readFile(cacheFile, 'utf8')).toBe(
83+
createHash('sha256').update(content).digest('hex') + content,
84+
);
85+
});
86+
87+
it('should not cache stylesheets when the cache is disabled', async () => {
88+
const processor = new InlineFontsProcessor({ cache: { ...cache, enabled: false } });
89+
90+
expect(await processor.processURL(url)).toBe(content);
91+
expect(await processor.processURL(url)).toBe(content);
92+
expect(getSpy).toHaveBeenCalledTimes(2);
93+
expect(existsSync(cache.path)).toBeFalse();
94+
});
95+
96+
it('should still return the stylesheet when the cache cannot be written', async () => {
97+
await writeFile(cache.path, 'not a directory');
98+
99+
expect(await new InlineFontsProcessor({ cache }).processURL(url)).toBe(content);
100+
expect(getSpy).toHaveBeenCalledTimes(1);
101+
});
102+
});
103+
12104
describe('Google fonts', () => {
13105
const content = `
14106
<html>

0 commit comments

Comments
 (0)