Home / Function/ createEndpoint() — astro Function Reference

createEndpoint() — astro Function Reference

Architecture documentation for the createEndpoint() function in endpoint.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  68ec4090_d8c0_118b_1279_46cbcbc1885f["createEndpoint()"]
  97c01b61_891f_8dac_ac27_a13227b30c79["endpoint.ts"]
  68ec4090_d8c0_118b_1279_46cbcbc1885f -->|defined in| 97c01b61_891f_8dac_ac27_a13227b30c79
  c5f573bc_c331_b3bf_df53_a41f0f3f2f93["getRequestData()"]
  68ec4090_d8c0_118b_1279_46cbcbc1885f -->|calls| c5f573bc_c331_b3bf_df53_a41f0f3f2f93
  49468a1d_0527_5782_dbba_2c2905ab91b9["badRequest()"]
  68ec4090_d8c0_118b_1279_46cbcbc1885f -->|calls| 49468a1d_0527_5782_dbba_2c2905ab91b9
  style 68ec4090_d8c0_118b_1279_46cbcbc1885f fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/server-islands/endpoint.ts lines 107–206

export function createEndpoint(manifest: SSRManifest) {
	const page: AstroComponentFactory = async (result) => {
		const params = result.params;
		if (!params.name) {
			return new Response(null, {
				status: 400,
				statusText: 'Bad request',
			});
		}
		const componentId = params.name;

		// Get the request data from the body or search params
		const data = await getRequestData(result.request);
		// probably error
		if (data instanceof Response) {
			return data;
		}

		const serverIslandMappings = await manifest.serverIslandMappings?.();
		const serverIslandMap = await serverIslandMappings?.serverIslandMap;
		let imp = serverIslandMap?.get(componentId);
		if (!imp) {
			return new Response(null, {
				status: 404,
				statusText: 'Not found',
			});
		}

		const key = await manifest.key;

		// Decrypt componentExport
		let componentExport: string;
		try {
			componentExport = await decryptString(key, data.encryptedComponentExport);
		} catch (_e) {
			return badRequest('Encrypted componentExport value is invalid.');
		}

		const encryptedProps = data.encryptedProps;
		let props = {};

		if (encryptedProps !== '') {
			try {
				const propString = await decryptString(key, encryptedProps);
				props = JSON.parse(propString);
			} catch (_e) {
				return badRequest('Encrypted props value is invalid.');
			}
		}

		// Decrypt slots
		let decryptedSlots: Record<string, any> = {};

		const encryptedSlots = data.encryptedSlots;

		if (encryptedSlots !== '') {
			try {
				const slotsString = await decryptString(key, encryptedSlots);
				decryptedSlots = JSON.parse(slotsString);
			} catch (_e) {
				return badRequest('Encrypted slots value is invalid.');
			}
		}

		const componentModule = await imp();
		let Component = (componentModule as any)[componentExport];

		const slots: ComponentSlots = {};
		for (const prop in decryptedSlots) {
			slots[prop] = createSlotValueFromString(decryptedSlots[prop]);
		}

		// Prevent server islands from being indexed
		result.response.headers.set('X-Robots-Tag', 'noindex');

		// Wrap Astro components so we can set propagation to
		// `self` which is needed to force the runtime to wait
		// on the component before sending out the response headers.
		// This allows the island to set headers (cookies).
		if (isAstroComponentFactory(Component)) {
			const ServerIsland = Component;

Domain

Subdomains

Frequently Asked Questions

What does createEndpoint() do?
createEndpoint() is a function in the astro codebase, defined in packages/astro/src/core/server-islands/endpoint.ts.
Where is createEndpoint() defined?
createEndpoint() is defined in packages/astro/src/core/server-islands/endpoint.ts at line 107.
What does createEndpoint() call?
createEndpoint() calls 2 function(s): badRequest, getRequestData.

Analyze Your Own Codebase

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

Try Supermodel Free