Home / File/ shared.ts — astro Source File

shared.ts — astro Source File

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

File typescript CoreAstro RenderingEngine 6 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  6a8f5c45_bf5b_34c9_0e8c_3118f0ca359e["shared.ts"]
  fe123607_0a2d_03dc_5e2c_f2f078ab4d7d["../internal.js"]
  6a8f5c45_bf5b_34c9_0e8c_3118f0ca359e --> fe123607_0a2d_03dc_5e2c_f2f078ab4d7d
  53084806_f991_1240_2e28_c545d7506087["../utils/etag.js"]
  6a8f5c45_bf5b_34c9_0e8c_3118f0ca359e --> 53084806_f991_1240_2e28_c545d7506087
  f6b816b8_cbf1_258a_b6ec_69418ae629ce["astro:assets"]
  6a8f5c45_bf5b_34c9_0e8c_3118f0ca359e --> f6b816b8_cbf1_258a_b6ec_69418ae629ce
  e4df8f29_fb2f_3d70_a962_fdf6a3670b22["path"]
  6a8f5c45_bf5b_34c9_0e8c_3118f0ca359e --> e4df8f29_fb2f_3d70_a962_fdf6a3670b22
  21f9daf1_9979_0313_afee_bbb8465c9f69["remote"]
  6a8f5c45_bf5b_34c9_0e8c_3118f0ca359e --> 21f9daf1_9979_0313_afee_bbb8465c9f69
  d5d79daf_16b4_bece_5a98_63bc92fc1431["mrmime"]
  6a8f5c45_bf5b_34c9_0e8c_3118f0ca359e --> d5d79daf_16b4_bece_5a98_63bc92fc1431
  style 6a8f5c45_bf5b_34c9_0e8c_3118f0ca359e fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

// @ts-expect-error
import { imageConfig } from 'astro:assets';
import { isRemotePath, removeQueryString } from '@astrojs/internal-helpers/path';
import { isRemoteAllowed } from '@astrojs/internal-helpers/remote';
import * as mime from 'mrmime';
import { getConfiguredImageService } from '../internal.js';
import { etag } from '../utils/etag.js';

export async function loadRemoteImage(src: URL): Promise<Buffer | undefined> {
	try {
		const res = await fetch(src);

		if (!res.ok) {
			return undefined;
		}

		return Buffer.from(await res.arrayBuffer());
	} catch {
		return undefined;
	}
}

export const handleImageRequest = async ({
	request,
	loadLocalImage,
}: {
	request: Request;
	loadLocalImage: (src: string, baseUrl: URL) => Promise<Buffer | undefined>;
}) => {
	const imageService = await getConfiguredImageService();

	if (!('transform' in imageService)) {
		throw new Error('Configured image service is not a local service');
	}

	const url = new URL(request.url);
	const transform = await imageService.parseURL(url, imageConfig);

	if (!transform?.src) {
		return new Response('Invalid request', { status: 400 });
	}

	let inputBuffer: Buffer | undefined = undefined;

	if (isRemotePath(transform.src)) {
		if (!isRemoteAllowed(transform.src, imageConfig)) {
			return new Response('Forbidden', { status: 403 });
		}

		inputBuffer = await loadRemoteImage(new URL(transform.src));
	} else {
		inputBuffer = await loadLocalImage(removeQueryString(transform.src), url);
	}

	if (!inputBuffer) {
		return new Response('Internal Server Error', { status: 500 });
	}

	const { data, format } = await imageService.transform(inputBuffer, transform, imageConfig);

	return new Response(data as Uint8Array<ArrayBuffer>, {
		status: 200,
		headers: {
			'Content-Type': mime.lookup(format) ?? `image/${format}`,
			'Cache-Control': 'public, max-age=31536000',
			ETag: etag(data.toString()),
			Date: new Date().toUTCString(),
		},
	});
};

Domain

Subdomains

Dependencies

  • ../internal.js
  • ../utils/etag.js
  • astro:assets
  • mrmime
  • path
  • remote

Frequently Asked Questions

What does shared.ts do?
shared.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 shared.ts?
shared.ts defines 2 function(s): handleImageRequest, loadRemoteImage.
What does shared.ts depend on?
shared.ts imports 6 module(s): ../internal.js, ../utils/etag.js, astro:assets, mrmime, path, remote.
Where is shared.ts in the architecture?
shared.ts is located at packages/astro/src/assets/endpoint/shared.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/assets/endpoint).

Analyze Your Own Codebase

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

Try Supermodel Free