Reference implementation for the Phoenix Architecture. Work in progress. aicoding.leaflet.pub/
ai coding crazy
at main 47 lines 1.3 kB view raw
1/** 2 * Content-Addressed Object Store 3 * 4 * Stores JSON objects by their content hash. 5 * Operates on the filesystem under .phoenix/store/objects/ 6 */ 7 8import { mkdirSync, writeFileSync, readFileSync, existsSync } from 'node:fs'; 9import { join } from 'node:path'; 10 11export class ContentStore { 12 private objectsDir: string; 13 14 constructor(phoenixRoot: string) { 15 this.objectsDir = join(phoenixRoot, 'store', 'objects'); 16 mkdirSync(this.objectsDir, { recursive: true }); 17 } 18 19 /** 20 * Store an object by its ID. ID is expected to be a hex hash. 21 */ 22 put(id: string, data: unknown): void { 23 // Use first 2 chars as subdirectory for fan-out 24 const subDir = join(this.objectsDir, id.slice(0, 2)); 25 mkdirSync(subDir, { recursive: true }); 26 const filePath = join(subDir, id); 27 writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf8'); 28 } 29 30 /** 31 * Retrieve an object by ID. Returns null if not found. 32 */ 33 get<T = unknown>(id: string): T | null { 34 const filePath = join(this.objectsDir, id.slice(0, 2), id); 35 if (!existsSync(filePath)) return null; 36 const raw = readFileSync(filePath, 'utf8'); 37 return JSON.parse(raw) as T; 38 } 39 40 /** 41 * Check if an object exists. 42 */ 43 has(id: string): boolean { 44 const filePath = join(this.objectsDir, id.slice(0, 2), id); 45 return existsSync(filePath); 46 } 47}