Slap.Yjs.Frame (slap_yjs v0.3.0)

Copy Markdown View Source

The framing of Yjs updates in a .updates stream, as lib0's writeVarUint8Array writes it and the reference y-durable-streams provider reads it: each update is its byte size as a varuint, then the update. Frames are concatenated, so a batch of frames is itself valid framing.

A varuint is little-endian base 128: 7 bits per byte, low groups first, the high bit set on every byte but the last.

Summary

Functions

Frames one update.

Frames several updates into one binary.

Splits concatenated frames into their updates. {:error, :truncated} if the binary ends inside a frame, {:error, :invalid} if a length does not fit in 53 bits (lib0's limit).

Decodes a varuint at the start of binary: {:ok, n, rest}, or an error as for parse/1.

Encodes a non-negative integer as a varuint.

Functions

frame(update)

@spec frame(binary()) :: binary()

Frames one update.

frames(updates)

@spec frames([binary()]) :: binary()

Frames several updates into one binary.

parse(binary)

@spec parse(binary()) :: {:ok, [binary()]} | {:error, :truncated | :invalid}

Splits concatenated frames into their updates. {:error, :truncated} if the binary ends inside a frame, {:error, :invalid} if a length does not fit in 53 bits (lib0's limit).

read_varuint(binary)

@spec read_varuint(binary()) ::
  {:ok, non_neg_integer(), binary()} | {:error, :truncated | :invalid}

Decodes a varuint at the start of binary: {:ok, n, rest}, or an error as for parse/1.

varuint(n)

@spec varuint(non_neg_integer()) :: binary()

Encodes a non-negative integer as a varuint.