Home / File/ base.ts — astro Source File

base.ts — astro Source File

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

File typescript CoreAstro RenderingEngine 9 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  3d5d82b6_fa01_d874_f359_b0b54bb2e974["base.ts"]
  d3861967_b647_84d2_ff48_15013353bd56["../core/logger/core.js"]
  3d5d82b6_fa01_d874_f359_b0b54bb2e974 --> d3861967_b647_84d2_ff48_15013353bd56
  6b38722c_7b48_0d95_7927_ae62c3991719["../template/4xx.js"]
  3d5d82b6_fa01_d874_f359_b0b54bb2e974 --> 6b38722c_7b48_0d95_7927_ae62c3991719
  e9b74c5a_8d34_34a7_e196_5e41b87214aa["../types/astro.js"]
  3d5d82b6_fa01_d874_f359_b0b54bb2e974 --> e9b74c5a_8d34_34a7_e196_5e41b87214aa
  e6a60d16_a973_8dde_286e_5c65924c8e9f["./response.js"]
  3d5d82b6_fa01_d874_f359_b0b54bb2e974 --> e6a60d16_a973_8dde_286e_5c65924c8e9f
  e16a223b_37f3_6b25_1ee1_2b7bcb9d9415["node:fs"]
  3d5d82b6_fa01_d874_f359_b0b54bb2e974 --> e16a223b_37f3_6b25_1ee1_2b7bcb9d9415
  c52a5f83_66e3_37d7_9ebb_767f7129bc62["node:path"]
  3d5d82b6_fa01_d874_f359_b0b54bb2e974 --> c52a5f83_66e3_37d7_9ebb_767f7129bc62
  e4df8f29_fb2f_3d70_a962_fdf6a3670b22["path"]
  3d5d82b6_fa01_d874_f359_b0b54bb2e974 --> e4df8f29_fb2f_3d70_a962_fdf6a3670b22
  10250468_0e83_bd69_43e9_3bcef2294a91["piccolore"]
  3d5d82b6_fa01_d874_f359_b0b54bb2e974 --> 10250468_0e83_bd69_43e9_3bcef2294a91
  263e522e_1aa5_ebc3_e7d6_45ebc51671f7["vite"]
  3d5d82b6_fa01_d874_f359_b0b54bb2e974 --> 263e522e_1aa5_ebc3_e7d6_45ebc51671f7
  style 3d5d82b6_fa01_d874_f359_b0b54bb2e974 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import * as fs from 'node:fs';
import path from 'node:path';
import { appendForwardSlash } from '@astrojs/internal-helpers/path';
import colors from 'piccolore';
import type * as vite from 'vite';
import type { Logger } from '../core/logger/core.js';
import { notFoundTemplate, subpathNotUsedTemplate } from '../template/4xx.js';
import type { AstroSettings } from '../types/astro.js';
import { writeHtmlResponse } from './response.js';

export function baseMiddleware(
	settings: AstroSettings,
	logger: Logger,
): vite.Connect.NextHandleFunction {
	const { config } = settings;
	// The base may be an empty string by now, causing the URL creation to fail. We provide a default instead
	const base = config.base || '/';
	const site = config.site ? new URL(base, config.site) : undefined;
	const devRootURL = new URL(base, 'http://localhost');
	const devRoot = site ? site.pathname : devRootURL.pathname;
	const devRootReplacement = devRoot.endsWith('/') ? '/' : '';

	return function devBaseMiddleware(req, res, next) {
		const url = req.url!;
		let pathname: string;
		try {
			pathname = decodeURI(new URL(url, 'http://localhost').pathname);
		} catch (e) {
			/* malformed uri */
			return next(e);
		}

		if (pathname.startsWith(devRoot)) {
			req.url = url.replace(devRoot, devRootReplacement);
			return next();
		}

		if (pathname === '/' || pathname === '/index.html') {
			const html = subpathNotUsedTemplate(devRoot, pathname);
			return writeHtmlResponse(res, 404, html);
		}

		if (req.headers.accept?.includes('text/html')) {
			const html = notFoundTemplate(pathname);
			return writeHtmlResponse(res, 404, html);
		}

		// Check to see if it's in public and if so 404
		const publicPath = new URL('.' + req.url, config.publicDir);
		fs.stat(publicPath, (_err, stats) => {
			if (stats) {
				const publicDir = appendForwardSlash(
					path.posix.relative(config.root.pathname, config.publicDir.pathname),
				);
				const expectedLocation = new URL(devRootURL.pathname + url, devRootURL).pathname;

				logger.error(
					'router',
					`Request URLs for ${colors.bold(
						publicDir,
					)} assets must also include your base. "${expectedLocation}" expected, but received "${url}".`,
				);
				const html = subpathNotUsedTemplate(devRoot, pathname);
				return writeHtmlResponse(res, 404, html);
			} else {
				next();
			}
		});
	};
}

Domain

Subdomains

Functions

Dependencies

  • ../core/logger/core.js
  • ../template/4xx.js
  • ../types/astro.js
  • ./response.js
  • node:fs
  • node:path
  • path
  • piccolore
  • vite

Frequently Asked Questions

What does base.ts do?
base.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 base.ts?
base.ts defines 1 function(s): baseMiddleware.
What does base.ts depend on?
base.ts imports 9 module(s): ../core/logger/core.js, ../template/4xx.js, ../types/astro.js, ./response.js, node:fs, node:path, path, piccolore, and 1 more.
Where is base.ts in the architecture?
base.ts is located at packages/astro/src/vite-plugin-astro-server/base.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/vite-plugin-astro-server).

Analyze Your Own Codebase

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

Try Supermodel Free