Skip to content
Source
Sign inSign up
samwillis/neon-durable-streams
Open History to see the last commit

replay-plan.ts

62 lines3.0 KB

1import { isSuccessor, parseOffset } from './offset.js'
2​
3interface Range {
4 readonly stream_id: string
5 readonly start_offset: string
6 readonly end_offset: string
7 /** Exact bytes for staging/whole blocks; a safe upper bound for boundary blocks. */
8 readonly payload_bytes: number
9}
10export interface PostgresFrame extends Range {
11 readonly storage: 'postgres'
12 readonly payload: Buffer
13 readonly message_ends: number[] | null
15export interface ObjectRange extends Range {
16 readonly storage: 'object'
17 readonly manifest: {
18 readonly id: string
19 readonly key: string
20 readonly sha256: string
21 readonly encodedBytes: number
22 readonly frameCount: number
23 readonly blockDirectory: Buffer
24 readonly start: string
25 readonly end: string
26 }
27 readonly firstFrame: number
28 readonly lastFrame: number
29 readonly exactBytes: boolean
31export type StoredFrame = PostgresFrame | ObjectRange
32export interface ReplayRow extends Range {
33 readonly payload: Buffer | null
34 readonly message_ends: number[] | null
35 readonly chunk_id: string | null
36 readonly object_key: string | null
37 readonly sha256: string | null
38 readonly encoded_bytes: number | null
39 readonly frame_count: number | null
40 readonly block_directory: Buffer | null
41 readonly chunk_start: string | null
42 readonly chunk_end: string | null
43 readonly first_frame: number | null
44 readonly last_frame: number | null
45 readonly cap?: string
47export function decodeReplayRow(row: ReplayRow): StoredFrame {
48 const range = { stream_id: row.stream_id, start_offset: row.start_offset, end_offset: row.end_offset, payload_bytes: row.payload_bytes }
49 parseOffset(row.start_offset); parseOffset(row.end_offset)
50 if (!Number.isSafeInteger(row.payload_bytes) || row.payload_bytes < 1 || row.end_offset <= row.start_offset) throw new Error('Invalid replay range')
51 if (row.chunk_id === null) {
52 if (!Buffer.isBuffer(row.payload) || row.payload.length !== row.payload_bytes || !isSuccessor(row.start_offset, row.end_offset)) throw new Error('Postgres replay frame is incomplete')
53 return { ...range, storage: 'postgres', payload: row.payload, message_ends: row.message_ends }
54 }
55 if (row.payload !== null || !row.object_key || !row.sha256 || !row.encoded_bytes || row.encoded_bytes < 1
56 || !row.frame_count || !Buffer.isBuffer(row.block_directory) || !row.block_directory.length || !row.chunk_start || !row.chunk_end || row.first_frame === null || row.last_frame === null
57 || !Number.isSafeInteger(row.first_frame) || !Number.isSafeInteger(row.last_frame)
58 || row.first_frame < 1 || row.last_frame < row.first_frame || row.last_frame > row.frame_count) throw new Error('Sealed range is missing its manifest')
59 return { ...range, storage: 'object', manifest: { id: row.chunk_id, key: row.object_key, sha256: row.sha256,
60 encodedBytes: row.encoded_bytes, frameCount: row.frame_count, blockDirectory: row.block_directory, start: row.chunk_start, end: row.chunk_end },
61 firstFrame: row.first_frame, lastFrame: row.last_frame, exactBytes: true }