diff options
| author | 2025-12-26 22:39:23 +0800 | |
|---|---|---|
| committer | 2025-12-26 22:39:23 +0800 | |
| commit | 32ca410f4edbff578d71781d943c41573912f476 (patch) | |
| tree | 49f7e1e5602657d23945082fe273fc4802959a40 /scripts/cache/manifest.ts | |
Initial commitmain
Diffstat (limited to 'scripts/cache/manifest.ts')
| -rw-r--r-- | scripts/cache/manifest.ts | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/scripts/cache/manifest.ts b/scripts/cache/manifest.ts new file mode 100644 index 0000000..7558047 --- /dev/null +++ b/scripts/cache/manifest.ts @@ -0,0 +1,76 @@ +import type { S3Client } from 'bun'; +import type { CacheEntry, CacheManifest } from './utils'; +import { writeJsonToS3 } from './utils'; + +const MANIFEST_KEY = 'manifest.json'; +const MANIFEST_VERSION = 1; + +export async function loadManifest(s3: S3Client): Promise<CacheManifest> { + const manifestFile = s3.file(MANIFEST_KEY); + + try { + if (await manifestFile.exists()) { + const data = await manifestFile.text(); + const manifest: CacheManifest = JSON.parse(data); + + if (manifest.version === MANIFEST_VERSION) { + return manifest; + } + } + } catch (e) { + console.error('Failed to load manifest:', e); + } + + // Return empty manifest if not found or invalid + return { + version: MANIFEST_VERSION, + caches: [] + }; +} + +export async function saveManifest(s3: S3Client, manifest: CacheManifest): Promise<void> { + await writeJsonToS3(s3, MANIFEST_KEY, manifest); +} + +export async function addCacheEntry( + s3: S3Client, + key: string, + hash: string, + timestamp: number +): Promise<void> { + const manifest = await loadManifest(s3); + + // Remove existing entry with same key if exists + manifest.caches = manifest.caches.filter((entry) => entry.key !== key); + + // Add new entry + manifest.caches.push({ + key, + hash, + timestamp, + lastAccessed: timestamp + }); + + await saveManifest(s3, manifest); +} + +export async function removeCacheEntry(s3: S3Client, key: string): Promise<void> { + const manifest = await loadManifest(s3); + manifest.caches = manifest.caches.filter((entry) => entry.key !== key); + await saveManifest(s3, manifest); +} + +export function findCacheByKey(manifest: CacheManifest, key: string): CacheEntry | null { + return manifest.caches.find((entry) => entry.key === key) || null; +} + +export function findCacheByPrefix(manifest: CacheManifest, prefix: string): CacheEntry | null { + const matching = manifest.caches.filter((entry) => entry.key.startsWith(prefix)); + + if (matching.length === 0) { + return null; + } + + // Return most recently created cache + return matching.sort((a, b) => b.timestamp - a.timestamp)[0]; +} |
