File size: 1,247 Bytes
e142a32 | 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 | /**
* Public-API synchronous Zstandard frame decoder fallback.
* @module dsh-session-persistence-jsonl/zstd-public-decoder
*/
import { zstdDecompressSync } from 'node:zlib'
import type { ZstdFrameDecoder, ZstdFrameRange } from './zstd.ts'
/** Multi-frame adapter built exclusively from Node's supported one-shot API. */
export class PublicZstdFrameDecoder implements ZstdFrameDecoder {
private started = false
private closed = false
/** @inheritdoc */
public *decode(source: Buffer, frames: readonly ZstdFrameRange[]): Generator<Buffer, void, void> {
if (this.started) throw new Error('Zstandard frame decoder was already started')
if (this.closed) throw new Error('cannot start a closed Zstandard frame decoder')
this.started = true
try {
for (const { start, end } of frames) {
let decoded: Buffer
try {
decoded = zstdDecompressSync(source.subarray(start, end))
} catch (error) {
throw new Error(`corrupt Zstandard session log: frame at byte ${start} failed validation`, {
cause: error,
})
}
yield decoded
}
} finally {
this.close()
}
}
/** @inheritdoc */
close(): void {
this.closed = true
}
}
|