Home / File/ image-binding-transform.ts — astro Source File

image-binding-transform.ts — astro Source File

Architecture documentation for image-binding-transform.ts, a typescript file in the astro codebase. 4 imports, 0 dependents.

File typescript CoreAstro RoutingSystem 4 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  e6a86237_c7e3_5b64_faf5_c771aa8ce203["image-binding-transform.ts"]
  f6b816b8_cbf1_258a_b6ec_69418ae629ce["astro:assets"]
  e6a86237_c7e3_5b64_faf5_c771aa8ce203 --> f6b816b8_cbf1_258a_b6ec_69418ae629ce
  e4df8f29_fb2f_3d70_a962_fdf6a3670b22["path"]
  e6a86237_c7e3_5b64_faf5_c771aa8ce203 --> e4df8f29_fb2f_3d70_a962_fdf6a3670b22
  21f9daf1_9979_0313_afee_bbb8465c9f69["remote"]
  e6a86237_c7e3_5b64_faf5_c771aa8ce203 --> 21f9daf1_9979_0313_afee_bbb8465c9f69
  ce97c362_f912_74f0_5c37_7a64c5d8a8be["workers-types"]
  e6a86237_c7e3_5b64_faf5_c771aa8ce203 --> ce97c362_f912_74f0_5c37_7a64c5d8a8be
  style e6a86237_c7e3_5b64_faf5_c771aa8ce203 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { imageConfig } from 'astro:assets';
import { isRemotePath } from '@astrojs/internal-helpers/path';
import { isRemoteAllowed } from '@astrojs/internal-helpers/remote';
import type { ImageTransform } from '@cloudflare/workers-types';

export async function transform(
	rawUrl: string,
	images: ImagesBinding,
	assets: Fetcher,
): Promise<Response> {
	const url = new URL(rawUrl);

	const href = url.searchParams.get('href');

	if (!href || (isRemotePath(href) && !isRemoteAllowed(href, imageConfig))) {
		return new Response('Forbidden', { status: 403 });
	}

	const imageSrc = new URL(href, url.origin);
	const content = await (isRemotePath(href) ? fetch(imageSrc) : assets.fetch(imageSrc));
	if (!content.body) {
		return new Response(null, { status: 404 });
	}
	const input = images.input(content.body);

	const format = url.searchParams.get('f');

	if (!format || !['avif', 'webp', 'jpeg'].includes(format)) {
		return new Response(`The "${format}" format is not supported`, { status: 400 });
	}

	return (
		await input
			.transform({
				width: url.searchParams.has('w') ? parseInt(url.searchParams.get('w')!) : undefined,
				height: url.searchParams.has('h') ? parseInt(url.searchParams.get('h')!) : undefined,
				// `quality` is documented, but doesn't appear to work in manual testing...
				// quality: url.searchParams.get('q'),
				fit: url.searchParams.get('fit') as ImageTransform['fit'],
			})
			.output({ format: `image/${format as 'webp' | 'avif' | 'jpeg'}` })
	).response();
}

Domain

Subdomains

Functions

Dependencies

  • astro:assets
  • path
  • remote
  • workers-types

Frequently Asked Questions

What does image-binding-transform.ts do?
image-binding-transform.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RoutingSystem subdomain.
What functions are defined in image-binding-transform.ts?
image-binding-transform.ts defines 1 function(s): transform.
What does image-binding-transform.ts depend on?
image-binding-transform.ts imports 4 module(s): astro:assets, path, remote, workers-types.
Where is image-binding-transform.ts in the architecture?
image-binding-transform.ts is located at packages/integrations/cloudflare/src/utils/image-binding-transform.ts (domain: CoreAstro, subdomain: RoutingSystem, directory: packages/integrations/cloudflare/src/utils).

Analyze Your Own Codebase

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

Try Supermodel Free