feat(snapshot): cache validiert format beim lesen

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jörg Lohrer 2026-04-28 08:20:51 +02:00
parent 998e08e073
commit 63b68411e4
2 changed files with 27 additions and 2 deletions

View File

@ -4,13 +4,23 @@ export interface CacheState {
}
export async function readCache(path: string): Promise<CacheState | undefined> {
let text: string
try {
const text = await Deno.readTextFile(path)
return JSON.parse(text) as CacheState
text = await Deno.readTextFile(path)
} catch (err) {
if (err instanceof Deno.errors.NotFound) return undefined
throw err
}
const parsed = JSON.parse(text) as unknown
if (
!parsed ||
typeof parsed !== 'object' ||
typeof (parsed as { lastKnownGoodCount?: unknown }).lastKnownGoodCount !== 'number' ||
!Array.isArray((parsed as { deletedCoords?: unknown }).deletedCoords)
) {
throw new Error('Cache-File hat unbekanntes Format — bitte loeschen und neu starten')
}
return parsed as CacheState
}
export async function writeCache(path: string, state: CacheState): Promise<void> {

View File

@ -17,3 +17,18 @@ Deno.test('writeCache + readCache: round-trip', async () => {
const out = await readCache(path)
assertEquals(out, state)
})
Deno.test('readCache wirft bei korruptem cache-file', async () => {
const dir = await Deno.makeTempDir()
const path = join(dir, 'cache.json')
await Deno.writeTextFile(path, '{"unsinn": 42}')
let threw = false
try {
await readCache(path)
} catch (err) {
threw = true
if (!(err instanceof Error)) throw err
if (!err.message.includes('Cache-File')) throw err
}
if (!threw) throw new Error('readCache haette werfen sollen')
})