29 lines
760 B
TypeScript
29 lines
760 B
TypeScript
|
|
import * as Y from "yjs";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Encode full Yjs document state as a base64 string for JSONB storage.
|
||
|
|
*/
|
||
|
|
export function yDocToBase64(doc: Y.Doc): string {
|
||
|
|
return Buffer.from(Y.encodeStateAsUpdate(doc)).toString("base64");
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Decode a base64 string to a Yjs update payload.
|
||
|
|
*/
|
||
|
|
export function base64ToYUpdate(base64: string): Uint8Array {
|
||
|
|
return new Uint8Array(Buffer.from(base64, "base64"));
|
||
|
|
}
|
||
|
|
|
||
|
|
const OBJECT_PREFIX = "object:";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Parse collaboration document names of the form `object:{uuid}`.
|
||
|
|
*/
|
||
|
|
export function extractObjectId(documentName: string): string | null {
|
||
|
|
if (!documentName.startsWith(OBJECT_PREFIX)) {
|
||
|
|
return null;
|
||
|
|
}
|
||
|
|
const id = documentName.slice(OBJECT_PREFIX.length).trim();
|
||
|
|
return id.length > 0 ? id : null;
|
||
|
|
}
|