| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent fc4de6b commit fe2f180
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,12 +6,35 @@ | |||
| 6 | 6 | * found in the LICENSE file at https://angular.dev/license | |
| 7 | 7 | */ | |
| 8 | 8 | ||
| 9 | - import { mkdirSync } from 'node:fs'; | ||
| 9 | + import { mkdirSync, rmSync } from 'node:fs'; | ||
| 10 | 10 | import { dirname } from 'node:path'; | |
| 11 | 11 | import { DatabaseSync, StatementSync } from 'node:sqlite'; | |
| 12 | 12 | import { deserialize, serialize } from 'node:v8'; | |
| 13 | 13 | import { Cache, PersistentCacheStore } from './cache'; | |
| 14 | 14 | ||
| 15 | + /** | ||
| 16 | + * Common SQLite primary result codes. | ||
| 17 | + * @see https://www.sqlite.org/rescode.html | ||
| 18 | + */ | ||
| 19 | + const enum SqliteResultCode { | ||
| 20 | + Busy = 5, | ||
| 21 | + Locked = 6, | ||
| 22 | + } | ||
| 23 | + | ||
| 24 | + interface SqliteError extends Error { | ||
| 25 | + code?: string; | ||
| 26 | + errcode?: number; | ||
| 27 | + errstr?: string; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + function isSqliteError(error: unknown): error is SqliteError { | ||
| 31 | + return ( | ||
| 32 | + error instanceof Error && | ||
| 33 | + ('errcode' in error || | ||
| 34 | + ('code' in error && (error as { code: unknown }).code === 'ERR_SQLITE_ERROR')) | ||
| 35 | + ); | ||
| 36 | + } | ||
| 37 | + | ||
| 15 | 38 | /** | |
| 16 | 39 | * A persistent cache store backed by SQLite. | |
| 17 | 40 | * | |
@@ -22,56 +45,114 @@ import { Cache, PersistentCacheStore } from './cache'; | |||
| 22 | 45 | */ | |
| 23 | 46 | export class SqliteCacheStore implements PersistentCacheStore<unknown> { | |
| 24 | 47 | #db: DatabaseSync | undefined; | |
| 48 | + #disabled = false; | ||
| 25 | 49 | #getStmt: StatementSync | undefined; | |
| 26 | 50 | #hasStmt: StatementSync | undefined; | |
| 27 | 51 | #setStmt: StatementSync | undefined; | |
| 28 | 52 | #updateAccessedStmt: StatementSync | undefined; | |
| 29 | 53 | readonly #pendingAccessedKeys = new Set<string>(); | |
| 30 | 54 | #flushTimeout: NodeJS.Timeout | undefined; | |
| 55 | + readonly #busyTimeoutMs: number; | ||
| 31 | 56 | ||
| 32 | 57 | constructor( | |
| 33 | 58 | readonly cachePath: string, | |
| 34 | 59 | private readonly maxPayloadSize = 1024 * 1024 * 1024, | |
| 35 | 60 | private readonly ttlDays = 14, | |
| 36 | - ) {} | ||
| 61 | + busyTimeoutMs = 5000, | ||
| 62 | + ) { | ||
| 63 | + this.#busyTimeoutMs = | ||
| 64 | + Number.isSafeInteger(busyTimeoutMs) && busyTimeoutMs >= 0 ? busyTimeoutMs : 5000; | ||
| 65 | + } | ||
| 37 | 66 | ||
| 38 | - #ensureDb(): DatabaseSync { | ||
| 39 | - if (!this.#db) { | ||
| 67 | + #openDatabase(): DatabaseSync { | ||
| 68 | + let db: DatabaseSync | undefined; | ||
| 69 | + try { | ||
| 40 | 70 | if (this.cachePath === ':memory:') { | |
| 41 | - this.#db = new DatabaseSync(this.cachePath); | ||
| 71 | + db = new DatabaseSync(this.cachePath); | ||
| 42 | 72 | } else { | |
| 43 | 73 | // Optimistically attempt to open the database file first to avoid directory creation | |
| 44 | 74 | // syscalls on warm builds where the parent directory already exists. | |
| 45 | 75 | try { | |
| 46 | - this.#db = new DatabaseSync(this.cachePath); | ||
| 76 | + db = new DatabaseSync(this.cachePath); | ||
| 47 | 77 | } catch { | |
| 48 | 78 | mkdirSync(dirname(this.cachePath), { recursive: true }); | |
| 49 | - this.#db = new DatabaseSync(this.cachePath); | ||
| 79 | + db = new DatabaseSync(this.cachePath); | ||
| 50 | 80 | } | |
| 51 | 81 | } | |
| 52 | 82 | ||
| 53 | 83 | // Optimize SQLite for cache usage | |
| 54 | - this.#db.exec('PRAGMA auto_vacuum = FULL;'); | ||
| 55 | - this.#db.exec('PRAGMA journal_mode = WAL;'); | ||
| 56 | - this.#db.exec('PRAGMA synchronous = NORMAL;'); | ||
| 57 | - this.#db.exec('PRAGMA busy_timeout = 5000;'); | ||
| 58 | - this.#db.exec('PRAGMA temp_store = MEMORY;'); | ||
| 59 | - this.#db.exec('PRAGMA mmap_size = 268435456;'); | ||
| 60 | - this.#db.exec( | ||
| 84 | + db.exec(`PRAGMA busy_timeout = ${this.#busyTimeoutMs};`); | ||
| 85 | + db.exec('PRAGMA auto_vacuum = FULL;'); | ||
| 86 | + db.exec('PRAGMA journal_mode = WAL;'); | ||
| 87 | + db.exec('PRAGMA synchronous = NORMAL;'); | ||
| 88 | + db.exec('PRAGMA temp_store = MEMORY;'); | ||
| 89 | + db.exec('PRAGMA mmap_size = 268435456;'); | ||
| 90 | + db.exec( | ||
| 61 | 91 | 'CREATE TABLE IF NOT EXISTS cache (key TEXT PRIMARY KEY, value BLOB, last_accessed INTEGER NOT NULL) WITHOUT ROWID;', | |
| 62 | 92 | ); | |
| 63 | - this.#db.exec( | ||
| 93 | + db.exec( | ||
| 64 | 94 | 'CREATE INDEX IF NOT EXISTS idx_cache_accessed ON cache (last_accessed DESC, key DESC);', | |
| 65 | 95 | ); | |
| 66 | 96 | ||
| 67 | - this.#getStmt = this.#db.prepare('SELECT value FROM cache WHERE key = ?'); | ||
| 68 | - this.#hasStmt = this.#db.prepare('SELECT 1 FROM cache WHERE key = ?'); | ||
| 69 | - this.#setStmt = this.#db.prepare( | ||
| 97 | + this.#getStmt = db.prepare('SELECT value FROM cache WHERE key = ?'); | ||
| 98 | + this.#hasStmt = db.prepare('SELECT 1 FROM cache WHERE key = ?'); | ||
| 99 | + this.#setStmt = db.prepare( | ||
| 70 | 100 | 'INSERT OR REPLACE INTO cache (key, value, last_accessed) VALUES (?, ?, unixepoch())', | |
| 71 | 101 | ); | |
| 72 | - this.#updateAccessedStmt = this.#db.prepare( | ||
| 102 | + this.#updateAccessedStmt = db.prepare( | ||
| 73 | 103 | 'UPDATE cache SET last_accessed = unixepoch() WHERE key = ?', | |
| 74 | 104 | ); | |
| 105 | + | ||
| 106 | + this.#db = db; | ||
| 107 | + | ||
| 108 | + return db; | ||
| 109 | + } catch (error) { | ||
| 110 | + try { | ||
| 111 | + db?.close(); | ||
| 112 | + } catch { | ||
| 113 | + // Ignore close error on corrupted handle | ||
| 114 | + } | ||
| 115 | + this.#getStmt = undefined; | ||
| 116 | + this.#hasStmt = undefined; | ||
| 117 | + this.#setStmt = undefined; | ||
| 118 | + this.#updateAccessedStmt = undefined; | ||
| 119 | + throw error; | ||
| 120 | + } | ||
| 121 | + } | ||
| 122 | + | ||
| 123 | + #ensureDb(): DatabaseSync | undefined { | ||
| 124 | + if (this.#disabled) { | ||
| 125 | + return undefined; | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + if (!this.#db) { | ||
| 129 | + try { | ||
| 130 | + return this.#openDatabase(); | ||
| 131 | + } catch (error) { | ||
| 132 | + // If the database is locked by another active process, | ||
| 133 | + // do not attempt to delete the database files as that could corrupt the active process's database. | ||
| 134 | + const isBusy = | ||
| 135 | + isSqliteError(error) && | ||
| 136 | + (error.errcode === SqliteResultCode.Busy || error.errcode === SqliteResultCode.Locked); | ||
| 137 | + | ||
| 138 | + // Attempt to recover from database corruption by deleting the corrupted files and recreating | ||
| 139 | + if (!isBusy && this.cachePath !== ':memory:') { | ||
| 140 | + try { | ||
| 141 | + rmSync(this.cachePath, { force: true }); | ||
| 142 | + rmSync(this.cachePath + '-wal', { force: true }); | ||
| 143 | + rmSync(this.cachePath + '-shm', { force: true }); | ||
| 144 | + rmSync(this.cachePath + '-journal', { force: true }); | ||
| 145 | + | ||
| 146 | + return this.#openDatabase(); | ||
| 147 | + } catch { | ||
| 148 | + // If recovery fails (e.g. read-only filesystem or permission denied), disable caching | ||
| 149 | + } | ||
| 150 | + } | ||
| 151 | + | ||
| 152 | + this.#disabled = true; | ||
| 153 | + | ||
| 154 | + return undefined; | ||
| 155 | + } | ||
| 75 | 156 | } | |
| 76 | 157 | ||
| 77 | 158 | return this.#db; | |
@@ -94,19 +175,21 @@ export class SqliteCacheStore implements PersistentCacheStore<unknown> { | |||
| 94 | 175 | this.#flushTimeout = undefined; | |
| 95 | 176 | } | |
| 96 | 177 | ||
| 97 | - if (!this.#db || this.#pendingAccessedKeys.size === 0 || !this.#updateAccessedStmt) { | ||
| 178 | + if (this.#pendingAccessedKeys.size === 0) { | ||
| 98 | 179 | return; | |
| 99 | 180 | } | |
| 100 | 181 | ||
| 101 | 182 | try { | |
| 102 | - this.#db.exec('BEGIN IMMEDIATE TRANSACTION;'); | ||
| 103 | - for (const key of this.#pendingAccessedKeys) { | ||
| 104 | - this.#updateAccessedStmt.run(key); | ||
| 183 | + if (this.#db && this.#updateAccessedStmt) { | ||
| 184 | + this.#db.exec('BEGIN IMMEDIATE TRANSACTION;'); | ||
| 185 | + for (const key of this.#pendingAccessedKeys) { | ||
| 186 | + this.#updateAccessedStmt.run(key); | ||
| 187 | + } | ||
| 188 | + this.#db.exec('COMMIT;'); | ||
| 105 | 189 | } | |
| 106 | - this.#db.exec('COMMIT;'); | ||
| 107 | 190 | } catch { | |
| 108 | 191 | try { | |
| 109 | - this.#db.exec('ROLLBACK;'); | ||
| 192 | + this.#db?.exec('ROLLBACK;'); | ||
| 110 | 193 | } catch { | |
| 111 | 194 | // Ignore rollback errors if transaction was not active | |
| 112 | 195 | } | |
@@ -117,35 +200,55 @@ export class SqliteCacheStore implements PersistentCacheStore<unknown> { | |||
| 117 | 200 | ||
| 118 | 201 | // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
| 119 | 202 | async get(key: string): Promise<any> { | |
| 120 | - this.#ensureDb(); | ||
| 121 | - // SQLite column types are dynamic, so the stored value is only known at runtime. | ||
| 122 | - const row = this.#getStmt?.get(key) as { value: unknown } | undefined; | ||
| 203 | + if (!this.#ensureDb()) { | ||
| 204 | + return undefined; | ||
| 205 | + } | ||
| 206 | + | ||
| 207 | + try { | ||
| 208 | + // SQLite column types are dynamic, so the stored value is only known at runtime. | ||
| 209 | + const row = this.#getStmt?.get(key) as { value: unknown } | undefined; | ||
| 123 | 210 | ||
| 124 | - if (row) { | ||
| 125 | - this.#queueAccessUpdate(key); | ||
| 211 | + if (row) { | ||
| 212 | + this.#queueAccessUpdate(key); | ||
| 126 | 213 | ||
| 127 | - if (row.value instanceof Uint8Array) { | ||
| 128 | - try { | ||
| 129 | - return deserialize(row.value); | ||
| 130 | - } catch { | ||
| 131 | - // Treat corrupt or unparseable cached payloads as a cache miss. | ||
| 214 | + if (row.value instanceof Uint8Array) { | ||
| 215 | + try { | ||
| 216 | + return deserialize(row.value); | ||
| 217 | + } catch { | ||
| 218 | + // Treat corrupt or unparseable cached payloads as a cache miss. | ||
| 219 | + } | ||
| 132 | 220 | } | |
| 133 | 221 | } | |
| 222 | + } catch { | ||
| 223 | + // Treat query errors (e.g. disk read failures) as a cache miss. | ||
| 134 | 224 | } | |
| 135 | 225 | ||
| 136 | 226 | return undefined; | |
| 137 | 227 | } | |
| 138 | 228 | ||
| 139 | 229 | has(key: string): boolean { | |
| 140 | - this.#ensureDb(); | ||
| 230 | + if (!this.#ensureDb()) { | ||
| 231 | + return false; | ||
| 232 | + } | ||
| 141 | 233 | ||
| 142 | - return !!this.#hasStmt?.get(key); | ||
| 234 | + try { | ||
| 235 | + return !!this.#hasStmt?.get(key); | ||
| 236 | + } catch { | ||
| 237 | + return false; | ||
| 238 | + } | ||
| 143 | 239 | } | |
| 144 | 240 | ||
| 145 | 241 | async set(key: string, value: unknown): Promise<this> { | |
| 146 | - this.#ensureDb(); | ||
| 147 | - this.#pendingAccessedKeys.delete(key); | ||
| 148 | - this.#setStmt?.run(key, serialize(value)); | ||
| 242 | + if (!this.#ensureDb()) { | ||
| 243 | + return this; | ||
| 244 | + } | ||
| 245 | + | ||
| 246 | + try { | ||
| 247 | + this.#pendingAccessedKeys.delete(key); | ||
| 248 | + this.#setStmt?.run(key, serialize(value)); | ||
| 249 | + } catch { | ||
| 250 | + // Writing to cache is non-fatal and should not fail the build. | ||
| 251 | + } | ||
| 149 | 252 | ||
| 150 | 253 | return this; | |
| 151 | 254 | } | |
@@ -155,11 +258,10 @@ export class SqliteCacheStore implements PersistentCacheStore<unknown> { | |||
| 155 | 258 | } | |
| 156 | 259 | ||
| 157 | 260 | close(): void { | |
| 261 | + this.#flushAccessUpdates(); | ||
| 262 | + | ||
| 158 | 263 | if (this.#db) { | |
| 159 | 264 | try { | |
| 160 | - // Flush any pending access updates in one transaction before pruning | ||
| 161 | - this.#flushAccessUpdates(); | ||
| 162 | - | ||
| 163 | 265 | this.#db.exec('BEGIN IMMEDIATE TRANSACTION;'); | |
| 164 | 266 | try { | |
| 165 | 267 | // 1. Delete items older than N days | |
@@ -202,12 +304,6 @@ export class SqliteCacheStore implements PersistentCacheStore<unknown> { | |||
| 202 | 304 | } catch { | |
| 203 | 305 | // Pruning errors should not block build success | |
| 204 | 306 | } finally { | |
| 205 | - if (this.#flushTimeout) { | ||
| 206 | - clearTimeout(this.#flushTimeout); | ||
| 207 | - this.#flushTimeout = undefined; | ||
| 208 | - } | ||
| 209 | - this.#pendingAccessedKeys.clear(); | ||
| 210 | - | ||
| 211 | 307 | this.#getStmt = undefined; | |
| 212 | 308 | this.#hasStmt = undefined; | |
| 213 | 309 | this.#setStmt = undefined; | |
@@ -221,5 +317,7 @@ export class SqliteCacheStore implements PersistentCacheStore<unknown> { | |||
| 221 | 317 | this.#db = undefined; | |
| 222 | 318 | } | |
| 223 | 319 | } | |
| 320 | + | ||
| 321 | + this.#disabled = false; | ||
| 224 | 322 | } | |
| 225 | 323 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments