| 8 | const PARTIAL_DIR = '_partial'; |
| 9 | |
| 10 | export class FileSnapshotStore implements ISnapshotStore { |
| 11 | private dir: string; |
| 12 | |
| 13 | constructor(rootDir?: string) { |
| 14 | this.dir = path.resolve(rootDir ?? process.cwd(), DEFAULT_DIR); |
| 15 | } |
| 16 | |
| 17 | // ==================== Completed snapshots ==================== |
| 18 | |
| 19 | async save(snapshot: ExecutionSnapshot): Promise<void> { |
| 20 | await fs.mkdir(this.dir, { recursive: true }); |
| 21 | |
| 22 | const ts = new Date(snapshot.startedAt).toISOString().replaceAll(':', '-'); |
| 23 | const shortId = snapshot.traceId.slice(0, 12); |
| 24 | const filename = `${ts}_${shortId}.json`; |
| 25 | const filePath = path.join(this.dir, filename); |
| 26 | |
| 27 | await fs.writeFile(filePath, JSON.stringify(snapshot, null, 2), 'utf8'); |
| 28 | |
| 29 | // Update latest symlink |
| 30 | const latestPath = path.join(this.dir, 'latest.json'); |
| 31 | try { |
| 32 | await fs.unlink(latestPath); |
| 33 | } catch { |
| 34 | // ignore if doesn't exist |
| 35 | } |
| 36 | await fs.symlink(filename, latestPath); |
| 37 | } |
| 38 | |
| 39 | async get(traceId: string): Promise<ExecutionSnapshot | null> { |
| 40 | if (traceId === 'latest') return this.getLatest(); |
| 41 | |
| 42 | // Search completed snapshots first |
| 43 | const files = await this.listFiles(); |
| 44 | const match = files.find((f) => f.includes(traceId.slice(0, 12))); |
| 45 | if (match) { |
| 46 | const content = await fs.readFile(path.join(this.dir, match), 'utf8'); |
| 47 | return JSON.parse(content) as ExecutionSnapshot; |
| 48 | } |
| 49 | |
| 50 | // Fallback to partials |
| 51 | const partial = await this.getPartial(traceId); |
| 52 | if (partial) return partialToSnapshot(partial); |
| 53 | |
| 54 | return null; |
| 55 | } |
| 56 | |
| 57 | async list(options?: { limit?: number }): Promise<SnapshotSummary[]> { |
| 58 | const files = await this.listFiles(); |
| 59 | const limit = options?.limit ?? 10; |
| 60 | const recent = files.slice(0, limit); |
| 61 | |
| 62 | const summaries: SnapshotSummary[] = []; |
| 63 | |
| 64 | for (const file of recent) { |
| 65 | try { |
| 66 | const content = await fs.readFile(path.join(this.dir, file), 'utf8'); |
| 67 | const snapshot = JSON.parse(content) as ExecutionSnapshot; |
nothing calls this directly
no outgoing calls
no test coverage detected