@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 103 lines 4.07 KB
Raw
1 import type * as firestore from "@distilled.cloud/gcp/firestore_v1";
2
3 /**
4 * Codec between plain JavaScript values and Firestore REST `Value`s.
5 * NOT exported from `index.ts`.
6 *
7 * Encoding: `null`/`undefined` → `nullValue`, booleans → `booleanValue`,
8 * integral numbers and bigints → `integerValue`, other numbers →
9 * `doubleValue`, strings → `stringValue`, `Date` → `timestampValue`,
10 * `Uint8Array` → `bytesValue`, arrays → `arrayValue`, plain objects →
11 * `mapValue`. Decoding reverses this; `integerValue`s outside the safe
12 * integer range decode to `bigint`, `geoPointValue` to
13 * `{ latitude, longitude }`, and `referenceValue` to its resource name.
14 */
15
16 const toBase64 = (bytes: Uint8Array): string => {
17 let binary = "";
18 for (let i = 0; i < bytes.length; i++) {
19 binary += String.fromCharCode(bytes[i]!);
20 }
21 return btoa(binary);
22 };
23
24 const fromBase64 = (value: string): Uint8Array => {
25 const binary = atob(value);
26 const bytes = new Uint8Array(binary.length);
27 for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
28 return bytes;
29 };
30
31 export const encodeValue = (value: unknown): firestore.Value => {
32 if (value === null || value === undefined) return { nullValue: "NULL_VALUE" };
33 if (typeof value === "boolean") return { booleanValue: value };
34 if (typeof value === "bigint") return { integerValue: value.toString() };
35 if (typeof value === "number") {
36 return Number.isSafeInteger(value) ? { integerValue: String(value) } : { doubleValue: value };
37 }
38 if (typeof value === "string") return { stringValue: value };
39 if (value instanceof Date) return { timestampValue: value.toISOString() };
40 if (value instanceof Uint8Array) return { bytesValue: toBase64(value) };
41 if (Array.isArray(value)) {
42 return { arrayValue: { values: value.map(encodeValue) } };
43 }
44 if (typeof value === "object") {
45 return { mapValue: { fields: encodeFields(value as object) } };
46 }
47 return { stringValue: String(value as symbol | ((...args: never[]) => unknown)) };
48 };
49
50 /** Encode a plain object's own enumerable keys; `undefined` keys are dropped. */
51 export const encodeFields = (fields: object): firestore.ValueMap => {
52 const out: Record<string, firestore.Value> = {};
53 for (const [key, value] of Object.entries(fields)) {
54 if (value !== undefined) out[key] = encodeValue(value);
55 }
56 return out;
57 };
58
59 const decodeDouble = (value: number | string): number =>
60 typeof value === "number" ? value : Number(value);
61
62 export const decodeValue = (value: firestore.Value): unknown => {
63 if (value.nullValue !== undefined) return null;
64 if (value.booleanValue !== undefined) return value.booleanValue;
65 if (value.integerValue !== undefined) {
66 const big = BigInt(value.integerValue);
67 return big >= BigInt(Number.MIN_SAFE_INTEGER) && big <= BigInt(Number.MAX_SAFE_INTEGER)
68 ? Number(big)
69 : big;
70 }
71 if (value.doubleValue !== undefined) return decodeDouble(value.doubleValue);
72 if (value.stringValue !== undefined) return value.stringValue;
73 if (value.timestampValue !== undefined) return new Date(value.timestampValue);
74 if (value.bytesValue !== undefined) return fromBase64(value.bytesValue);
75 if (value.referenceValue !== undefined) return value.referenceValue;
76 if (value.geoPointValue !== undefined) {
77 return {
78 latitude: value.geoPointValue.latitude ?? 0,
79 longitude: value.geoPointValue.longitude ?? 0,
80 };
81 }
82 if (value.arrayValue !== undefined) {
83 return (value.arrayValue.values ?? []).map(decodeValue);
84 }
85 if (value.mapValue !== undefined) {
86 return decodeFields(value.mapValue.fields);
87 }
88 return null;
89 };
90
91 export const decodeFields = (fields: firestore.ValueMap | undefined): Record<string, unknown> => {
92 const out: Record<string, unknown> = {};
93 for (const [key, value] of Object.entries(fields ?? {})) {
94 if (value !== undefined) out[key] = decodeValue(value);
95 }
96 return out;
97 };
98
99 /** Quote a field name for an update mask when it is not a simple identifier. */
100 export const fieldPath = (key: string): string =>
101 /^[_a-zA-Z][_a-zA-Z0-9]*$/.test(key)
102 ? key
103 : `\`${key.replace(/\\/g, "\\\\").replace(/`/g, "\\`")}\``;