Home / File/ node.ts — astro Source File

node.ts — astro Source File

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

File typescript CoreAstro RenderingEngine 7 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  b4396eb3_3c48_69c3_9cdf_b53ef92bae86["node.ts"]
  135a8084_d596_67c2_9209_cca6693604e6["../types/public/common.js"]
  b4396eb3_3c48_69c3_9cdf_b53ef92bae86 --> 135a8084_d596_67c2_9209_cca6693604e6
  eefa972a_e653_1010_7427_7196d6bd93ad["./shared.js"]
  b4396eb3_3c48_69c3_9cdf_b53ef92bae86 --> eefa972a_e653_1010_7427_7196d6bd93ad
  f6b816b8_cbf1_258a_b6ec_69418ae629ce["astro:assets"]
  b4396eb3_3c48_69c3_9cdf_b53ef92bae86 --> f6b816b8_cbf1_258a_b6ec_69418ae629ce
  5d6d1861_a18d_b246_cd94_08889ab7e74c["promises"]
  b4396eb3_3c48_69c3_9cdf_b53ef92bae86 --> 5d6d1861_a18d_b246_cd94_08889ab7e74c
  c52a5f83_66e3_37d7_9ebb_767f7129bc62["node:path"]
  b4396eb3_3c48_69c3_9cdf_b53ef92bae86 --> c52a5f83_66e3_37d7_9ebb_767f7129bc62
  d9a92db9_c95e_9165_13ac_24b3d859d946["node:url"]
  b4396eb3_3c48_69c3_9cdf_b53ef92bae86 --> d9a92db9_c95e_9165_13ac_24b3d859d946
  e4df8f29_fb2f_3d70_a962_fdf6a3670b22["path"]
  b4396eb3_3c48_69c3_9cdf_b53ef92bae86 --> e4df8f29_fb2f_3d70_a962_fdf6a3670b22
  style b4396eb3_3c48_69c3_9cdf_b53ef92bae86 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

// @ts-expect-error
import { outDir, serverDir } from 'astro:assets';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { isParentDirectory } from '@astrojs/internal-helpers/path';
import type { APIRoute } from '../../types/public/common.js';
import { handleImageRequest } from './shared.js';

async function loadLocalImage(src: string, url: URL) {
	const outDirURL = resolveOutDir();
	// If the _image segment isn't at the start of the path, we have a base
	const idx = url.pathname.indexOf('/_image');
	if (idx > 0) {
		// Remove the base path
		src = src.slice(idx);
	}
	if (!URL.canParse('.' + src, outDirURL)) {
		return undefined;
	}
	const fileUrl = new URL('.' + src, outDirURL);
	if (fileUrl.protocol !== 'file:') {
		return undefined;
	}
	if (!isParentDirectory(fileURLToPath(outDirURL), fileURLToPath(fileUrl))) {
		return undefined;
	}

	try {
		return await readFile(fileUrl);
	} catch {
		return undefined;
	}
}

/**
 * Endpoint used in dev and SSR to serve optimized images by the base image services
 */
export const GET: APIRoute = async ({ request }) => {
	try {
		return await handleImageRequest({ request, loadLocalImage });
	} catch (err: unknown) {
		console.error('Could not process image request:', err);
		return new Response('Internal Server Error', {
			status: 500,
		});
	}
};

function resolveOutDir() {
	const serverDirPath = fileURLToPath(serverDir);
	const rel = path.relative(serverDirPath, fileURLToPath(outDir));

	const serverFolder = path.basename(serverDirPath);
	let serverEntryFolderURL = path.dirname(import.meta.url);
	while (!serverEntryFolderURL.endsWith(serverFolder)) {
		serverEntryFolderURL = path.dirname(serverEntryFolderURL);
	}
	const serverEntryURL = serverEntryFolderURL + '/entry.mjs';
	const outDirURL = new URL(appendForwardSlash(rel), serverEntryURL);
	return outDirURL;
}

function appendForwardSlash(pth: string) {
	return pth.endsWith('/') ? pth : pth + '/';
}

Domain

Subdomains

Dependencies

  • ../types/public/common.js
  • ./shared.js
  • astro:assets
  • node:path
  • node:url
  • path
  • promises

Frequently Asked Questions

What does node.ts do?
node.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 node.ts?
node.ts defines 4 function(s): GET, appendForwardSlash, loadLocalImage, resolveOutDir.
What does node.ts depend on?
node.ts imports 7 module(s): ../types/public/common.js, ./shared.js, astro:assets, node:path, node:url, path, promises.
Where is node.ts in the architecture?
node.ts is located at packages/astro/src/assets/endpoint/node.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