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
| 1 | /** |
| 2 | * Helpers for the observed-attribute projections `read` and `reconcile` |
| 3 | * share, so `alchemy drift` compares like with like. |
| 4 | */ |
| 5 | |
| 6 | const sortKeysDeep = (value: unknown): unknown => { |
| 7 | if (Array.isArray(value)) return value.map(sortKeysDeep); |
| 8 | if (value !== null && typeof value === "object") { |
| 9 | return Object.fromEntries( |
| 10 | Object.entries(value as Record<string, unknown>) |
| 11 | .sort(([a], [b]) => a.localeCompare(b)) |
| 12 | .map(([key, nested]) => [key, sortKeysDeep(nested)]), |
| 13 | ); |
| 14 | } |
| 15 | return value; |
| 16 | }; |
| 17 | |
| 18 | /** |
| 19 | * A JSON document (string or object) in canonical form: keys sorted at every |
| 20 | * depth, no whitespace. AWS often reorders or re-spaces documents it stores |
| 21 | * (policies, event patterns, state machine definitions), so drift compares |
| 22 | * this form. Strings that do not parse are returned unchanged. |
| 23 | */ |
| 24 | export const canonicalJson = (document: string | object | undefined): string | undefined => { |
| 25 | if (document === undefined) return undefined; |
| 26 | if (typeof document !== "string") return JSON.stringify(sortKeysDeep(document)); |
| 27 | try { |
| 28 | return JSON.stringify(sortKeysDeep(JSON.parse(document))); |
| 29 | } catch { |
| 30 | return document; |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | /** |
| 35 | * A value made safe to persist and compare as plain JSON: byte arrays |
| 36 | * become base64 strings, `null` becomes `undefined`, and the keys of every |
| 37 | * object are sorted. Use it on SDK response structures stored in observed |
| 38 | * attributes. |
| 39 | */ |
| 40 | export const plainJson = <T>(value: T): T => toPlain(value) as T; |
| 41 | |
| 42 | const toPlain = (value: unknown): unknown => { |
| 43 | if (value === null) return undefined; |
| 44 | if (value instanceof Uint8Array) return Buffer.from(value).toString("base64"); |
| 45 | if (value instanceof Date) return value.toISOString(); |
| 46 | if (Array.isArray(value)) return value.map(toPlain); |
| 47 | if (typeof value === "object" && value !== undefined) { |
| 48 | return Object.fromEntries( |
| 49 | Object.entries(value as Record<string, unknown>) |
| 50 | .filter(([, nested]) => nested !== undefined && nested !== null) |
| 51 | .sort(([a], [b]) => a.localeCompare(b)) |
| 52 | .map(([key, nested]) => [key, toPlain(nested)]), |
| 53 | ); |
| 54 | } |
| 55 | return value; |
| 56 | }; |
| 57 | |
| 58 | /** |
| 59 | * A tag list whose `Key`/`Value` are optional on the wire (EC2, ELBv2, …) |
| 60 | * as a record. Entries without a key are dropped. |
| 61 | */ |
| 62 | export const tagListRecord = ( |
| 63 | tags: ReadonlyArray<{ Key?: string; Value?: string }> | undefined, |
| 64 | ): Record<string, string> => |
| 65 | Object.fromEntries( |
| 66 | (tags ?? []).flatMap((tag) => (tag.Key === undefined ? [] : [[tag.Key, tag.Value ?? ""]])), |
| 67 | ); |