All files / packages/topic/src message.ts

87.5% Statements 14/16
85.71% Branches 12/14
50% Functions 1/2
87.5% Lines 14/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                                                                        167952x 167952x 167952x 167952x 167952x 167952x 167952x 167952x 167952x 16807x   167952x 16807x   167952x 4x                  
import type { Codec } from '@ydbjs/api/topic'
import type { TopicPartitionSession } from './partition-session.js'
 
type TopicMessageOptions = {
	partitionSession: TopicPartitionSession
	producer: string
	payload: Uint8Array
	codec: Codec
	seqNo: bigint
	offset?: bigint
	uncompressedSize?: bigint
 
	createdAt?: number
	writtenAt?: number
	metadataItems?: Record<string, Uint8Array>
 
	// Start of this message's commit range: covers the server-side offset hole
	// (retention, readFrom skip) immediately preceding the message, stitched at
	// delivery time. Defaults to the message's own offset.
	commitRangeStart?: bigint
}
 
export class TopicMessage {
	readonly partitionSession: WeakRef<TopicPartitionSession>
	readonly producer: string
	readonly payload: Uint8Array
	readonly codec: Codec
	readonly seqNo: bigint
	readonly offset?: bigint
	readonly uncompressedSize?: bigint
	readonly createdAt?: number
	readonly writtenAt?: number
	readonly metadataItems?: Record<string, Uint8Array>
	readonly commitRangeStart: bigint
 
	constructor(options: TopicMessageOptions) {
		this.partitionSession = new WeakRef(options.partitionSession)
		this.producer = options.producer
		this.codec = options.codec
		this.seqNo = options.seqNo
		this.offset = options.offset ?? 0n
		this.payload = options.payload
		this.uncompressedSize = options.uncompressedSize ?? 0n
		this.commitRangeStart = options.commitRangeStart ?? this.offset
		if (options.createdAt !== undefined) {
			this.createdAt = options.createdAt
		}
		if (options.writtenAt !== undefined) {
			this.writtenAt = options.writtenAt
		}
		if (options.metadataItems !== undefined) {
			this.metadataItems = options.metadataItems
		}
	}
 
	get alive(): boolean {
		const session = this.partitionSession.deref()
		return session ? !session.isStopped : false
	}
}