Home / File/ remoteProbe.ts — astro Source File

remoteProbe.ts — astro Source File

Architecture documentation for remoteProbe.ts, a typescript file in the astro codebase. 3 imports, 0 dependents.

File typescript CoreAstro RenderingEngine 3 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  8fcf1205_21af_084c_62d3_5d2147e26c7d["remoteProbe.ts"]
  ef8a1e3f_e350_75a6_b92d_62a8566d8db9["../core/errors/index.js"]
  8fcf1205_21af_084c_62d3_5d2147e26c7d --> ef8a1e3f_e350_75a6_b92d_62a8566d8db9
  416eda3d_47e6_c298_102b_b7a37d86a44e["../../assets/types.js"]
  8fcf1205_21af_084c_62d3_5d2147e26c7d --> 416eda3d_47e6_c298_102b_b7a37d86a44e
  d6a65eab_8a39_9f47_f7ff_a21a736505c6["./metadata.js"]
  8fcf1205_21af_084c_62d3_5d2147e26c7d --> d6a65eab_8a39_9f47_f7ff_a21a736505c6
  style 8fcf1205_21af_084c_62d3_5d2147e26c7d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { AstroError, AstroErrorData } from '../../core/errors/index.js';
import type { ImageMetadata } from '../types.js';
import { imageMetadata } from './metadata.js';

/**
 * Infers the dimensions of a remote image by streaming its data and analyzing it progressively until sufficient metadata is available.
 *
 * @param {string} url - The URL of the remote image from which to infer size metadata.
 * @return {Promise<Omit<ImageMetadata, 'src' | 'fsPath'>>} Returns a promise that resolves to an object containing the image dimensions metadata excluding `src` and `fsPath`.
 * @throws {AstroError} Thrown when the fetching fails or metadata cannot be extracted.
 */
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

Functions

Dependencies

  • ../../assets/types.js
  • ../core/errors/index.js
  • ./metadata.js

Frequently Asked Questions

What does remoteProbe.ts do?
remoteProbe.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in remoteProbe.ts?
remoteProbe.ts defines 1 function(s): inferRemoteSize.
What does remoteProbe.ts depend on?
remoteProbe.ts imports 3 module(s): ../../assets/types.js, ../core/errors/index.js, ./metadata.js.
Where is remoteProbe.ts in the architecture?
remoteProbe.ts is located at packages/astro/src/assets/utils/remoteProbe.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/assets/utils).

Analyze Your Own Codebase

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

Try Supermodel Free