colabbd/src/protocol.ts

25 lines
679 B
TypeScript

// message types between daemon and adapters
export type ClientMessage =
| { type: "join"; room: string }
| { type: "leave" }
| { type: "update"; data: number[] } // yjs update as byte array
| { type: "awareness"; data: number[] };
export type ServerMessage =
| { type: "sync"; data: number[] } // full yjs state
| { type: "update"; data: number[] }
| { type: "awareness"; data: number[] }
| { type: "peers"; count: number };
export function encode(msg: ServerMessage): string {
return JSON.stringify(msg);
}
export function decode(raw: string): ClientMessage | null {
try {
return JSON.parse(raw) as ClientMessage;
} catch {
return null;
}
}