Protocol
elva.protocol
Module holding the Y-Protocol specification.
Classes:
-
YCodec–Codec for Y messages according to the Yjs base encoding.
-
Message–Base class for Y messages according to the Yjs sync and awareness protocol.
-
YMessage–Implementation of Y messages according to the Yjs sync and awareness protocol.
-
ElvaMessage–Extension of Y messages with additional message types.
Functions:
-
write_var_uint–Encode an integer into a variable unsigned integer.
-
read_var_uint–Read the first variable unsigned integer in
data. -
prepend_var_uint–Prepend the length of
dataas variable unsigned integer todata. -
strip_var_uint–Read and strip off the variable unsigned integer length from
data.
Attributes:
-
STATE_ZERO–The state of an empty YDoc.
-
EMPTY_UPDATE–The update between two equivalent YDoc states.
STATE_ZERO = b'\x00'
module-attribute
The state of an empty YDoc.
EMPTY_UPDATE = b'\x00\x00'
module-attribute
The update between two equivalent YDoc states.
YCodec
Bases: Codec
Codec for Y messages according to the Yjs base encoding.
Methods:
-
encode–Prepend the size of
payloadto itself as a variable unsigned integer. -
decode–Read and strip off the encoded size from
message.
encode(payload, errors='strict')
Prepend the size of payload to itself as a variable unsigned integer.
Parameters:
Returns:
-
tuple[bytes, int]–A tuple of two values:
datawith the variable unsigned integer prepended and the length ofdata.
Source code in src/elva/protocol.py
decode(message, errors='strict')
Read and strip off the encoded size from message.
Parameters:
-
message(bytes) –the payload of a Y protocol message plus its length as variable unsigned integer prepended.
-
errors(str, default:'strict') –no-op.
Returns:
-
tuple[bytes, int]–A tuple of two values:
datawith the variable unsigned integer stripped and the length of bytes ofdatabeing processed.
Source code in src/elva/protocol.py
Message(*magic_bytes)
Base class for Y messages according to the Yjs sync and awareness protocol.
Parameters:
-
magic_bytes(bytes, default:()) –arbitrary number of bytes prepended in the encoded payload.
Methods:
-
get_types–The message types associated with this codec.
-
encode–Calculate the encoded
payloadwith the message type's magic bytes prepended. -
decode–Remove magic bytes and extract the payload of
message. -
infer_and_decode–Infer the type of the given message and return its decoded form.
Source code in src/elva/protocol.py
get_types()
classmethod
The message types associated with this codec.
Returns:
Source code in src/elva/protocol.py
encode(payload, errors='strict')
Calculate the encoded payload with the message type's magic bytes prepended.
Parameters:
Returns:
-
tuple[bytes, int]–A tuple of two objects: the encoded payload with the message type's magic bytes prepended and the length of bytes being processed.
Source code in src/elva/protocol.py
decode(message, errors='strict')
Remove magic bytes and extract the payload of message.
Parameters:
Returns:
-
tuple[bytes, int]–A tuple of two objects: the decoded message with the message type's magic bytes removed and the length of bytes being processed.
Source code in src/elva/protocol.py
_decode(data, errors='strict')
Hook extracting the payload of message.
Parameters:
-
data(bytes) –the payload of a Y protocol message plus its length as variable unsigned integer prepended.
-
errors(str, default:'strict') –no-op.
Returns:
-
tuple[bytes, int]–A tuple of two objects:
datawith the variable unsigned integer stripped and the length of bytes ofdatabeing processed.
Source code in src/elva/protocol.py
infer_and_decode(message, errors='strict')
classmethod
Infer the type of the given message and return its decoded form.
Parameters:
Returns:
-
tuple[Self, bytes, int]–A tuple of three objects: the inferred message type of
message, the decoded form ofmessageand the length of processed bytes frommessage.
Source code in src/elva/protocol.py
YMessage(*magic_bytes)
Bases: Message
Implementation of Y messages according to the Yjs sync and awareness protocol.
Attributes:
-
SYNC_STEP1–Synchronization request message.
-
SYNC_STEP2–Synchronization reply message.
-
SYNC_UPDATE–Update message.
-
AWARENESS–Awareness message.
Source code in src/elva/protocol.py
SYNC_STEP1 = (0, 0)
class-attribute
instance-attribute
Synchronization request message.
SYNC_STEP2 = (0, 1)
class-attribute
instance-attribute
Synchronization reply message.
SYNC_UPDATE = (0, 2)
class-attribute
instance-attribute
Update message.
AWARENESS = (1,)
class-attribute
instance-attribute
Awareness message.
ElvaMessage(*magic_bytes)
Bases: Message
Extension of Y messages with additional message types.
Attributes:
-
SYNC_STEP1–Synchronization request message.
-
SYNC_STEP2–Synchronization reply message.
-
SYNC_UPDATE–Update message.
-
SYNC_CROSS–Cross-synchronization message holding
-
AWARENESS–Awareness message.
-
ID–Identitity message.
-
READ–Read-only message.
-
READ_WRITE–Read-write message.
-
DATA_REQUEST–Message requesting a specific blob of data.
-
DATA_OFFER–Message offering a requested blob of data.
-
DATA_ORDER–Message ordering an offered blob of data.
-
DATA_TRANSFER–Message transferring an ordered blob of data.
Source code in src/elva/protocol.py
SYNC_STEP1 = (0, 0)
class-attribute
instance-attribute
Synchronization request message.
SYNC_STEP2 = (0, 1)
class-attribute
instance-attribute
Synchronization reply message.
SYNC_UPDATE = (0, 2)
class-attribute
instance-attribute
Update message.
SYNC_CROSS = (0, 3)
class-attribute
instance-attribute
Cross-synchronization message holding
SYNC_STEP1 and
SYNC_STEP2.
AWARENESS = (1,)
class-attribute
instance-attribute
Awareness message.
ID = (ord('I'), ord('D'))
class-attribute
instance-attribute
Identitity message.
READ = (ord('R'), ord('O'))
class-attribute
instance-attribute
Read-only message.
READ_WRITE = (ord('R'), ord('W'))
class-attribute
instance-attribute
Read-write message.
DATA_REQUEST = (ord('D'), 1)
class-attribute
instance-attribute
Message requesting a specific blob of data.
DATA_OFFER = (ord('D'), 2)
class-attribute
instance-attribute
Message offering a requested blob of data.
DATA_ORDER = (ord('D'), 3)
class-attribute
instance-attribute
Message ordering an offered blob of data.
DATA_TRANSFER = (ord('D'), 4)
class-attribute
instance-attribute
Message transferring an ordered blob of data.
write_var_uint(num)
Encode an integer into a variable unsigned integer.
Parameters:
-
num(int) –the integer to encode.
Returns:
Source code in src/elva/protocol.py
read_var_uint(data)
Read the first variable unsigned integer in data.
Parameters:
-
data(bytes) –data holding the variable unsigned integer.
Returns:
Source code in src/elva/protocol.py
prepend_var_uint(data)
Prepend the length of data as variable unsigned integer to data.
See Yjs base encoding for reference.
Parameters:
-
data(bytes) –the payload of a Y protocol message.
Returns:
-
tuple[bytes, int]–A tuple of two values:
datawith the variable unsigned integer prepended and the length ofdata.
Source code in src/elva/protocol.py
strip_var_uint(data)
Read and strip off the variable unsigned integer length from data.
See Yjs base encoding for reference.
Parameters:
-
data(bytes) –the payload of a Y protocol message plus its length as variable unsigned integer prepended.
Returns:
-
tuple[bytes, int]–A tuple of two values:
datawith the variable unsigned integer length stripped and the length of bytes ofdatabeing processed.