import * as decoding from "lib0/decoding"; import * as encoding from "lib0/encoding"; import type { Awareness } from "y-protocols/awareness"; import { encodeAwarenessUpdate } from "y-protocols/awareness"; import { readSyncMessage, writeSyncStep1, writeUpdate } from "y-protocols/sync"; import type * as Y from "yjs"; /** * Wire format for the collab WebSocket, shared by the server relay * (src/server/collab) and the browser provider (public/editor/collab.ts). * Every frame starts with a varUint message type. SYNC and AWARENESS payloads * use the standard y-protocols encoding (the same framing y-websocket uses, * minus its room/auth layers); CONTROL carries a small JSON payload for * save/discard/reload coordination. */ export const MSG_SYNC = 0; export const MSG_AWARENESS = 1; export const MSG_CONTROL = 2; export type ControlMessage = | { t: "saved" } | { t: "discard" } | { t: "reload" }; /** Sync step 1 (state-vector request) — sent by both sides on connect. */ export function encodeSyncStep1(doc: Y.Doc): Uint8Array { const encoder = encoding.createEncoder(); encoding.writeVarUint(encoder, MSG_SYNC); writeSyncStep1(encoder, doc); return encoding.toUint8Array(encoder); } /** A document update to relay to peers. */ export function encodeUpdate(update: Uint8Array): Uint8Array { const encoder = encoding.createEncoder(); encoding.writeVarUint(encoder, MSG_SYNC); writeUpdate(encoder, update); return encoding.toUint8Array(encoder); } /** Awareness (cursor/presence) update for the given client ids. */ export function encodeAwareness( awareness: Awareness, clients: number[], ): Uint8Array { const encoder = encoding.createEncoder(); encoding.writeVarUint(encoder, MSG_AWARENESS); encoding.writeVarUint8Array( encoder, encodeAwarenessUpdate(awareness, clients), ); return encoding.toUint8Array(encoder); } /** A control frame (JSON payload) for save/discard/reload coordination. */ export function encodeControl(msg: ControlMessage): Uint8Array { const encoder = encoding.createEncoder(); encoding.writeVarUint(encoder, MSG_CONTROL); encoding.writeVarString(encoder, JSON.stringify(msg)); return encoding.toUint8Array(encoder); } export interface IncomingFrame { type: number; decoder: decoding.Decoder; } /** Read the leading message type; the returned decoder is positioned after it. */ export function readFrameType(data: Uint8Array): IncomingFrame { const decoder = decoding.createDecoder(data); const type = decoding.readVarUint(decoder); return { type, decoder }; } /** * Apply an inbound SYNC frame to `doc` (decoder positioned after the type byte), * returning any reply bytes to send back to the sender, or null if none. */ export function readSyncFrame( decoder: decoding.Decoder, doc: Y.Doc, origin: unknown, ): Uint8Array | null { const encoder = encoding.createEncoder(); encoding.writeVarUint(encoder, MSG_SYNC); readSyncMessage(decoder, encoder, doc, origin); // length 1 means only the type byte was written — nothing to reply. return encoding.length(encoder) > 1 ? encoding.toUint8Array(encoder) : null; } /** Read an AWARENESS frame's update payload (decoder positioned after the type byte). */ export function readAwarenessFrame(decoder: decoding.Decoder): Uint8Array { return decoding.readVarUint8Array(decoder); } /** Read a CONTROL frame's JSON payload (decoder positioned after the type byte). */ export function readControlFrame(decoder: decoding.Decoder): ControlMessage { return JSON.parse(decoding.readVarString(decoder)) as ControlMessage; }