wip library to store cold objects in s3, warm objects on disk, and hot objects in memory
nodejs
typescript
1/**
2 * Default JSON serializer.
3 *
4 * @param data - Data to serialize (must be JSON-serializable)
5 * @returns Serialized data as Uint8Array (UTF-8 encoded JSON)
6 *
7 * @remarks
8 * This is the default serializer used if no custom serializer is provided.
9 * Handles most JavaScript types but cannot serialize:
10 * - Functions
11 * - Symbols
12 * - undefined values (they become null)
13 * - Circular references
14 *
15 * @example
16 * ```typescript
17 * const data = { name: 'Alice', age: 30 };
18 * const serialized = await defaultSerialize(data);
19 * ```
20 */
21export async function defaultSerialize(data: unknown): Promise<Uint8Array> {
22 const json = JSON.stringify(data);
23 return new TextEncoder().encode(json);
24}
25
26/**
27 * Default JSON deserializer.
28 *
29 * @param data - Serialized data (UTF-8 encoded JSON)
30 * @returns Deserialized data
31 * @throws SyntaxError if data is not valid JSON
32 *
33 * @remarks
34 * This is the default deserializer used if no custom deserializer is provided.
35 * Parses UTF-8 encoded JSON back to JavaScript objects.
36 *
37 * @example
38 * ```typescript
39 * const data = await defaultDeserialize(serialized);
40 * console.log(data.name); // 'Alice'
41 * ```
42 */
43export async function defaultDeserialize(data: Uint8Array): Promise<unknown> {
44 const json = new TextDecoder().decode(data);
45 return JSON.parse(json);
46}