import type { TelegramFs } from "./fs"; /** Thrown when a JSON file cannot be parsed. Carries the offending path. */ export class TelegramJsonError extends Error { constructor( message: string, public readonly path: string, ) { super(message); this.name = "TelegramJsonError"; } } export async function readJson(fs: TelegramFs, path: string): Promise { try { return JSON.parse(await fs.readText(path)) as T; } catch (error) { if (error instanceof SyntaxError) { throw new TelegramJsonError( `Invalid JSON at ${path}: ${error.message}`, path, ); } throw error; } } export async function writeJson( fs: TelegramFs, path: string, value: T, ): Promise { // Always atomic: crash mid-write never corrupts the target record. await fs.writeTextAtomic(path, `${JSON.stringify(value, null, 2)}\n`); } export async function readJsonIfExists( fs: TelegramFs, path: string, ): Promise { if (!(await fs.exists(path))) { return null; } return await readJson(fs, path); }