summaryrefslogtreecommitdiffstats
path: root/scripts/cache/utils.ts
blob: c0950001d8e93ee786f311d36725fd6d796f017e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import type { S3Client } from 'bun';

// ============================================================================
// Types
// ============================================================================

export interface FileMetadata {
    checksum: string;
    size: number;
}

export interface CacheMetadata {
    key: string;
    hash: string;
    timestamp: number;
    lastAccessed: number;
    files: Record<string, FileMetadata>;
    version: number;
}

export interface CacheLock {
    locked: boolean;
    timestamp: number;
    instance: string;
    ttl: number;
    renewedAt?: number;
    pid: number;
    hostname: string;
}

export interface S3ListObject {
    key: string;
    lastModified?: string;
}

export interface CacheEntry {
    key: string;
    timestamp: number;
    lastAccessed: number;
    hash: string;
}

export interface CacheManifest {
    version: number;
    caches: CacheEntry[];
}

// ============================================================================
// Constants
// ============================================================================

export const LOCK_TIMEOUT_MS = 30 * 60 * 1000; // 30 minutes (matches Restic)
export const LOCK_RETRY_START_MS = 5000; // 5 seconds (initial retry delay)
export const LOCK_RETRY_MAX_MS = 60000; // 60 seconds (max retry delay)
export const LOCK_MAX_RETRIES = 6; // With exponential backoff: 5s, 10s, 20s, 40s, 60s, 60s
export const LOCK_DOUBLE_CHECK_MS = 200; // 200ms delay for double-check pattern (matches Restic)

export const METADATA_VERSION = 1;
export const METADATA_KEY = 'metadata.json';
export const LOCK_KEY = 'cache.lock';

export const MAX_CACHE_FILES = 7;
export const MAX_CACHE_AGE_DAYS = 7;

export const TMP_DIR = 'tmp';
export const STATIC_DIR = 'static';
export const CACHE_FILE_NAME = 'extensions-cache.tzst';

// Cache configuration
export const CACHE_PATHS = ['static'];
export const CACHE_KEY_PREFIX = 'extensions-';
export const CACHE_RESTORE_KEYS = ['extensions-'];
export const EXTENSIONS_CONFIG_FILE = 'extensions.json';

// Helper to generate cache key from extensions.json
export async function generateCacheKey(): Promise<string> {
    const content = await Bun.file(EXTENSIONS_CONFIG_FILE).arrayBuffer();
    const hash = new Bun.CryptoHasher('sha256').update(content).digest('hex');
    return `${CACHE_KEY_PREFIX}${hash}.tzst`;
}

// Helper to write JSON to S3 file
export async function writeJsonToS3(s3: S3Client, key: string, data: any): Promise<void> {
    await Bun.write(s3.file(key), JSON.stringify(data, null, 2));
}