// fixtures/frames.mjs — the updates wire's framing (spec plan, Global // Constraints: "each update framed as a 4-byte big-endian length then // the bytes, in seq order"). live.go's updatesGet writes it; this reads // it back into the individual Yjs updates Y.applyUpdate takes one at a // time. // // Docs' endpoint concatenates updates with no framing and its client // loops Y.readUpdate over one decoder; Slides frames them so the // client never has to know how a Yjs update finds its own end, and so // a snapshot (one large update) and a tail (many small ones) travel the // same way. Pure bytes in, bytes out, so it is tested under plain Node. /** The frames a body carries, in order. Throws on a body that does not divide cleanly. */ export function parseFrames (bytes) { const out = [] let at = 0 while (at < bytes.length) { if (bytes.length - at < 4) throw new Error(`frame: ${bytes.length - at} trailing bytes with no length prefix`) const n = ((bytes[at] << 24) >>> 0) + (bytes[at + 1] << 16) + (bytes[at + 2] << 8) + bytes[at + 3] at += 4 if (bytes.length - at < n) throw new Error(`frame: a frame claims ${n} bytes and ${bytes.length - at} remain`) out.push(bytes.subarray(at, at + n)) at += n } return out } /** One frame: the length prefix then the bytes (what a test or a beacon builds). */ export function frame (update) { const out = new Uint8Array(4 + update.length) out[0] = (update.length >>> 24) & 255 out[1] = (update.length >>> 16) & 255 out[2] = (update.length >>> 8) & 255 out[3] = update.length & 255 out.set(update, 4) return out }