import { readFile, writeFile } from 'node:fs/promises'; import { readFileSync, writeFileSync } from 'node:fs'; export class lazyKv { path: string; constructor(_path: string) { this.path = _path; this.init(); } init(): void { try { readFileSync(this.path, 'utf-8'); } catch { writeFileSync(this.path, '{}', 'utf-8'); } finally { console.log(`LazyKv: initialized ${this.path}`); } } async get(key: string, fallback?: T): Promise { const data = await readFile(this.path, 'utf-8'); const json = JSON.parse(data) as Record; return json[key] ?? (fallback || null); } async set(key: string, value: T): Promise { const data = await readFile(this.path, 'utf-8'); const json = JSON.parse(data) as Record; json[key] = value; await writeFile(this.path, JSON.stringify(json, null, 2), 'utf-8'); } async delete(key: string): Promise { const data = await readFile(this.path, 'utf-8'); const json = JSON.parse(data) as Record; delete json[key]; await writeFile(this.path, JSON.stringify(json, null, 2), 'utf-8'); } }