1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//! The binary wire frame: `[u32 BE header length][JSON header][raw
//! payload bytes]`, all in ONE WebSocket binary message.
//!
//! Bulk payloads (laboratory-transfer chunks) never ride base64 inside
//! JSON — the typed envelope a text frame would carry serializes
//! UNCHANGED as the header (its `data` field is `#[serde(skip)]`), and
//! the raw bytes follow out of band. Correlation is exactly the text
//! frame's: the header holds the same ids. A receiver reads the
//! length, parses the header with the same serde types it already
//! uses, and attaches the remaining bytes to the chunk field.
//!
//! Std-only and transport-agnostic — both WebSocket hops (daemon ↔
//! laboratory host, API ↔ daemon reverse channel) share it.
/// One decoded-or-encoded wire frame: JSON text for every ordinary
/// message, the binary sandwich for chunk-bearing ones.
/// Build the binary sandwich. `header` is the frame's full JSON
/// envelope (minus the payload bytes, which its serde representation
/// skips); `payload` may be empty (chunk variants are ALWAYS binary,
/// content notwithstanding — the framing is variant-keyed, never
/// content-sniffed).
/// Split a binary sandwich back into `(header JSON, payload bytes)`.
/// `None` on anything malformed — a short frame, a header length past
/// the end, or a non-UTF-8 header. Malformed frames are DROPPED by
/// receivers (forward-compat posture, mirroring unknown text frames).