|
| 1 | +import { EphemeralStore } from "loro-crdt"; |
| 2 | +import { |
| 3 | + CrdtType, |
| 4 | + Permission, |
| 5 | + MessageType, |
| 6 | + JoinResponseOk, |
| 7 | + UpdateError, |
| 8 | + UpdateErrorCode, |
| 9 | +} from "loro-protocol"; |
| 10 | +import type { CrdtServerAdaptor } from "../types"; |
| 11 | + |
| 12 | +export interface LoroPersistentStoreServerAdaptorConfig { |
| 13 | + timeout?: number; |
| 14 | +} |
| 15 | + |
| 16 | +export class LoroPersistentStoreServerAdaptor implements CrdtServerAdaptor { |
| 17 | + readonly crdtType = CrdtType.LoroEphemeralStorePersisted; |
| 18 | + private readonly timeout: number; |
| 19 | + |
| 20 | + constructor(config: LoroPersistentStoreServerAdaptorConfig = {}) { |
| 21 | + this.timeout = config.timeout ?? 10_000; |
| 22 | + } |
| 23 | + |
| 24 | + createEmpty(): Uint8Array { |
| 25 | + const store = new EphemeralStore(this.timeout); |
| 26 | + try { |
| 27 | + return store.encodeAll(); |
| 28 | + } finally { |
| 29 | + store.inner.free(); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + handleJoinRequest( |
| 34 | + documentData: Uint8Array, |
| 35 | + _clientVersion: Uint8Array, |
| 36 | + permission: Permission |
| 37 | + ): { |
| 38 | + response: JoinResponseOk; |
| 39 | + updates?: Uint8Array[]; |
| 40 | + } { |
| 41 | + const response: JoinResponseOk = { |
| 42 | + type: MessageType.JoinResponseOk, |
| 43 | + crdt: this.crdtType, |
| 44 | + roomId: "", |
| 45 | + permission, |
| 46 | + version: new Uint8Array(), |
| 47 | + }; |
| 48 | + |
| 49 | + const updates = documentData.length > 0 ? [documentData] : undefined; |
| 50 | + return { response, updates }; |
| 51 | + } |
| 52 | + |
| 53 | + applyUpdates( |
| 54 | + documentData: Uint8Array, |
| 55 | + updates: Uint8Array[], |
| 56 | + permission: Permission |
| 57 | + ): { |
| 58 | + success: boolean; |
| 59 | + newDocumentData?: Uint8Array; |
| 60 | + error?: UpdateError; |
| 61 | + broadcastUpdates?: Uint8Array[]; |
| 62 | + } { |
| 63 | + if (permission === "read") { |
| 64 | + return { |
| 65 | + success: false, |
| 66 | + error: { |
| 67 | + type: MessageType.UpdateError, |
| 68 | + crdt: this.crdtType, |
| 69 | + roomId: "", |
| 70 | + code: UpdateErrorCode.PermissionDenied, |
| 71 | + message: "Read-only permission, cannot apply updates", |
| 72 | + }, |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + const store = new EphemeralStore(this.timeout); |
| 77 | + const broadcastUpdates: Uint8Array[] = []; |
| 78 | + |
| 79 | + try { |
| 80 | + if (documentData.length > 0) { |
| 81 | + store.apply(documentData); |
| 82 | + } |
| 83 | + for (const update of updates) { |
| 84 | + if (update.length > 0) { |
| 85 | + store.apply(update); |
| 86 | + broadcastUpdates.push(update); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + const newDocumentData = store.encodeAll(); |
| 91 | + |
| 92 | + return { |
| 93 | + success: true, |
| 94 | + newDocumentData, |
| 95 | + broadcastUpdates: |
| 96 | + broadcastUpdates.length > 0 ? broadcastUpdates : undefined, |
| 97 | + }; |
| 98 | + } catch (error) { |
| 99 | + return { |
| 100 | + success: false, |
| 101 | + error: { |
| 102 | + type: MessageType.UpdateError, |
| 103 | + crdt: this.crdtType, |
| 104 | + roomId: "", |
| 105 | + code: UpdateErrorCode.InvalidUpdate, |
| 106 | + message: error instanceof Error ? error.message : "Invalid update", |
| 107 | + }, |
| 108 | + }; |
| 109 | + } finally { |
| 110 | + store.destroy(); |
| 111 | + store.inner.free(); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + getVersion(_documentData: Uint8Array): Uint8Array { |
| 116 | + return new Uint8Array(); |
| 117 | + } |
| 118 | + |
| 119 | + merge(documents: Uint8Array[]): Uint8Array { |
| 120 | + const store = new EphemeralStore(this.timeout); |
| 121 | + for (const data of documents) { |
| 122 | + if (data.length > 0) { |
| 123 | + store.apply(data); |
| 124 | + } |
| 125 | + } |
| 126 | + try { |
| 127 | + return store.encodeAll(); |
| 128 | + } finally { |
| 129 | + store.destroy(); |
| 130 | + store.inner.free(); |
| 131 | + } |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +export const loroPersistentStoreServerAdaptor = |
| 136 | + new LoroPersistentStoreServerAdaptor(); |
0 commit comments