Skip to content

hertzforge.service.frames

frames

The sds/1 frame codec: messages to WebSocket frames and back.

A message that carries no arrays is a text frame, one UTF-8 JSON object. A message that carries arrays is a binary frame (schema/PROTOCOL.md, "Frames"):

b'SDS1' | u32 header length H | JSON header, space-padded so 8 + H is a
multiple of 8 | payload, each array starting at a multiple of 8

The header is the message plus a top-level "arrays" list; inside the message each array is the reference {"$array": id}. Encoding is byte-for-byte what schema/make_golden.py writes, so the golden frames are this codec's test.

Two entry points each way:

  • encode(message, arrays) frames a message already holding references, with the arrays given by id in payload order (the golden generator's form).
  • pack(message) takes a message holding numpy arrays in place, replaces each with a reference named a0, a1, ... in traversal order, and frames it (text when nothing was replaced).
  • decode(frame) reads either kind of frame and returns the message with every reference replaced by a read-only numpy array.
  • to_wire(message) returns the message with references in place and the arrays split out, which is the form the JSON schema validates.

Classes:

Name Description
FrameError

A frame or message that cannot be read or written as sds/1. The

Functions:

Name Description
dtype_name

The protocol name of an array's dtype; refused when the protocol has

encode_text

A message with no arrays as the text of one text frame.

encode

Frame message, whose array values are {"$array": id} references,

to_wire

(message with references, {id: array}): every numpy array in the

pack

A message holding numpy arrays in place as one frame: text when it

decode

Read one frame. Text is a JSON object with no array references;

Classes

FrameError

Bases: ValueError

A frame or message that cannot be read or written as sds/1. The message names what is wrong with it.

Functions:

dtype_name

dtype_name(array: ndarray) -> str

The protocol name of an array's dtype; refused when the protocol has no such dtype (convert explicitly rather than have the codec guess).

encode_text

encode_text(message: Mapping[str, Any]) -> str

A message with no arrays as the text of one text frame.

encode

encode(message: Mapping[str, Any], arrays: Mapping[str, ndarray]) -> bytes

Frame message, whose array values are {"$array": id} references, with arrays (id -> array) written in the order given.

to_wire

to_wire(message: Mapping[str, Any]) -> tuple[dict[str, Any], dict[str, ndarray]]

(message with references, {id: array}): every numpy array in the message replaced by {"$array": "a"}, k counting in traversal order (dict insertion order, list order).

pack

pack(message: Mapping[str, Any]) -> str | bytes

A message holding numpy arrays in place as one frame: text when it holds none, binary otherwise.

decode

decode(frame: str | bytes | bytearray | memoryview) -> dict[str, Any]

Read one frame. Text is a JSON object with no array references; binary is checked against the layout and every reference resolved to a read-only numpy array (C order, shape as declared).