* Truncate `bytes` to the tail (last `SCROLLBACK_CAP_BYTES`) and write to * ` / / .bin`. Returns the path relative to `dir` so * callers can stash it in the SessionRecord.
(wsId: string, sessionId: string, bytes: Buffer)
| 33 | * callers can stash it in the SessionRecord. |
| 34 | */ |
| 35 | async dump(wsId: string, sessionId: string, bytes: Buffer): Promise<string> { |
| 36 | const rel = `${wsId}/${sessionId}.bin`; |
| 37 | const abs = join(this.dir, rel); |
| 38 | await mkdir(dirname(abs), { recursive: true }); |
| 39 | const tail = |
| 40 | bytes.length > SCROLLBACK_CAP_BYTES ? bytes.subarray(bytes.length - SCROLLBACK_CAP_BYTES) : bytes; |
| 41 | // Atomic write: write to tmp, rename. Crash mid-write doesn't leave a |
| 42 | // half-flushed scrollback file (and prevents readers from racing). |
| 43 | const tmp = `${abs}.tmp`; |
| 44 | await writeFile(tmp, tail); |
| 45 | const { rename } = await import('node:fs/promises'); |
| 46 | await rename(tmp, abs); |
| 47 | this.logger.info('scrollback.dumped', { |
| 48 | wsId, |
| 49 | sessionId, |
| 50 | original: bytes.length, |
| 51 | written: tail.length, |
| 52 | }); |
| 53 | return rel; |
| 54 | } |
| 55 | |
| 56 | async read(rel: string): Promise<Buffer | null> { |
| 57 | try { |
no test coverage detected