Home / File/ request.ts — astro Source File

request.ts — astro Source File

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

File typescript CoreAstro CoreMiddleware 2 imports 1 functions

Entity Profile

Dependency Diagram

graph LR
  efdcd621_1ac1_b986_346b_ad401214e3a2["request.ts"]
  d3861967_b647_84d2_ff48_15013353bd56["../core/logger/core.js"]
  efdcd621_1ac1_b986_346b_ad401214e3a2 --> d3861967_b647_84d2_ff48_15013353bd56
  c2f6615e_96e9_c4eb_5f71_cf120e271705["node:http"]
  efdcd621_1ac1_b986_346b_ad401214e3a2 --> c2f6615e_96e9_c4eb_5f71_cf120e271705
  style efdcd621_1ac1_b986_346b_ad401214e3a2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { IncomingHttpHeaders } from 'node:http';
import type { Logger } from './logger/core.js';

type HeaderType = Headers | Record<string, any> | IncomingHttpHeaders;

interface CreateRequestOptions {
	url: URL | string;
	clientAddress?: string | undefined;
	headers: HeaderType;
	method?: string;
	body?: RequestInit['body'];
	logger: Logger;
	locals?: object | undefined;
	/**
	 * Whether the request is being created for a static build or for a prerendered page within a hybrid/SSR build, or for emulating one of those in dev mode.
	 *
	 * When `true`, the request will not include search parameters or body, and warn when headers are accessed.
	 *
	 * @default false
	 */
	isPrerendered?: boolean;

	routePattern: string;

	init?: RequestInit;
}

/**
 * Used by astro internals to create a web standard request object.
 *
 * The user of this function may provide the data in a runtime-agnostic way.
 *
 * This is used by the static build to create fake requests for prerendering, and by the dev server to convert node requests into the standard request object.
 */
export function createRequest({
	url,
	headers,
	method = 'GET',
	body = undefined,
	logger,
	isPrerendered = false,
	routePattern,
	init,
}: CreateRequestOptions): Request {
	// headers are made available on the created request only if the request is for a page that will be on-demand rendered
	const headersObj = isPrerendered
		? undefined
		: headers instanceof Headers
			? headers
			: new Headers(
					// Filter out HTTP/2 pseudo-headers. These are internally-generated headers added to all HTTP/2 requests with trusted metadata about the request.
					// Examples include `:method`, `:scheme`, `:authority`, and `:path`.
					// They are always prefixed with a colon to distinguish them from other headers, and it is an error to add the to a Headers object manually.
					// See https://httpwg.org/specs/rfc7540.html#HttpRequest
					Object.entries(headers as Record<string, any>).filter(([name]) => !name.startsWith(':')),
				);

	if (typeof url === 'string') url = new URL(url);

	// Remove search parameters if the request is for a page that will be on-demand rendered
	if (isPrerendered) {
		url.search = '';
	}

	const request = new Request(url, {
		method: method,
		headers: headersObj,
		// body is made available only if the request is for a page that will be on-demand rendered
		body: isPrerendered ? null : body,
		...init,
	});

	if (isPrerendered) {
		// Warn when accessing headers in SSG mode
		let _headers = request.headers;

		// We need to remove descriptor's value and writable properties because we're adding getters and setters.
		const { value, writable, ...headersDesc } =
			Object.getOwnPropertyDescriptor(request, 'headers') || {};

		Object.defineProperty(request, 'headers', {
			...headersDesc,
			get() {
				logger.warn(
					null,
					`\`Astro.request.headers\` was used when rendering the route \`${routePattern}'\`. \`Astro.request.headers\` is not available on prerendered pages. If you need access to request headers, make sure that the page is server-rendered using \`export const prerender = false;\` or by setting \`output\` to \`"server"\` in your Astro config to make all your pages server-rendered by default.`,
				);
				return _headers;
			},
			set(newHeaders: Headers) {
				_headers = newHeaders;
			},
		});
	}

	return request;
}

Domain

Subdomains

Functions

Dependencies

  • ../core/logger/core.js
  • node:http

Frequently Asked Questions

What does request.ts do?
request.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 request.ts?
request.ts defines 1 function(s): createRequest.
What does request.ts depend on?
request.ts imports 2 module(s): ../core/logger/core.js, node:http.
Where is request.ts in the architecture?
request.ts is located at packages/astro/src/core/request.ts (domain: CoreAstro, subdomain: CoreMiddleware, directory: packages/astro/src/core).

Analyze Your Own Codebase

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

Try Supermodel Free