|
6 | 6 | * found in the LICENSE file at https://angular.dev/license |
7 | 7 | */ |
8 | 8 |
|
| 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'; |
9 | 18 | import { InlineFontsProcessor } from './inline-fonts'; |
10 | 19 |
|
11 | 20 | 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 | + |
12 | 104 | describe('Google fonts', () => { |
13 | 105 | const content = ` |
14 | 106 | <html> |
|
0 commit comments