Home / Class/ NodeApp Class — astro Architecture

NodeApp Class — astro Architecture

Architecture documentation for the NodeApp class in node.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  1354a766_a33e_3a71_462c_58c895a2cb0b["NodeApp"]
  81a03fae_a2bc_f6d1_94ef_f29ffefe8af6["node.ts"]
  1354a766_a33e_3a71_462c_58c895a2cb0b -->|defined in| 81a03fae_a2bc_f6d1_94ef_f29ffefe8af6
  a3998ddb_233d_dd9e_f999_b8826155200d["setHeadersMap()"]
  1354a766_a33e_3a71_462c_58c895a2cb0b -->|method| a3998ddb_233d_dd9e_f999_b8826155200d
  a5be4a1b_0322_df84_edf6_05ca3b1d2e1d["match()"]
  1354a766_a33e_3a71_462c_58c895a2cb0b -->|method| a5be4a1b_0322_df84_edf6_05ca3b1d2e1d
  6e6caa7d_367e_071a_401e_c7855246ce6b["render()"]
  1354a766_a33e_3a71_462c_58c895a2cb0b -->|method| 6e6caa7d_367e_071a_401e_c7855246ce6b
  859c2d6f_02be_836d_7e05_a64bead34fc2["createRequest()"]
  1354a766_a33e_3a71_462c_58c895a2cb0b -->|method| 859c2d6f_02be_836d_7e05_a64bead34fc2
  2dab0a5b_d1f0_3415_f598_d7cd92b794e9["writeResponse()"]
  1354a766_a33e_3a71_462c_58c895a2cb0b -->|method| 2dab0a5b_d1f0_3415_f598_d7cd92b794e9

Relationship Graph

Source Code

packages/astro/src/core/app/node.ts lines 22–240

export class NodeApp extends App {
	headersMap: NodeAppHeadersJson | undefined = undefined;

	public setHeadersMap(headers: NodeAppHeadersJson) {
		this.headersMap = headers;
	}

	match(req: NodeRequest | Request, allowPrerenderedRoutes = false) {
		if (!(req instanceof Request)) {
			req = NodeApp.createRequest(req, {
				skipBody: true,
				allowedDomains: this.manifest.allowedDomains,
			});
		}
		return super.match(req, allowPrerenderedRoutes);
	}

	render(request: NodeRequest | Request, options?: RenderOptions): Promise<Response> {
		if (!(request instanceof Request)) {
			request = NodeApp.createRequest(request, {
				allowedDomains: this.manifest.allowedDomains,
			});
		}
		return super.render(request, options);
	}

	/**
	 * Converts a NodeJS IncomingMessage into a web standard Request.
	 * ```js
	 * import { NodeApp } from 'astro/app/node';
	 * import { createServer } from 'node:http';
	 *
	 * const server = createServer(async (req, res) => {
	 *     const request = NodeApp.createRequest(req);
	 *     const response = await app.render(request);
	 *     await NodeApp.writeResponse(response, res);
	 * })
	 * ```
	 */
	static createRequest(
		req: NodeRequest,
		{
			skipBody = false,
			allowedDomains = [],
		}: { skipBody?: boolean; allowedDomains?: Partial<RemotePattern>[] } = {},
	): Request {
		const controller = new AbortController();

		const isEncrypted = 'encrypted' in req.socket && req.socket.encrypted;

		// Parses multiple header and returns first value if available.
		const getFirstForwardedValue = (multiValueHeader?: string | string[]) => {
			return multiValueHeader
				?.toString()
				?.split(',')
				.map((e) => e.trim())?.[0];
		};

		const providedProtocol = isEncrypted ? 'https' : 'http';
		const providedHostname = req.headers.host ?? req.headers[':authority'];

		// Validate forwarded headers
		// NOTE: Header values may have commas/spaces from proxy chains, extract first value
		const validated = validateForwardedHeaders(
			getFirstForwardedValue(req.headers['x-forwarded-proto']),
			getFirstForwardedValue(req.headers['x-forwarded-host']),
			getFirstForwardedValue(req.headers['x-forwarded-port']),
			allowedDomains,
		);

		const protocol = validated.protocol ?? providedProtocol;
		// validated.host is already sanitized, only sanitize providedHostname
		const sanitizedProvidedHostname = sanitizeHost(
			typeof providedHostname === 'string' ? providedHostname : undefined,
		);
		const hostname = validated.host ?? sanitizedProvidedHostname;
		const port = validated.port;

		let url: URL;
		try {
			const hostnamePort = getHostnamePort(hostname, port);

Domain

Frequently Asked Questions

What is the NodeApp class?
NodeApp is a class in the astro codebase, defined in packages/astro/src/core/app/node.ts.
Where is NodeApp defined?
NodeApp is defined in packages/astro/src/core/app/node.ts at line 22.

Analyze Your Own Codebase

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

Try Supermodel Free