Home / File/ middleware.ts — astro Source File

middleware.ts — astro Source File

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

File typescript CoreAstro CoreMiddleware 5 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  9aa7bc1a_4e9b_33e0_6f2b_0f9abc877693["middleware.ts"]
  094e8b99_b744_500d_30af_8edfef4c4276["../index.js"]
  9aa7bc1a_4e9b_33e0_6f2b_0f9abc877693 --> 094e8b99_b744_500d_30af_8edfef4c4276
  e16a223b_37f3_6b25_1ee1_2b7bcb9d9415["node:fs"]
  9aa7bc1a_4e9b_33e0_6f2b_0f9abc877693 --> e16a223b_37f3_6b25_1ee1_2b7bcb9d9415
  3955a637_4c78_0528_fe7c_92190a232cb0["node:module"]
  9aa7bc1a_4e9b_33e0_6f2b_0f9abc877693 --> 3955a637_4c78_0528_fe7c_92190a232cb0
  d9a92db9_c95e_9165_13ac_24b3d859d946["node:url"]
  9aa7bc1a_4e9b_33e0_6f2b_0f9abc877693 --> d9a92db9_c95e_9165_13ac_24b3d859d946
  f16d8c76_2866_6150_bd14_0347b59abfe9["astro"]
  9aa7bc1a_4e9b_33e0_6f2b_0f9abc877693 --> f16d8c76_2866_6150_bd14_0347b59abfe9
  style 9aa7bc1a_4e9b_33e0_6f2b_0f9abc877693 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import { existsSync } from 'node:fs';
import { builtinModules } from 'node:module';
import { fileURLToPath, pathToFileURL } from 'node:url';
import type { AstroIntegrationLogger } from 'astro';
import {
	ASTRO_LOCALS_HEADER,
	ASTRO_MIDDLEWARE_SECRET_HEADER,
	ASTRO_PATH_HEADER,
	NODE_PATH,
} from '../index.js';

/**
 * It generates the Vercel Edge Middleware file.
 *
 * It creates a temporary file, the edge middleware, with some dynamic info.
 *
 * Then this file gets bundled with esbuild. The bundle phase will inline the Astro middleware code.
 *
 * @param astroMiddlewareEntryPointPath
 * @param root
 * @param vercelEdgeMiddlewareHandlerPath
 * @param outPath
 * @param middlewareSecret
 * @param logger
 * @returns {Promise<URL>} The path to the bundled file
 */
export async function generateEdgeMiddleware(
	astroMiddlewareEntryPointPath: URL,
	root: URL,
	vercelEdgeMiddlewareHandlerPath: URL,
	outPath: URL,
	middlewareSecret: string,
	logger: AstroIntegrationLogger,
): Promise<URL> {
	const code = edgeMiddlewareTemplate(
		astroMiddlewareEntryPointPath,
		vercelEdgeMiddlewareHandlerPath,
		middlewareSecret,
		logger,
	);
	// https://vercel.com/docs/concepts/functions/edge-middleware#create-edge-middleware
	const bundledFilePath = fileURLToPath(outPath);
	const esbuild = await import('esbuild');
	try {
		await esbuild.build({
			stdin: {
				contents: code,
				resolveDir: fileURLToPath(root),
			},
			// Vercel Edge runtime targets ESNext, because Cloudflare Workers update v8 weekly
			// https://github.com/vercel/vercel/blob/1006f2ae9d67ea4b3cbb1073e79d14d063d42436/packages/next/scripts/build-edge-function-template.js
			target: 'esnext',
			platform: 'browser',
			// esbuild automatically adds the browser, import and default conditions
			// https://esbuild.github.io/api/#conditions
			// https://runtime-keys.proposal.wintercg.org/#edge-light
			conditions: ['edge-light', 'workerd', 'worker'],
			outfile: bundledFilePath,
			allowOverwrite: true,
			format: 'esm',
// ... (91 more lines)

Domain

Subdomains

Dependencies

  • ../index.js
  • astro
  • node:fs
  • node:module
  • node:url

Frequently Asked Questions

What does middleware.ts do?
middleware.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, CoreMiddleware subdomain.
What functions are defined in middleware.ts?
middleware.ts defines 2 function(s): edgeMiddlewareTemplate, generateEdgeMiddleware.
What does middleware.ts depend on?
middleware.ts imports 5 module(s): ../index.js, astro, node:fs, node:module, node:url.
Where is middleware.ts in the architecture?
middleware.ts is located at packages/integrations/vercel/src/serverless/middleware.ts (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/integrations/vercel/src/serverless).

Analyze Your Own Codebase

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

Try Supermodel Free