encoding

package
v1.0.6-beta.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package encoding provides canonical, bounded binary frames for CRDT state.

Index

Constants

View Source
const FormatVersion uint64 = 1

FormatVersion identifies the canonical frame layout implemented by this package. Unknown versions are rejected rather than guessed.

Variables

View Source
var (
	ErrInvalidFrame = errors.New("encoding: invalid frame")
	ErrFrameLimit   = errors.New("encoding: frame limit exceeded")
)

Functions

func AppendTag

func AppendTag(dst []byte, tag crdt.Tag) []byte

AppendTag appends the canonical CRDT tag payload shared by framed CRDTs.

func AppendUvarint

func AppendUvarint(dst []byte, value uint64) []byte

AppendUvarint appends the unique shortest representation of value.

func MarshalFrame

func MarshalFrame(frame Frame) ([]byte, error)

MarshalFrame returns the canonical v1 encoding of frame.

func MarshalFrameWithPayload

func MarshalFrameWithPayload(typeID uint64, codecID string, payloadLength int, writePayload PayloadWriter) ([]byte, error)

MarshalFrameWithPayload returns the canonical v1 frame for a payload written directly into its final output buffer. When writePayload follows the PayloadWriter buffer-lifetime contract, this function owns the envelope and computes a checksum matching the completed payload.

func ReadBytes

func ReadBytes(data []byte, position, max int) ([]byte, int, bool)

ReadBytes reads a length-prefixed byte sequence without allocating. max is an explicit bound for the sequence and must be non-negative.

func ReadTag

func ReadTag(data []byte, position, maxStringBytes int) (crdt.Tag, int, bool)

ReadTag decodes one bounded canonical tag without retaining data's backing storage. The caller owns the returned tag value.

func ReadUvarint

func ReadUvarint(data []byte, position int) (uint64, int, bool)

ReadUvarint reads one shortest-form unsigned varint at position. It rejects truncated, overflowing, and non-canonical encodings.

func TagSize

func TagSize(tag crdt.Tag) int

TagSize returns the number of bytes used by AppendTag.

func UvarintSize

func UvarintSize(value uint64) int

UvarintSize returns the size of value's canonical unsigned-varint encoding.

Types

type DecoderLimits

type DecoderLimits struct {
	MaxFrameBytes  int
	MaxPayload     int
	MaxCodecID     int
	MaxElements    int
	MaxTags        int
	MaxStringBytes int
}

DecoderLimits bounds decoder allocation and input work. All limits must be positive. MaxElements and MaxTags apply to a single decoded payload.

func DefaultLimits

func DefaultLimits() DecoderLimits

DefaultLimits returns conservative bounds for in-memory library use. It returns a value rather than exposing mutable process-wide configuration.

type Frame

type Frame struct {
	TypeID  uint64
	CodecID string
	Payload []byte
}

Frame is the versioned outer envelope of a CRDT state or delta payload.

func UnmarshalFrame

func UnmarshalFrame(data []byte, limits Limits) (Frame, error)

UnmarshalFrame validates and decodes one complete canonical v1 frame.

type Limits

type Limits = DecoderLimits

Limits is retained as a short name for DecoderLimits.

type PayloadWriter

type PayloadWriter func([]byte) error

PayloadWriter writes exactly one framed payload into the supplied buffer. The buffer has the requested payload length and is only valid for the duration of the call. Writers must not retain or modify it after returning.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL