import { mkdir, open, readFile, writeFile } from 'node:fs/promises'; import { dirname } from 'node:path'; export type BridgeState = { cursors: Record; processedMessages: Record; }; const EMPTY_STATE: BridgeState = { cursors: {}, processedMessages: {} }; export class StateStore { private state: BridgeState = structuredClone(EMPTY_STATE); constructor(private readonly path: string) {} async load(): Promise { await mkdir(dirname(this.path), { recursive: true, mode: 0o700 }); try { const text = await readFile(this.path, 'utf8'); this.state = { ...structuredClone(EMPTY_STATE), ...JSON.parse(text) }; } catch (error) { if ((error as NodeJS.ErrnoException).code !== 'ENOENT') throw error; await this.save(); } } getCursor(key: string): string { return this.state.cursors[key] ?? '0'; } async setCursor(key: string, cursor: string): Promise { this.state.cursors[key] = cursor; await this.save(); } hasProcessed(messageId: string): boolean { return Boolean(this.state.processedMessages[messageId]); } async markProcessed(messageId: string, repliedNodeId: string): Promise { this.state.processedMessages[messageId] = { repliedNodeId, at: new Date().toISOString() }; await this.save(); } private async save(): Promise { const handle = await open(this.path, 'w', 0o600); try { await handle.writeFile(`${JSON.stringify(this.state, null, 2)}\n`, 'utf8'); await handle.chmod(0o600); } finally { await handle.close(); } } }