Home / File/ serialize.ts — astro Source File

serialize.ts — astro Source File

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

File typescript CoreAstro RenderingEngine 2 imports 4 functions

Entity Profile

Dependency Diagram

graph LR
  503a0de8_cf3d_458a_44c0_c9f295739fb6["serialize.ts"]
  aa7ecf5d_ecc0_6ab7_c8df_09c64e200c44["../../type-utils.js"]
  503a0de8_cf3d_458a_44c0_c9f295739fb6 --> aa7ecf5d_ecc0_6ab7_c8df_09c64e200c44
  10d4e39f_edb6_3e34_aa93_ae1211e7da05["../types/public/internal.js"]
  503a0de8_cf3d_458a_44c0_c9f295739fb6 --> 10d4e39f_edb6_3e34_aa93_ae1211e7da05
  style 503a0de8_cf3d_458a_44c0_c9f295739fb6 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

import type { ValueOf } from '../../type-utils.js';
import type { AstroComponentMetadata } from '../../types/public/internal.js';

const PROP_TYPE = {
	Value: 0,
	JSON: 1, // Actually means Array
	RegExp: 2,
	Date: 3,
	Map: 4,
	Set: 5,
	BigInt: 6,
	URL: 7,
	Uint8Array: 8,
	Uint16Array: 9,
	Uint32Array: 10,
	Infinity: 11,
};

function serializeArray(
	value: any[],
	metadata: AstroComponentMetadata | Record<string, any> = {},
	parents = new WeakSet<any>(),
): any[] {
	if (parents.has(value)) {
		throw new Error(`Cyclic reference detected while serializing props for <${metadata.displayName} client:${metadata.hydrate}>!

Cyclic references cannot be safely serialized for client-side usage. Please remove the cyclic reference.`);
	}
	parents.add(value);
	const serialized = value.map((v) => {
		return convertToSerializedForm(v, metadata, parents);
	});
	parents.delete(value);
	return serialized;
}

function serializeObject(
	value: Record<any, any>,
	metadata: AstroComponentMetadata | Record<string, any> = {},
	parents = new WeakSet<any>(),
): Record<any, any> {
	if (parents.has(value)) {
		throw new Error(`Cyclic reference detected while serializing props for <${metadata.displayName} client:${metadata.hydrate}>!

Cyclic references cannot be safely serialized for client-side usage. Please remove the cyclic reference.`);
	}
	parents.add(value);
	const serialized = Object.fromEntries(
		Object.entries(value).map(([k, v]) => {
			return [k, convertToSerializedForm(v, metadata, parents)];
		}),
	);
	parents.delete(value);
	return serialized;
}

function convertToSerializedForm(
	value: any,
	metadata: AstroComponentMetadata | Record<string, any> = {},
	parents = new WeakSet<any>(),
): [ValueOf<typeof PROP_TYPE>, any] | [ValueOf<typeof PROP_TYPE>] {
	const tag = Object.prototype.toString.call(value);
	switch (tag) {
		case '[object Date]': {
			return [PROP_TYPE.Date, (value as Date).toISOString()];
		}
		case '[object RegExp]': {
			return [PROP_TYPE.RegExp, (value as RegExp).source];
		}
		case '[object Map]': {
			return [PROP_TYPE.Map, serializeArray(Array.from(value as Map<any, any>), metadata, parents)];
		}
		case '[object Set]': {
			return [PROP_TYPE.Set, serializeArray(Array.from(value as Set<any>), metadata, parents)];
		}
		case '[object BigInt]': {
			return [PROP_TYPE.BigInt, (value as bigint).toString()];
		}
		case '[object URL]': {
			return [PROP_TYPE.URL, (value as URL).toString()];
		}
		case '[object Array]': {
			return [PROP_TYPE.JSON, serializeArray(value, metadata, parents)];
		}
		case '[object Uint8Array]': {
			return [PROP_TYPE.Uint8Array, Array.from(value as Uint8Array)];
		}
		case '[object Uint16Array]': {
			return [PROP_TYPE.Uint16Array, Array.from(value as Uint16Array)];
		}
		case '[object Uint32Array]': {
			return [PROP_TYPE.Uint32Array, Array.from(value as Uint32Array)];
		}
		default: {
			if (value !== null && typeof value === 'object') {
				return [PROP_TYPE.Value, serializeObject(value, metadata, parents)];
			}
			if (value === Infinity) {
				return [PROP_TYPE.Infinity, 1];
			}
			if (value === -Infinity) {
				return [PROP_TYPE.Infinity, -1];
			}
			if (value === undefined) {
				return [PROP_TYPE.Value];
			}
			return [PROP_TYPE.Value, value];
		}
	}
}

export function serializeProps(props: any, metadata: AstroComponentMetadata) {
	const serialized = JSON.stringify(serializeObject(props, metadata));
	return serialized;
}

Domain

Subdomains

Dependencies

  • ../../type-utils.js
  • ../types/public/internal.js

Frequently Asked Questions

What does serialize.ts do?
serialize.ts is a source file in the astro codebase, written in typescript. It belongs to the CoreAstro domain, RenderingEngine subdomain.
What functions are defined in serialize.ts?
serialize.ts defines 4 function(s): convertToSerializedForm, serializeArray, serializeObject, serializeProps.
What does serialize.ts depend on?
serialize.ts imports 2 module(s): ../../type-utils.js, ../types/public/internal.js.
Where is serialize.ts in the architecture?
serialize.ts is located at packages/astro/src/runtime/server/serialize.ts (domain: CoreAstro, subdomain: RenderingEngine, directory: packages/astro/src/runtime/server).

Analyze Your Own Codebase

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

Try Supermodel Free