Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@
"@types/babel__core": "7.20.1",
"@types/babel__template": "7.4.1",
"@types/browserslist": "^4.15.0",
"@types/cacache": "^15.0.0",
"@types/express": "^4.16.0",
"@types/http-proxy": "^1.17.4",
"@types/ini": "^1.3.31",
Expand Down Expand Up @@ -129,7 +128,6 @@
"bootstrap": "^4.0.0",
"browserslist": "^4.21.5",
"buffer": "6.0.3",
"cacache": "17.1.3",
"chokidar": "3.5.3",
"copy-webpack-plugin": "11.0.0",
"critters": "0.0.18",
Expand Down
2 changes: 0 additions & 2 deletions packages/angular_devkit/build_angular/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ ts_library(
"@npm//@types/babel__core",
"@npm//@types/babel__template",
"@npm//@types/browserslist",
"@npm//@types/cacache",
"@npm//@types/inquirer",
"@npm//@types/karma",
"@npm//@types/less",
Expand All @@ -145,7 +144,6 @@ ts_library(
"@npm//babel-loader",
"@npm//babel-plugin-istanbul",
"@npm//browserslist",
"@npm//cacache",
"@npm//chokidar",
"@npm//copy-webpack-plugin",
"@npm//critters",
Expand Down
1 change: 0 additions & 1 deletion packages/angular_devkit/build_angular/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
"babel-loader": "9.1.2",
"babel-plugin-istanbul": "6.1.1",
"browserslist": "^4.21.5",
"cacache": "17.1.3",
"chokidar": "3.5.3",
"copy-webpack-plugin": "11.0.0",
"critters": "0.0.18",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* found in the LICENSE file at https://angular.io/license
*/

import * as cacache from 'cacache';
import * as fs from 'fs';
import * as https from 'https';
import proxyAgent from 'https-proxy-agent';
import { join } from 'path';
import { URL } from 'url';
import { createHash } from 'node:crypto';
import { readFile, rm, writeFile } from 'node:fs/promises';
import * as https from 'node:https';
import { join } from 'node:path';
import { URL } from 'node:url';
import { NormalizedCachedOptions } from '../normalize-cache';
import { VERSION } from '../package-version';
import { htmlRewritingStream } from './html-rewriting-stream';
Expand All @@ -34,6 +34,16 @@ const SUPPORTED_PROVIDERS: Record<string, FontProviderDetails> = {
},
};

/**
* Hash algorithm used for cached files.
*/
const CONTENT_HASH_ALGORITHM = 'sha256';

/**
* String length of the SHA-256 content hash stored in cached files.
*/
const CONTENT_HASH_LENGTH = 64;

export class InlineFontsProcessor {
private readonly cachePath: string | undefined;
constructor(private options: InlineFontsOptions) {
Expand Down Expand Up @@ -161,13 +171,29 @@ export class InlineFontsProcessor {
}

private async getResponse(url: URL): Promise<string> {
const key = `${VERSION}|${url}`;

let cacheFile;
if (this.cachePath) {
const entry = await cacache.get.info(this.cachePath, key);
if (entry) {
return fs.promises.readFile(entry.path, 'utf8');
}
const key = createHash(CONTENT_HASH_ALGORITHM).update(`${VERSION}|${url}`).digest('hex');
cacheFile = join(this.cachePath, key);
}

if (cacheFile) {
try {
const data = await readFile(cacheFile, 'utf8');
// Check for valid content via stored hash
if (data.length > CONTENT_HASH_LENGTH) {
const storedHash = data.slice(0, CONTENT_HASH_LENGTH);
const content = data.slice(CONTENT_HASH_LENGTH);
const contentHash = createHash(CONTENT_HASH_ALGORITHM).update(content).digest('base64');
if (storedHash === contentHash) {
// Return valid content
return content;
} else {
// Delete corrupted cache content
await rm(cacheFile);
}
}
} catch {}
}

let agent: proxyAgent.HttpsProxyAgent | undefined;
Expand Down Expand Up @@ -214,8 +240,11 @@ export class InlineFontsProcessor {
);
});

if (this.cachePath) {
await cacache.put(this.cachePath, key, data);
if (cacheFile) {
try {
const dataHash = createHash(CONTENT_HASH_ALGORITHM).update(data).digest('hex');
await writeFile(cacheFile, dataHash + data);
} catch {}
}

return data;
Expand Down
7 changes: 0 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3305,13 +3305,6 @@
dependencies:
browserslist "*"

"@types/cacache@^15.0.0":
version "15.0.1"
resolved "https://registry.yarnpkg.com/@types/cacache/-/cacache-15.0.1.tgz#3d1943cc80ade160c9ae98bd5c1ebcc538f9cd57"
integrity sha512-JhL2GFJuHMx4RMg4z0XfXB4ZkKdyiOaOLpjoYMXcyKfrkF3IBXNZBj6/Peo9zX/7PPHyfI63NWVD589cI2YTzg==
dependencies:
"@types/node" "*"

"@types/connect-history-api-fallback@^1.3.5":
version "1.5.0"
resolved "https://registry.yarnpkg.com/@types/connect-history-api-fallback/-/connect-history-api-fallback-1.5.0.tgz#9fd20b3974bdc2bcd4ac6567e2e0f6885cb2cf41"
Expand Down