Home / Function/ loadLocalImage() — astro Function Reference

loadLocalImage() — astro Function Reference

Architecture documentation for the loadLocalImage() function in dev.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  3b8cfb7e_1ed1_deba_4cee_b04f0b3e4a28["loadLocalImage()"]
  2de36a05_ff67_e84f_76ce_b29404d0679b["dev.ts"]
  3b8cfb7e_1ed1_deba_4cee_b04f0b3e4a28 -->|defined in| 2de36a05_ff67_e84f_76ce_b29404d0679b
  e21f9238_a284_456d_7802_7050b57d8259["replaceFileSystemReferences()"]
  3b8cfb7e_1ed1_deba_4cee_b04f0b3e4a28 -->|calls| e21f9238_a284_456d_7802_7050b57d8259
  style 3b8cfb7e_1ed1_deba_4cee_b04f0b3e4a28 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/assets/endpoint/dev.ts lines 14–77

async function loadLocalImage(src: string, url: URL) {
	let returnValue: Buffer | undefined;
	let fsPath: string | undefined;

	// Vite uses /@fs/ to denote filesystem access, but we need to convert that to a real path to load it
	if (src.startsWith('/@fs/')) {
		fsPath = replaceFileSystemReferences(src);
	}

	// Vite only uses the fs config, but the types ask for the full config
	// fsDenyGlob's implementation is internal from https://github.com/vitejs/vite/blob/e6156f71f0e21f4068941b63bcc17b0e9b0a7455/packages/vite/src/node/config.ts#L1931
	if (
		fsPath &&
		isFileLoadingAllowed(
			{
				fsDenyGlob: picomatch(
					// matchBase: true does not work as it's documented
					// https://github.com/micromatch/picomatch/issues/89
					// convert patterns without `/` on our side for now
					viteFSConfig.deny.map((pattern: string) =>
						pattern.includes('/') ? pattern : `**/${pattern}`,
					),
					{
						matchBase: false,
						nocase: true,
						dot: true,
					},
				),
				server: { fs: viteFSConfig },
				safeModulePaths,
			} as unknown as ResolvedConfig & { fsDenyGlob: AnymatchFn; safeModulePaths: Set<string> },
			fsPath,
		)
	) {
		try {
			returnValue = await readFile(fsPath);
		} catch {
			returnValue = undefined;
		}

		// If we couldn't load it directly, try loading it through Vite as a fallback, which will also respect Vite's fs rules
		if (!returnValue) {
			try {
				const res = await fetch(new URL(src, url));

				if (res.ok) {
					returnValue = Buffer.from(await res.arrayBuffer());
				}
			} catch {
				returnValue = undefined;
			}
		}
	} else {
		// Otherwise we'll assume it's a local URL and try to load it via fetch
		const sourceUrl = new URL(src, url.origin);
		// This is only allowed if this is the same origin
		if (sourceUrl.origin !== url.origin) {
			returnValue = undefined;
		}
		return loadRemoteImage(sourceUrl);
	}

	return returnValue;
}

Domain

Subdomains

Frequently Asked Questions

What does loadLocalImage() do?
loadLocalImage() is a function in the astro codebase, defined in packages/astro/src/assets/endpoint/dev.ts.
Where is loadLocalImage() defined?
loadLocalImage() is defined in packages/astro/src/assets/endpoint/dev.ts at line 14.
What does loadLocalImage() call?
loadLocalImage() calls 1 function(s): replaceFileSystemReferences.

Analyze Your Own Codebase

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

Try Supermodel Free