Home / Function/ inferRemoteSize() — astro Function Reference

inferRemoteSize() — astro Function Reference

Architecture documentation for the inferRemoteSize() function in remoteProbe.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  7d478df5_9a18_d08a_27d6_00f07adcbb69["inferRemoteSize()"]
  8fcf1205_21af_084c_62d3_5d2147e26c7d["remoteProbe.ts"]
  7d478df5_9a18_d08a_27d6_00f07adcbb69 -->|defined in| 8fcf1205_21af_084c_62d3_5d2147e26c7d
  style 7d478df5_9a18_d08a_27d6_00f07adcbb69 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/assets/utils/remoteProbe.ts lines 12–63

export async function inferRemoteSize(url: string): Promise<Omit<ImageMetadata, 'src' | 'fsPath'>> {
	// Start fetching the image
	const response = await fetch(url);
	if (!response.body || !response.ok) {
		throw new AstroError({
			...AstroErrorData.FailedToFetchRemoteImageDimensions,
			message: AstroErrorData.FailedToFetchRemoteImageDimensions.message(url),
		});
	}

	const reader = response.body.getReader();

	let done: boolean | undefined, value: Uint8Array;
	let accumulatedChunks = new Uint8Array();

	// Process the stream chunk by chunk
	while (!done) {
		const readResult = await reader.read();
		done = readResult.done;

		if (done) break;

		if (readResult.value) {
			value = readResult.value;

			// Accumulate chunks
			let tmp = new Uint8Array(accumulatedChunks.length + value.length);
			tmp.set(accumulatedChunks, 0);
			tmp.set(value, accumulatedChunks.length);
			accumulatedChunks = tmp;

			try {
				// Attempt to determine the size with each new chunk
				const dimensions = await imageMetadata(accumulatedChunks, url);

				if (dimensions) {
					await reader.cancel(); // stop stream as we have size now

					return dimensions;
				}
			} catch {
				// This catch block is specifically for `imageMetadata` errors
				// which might occur if the accumulated data isn't yet sufficient.
			}
		}
	}

	throw new AstroError({
		...AstroErrorData.NoImageMetadata,
		message: AstroErrorData.NoImageMetadata.message(url),
	});
}

Domain

Subdomains

Frequently Asked Questions

What does inferRemoteSize() do?
inferRemoteSize() is a function in the astro codebase, defined in packages/astro/src/assets/utils/remoteProbe.ts.
Where is inferRemoteSize() defined?
inferRemoteSize() is defined in packages/astro/src/assets/utils/remoteProbe.ts at line 12.

Analyze Your Own Codebase

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

Try Supermodel Free