11import { EphemeralStore } from "loro-crdt" ;
22import {
33 CrdtType ,
4- Permission ,
54 MessageType ,
65 JoinResponseOk ,
7- UpdateError ,
8- UpdateErrorCode ,
96} from "loro-protocol" ;
107import type { CrdtServerAdaptor } from "../types" ;
118
@@ -33,16 +30,12 @@ export class LoroPersistentStoreServerAdaptor implements CrdtServerAdaptor {
3330 handleJoinRequest (
3431 documentData : Uint8Array ,
3532 _clientVersion : Uint8Array ,
36- permission : Permission
37- ) : {
38- response : JoinResponseOk ;
39- updates ?: Uint8Array [ ] ;
40- } {
33+ ) : { response : JoinResponseOk , updates ?: Uint8Array [ ] } {
4134 const response : JoinResponseOk = {
4235 type : MessageType . JoinResponseOk ,
4336 crdt : this . crdtType ,
4437 roomId : "" ,
45- permission,
38+ permission : "write" ,
4639 version : new Uint8Array ( ) ,
4740 } ;
4841
@@ -53,62 +46,23 @@ export class LoroPersistentStoreServerAdaptor implements CrdtServerAdaptor {
5346 applyUpdates (
5447 documentData : Uint8Array ,
5548 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-
49+ ) : Uint8Array {
7650 const store = new EphemeralStore ( this . timeout ) ;
77- const broadcastUpdates : Uint8Array [ ] = [ ] ;
78-
7951 try {
52+
8053 if ( documentData . length > 0 ) {
8154 store . apply ( documentData ) ;
8255 }
8356 for ( const update of updates ) {
8457 if ( update . length > 0 ) {
8558 store . apply ( update ) ;
86- broadcastUpdates . push ( update ) ;
8759 }
8860 }
8961
9062 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- } ;
63+ return newDocumentData ;
10964 } finally {
110- store . destroy ( ) ;
111- store . inner . free ( ) ;
65+ store . inner . free ( )
11266 }
11367 }
11468
@@ -118,12 +72,12 @@ export class LoroPersistentStoreServerAdaptor implements CrdtServerAdaptor {
11872
11973 merge ( documents : Uint8Array [ ] ) : Uint8Array {
12074 const store = new EphemeralStore ( this . timeout ) ;
121- for ( const data of documents ) {
122- if ( data . length > 0 ) {
123- store . apply ( data ) ;
124- }
125- }
12675 try {
76+ for ( const data of documents ) {
77+ if ( data . length > 0 ) {
78+ store . apply ( data ) ;
79+ }
80+ }
12781 return store . encodeAll ( ) ;
12882 } finally {
12983 store . destroy ( ) ;
0 commit comments