Home / Function/ storage() — astro Function Reference

storage() — astro Function Reference

Architecture documentation for the storage() function in runtime.ts from the astro codebase.

Entity Profile

Dependency Diagram

graph TD
  8249bedb_c4c6_d6ed_cd7e_2f629ae41af9["storage()"]
  91969197_292d_3b15_21c6_0e876968e961["AstroSession"]
  8249bedb_c4c6_d6ed_cd7e_2f629ae41af9 -->|defined in| 91969197_292d_3b15_21c6_0e876968e961
  5c79ae32_65c5_f84c_14bd_7780a03d5e74["destroy()"]
  8249bedb_c4c6_d6ed_cd7e_2f629ae41af9 -->|calls| 5c79ae32_65c5_f84c_14bd_7780a03d5e74
  b8960b45_5233_5b43_3557_e134954c4977["unflatten()"]
  8249bedb_c4c6_d6ed_cd7e_2f629ae41af9 -->|calls| b8960b45_5233_5b43_3557_e134954c4977
  8a9e92da_c3c2_384b_2d6b_b1eec8e95109["has()"]
  8249bedb_c4c6_d6ed_cd7e_2f629ae41af9 -->|calls| 8a9e92da_c3c2_384b_2d6b_b1eec8e95109
  59668aee_368a_2af5_a60c_273e7404dc42["set()"]
  8249bedb_c4c6_d6ed_cd7e_2f629ae41af9 -->|calls| 59668aee_368a_2af5_a60c_273e7404dc42
  style 8249bedb_c4c6_d6ed_cd7e_2f629ae41af9 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

packages/astro/src/core/session/runtime.ts lines 341–400

	async #ensureData() {
		const storage = await this.#ensureStorage();
		if (this.#data && !this.#partial) {
			return this.#data;
		}
		this.#data ??= new Map();

		// We stored this as a devalue string, but unstorage will have parsed it as JSON
		const raw = await storage.get<any[]>(this.#ensureSessionID());
		if (!raw) {
			// If there is no existing data in storage we don't need to merge anything
			// and can just return the existing local data.
			return this.#data;
		}

		try {
			const storedMap = unflatten(raw);
			if (!(storedMap instanceof Map)) {
				await this.destroy();
				throw new AstroError({
					...SessionStorageInitError,
					message: SessionStorageInitError.message(
						'The session data was an invalid type.',
						this.#config.driver,
					),
				});
			}

			const now = Date.now();

			// Only copy values from storage that:
			// 1. Don't exist in memory (preserving in-memory changes)
			// 2. Haven't been marked for deletion
			// 3. Haven't expired
			for (const [key, value] of storedMap) {
				const expired = typeof value.expires === 'number' && value.expires < now;
				if (!this.#data.has(key) && !this.#toDelete.has(key) && !expired) {
					this.#data.set(key, value);
				}
			}

			this.#partial = false;
			return this.#data;
		} catch (err) {
			await this.destroy();
			if (err instanceof AstroError) {
				throw err;
			}
			throw new AstroError(
				{
					...SessionStorageInitError,
					message: SessionStorageInitError.message(
						'The session data could not be parsed.',
						this.#config.driver,
					),
				},
				{ cause: err },
			);
		}
	}

Domain

Subdomains

Frequently Asked Questions

What does storage() do?
storage() is a function in the astro codebase, defined in packages/astro/src/core/session/runtime.ts.
Where is storage() defined?
storage() is defined in packages/astro/src/core/session/runtime.ts at line 341.
What does storage() call?
storage() calls 4 function(s): destroy, has, set, unflatten.

Analyze Your Own Codebase

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

Try Supermodel Free