Home / File/ dev.ts — astro Source File

dev.ts — astro Source File

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

File typescript CoreAstro RoutingSystem 7 imports 3 functions

Entity Profile

Dependency Diagram

graph LR
  2de36a05_ff67_e84f_76ce_b29404d0679b["dev.ts"]
  135a8084_d596_67c2_9209_cca6693604e6["../types/public/common.js"]
  2de36a05_ff67_e84f_76ce_b29404d0679b --> 135a8084_d596_67c2_9209_cca6693604e6
  eefa972a_e653_1010_7427_7196d6bd93ad["./shared.js"]
  2de36a05_ff67_e84f_76ce_b29404d0679b --> eefa972a_e653_1010_7427_7196d6bd93ad
  f6b816b8_cbf1_258a_b6ec_69418ae629ce["astro:assets"]
  2de36a05_ff67_e84f_76ce_b29404d0679b --> f6b816b8_cbf1_258a_b6ec_69418ae629ce
  5d6d1861_a18d_b246_cd94_08889ab7e74c["promises"]
  2de36a05_ff67_e84f_76ce_b29404d0679b --> 5d6d1861_a18d_b246_cd94_08889ab7e74c
  b326953c_dc9d_ec9e_dc34_4beead549f6e["node:os"]
  2de36a05_ff67_e84f_76ce_b29404d0679b --> b326953c_dc9d_ec9e_dc34_4beead549f6e
  d0400f4e_b2b2_3254_3d6b_e70ab2075b1d["picomatch"]
  2de36a05_ff67_e84f_76ce_b29404d0679b --> d0400f4e_b2b2_3254_3d6b_e70ab2075b1d
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  2de36a05_ff67_e84f_76ce_b29404d0679b --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style 2de36a05_ff67_e84f_76ce_b29404d0679b fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

// @ts-expect-error
import { safeModulePaths, viteFSConfig } from 'astro:assets';
import { readFile } from 'node:fs/promises';
import os from 'node:os';
import picomatch from 'picomatch';
import { type AnymatchFn, isFileLoadingAllowed, type ResolvedConfig } from 'vite';
import type { APIRoute } from '../../types/public/common.js';
import { handleImageRequest, loadRemoteImage } from './shared.js';

function replaceFileSystemReferences(src: string) {
	return os.platform().includes('win32') ? src.replace(/^\/@fs\//, '') : src.replace(/^\/@fs/, '');
}

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;
}

/**
 * Endpoint used in dev and SSR to serve optimized images by the base image services
 */
export const GET: APIRoute = async ({ request }) => {
	if (!import.meta.env.DEV) {
		console.error('The dev image endpoint can only be used in dev mode.');
		return new Response('Invalid endpoint', { status: 500 });
	}
	try {
		return await handleImageRequest({ request, loadLocalImage });
	} catch (err: unknown) {
		console.error('Could not process image request:', err);
		return new Response(`Could not process image request: ${err}`, {
			status: 500,
		});
	}
};

Domain

Subdomains

Dependencies

  • ../types/public/common.js
  • ./shared.js
  • astro:assets
  • node:os
  • picomatch
  • promises
  • vite

Frequently Asked Questions

What does dev.ts do?
dev.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 dev.ts?
dev.ts defines 3 function(s): GET, loadLocalImage, replaceFileSystemReferences.
What does dev.ts depend on?
dev.ts imports 7 module(s): ../types/public/common.js, ./shared.js, astro:assets, node:os, picomatch, promises, vite.
Where is dev.ts in the architecture?
dev.ts is located at packages/astro/src/assets/endpoint/dev.ts (domain: CoreAstro, subdomain: RoutingSystem, 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