also looking at this
fix: hydrate durable registry snapshots before serving writes
#244
11 files
+202
-35
| @@ -1,4 +1,5 @@ | ||
| 1 | 1 | import type { S3Client } from './aws-client' |
| 2 | import { isS3ObjectNotFound } from './aws-client' | |
| 2 | 3 | |
| 3 | 4 | /** |
| 4 | 5 | * Reusable debounced JSON-snapshot persistence to an S3-compatible bucket. |
| @@ -13,6 +14,7 @@ import type { S3Client } from './aws-client' | ||
| 13 | 14 | * the bucket under their own keys by the owning store. |
| 14 | 15 | */ |
| 15 | 16 | export class ObjectSnapshot { |
| 17 | private hydrated = false | |
| 16 | 18 | private saveTimeout: ReturnType<typeof setTimeout> | null = null |
| 17 | 19 | private savePromise: Promise<void> | null = null |
| 18 | 20 | |
| @@ -27,17 +29,13 @@ export class ObjectSnapshot { | ||
| 27 | 29 | async load(): Promise<unknown | null> { |
| 28 | 30 | try { |
| 29 | 31 | const buf = await this.s3.getObjectBuffer(this.bucket, this.key) |
| 30 | if (!buf || buf.byteLength === 0) | |
| 31 | return null | |
| 32 | return JSON.parse(buf.toString('utf-8')) | |
| 32 | const state = JSON.parse(buf.toString('utf-8')) | |
| 33 | this.hydrated = true | |
| 34 | return state | |
| 33 | 35 | } |
| 34 | 36 | catch (err) { |
| 35 | // A missing object on first run is expected (404/403). Anything else we | |
| 36 | // surface but still start empty rather than block boot — and we do NOT | |
| 37 | // overwrite the remote object until the next real save. | |
| 38 | const msg = (err as Error).message || '' | |
| 39 | if (!/\b40[34]\b/.test(msg)) | |
| 40 | console.error(`ObjectSnapshot: failed to load ${this.bucket}/${this.key}: ${msg}. Starting empty.`) | |
| 37 | if (!isS3ObjectNotFound(err)) throw err | |
| 38 | this.hydrated = true | |
| 41 | 39 | return null |
| 42 | 40 | } |
| 43 | 41 | } |
| @@ -53,6 +51,8 @@ export class ObjectSnapshot { | ||
| 53 | 51 | } |
| 54 | 52 | |
| 55 | 53 | private async save(): Promise<void> { |
| 54 | if (!this.hydrated) | |
| 55 | throw new Error(`Snapshot not hydrated: ${this.key}`) | |
| 56 | 56 | // Serialize saves so a slow upload can't be overtaken by a newer one. |
| 57 | 57 | while (this.savePromise) await this.savePromise |
| 58 | 58 | this.savePromise = this.s3.putObject({ |