Home / Class/ AstroCookies Class — astro Architecture

AstroCookies Class — astro Architecture

Architecture documentation for the AstroCookies class in cookies.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  28935751_f2b1_4797_d41a_fc616c40e0a8["AstroCookies"]
  b5fc2880_5111_4957_5d79_b9e1839f86d6["cookies.ts"]
  28935751_f2b1_4797_d41a_fc616c40e0a8 -->|defined in| b5fc2880_5111_4957_5d79_b9e1839f86d6
  776f5473_5d08_95ed_8839_358320840af7["constructor()"]
  28935751_f2b1_4797_d41a_fc616c40e0a8 -->|method| 776f5473_5d08_95ed_8839_358320840af7
  334ca8ee_5039_a932_680a_7755acc78324["delete()"]
  28935751_f2b1_4797_d41a_fc616c40e0a8 -->|method| 334ca8ee_5039_a932_680a_7755acc78324
  f12348cf_2aaf_fe08_4139_53f1735dea1d["get()"]
  28935751_f2b1_4797_d41a_fc616c40e0a8 -->|method| f12348cf_2aaf_fe08_4139_53f1735dea1d
  0c4f30da_9c0f_13fa_b899_f09c97c9c0cc["has()"]
  28935751_f2b1_4797_d41a_fc616c40e0a8 -->|method| 0c4f30da_9c0f_13fa_b899_f09c97c9c0cc
  1eca20ec_d5f5_80f3_2ce1_84641921b4ed["set()"]
  28935751_f2b1_4797_d41a_fc616c40e0a8 -->|method| 1eca20ec_d5f5_80f3_2ce1_84641921b4ed
  c8875d28_14c6_0b92_021c_fcc01ea915ac["merge()"]
  28935751_f2b1_4797_d41a_fc616c40e0a8 -->|method| c8875d28_14c6_0b92_021c_fcc01ea915ac
  bc816256_d41f_6b94_ffd0_604ab3b8939b["headers()"]
  28935751_f2b1_4797_d41a_fc616c40e0a8 -->|method| bc816256_d41f_6b94_ffd0_604ab3b8939b
  80e5dd01_3818_3850_89d3_f9b30548dccb["consume()"]
  28935751_f2b1_4797_d41a_fc616c40e0a8 -->|method| 80e5dd01_3818_3850_89d3_f9b30548dccb
  9a18be0f_c82f_a721_7222_680a117c5454["Record()"]
  28935751_f2b1_4797_d41a_fc616c40e0a8 -->|method| 9a18be0f_c82f_a721_7222_680a117c5454
  dcac3e70_1c1d_dc59_0827_9d255225bd43["Map()"]
  28935751_f2b1_4797_d41a_fc616c40e0a8 -->|method| dcac3e70_1c1d_dc59_0827_9d255225bd43
  7c061eb4_0dde_43f8_b6d4_36db2f10255b["raw()"]
  28935751_f2b1_4797_d41a_fc616c40e0a8 -->|method| 7c061eb4_0dde_43f8_b6d4_36db2f10255b

Relationship Graph

Source Code

packages/astro/src/core/cookies/cookies.ts lines 66–277

class AstroCookies implements AstroCookiesInterface {
	#request: Request;
	#requestValues: Record<string, string | undefined> | null;
	#outgoing: Map<string, [string, string, boolean]> | null;
	#consumed: boolean;
	constructor(request: Request) {
		this.#request = request;
		this.#requestValues = null;
		this.#outgoing = null;
		this.#consumed = false;
	}

	/**
	 * Astro.cookies.delete(key) is used to delete a cookie. Using this method will result
	 * in a Set-Cookie header added to the response.
	 * @param key The cookie to delete
	 * @param options Options related to this deletion, such as the path of the cookie.
	 */
	delete(key: string, options?: AstroCookieDeleteOptions): void {
		/**
		 * The `@ts-expect-error` is necessary because `maxAge` and `expires` properties
		 * must not appear in the AstroCookieDeleteOptions type.
		 */
		const {
			// @ts-expect-error
			maxAge: _ignoredMaxAge,
			// @ts-expect-error
			expires: _ignoredExpires,
			...sanitizedOptions
		} = options || {};
		const serializeOptions: SerializeOptions = {
			expires: DELETED_EXPIRATION,
			...sanitizedOptions,
		};

		// Set-Cookie: token=deleted; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT
		this.#ensureOutgoingMap().set(key, [
			DELETED_VALUE,
			serialize(key, DELETED_VALUE, serializeOptions),
			false,
		]);
	}

	/**
	 * Astro.cookies.get(key) is used to get a cookie value. The cookie value is read from the
	 * request. If you have set a cookie via Astro.cookies.set(key, value), the value will be taken
	 * from that set call, overriding any values already part of the request.
	 * @param key The cookie to get.
	 * @returns An object containing the cookie value as well as convenience methods for converting its value.
	 */
	get(
		key: string,
		options: AstroCookieGetOptions | undefined = undefined,
	): AstroCookie | undefined {
		// Check for outgoing Set-Cookie values first
		if (this.#outgoing?.has(key)) {
			let [serializedValue, , isSetValue] = this.#outgoing.get(key)!;
			if (isSetValue) {
				return new AstroCookie(serializedValue);
			} else {
				return undefined;
			}
		}
		// decodeURIComponent is the default decode function for cookies
		const decode = options?.decode ?? decodeURIComponent;

		const values = this.#ensureParsed();
		if (key in values) {
			const value = values[key];
			if (value) {
				let decodedValue: string;
				try {
					decodedValue = decode(value);
				} catch (_error) {
					// If decoding fails (e.g., invalid URI sequences), use the original value
					// This aligns with the behavior of the `cookie` package
					decodedValue = value;
				}
				return new AstroCookie(decodedValue);
			}
		}

Domain

Frequently Asked Questions

What is the AstroCookies class?
AstroCookies is a class in the astro codebase, defined in packages/astro/src/core/cookies/cookies.ts.
Where is AstroCookies defined?
AstroCookies is defined in packages/astro/src/core/cookies/cookies.ts at line 66.

Analyze Your Own Codebase

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

Try Supermodel Free