Home / File/ cached-font-fetcher.ts — astro Source File

cached-font-fetcher.ts — astro Source File

Architecture documentation for cached-font-fetcher.ts, a typescript file in the astro codebase. 4 imports, 0 dependents.

Entity Profile

Dependency Diagram

graph LR
  475b9c94_90d9_2e4a_30c3_527d7b3eb75e["cached-font-fetcher.ts"]
  ef8a1e3f_e350_75a6_b92d_62a8566d8db9["../core/errors/index.js"]
  475b9c94_90d9_2e4a_30c3_527d7b3eb75e --> ef8a1e3f_e350_75a6_b92d_62a8566d8db9
  e92fda44_4a5d_b83b_1a45_958cdae6c91b["./definitions.js"]
  475b9c94_90d9_2e4a_30c3_527d7b3eb75e --> e92fda44_4a5d_b83b_1a45_958cdae6c91b
  ea387312_d0a2_bb52_dafc_3872f7307f95["../../src/assets/fonts/types.js"]
  475b9c94_90d9_2e4a_30c3_527d7b3eb75e --> ea387312_d0a2_bb52_dafc_3872f7307f95
  c52a5f83_66e3_37d7_9ebb_767f7129bc62["node:path"]
  475b9c94_90d9_2e4a_30c3_527d7b3eb75e --> c52a5f83_66e3_37d7_9ebb_767f7129bc62
  style 475b9c94_90d9_2e4a_30c3_527d7b3eb75e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { isAbsolute } from 'node:path';
import { AstroError, AstroErrorData } from '../../../core/errors/index.js';
import type { FontFetcher, Storage } from '../definitions.js';
import type { FontFileData } from '../types.js';

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

Subdomains

Dependencies

  • ../../src/assets/fonts/types.js
  • ../core/errors/index.js
  • ./definitions.js
  • node:path

Frequently Asked Questions

What does cached-font-fetcher.ts do?
cached-font-fetcher.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What does cached-font-fetcher.ts depend on?
cached-font-fetcher.ts imports 4 module(s): ../../src/assets/fonts/types.js, ../core/errors/index.js, ./definitions.js, node:path.
Where is cached-font-fetcher.ts in the architecture?
cached-font-fetcher.ts is located at packages/astro/src/assets/fonts/infra/cached-font-fetcher.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/assets/fonts/infra).

Analyze Your Own Codebase

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

Try Supermodel Free