protocol

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package protocol implements the Yjs binary wire format: LEB128 unsigned varints, variable-length byte arrays, and UTF-8 strings, matching lib0/encoding exactly.

Index

Constants

View Source
const (
	MessageYjsSyncStep1 = uint64(0)
	MessageYjsSyncStep2 = uint64(1)
	MessageYjsUpdate    = uint64(2)
)

y-protocols sync message types.

Variables

This section is empty.

Functions

func DecodeStateVectorV1

func DecodeStateVectorV1(data []byte) (map[uint64]uint64, error)

DecodeStateVectorV1 parses a state vector from v1 bytes.

func EncodeStateVectorV1

func EncodeStateVectorV1(sv map[uint64]uint64) ([]byte, error)

EncodeStateVectorV1 serializes a state vector in v1 format. Format: varuint(n), then n*(varuint(clientID), varuint(clock)), sorted descending clientID.

func EncodeSyncStep1

func EncodeSyncStep1(stateVector []byte) []byte

EncodeSyncStep1 returns the binary encoding of a sync step 1 message.

func EncodeSyncStep2

func EncodeSyncStep2(update []byte) []byte

EncodeSyncStep2 returns the binary encoding of a sync step 2 message.

func EncodeUpdate

func EncodeUpdate(update []byte) []byte

EncodeUpdate returns the binary encoding of an update message.

func EncodeUpdateV1

func EncodeUpdateV1(u *UpdateV1) ([]byte, error)

EncodeUpdateV1 encodes a v1 update to bytes.

func ReadAny

func ReadAny(r io.Reader) (interface{}, error)

ReadAny reads a lib0-encoded "any" value from r.

func ReadVarBytes

func ReadVarBytes(r io.Reader) ([]byte, error)

ReadVarBytes reads a length-prefixed byte slice.

func ReadVarString

func ReadVarString(r io.Reader) (string, error)

ReadVarString reads a length-prefixed UTF-8 string.

func ReadVarUint

func ReadVarUint(r io.Reader) (uint64, error)

ReadVarUint reads a LEB128 unsigned varint from r.

func WriteAny

func WriteAny(w io.Writer, v interface{}) error

WriteAny writes a lib0-encoded "any" value to w.

func WriteSyncStep1

func WriteSyncStep1(w io.Writer, stateVector []byte) error

WriteSyncStep1 writes a sync step 1 message: the sender's state vector. Format: varuint(0), varBytes(stateVector)

func WriteSyncStep2

func WriteSyncStep2(w io.Writer, update []byte) error

WriteSyncStep2 writes a sync step 2 message: the update the receiver needs. Format: varuint(1), varBytes(update)

func WriteUpdate

func WriteUpdate(w io.Writer, update []byte) error

WriteUpdate writes an update message. Format: varuint(2), varBytes(update)

func WriteVarBytes

func WriteVarBytes(w io.Writer, b []byte) error

WriteVarBytes writes a length-prefixed byte slice: varuint(len) + bytes.

func WriteVarString

func WriteVarString(w io.Writer, s string) error

WriteVarString writes a UTF-8 string as varuint(UTF-8 byte length) + UTF-8 bytes.

func WriteVarUint

func WriteVarUint(w io.Writer, v uint64) error

WriteVarUint writes a uint64 as a LEB128 unsigned varint (7-bit payload per byte, MSB set when more bytes follow). This matches lib0/encoding.writeVarUint.

Types

type EncodedDeleteRange

type EncodedDeleteRange struct {
	Clock uint64
	Len   uint64
}

EncodedDeleteRange is a [clock, clock+len) deletion range.

type EncodedID

type EncodedID struct {
	Client uint64
	Clock  uint64
}

EncodedID is a (clientID, clock) pair.

type EncodedItem

type EncodedItem struct {
	ClientID    uint64
	Clock       uint64
	Length      uint64
	IsGC        bool
	IsSkip      bool
	InfoByte    byte
	OriginLeft  *EncodedID
	OriginRight *EncodedID
	// Parent (only when OriginLeft == nil && OriginRight == nil):
	ParentIsYKey bool
	ParentYKey   string
	ParentID     *EncodedID
	ParentSub    *string
	ContentRef   byte
	ContentData  interface{}
}

EncodedItem is a fully decoded struct from a v1 update.

type SyncMessage

type SyncMessage struct {
	Type uint64
	// For step1: StateVector bytes.
	// For step2 / update: Update bytes.
	Payload []byte
}

SyncMessage is a parsed y-protocols sync message.

func ReadSyncMessage

func ReadSyncMessage(r io.Reader) (*SyncMessage, error)

ReadSyncMessage reads one sync message from r.

type UpdateV1

type UpdateV1 struct {
	Items     []*EncodedItem
	DeleteSet map[uint64][]EncodedDeleteRange
}

UpdateV1 is the fully decoded content of a Yjs v1 update message.

func DecodeUpdateV1

func DecodeUpdateV1(data []byte) (*UpdateV1, error)

DecodeUpdateV1 decodes a v1 update from raw bytes.

Jump to

Keyboard shortcuts

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