Home / Class/ CachedFontFetcher Class — astro Architecture

CachedFontFetcher Class — astro Architecture

Architecture documentation for the CachedFontFetcher class in cached-font-fetcher.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  767f384c_0dbf_0041_0eb2_69933f4c7602["CachedFontFetcher"]
  475b9c94_90d9_2e4a_30c3_527d7b3eb75e["cached-font-fetcher.ts"]
  767f384c_0dbf_0041_0eb2_69933f4c7602 -->|defined in| 475b9c94_90d9_2e4a_30c3_527d7b3eb75e
  288cb6c0_b8f2_bcd1_934d_77cbb892bb57["constructor()"]
  767f384c_0dbf_0041_0eb2_69933f4c7602 -->|method| 288cb6c0_b8f2_bcd1_934d_77cbb892bb57
  f13528c3_0413_1df5_d9e5_f45828f576da["storage()"]
  767f384c_0dbf_0041_0eb2_69933f4c7602 -->|method| f13528c3_0413_1df5_d9e5_f45828f576da
  1433ecdf_5efc_6b47_6da2_e0c4bcebae21["fetch()"]
  767f384c_0dbf_0041_0eb2_69933f4c7602 -->|method| 1433ecdf_5efc_6b47_6da2_e0c4bcebae21

Relationship Graph

Source Code

packages/astro/src/assets/fonts/infra/cached-font-fetcher.ts lines 6–57

export class CachedFontFetcher implements FontFetcher {
	readonly #storage: Storage;
	readonly #fetch: (url: string, init?: RequestInit) => Promise<Response>;
	readonly #readFile: (url: string) => Promise<Buffer>;

	constructor({
		storage,
		fetch,
		readFile,
	}: {
		storage: Storage;
		fetch: (url: string, init?: RequestInit) => Promise<Response>;
		readFile: (url: string) => Promise<Buffer>;
	}) {
		this.#storage = storage;
		this.#fetch = fetch;
		this.#readFile = readFile;
	}

	async #cache(storage: Storage, key: string, cb: () => Promise<Buffer>): Promise<Buffer> {
		const existing = await storage.getItemRaw(key);
		if (existing) {
			return existing;
		}
		const data = await cb();
		await storage.setItemRaw(key, data);
		return data;
	}

	async fetch({ id, url, init }: FontFileData): Promise<Buffer> {
		return await this.#cache(this.#storage, id, async () => {
			try {
				if (isAbsolute(url)) {
					return await this.#readFile(url);
				}
				const response = await this.#fetch(url, init ?? undefined);
				if (!response.ok) {
					throw new Error(`Response was not successful, received status code ${response.status}`);
				}
				return Buffer.from(await response.arrayBuffer());
			} catch (cause) {
				throw new AstroError(
					{
						...AstroErrorData.CannotFetchFontFile,
						message: AstroErrorData.CannotFetchFontFile.message(url),
					},
					{ cause },
				);
			}
		});
	}
}

Domain

Frequently Asked Questions

What is the CachedFontFetcher class?
CachedFontFetcher is a class in the astro codebase, defined in packages/astro/src/assets/fonts/infra/cached-font-fetcher.ts.
Where is CachedFontFetcher defined?
CachedFontFetcher is defined in packages/astro/src/assets/fonts/infra/cached-font-fetcher.ts at line 6.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free