Home / File/ match.ts — astro Source File

match.ts — astro Source File

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

File typescript CoreAstro RoutingSystem 4 imports 9 functions

Entity Profile

Dependency Diagram

graph LR
  0b4969b2_7e25_9c4a_3b7f_911a988f8209["match.ts"]
  e9b74c5a_8d34_34a7_e196_5e41b87214aa["../types/astro.js"]
  0b4969b2_7e25_9c4a_3b7f_911a988f8209 --> e9b74c5a_8d34_34a7_e196_5e41b87214aa
  10d4e39f_edb6_3e34_aa93_ae1211e7da05["../types/public/internal.js"]
  0b4969b2_7e25_9c4a_3b7f_911a988f8209 --> 10d4e39f_edb6_3e34_aa93_ae1211e7da05
  3ac52e86_a82a_c6b7_3d14_13920a4b4a22["../redirects/render.js"]
  0b4969b2_7e25_9c4a_3b7f_911a988f8209 --> 3ac52e86_a82a_c6b7_3d14_13920a4b4a22
  6753d7be_860e_1564_f3fe_9b97397c52b4["../server-islands/endpoint.js"]
  0b4969b2_7e25_9c4a_3b7f_911a988f8209 --> 6753d7be_860e_1564_f3fe_9b97397c52b4
  style 0b4969b2_7e25_9c4a_3b7f_911a988f8209 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { RoutesList } from '../../types/astro.js';
import type { RouteData } from '../../types/public/internal.js';
import { redirectIsExternal } from '../redirects/render.js';
import { SERVER_ISLAND_BASE_PREFIX, SERVER_ISLAND_COMPONENT } from '../server-islands/endpoint.js';

/** Find matching route from pathname */
export function matchRoute(pathname: string, manifest: RoutesList): RouteData | undefined {
	return manifest.routes.find((route) => {
		return (
			route.pattern.test(pathname) ||
			route.fallbackRoutes.some((fallbackRoute) => fallbackRoute.pattern.test(pathname))
		);
	});
}

/** Finds all matching routes from pathname */
export function matchAllRoutes(pathname: string, manifest: RoutesList): RouteData[] {
	return manifest.routes.filter((route) => route.pattern.test(pathname));
}

const ROUTE404_RE = /^\/404\/?$/;
const ROUTE500_RE = /^\/500\/?$/;

export function isRoute404(route: string) {
	return ROUTE404_RE.test(route);
}

export function isRoute500(route: string) {
	return ROUTE500_RE.test(route);
}

/**
 * Determines if the given route matches a 404 or 500 error page.
 *
 * @param {RouteData} route - The route data to check.
 * @returns {boolean} `true` if the route matches a 404 or 500 error page, otherwise `false`.
 */
export function isRoute404or500(route: RouteData): boolean {
	return isRoute404(route.route) || isRoute500(route.route);
}

/**
 * Determines if a given route is associated with the server island component.
 *
 * @param {RouteData} route - The route data object to evaluate.
 * @return {boolean} Returns true if the route's component is the server island component, otherwise false.
 */
export function isRouteServerIsland(route: RouteData): boolean {
	return route.component === SERVER_ISLAND_COMPONENT;
}

/**
 * Determines whether the given `Request` is targeted to a "server island" based on its URL.
 *
 * @param {Request} request - The request object to be evaluated.
 * @param {string} [base=''] - The base path provided via configuration.
 * @return {boolean} - Returns `true` if the request is for a server island, otherwise `false`.
 */
export function isRequestServerIsland(request: Request, base = ''): boolean {
	const url = new URL(request.url);
	const pathname =
		base === '/' ? url.pathname.slice(base.length) : url.pathname.slice(base.length + 1);

	return pathname.startsWith(SERVER_ISLAND_BASE_PREFIX);
}

/**
 * Checks if the given request corresponds to a 404 or 500 route based on the specified base path.
 *
 * @param {Request} request - The HTTP request object to be checked.
 * @param {string} [base=''] - The base path to trim from the request's URL before checking the route. Default is an empty string.
 * @return {boolean} Returns true if the request matches a 404 or 500 route; otherwise, returns false.
 */
export function requestIs404Or500(request: Request, base = '') {
	const url = new URL(request.url);
	const pathname = url.pathname.slice(base.length);

	return isRoute404(pathname) || isRoute500(pathname);
}

/**
 * Determines whether a given route is an external redirect.
 *
 * @param {RouteData} route - The route object to check.
 * @return {boolean} Returns true if the route is an external redirect, otherwise false.
 */
export function isRouteExternalRedirect(route: RouteData): boolean {
	return !!(route.type === 'redirect' && route.redirect && redirectIsExternal(route.redirect));
}

Domain

Subdomains

Dependencies

  • ../redirects/render.js
  • ../server-islands/endpoint.js
  • ../types/astro.js
  • ../types/public/internal.js

Frequently Asked Questions

What does match.ts do?
match.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 match.ts?
match.ts defines 9 function(s): isRequestServerIsland, isRoute404, isRoute404or500, isRoute500, isRouteExternalRedirect, isRouteServerIsland, matchAllRoutes, matchRoute, requestIs404Or500.
What does match.ts depend on?
match.ts imports 4 module(s): ../redirects/render.js, ../server-islands/endpoint.js, ../types/astro.js, ../types/public/internal.js.
Where is match.ts in the architecture?
match.ts is located at packages/astro/src/core/routing/match.ts (domain: CoreAstro, subdomain: RoutingSystem, directory: packages/astro/src/core/routing).

Analyze Your Own Codebase

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

Try Supermodel Free