|
| 1 | +import fs from "node:fs/promises"; |
| 2 | +import path from "node:path"; |
| 3 | + |
| 4 | +type RepairReport = { |
| 5 | + repaired: boolean; |
| 6 | + droppedLines: number; |
| 7 | + backupPath?: string; |
| 8 | + reason?: string; |
| 9 | +}; |
| 10 | + |
| 11 | +function isSessionHeader(entry: unknown): entry is { type: string; id: string } { |
| 12 | + if (!entry || typeof entry !== "object") { |
| 13 | + return false; |
| 14 | + } |
| 15 | + const record = entry as { type?: unknown; id?: unknown }; |
| 16 | + return record.type === "session" && typeof record.id === "string" && record.id.length > 0; |
| 17 | +} |
| 18 | + |
| 19 | +export async function repairSessionFileIfNeeded(params: { |
| 20 | + sessionFile: string; |
| 21 | + warn?: (message: string) => void; |
| 22 | +}): Promise<RepairReport> { |
| 23 | + const sessionFile = params.sessionFile.trim(); |
| 24 | + if (!sessionFile) { |
| 25 | + return { repaired: false, droppedLines: 0, reason: "missing session file" }; |
| 26 | + } |
| 27 | + |
| 28 | + let content: string; |
| 29 | + try { |
| 30 | + content = await fs.readFile(sessionFile, "utf-8"); |
| 31 | + } catch { |
| 32 | + return { repaired: false, droppedLines: 0, reason: "missing session file" }; |
| 33 | + } |
| 34 | + |
| 35 | + const lines = content.split("\n"); |
| 36 | + const entries: unknown[] = []; |
| 37 | + let droppedLines = 0; |
| 38 | + |
| 39 | + for (const line of lines) { |
| 40 | + if (!line.trim()) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + try { |
| 44 | + const entry = JSON.parse(line); |
| 45 | + entries.push(entry); |
| 46 | + } catch { |
| 47 | + droppedLines += 1; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + if (entries.length === 0) { |
| 52 | + return { repaired: false, droppedLines, reason: "empty session file" }; |
| 53 | + } |
| 54 | + |
| 55 | + if (!isSessionHeader(entries[0])) { |
| 56 | + return { repaired: false, droppedLines, reason: "invalid session header" }; |
| 57 | + } |
| 58 | + |
| 59 | + if (droppedLines === 0) { |
| 60 | + return { repaired: false, droppedLines: 0 }; |
| 61 | + } |
| 62 | + |
| 63 | + const cleaned = `${entries.map((entry) => JSON.stringify(entry)).join("\n")}\n`; |
| 64 | + const backupPath = `${sessionFile}.bak-${process.pid}-${Date.now()}`; |
| 65 | + const tmpPath = `${sessionFile}.repair-${process.pid}-${Date.now()}.tmp`; |
| 66 | + try { |
| 67 | + const stat = await fs.stat(sessionFile).catch(() => null); |
| 68 | + await fs.writeFile(backupPath, content, "utf-8"); |
| 69 | + if (stat) { |
| 70 | + await fs.chmod(backupPath, stat.mode); |
| 71 | + } |
| 72 | + await fs.writeFile(tmpPath, cleaned, "utf-8"); |
| 73 | + if (stat) { |
| 74 | + await fs.chmod(tmpPath, stat.mode); |
| 75 | + } |
| 76 | + await fs.rename(tmpPath, sessionFile); |
| 77 | + } catch (err) { |
| 78 | + try { |
| 79 | + await fs.unlink(tmpPath); |
| 80 | + } catch { |
| 81 | + // ignore cleanup failures |
| 82 | + } |
| 83 | + return { |
| 84 | + repaired: false, |
| 85 | + droppedLines, |
| 86 | + reason: `repair failed: ${err instanceof Error ? err.message : "unknown error"}`, |
| 87 | + }; |
| 88 | + } |
| 89 | + |
| 90 | + params.warn?.( |
| 91 | + `session file repaired: dropped ${droppedLines} malformed line(s) (${path.basename( |
| 92 | + sessionFile, |
| 93 | + )})`, |
| 94 | + ); |
| 95 | + return { repaired: true, droppedLines, backupPath }; |
| 96 | +} |
0 commit comments