@liveblocks/core 1.0.7 → 1.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/dist/index.d.ts +353 -306
  2. package/dist/index.js +203 -194
  3. package/package.json +2 -1
package/dist/index.d.ts CHANGED
@@ -129,6 +129,63 @@ declare type UpdateDelta = {
129
129
  type: "delete";
130
130
  };
131
131
 
132
+ /**
133
+ * "Plain LSON" is a JSON-based format that's used when serializing Live structures
134
+ * to send them over HTTP (e.g. in the API endpoint to let users upload their initial
135
+ * Room storage, in the API endpoint to fetch a Room's storage, ...).
136
+ *
137
+ * In the client, you would typically create LSON values using:
138
+ *
139
+ * new LiveObject({ x: 0, y: 0 })
140
+ *
141
+ * But over HTTP, this has to be serialized somehow. The "Plain LSON" format
142
+ * is what's used in the POST /init-storage-new endpoint, to allow users to
143
+ * control which parts of their data structure should be considered "Live"
144
+ * objects, and which parts are "normal" objects.
145
+ *
146
+ * So if they have a structure like:
147
+ *
148
+ * { x: 0, y: 0 }
149
+ *
150
+ * And want to make it a Live object, they can serialize it by wrapping it in
151
+ * a special "annotation":
152
+ *
153
+ * {
154
+ * "liveblocksType": "LiveObject",
155
+ * "data": { x: 0, y: 0 },
156
+ * }
157
+ *
158
+ * This "Plain LSON" data format defines exactly those wrappings.
159
+ *
160
+ * To summarize:
161
+ *
162
+ * LSON value | Plain LSON equivalent
163
+ * ----------------------+----------------------------------------------
164
+ * 42 | 42
165
+ * [1, 2, 3] | [1, 2, 3]
166
+ * { x: 0, y: 0 } | { x: 0, y: 0 }
167
+ * ----------------------+----------------------------------------------
168
+ * new LiveList(...) | { liveblocksType: "LiveList", data: ... }
169
+ * new LiveMap(...) | { liveblocksType: "LiveMap", data: ... }
170
+ * new LiveObject(...) | { liveblocksType: "LiveObject", data: ... }
171
+ *
172
+ */
173
+
174
+ declare type PlainLsonFields = Record<string, PlainLson>;
175
+ declare type PlainLsonObject = {
176
+ liveblocksType: "LiveObject";
177
+ data: PlainLsonFields;
178
+ };
179
+ declare type PlainLsonMap = {
180
+ liveblocksType: "LiveMap";
181
+ data: PlainLsonFields;
182
+ };
183
+ declare type PlainLsonList = {
184
+ liveblocksType: "LiveList";
185
+ data: PlainLson[];
186
+ };
187
+ declare type PlainLson = PlainLsonObject | PlainLsonMap | PlainLsonList | Json;
188
+
132
189
  declare type LiveObjectUpdateDelta<O extends {
133
190
  [key: string]: unknown;
134
191
  }> = {
@@ -197,6 +254,10 @@ declare class LiveObject<O extends LsonObject> extends AbstractCrdt {
197
254
  declare type ToImmutable<L extends Lson | LsonObject> = L extends LiveList<infer I> ? readonly ToImmutable<I>[] : L extends LiveObject<infer O> ? ToImmutable<O> : L extends LiveMap<infer K, infer V> ? ReadonlyMap<K, ToImmutable<V>> : L extends LsonObject ? {
198
255
  readonly [K in keyof L]: ToImmutable<Exclude<L[K], undefined>> | (undefined extends L[K] ? undefined : never);
199
256
  } : L extends Json ? L : never;
257
+ /**
258
+ * Returns PlainLson for a given Json or LiveStructure, suitable for calling the storage init api
259
+ */
260
+ declare function toPlainLson(lson: Lson): PlainLson;
200
261
 
201
262
  /**
202
263
  * A LiveMap notification that is sent in-client to any subscribers whenever
@@ -268,12 +329,15 @@ declare class LiveMap<TKey extends string, TValue extends Lson> extends Abstract
268
329
  }
269
330
 
270
331
  declare type StorageCallback = (updates: StorageUpdate[]) => void;
332
+ declare type LiveMapUpdate = LiveMapUpdates<string, Lson>;
333
+ declare type LiveObjectUpdate = LiveObjectUpdates<LsonObject>;
334
+ declare type LiveListUpdate = LiveListUpdates<Lson>;
271
335
  /**
272
336
  * The payload of notifications sent (in-client) when LiveStructures change.
273
337
  * Messages of this kind are not originating from the network, but are 100%
274
338
  * in-client.
275
339
  */
276
- declare type StorageUpdate = LiveMapUpdates<string, Lson> | LiveObjectUpdates<LsonObject> | LiveListUpdates<Lson>;
340
+ declare type StorageUpdate = LiveMapUpdate | LiveObjectUpdate | LiveListUpdate;
277
341
 
278
342
  declare abstract class AbstractCrdt {
279
343
  get roomId(): string | null;
@@ -508,78 +572,54 @@ declare type Observable<T> = {
508
572
  subscribeOnce(callback: Callback<T>): UnsubscribeCallback;
509
573
  };
510
574
 
511
- declare type AppOnlyAuthToken = {
512
- appId: string;
513
- roomId?: never;
514
- scopes: string[];
515
- };
516
- declare type RoomAuthToken = {
517
- appId: string;
518
- roomId: string;
519
- scopes: string[];
520
- actor: number;
521
- maxConnectionsPerRoom?: number;
522
- info?: Json;
523
- groupIds?: string[];
524
- } & ({
525
- id: string;
526
- anonymousId?: never;
527
- } | {
528
- id?: never;
529
- anonymousId: string;
530
- });
531
- declare type AuthToken = AppOnlyAuthToken | RoomAuthToken;
532
- declare function isAppOnlyAuthToken(data: JsonObject): data is AppOnlyAuthToken;
533
- declare function isRoomAuthToken(data: JsonObject): data is RoomAuthToken;
534
- declare function isAuthToken(data: JsonObject): data is AuthToken;
535
-
536
- declare enum ClientMsgCode {
537
- UPDATE_PRESENCE = 100,
538
- BROADCAST_EVENT = 103,
539
- FETCH_STORAGE = 200,
540
- UPDATE_STORAGE = 201
575
+ interface IWebSocketEvent {
576
+ type: string;
577
+ }
578
+ interface IWebSocketCloseEvent extends IWebSocketEvent {
579
+ readonly code: number;
580
+ readonly wasClean: boolean;
581
+ readonly reason: string;
582
+ }
583
+ interface IWebSocketMessageEvent extends IWebSocketEvent {
584
+ readonly data: string | Buffer | ArrayBuffer | readonly Buffer[];
585
+ }
586
+ interface IWebSocketInstance {
587
+ readonly CONNECTING: number;
588
+ readonly OPEN: number;
589
+ readonly CLOSING: number;
590
+ readonly CLOSED: number;
591
+ readonly readyState: number;
592
+ addEventListener(type: "close", listener: (this: IWebSocketInstance, ev: IWebSocketCloseEvent) => unknown): void;
593
+ addEventListener(type: "message", listener: (this: IWebSocketInstance, ev: IWebSocketMessageEvent) => unknown): void;
594
+ addEventListener(type: "open" | "error", listener: (this: IWebSocketInstance, ev: IWebSocketEvent) => unknown): void;
595
+ removeEventListener(type: "close", listener: (this: IWebSocketInstance, ev: IWebSocketCloseEvent) => unknown): void;
596
+ removeEventListener(type: "message", listener: (this: IWebSocketInstance, ev: IWebSocketMessageEvent) => unknown): void;
597
+ removeEventListener(type: "open" | "error", listener: (this: IWebSocketInstance, ev: IWebSocketEvent) => unknown): void;
598
+ close(): void;
599
+ send(data: string): void;
541
600
  }
542
601
  /**
543
- * Messages that can be sent from the client to the server.
602
+ * Either the browser-based WebSocket API or Node.js' WebSocket API (from the
603
+ * 'ws' package).
604
+ *
605
+ * This type defines the minimal WebSocket API that Liveblocks needs from
606
+ * a WebSocket implementation, and is a minimal subset of the browser-based
607
+ * WebSocket APIs and Node.js' WebSocket API so that both implementations are
608
+ * assignable to this type.
544
609
  */
545
- declare type ClientMsg<TPresence extends JsonObject, TRoomEvent extends Json> = BroadcastEventClientMsg<TRoomEvent> | UpdatePresenceClientMsg<TPresence> | UpdateStorageClientMsg | FetchStorageClientMsg;
546
- declare type BroadcastEventClientMsg<TRoomEvent extends Json> = {
547
- type: ClientMsgCode.BROADCAST_EVENT;
548
- event: TRoomEvent;
549
- };
550
- declare type UpdatePresenceClientMsg<TPresence extends JsonObject> = {
551
- readonly type: ClientMsgCode.UPDATE_PRESENCE;
552
- /**
553
- * Set this to any number to signify that this is a Full Presence™
554
- * update, not a patch.
555
- *
556
- * The numeric value itself no longer has specific meaning. Historically,
557
- * this field was intended so that clients could ignore these broadcasted
558
- * full presence messages, but it turned out that getting a full presence
559
- * "keyframe" from time to time was useful.
560
- *
561
- * So nowadays, the presence (pun intended) of this `targetActor` field
562
- * is a backward-compatible way of expressing that the `data` contains
563
- * all presence fields, and isn't a partial "patch".
564
- */
565
- readonly targetActor: number;
566
- readonly data: TPresence;
567
- } | {
568
- readonly type: ClientMsgCode.UPDATE_PRESENCE;
569
- /**
570
- * Absence of the `targetActor` field signifies that this is a Partial
571
- * Presence™ "patch".
572
- */
573
- readonly targetActor?: undefined;
574
- readonly data: Partial<TPresence>;
575
- };
576
- declare type UpdateStorageClientMsg = {
577
- readonly type: ClientMsgCode.UPDATE_STORAGE;
578
- readonly ops: Op[];
579
- };
580
- declare type FetchStorageClientMsg = {
581
- readonly type: ClientMsgCode.FETCH_STORAGE;
582
- };
610
+ interface IWebSocket {
611
+ new (address: string): IWebSocketInstance;
612
+ }
613
+ declare enum WebsocketCloseCodes {
614
+ CLOSE_ABNORMAL = 1006,
615
+ INVALID_MESSAGE_FORMAT = 4000,
616
+ NOT_ALLOWED = 4001,
617
+ MAX_NUMBER_OF_MESSAGES_PER_SECONDS = 4002,
618
+ MAX_NUMBER_OF_CONCURRENT_CONNECTIONS = 4003,
619
+ MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP = 4004,
620
+ MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM = 4005,
621
+ CLOSE_WITHOUT_RETRY = 4999
622
+ }
583
623
 
584
624
  declare type ReadonlyArrayWithLegacyMethods<T> = readonly T[] & {
585
625
  /**
@@ -638,48 +678,6 @@ declare type OthersEvent<TPresence extends JsonObject, TUserMeta extends BaseUse
638
678
  type: "reset";
639
679
  };
640
680
 
641
- declare type JsonTreeNode = {
642
- readonly type: "Json";
643
- readonly id: string;
644
- readonly key: string;
645
- readonly payload: Json;
646
- };
647
- declare type LiveTreeNode<TName extends `Live${string}` = `Live${string}`> = {
648
- readonly type: TName;
649
- readonly id: string;
650
- readonly key: string;
651
- readonly payload: LsonTreeNode[];
652
- };
653
- declare type LsonTreeNode = LiveTreeNode | JsonTreeNode;
654
- declare type UserTreeNode = {
655
- readonly type: "User";
656
- readonly id: string;
657
- readonly key: string;
658
- readonly payload: {
659
- readonly connectionId: number;
660
- readonly id?: string;
661
- readonly info?: Json;
662
- readonly presence: JsonObject;
663
- readonly isReadOnly: boolean;
664
- };
665
- };
666
- declare type TreeNode = LsonTreeNode | UserTreeNode;
667
-
668
- type DevToolsTreeNode_JsonTreeNode = JsonTreeNode;
669
- type DevToolsTreeNode_LiveTreeNode<TName extends `Live${string}` = `Live${string}`> = LiveTreeNode<TName>;
670
- type DevToolsTreeNode_LsonTreeNode = LsonTreeNode;
671
- type DevToolsTreeNode_UserTreeNode = UserTreeNode;
672
- type DevToolsTreeNode_TreeNode = TreeNode;
673
- declare namespace DevToolsTreeNode {
674
- export {
675
- DevToolsTreeNode_JsonTreeNode as JsonTreeNode,
676
- DevToolsTreeNode_LiveTreeNode as LiveTreeNode,
677
- DevToolsTreeNode_LsonTreeNode as LsonTreeNode,
678
- DevToolsTreeNode_UserTreeNode as UserTreeNode,
679
- DevToolsTreeNode_TreeNode as TreeNode,
680
- };
681
- }
682
-
683
681
  declare type CustomEvent<TRoomEvent extends Json> = {
684
682
  connectionId: number;
685
683
  event: TRoomEvent;
@@ -792,6 +790,128 @@ declare type BroadcastOptions = {
792
790
  */
793
791
  shouldQueueEventIfNotReady: boolean;
794
792
  };
793
+ declare type SubscribeFn<TPresence extends JsonObject, _TStorage extends LsonObject, TUserMeta extends BaseUserMeta, TRoomEvent extends Json> = {
794
+ /**
795
+ * Subscribe to the current user presence updates.
796
+ *
797
+ * @param listener the callback that is called every time the current user presence is updated with {@link Room.updatePresence}.
798
+ *
799
+ * @returns Unsubscribe function.
800
+ *
801
+ * @example
802
+ * room.subscribe("my-presence", (presence) => {
803
+ * // Do something
804
+ * });
805
+ */
806
+ (type: "my-presence", listener: Callback<TPresence>): () => void;
807
+ /**
808
+ * Subscribe to the other users updates.
809
+ *
810
+ * @param listener the callback that is called when a user enters or leaves the room or when a user update its presence.
811
+ *
812
+ * @returns Unsubscribe function.
813
+ *
814
+ * @example
815
+ * room.subscribe("others", (others) => {
816
+ * // Do something
817
+ * });
818
+ *
819
+ */
820
+ (type: "others", listener: (others: Others<TPresence, TUserMeta>, event: OthersEvent<TPresence, TUserMeta>) => void): () => void;
821
+ /**
822
+ * Subscribe to events broadcasted by {@link Room.broadcastEvent}
823
+ *
824
+ * @param listener the callback that is called when a user calls {@link Room.broadcastEvent}
825
+ *
826
+ * @returns Unsubscribe function.
827
+ *
828
+ * @example
829
+ * room.subscribe("event", ({ event, connectionId }) => {
830
+ * // Do something
831
+ * });
832
+ *
833
+ */
834
+ (type: "event", listener: Callback<CustomEvent<TRoomEvent>>): () => void;
835
+ /**
836
+ * Subscribe to errors thrown in the room.
837
+ *
838
+ * @returns Unsubscribe function.
839
+ *
840
+ */
841
+ (type: "error", listener: ErrorCallback): () => void;
842
+ /**
843
+ * Subscribe to connection state updates.
844
+ *
845
+ * @returns Unsubscribe function.
846
+ *
847
+ */
848
+ (type: "connection", listener: Callback<ConnectionStatus>): () => void;
849
+ /**
850
+ * Subscribes to changes made on a Live structure. Returns an unsubscribe function.
851
+ * In a future version, we will also expose what exactly changed in the Live structure.
852
+ *
853
+ * @param callback The callback this called when the Live structure changes.
854
+ *
855
+ * @returns Unsubscribe function.
856
+ *
857
+ * @example
858
+ * const liveMap = new LiveMap(); // Could also be LiveList or LiveObject
859
+ * const unsubscribe = room.subscribe(liveMap, (liveMap) => { });
860
+ * unsubscribe();
861
+ */
862
+ <L extends LiveStructure>(liveStructure: L, callback: (node: L) => void): () => void;
863
+ /**
864
+ * Subscribes to changes made on a Live structure and all the nested data
865
+ * structures. Returns an unsubscribe function. In a future version, we
866
+ * will also expose what exactly changed in the Live structure.
867
+ *
868
+ * @param callback The callback this called when the Live structure, or any
869
+ * of its nested values, changes.
870
+ *
871
+ * @returns Unsubscribe function.
872
+ *
873
+ * @example
874
+ * const liveMap = new LiveMap(); // Could also be LiveList or LiveObject
875
+ * const unsubscribe = room.subscribe(liveMap, (updates) => { }, { isDeep: true });
876
+ * unsubscribe();
877
+ */
878
+ <L extends LiveStructure>(liveStructure: L, callback: StorageCallback, options: {
879
+ isDeep: true;
880
+ }): () => void;
881
+ /**
882
+ * Subscribe to the current user's history changes.
883
+ *
884
+ * @returns Unsubscribe function.
885
+ *
886
+ * @example
887
+ * room.subscribe("history", ({ canUndo, canRedo }) => {
888
+ * // Do something
889
+ * });
890
+ */
891
+ (type: "history", listener: Callback<HistoryEvent>): () => void;
892
+ /**
893
+ * Subscribe to storage status changes.
894
+ *
895
+ * @returns Unsubscribe function.
896
+ *
897
+ * @example
898
+ * room.subscribe("storage-status", (status) => {
899
+ * switch(status) {
900
+ * case "not-loaded":
901
+ * break;
902
+ * case "loading":
903
+ * break;
904
+ * case "synchronizing":
905
+ * break;
906
+ * case "synchronized":
907
+ * break;
908
+ * default:
909
+ * break;
910
+ * }
911
+ * });
912
+ */
913
+ (type: "storage-status", listener: Callback<StorageStatus>): () => void;
914
+ };
795
915
  declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUserMeta extends BaseUserMeta, TRoomEvent extends Json> = {
796
916
  /**
797
917
  * The id of the room.
@@ -803,128 +923,7 @@ declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUs
803
923
  */
804
924
  isSelfAware(): boolean;
805
925
  getConnectionState(): ConnectionStatus;
806
- readonly subscribe: {
807
- /**
808
- * Subscribe to the current user presence updates.
809
- *
810
- * @param listener the callback that is called every time the current user presence is updated with {@link Room.updatePresence}.
811
- *
812
- * @returns Unsubscribe function.
813
- *
814
- * @example
815
- * room.subscribe("my-presence", (presence) => {
816
- * // Do something
817
- * });
818
- */
819
- (type: "my-presence", listener: Callback<TPresence>): () => void;
820
- /**
821
- * Subscribe to the other users updates.
822
- *
823
- * @param listener the callback that is called when a user enters or leaves the room or when a user update its presence.
824
- *
825
- * @returns Unsubscribe function.
826
- *
827
- * @example
828
- * room.subscribe("others", (others) => {
829
- * // Do something
830
- * });
831
- *
832
- */
833
- (type: "others", listener: (others: Others<TPresence, TUserMeta>, event: OthersEvent<TPresence, TUserMeta>) => void): () => void;
834
- /**
835
- * Subscribe to events broadcasted by {@link Room.broadcastEvent}
836
- *
837
- * @param listener the callback that is called when a user calls {@link Room.broadcastEvent}
838
- *
839
- * @returns Unsubscribe function.
840
- *
841
- * @example
842
- * room.subscribe("event", ({ event, connectionId }) => {
843
- * // Do something
844
- * });
845
- *
846
- */
847
- (type: "event", listener: Callback<CustomEvent<TRoomEvent>>): () => void;
848
- /**
849
- * Subscribe to errors thrown in the room.
850
- *
851
- * @returns Unsubscribe function.
852
- *
853
- */
854
- (type: "error", listener: ErrorCallback): () => void;
855
- /**
856
- * Subscribe to connection state updates.
857
- *
858
- * @returns Unsubscribe function.
859
- *
860
- */
861
- (type: "connection", listener: Callback<ConnectionStatus>): () => void;
862
- /**
863
- * Subscribes to changes made on a Live structure. Returns an unsubscribe function.
864
- * In a future version, we will also expose what exactly changed in the Live structure.
865
- *
866
- * @param callback The callback this called when the Live structure changes.
867
- *
868
- * @returns Unsubscribe function.
869
- *
870
- * @example
871
- * const liveMap = new LiveMap(); // Could also be LiveList or LiveObject
872
- * const unsubscribe = room.subscribe(liveMap, (liveMap) => { });
873
- * unsubscribe();
874
- */
875
- <L extends LiveStructure>(liveStructure: L, callback: (node: L) => void): () => void;
876
- /**
877
- * Subscribes to changes made on a Live structure and all the nested data
878
- * structures. Returns an unsubscribe function. In a future version, we
879
- * will also expose what exactly changed in the Live structure.
880
- *
881
- * @param callback The callback this called when the Live structure, or any
882
- * of its nested values, changes.
883
- *
884
- * @returns Unsubscribe function.
885
- *
886
- * @example
887
- * const liveMap = new LiveMap(); // Could also be LiveList or LiveObject
888
- * const unsubscribe = room.subscribe(liveMap, (updates) => { }, { isDeep: true });
889
- * unsubscribe();
890
- */
891
- <L extends LiveStructure>(liveStructure: L, callback: StorageCallback, options: {
892
- isDeep: true;
893
- }): () => void;
894
- /**
895
- * Subscribe to the current user's history changes.
896
- *
897
- * @returns Unsubscribe function.
898
- *
899
- * @example
900
- * room.subscribe("history", ({ canUndo, canRedo }) => {
901
- * // Do something
902
- * });
903
- */
904
- (type: "history", listener: Callback<HistoryEvent>): () => void;
905
- /**
906
- * Subscribe to storage status changes.
907
- *
908
- * @returns Unsubscribe function.
909
- *
910
- * @example
911
- * room.subscribe("storage-status", (status) => {
912
- * switch(status) {
913
- * case "not-loaded":
914
- * break;
915
- * case "loading":
916
- * break;
917
- * case "synchronizing":
918
- * break;
919
- * case "synchronized":
920
- * break;
921
- * default:
922
- * break;
923
- * }
924
- * });
925
- */
926
- (type: "storage-status", listener: Callback<StorageStatus>): () => void;
927
- };
926
+ readonly subscribe: SubscribeFn<TPresence, TStorage, TUserMeta, TRoomEvent>;
928
927
  /**
929
928
  * Room's history contains functions that let you undo and redo operation made on by the current client on the presence and storage.
930
929
  */
@@ -1004,24 +1003,25 @@ declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUs
1004
1003
  */
1005
1004
  getStorageSnapshot(): LiveObject<TStorage> | null;
1006
1005
  readonly events: {
1007
- customEvent: Observable<{
1006
+ readonly customEvent: Observable<{
1008
1007
  connectionId: number;
1009
1008
  event: TRoomEvent;
1010
1009
  }>;
1011
- me: Observable<TPresence>;
1012
- others: Observable<{
1010
+ readonly me: Observable<TPresence>;
1011
+ readonly others: Observable<{
1013
1012
  others: Others<TPresence, TUserMeta>;
1014
1013
  event: OthersEvent<TPresence, TUserMeta>;
1015
1014
  }>;
1016
- error: Observable<Error>;
1017
- connection: Observable<ConnectionStatus>;
1018
- storage: Observable<StorageUpdate[]>;
1019
- history: Observable<HistoryEvent>;
1015
+ readonly error: Observable<Error>;
1016
+ readonly connection: Observable<ConnectionStatus>;
1017
+ readonly storage: Observable<StorageUpdate[]>;
1018
+ readonly history: Observable<HistoryEvent>;
1020
1019
  /**
1021
1020
  * Subscribe to the storage loaded event. Will fire at most once during the
1022
1021
  * lifetime of a Room.
1023
1022
  */
1024
- storageDidLoad: Observable<void>;
1023
+ readonly storageDidLoad: Observable<void>;
1024
+ readonly storageStatus: Observable<StorageStatus>;
1025
1025
  };
1026
1026
  /**
1027
1027
  * Batches modifications made during the given function.
@@ -1054,7 +1054,7 @@ declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUs
1054
1054
  declare type Polyfills = {
1055
1055
  atob?: (data: string) => string;
1056
1056
  fetch?: typeof fetch;
1057
- WebSocket?: any;
1057
+ WebSocket?: IWebSocket;
1058
1058
  };
1059
1059
  declare type RoomInitializers<TPresence extends JsonObject, TStorage extends LsonObject> = Resolve<{
1060
1060
  /**
@@ -1329,6 +1329,79 @@ declare function asPos(str: string): Pos;
1329
1329
  */
1330
1330
  declare function shallow(a: unknown, b: unknown): boolean;
1331
1331
 
1332
+ declare type AppOnlyAuthToken = {
1333
+ appId: string;
1334
+ roomId?: never;
1335
+ scopes: string[];
1336
+ };
1337
+ declare type RoomAuthToken = {
1338
+ appId: string;
1339
+ roomId: string;
1340
+ scopes: string[];
1341
+ actor: number;
1342
+ maxConnectionsPerRoom?: number;
1343
+ info?: Json;
1344
+ groupIds?: string[];
1345
+ } & ({
1346
+ id: string;
1347
+ anonymousId?: never;
1348
+ } | {
1349
+ id?: never;
1350
+ anonymousId: string;
1351
+ });
1352
+ declare type AuthToken = AppOnlyAuthToken | RoomAuthToken;
1353
+ declare function isAppOnlyAuthToken(data: JsonObject): data is AppOnlyAuthToken;
1354
+ declare function isRoomAuthToken(data: JsonObject): data is RoomAuthToken;
1355
+ declare function isAuthToken(data: JsonObject): data is AuthToken;
1356
+
1357
+ declare enum ClientMsgCode {
1358
+ UPDATE_PRESENCE = 100,
1359
+ BROADCAST_EVENT = 103,
1360
+ FETCH_STORAGE = 200,
1361
+ UPDATE_STORAGE = 201
1362
+ }
1363
+ /**
1364
+ * Messages that can be sent from the client to the server.
1365
+ */
1366
+ declare type ClientMsg<TPresence extends JsonObject, TRoomEvent extends Json> = BroadcastEventClientMsg<TRoomEvent> | UpdatePresenceClientMsg<TPresence> | UpdateStorageClientMsg | FetchStorageClientMsg;
1367
+ declare type BroadcastEventClientMsg<TRoomEvent extends Json> = {
1368
+ type: ClientMsgCode.BROADCAST_EVENT;
1369
+ event: TRoomEvent;
1370
+ };
1371
+ declare type UpdatePresenceClientMsg<TPresence extends JsonObject> = {
1372
+ readonly type: ClientMsgCode.UPDATE_PRESENCE;
1373
+ /**
1374
+ * Set this to any number to signify that this is a Full Presence™
1375
+ * update, not a patch.
1376
+ *
1377
+ * The numeric value itself no longer has specific meaning. Historically,
1378
+ * this field was intended so that clients could ignore these broadcasted
1379
+ * full presence messages, but it turned out that getting a full presence
1380
+ * "keyframe" from time to time was useful.
1381
+ *
1382
+ * So nowadays, the presence (pun intended) of this `targetActor` field
1383
+ * is a backward-compatible way of expressing that the `data` contains
1384
+ * all presence fields, and isn't a partial "patch".
1385
+ */
1386
+ readonly targetActor: number;
1387
+ readonly data: TPresence;
1388
+ } | {
1389
+ readonly type: ClientMsgCode.UPDATE_PRESENCE;
1390
+ /**
1391
+ * Absence of the `targetActor` field signifies that this is a Partial
1392
+ * Presence™ "patch".
1393
+ */
1394
+ readonly targetActor?: undefined;
1395
+ readonly data: Partial<TPresence>;
1396
+ };
1397
+ declare type UpdateStorageClientMsg = {
1398
+ readonly type: ClientMsgCode.UPDATE_STORAGE;
1399
+ readonly ops: Op[];
1400
+ };
1401
+ declare type FetchStorageClientMsg = {
1402
+ readonly type: ClientMsgCode.FETCH_STORAGE;
1403
+ };
1404
+
1332
1405
  declare type IdTuple<T> = [id: string, value: T];
1333
1406
  declare enum CrdtType {
1334
1407
  OBJECT = 0,
@@ -1537,72 +1610,46 @@ SerializedCrdt>;
1537
1610
  declare type ParentToChildNodeMap = Map<string, // Parent's node ID
1538
1611
  IdTuple<SerializedChild>[]>;
1539
1612
 
1540
- /**
1541
- * "Plain LSON" is a JSON-based format that's used when serializing Live structures
1542
- * to send them over HTTP (e.g. in the API endpoint to let users upload their initial
1543
- * Room storage, in the API endpoint to fetch a Room's storage, ...).
1544
- *
1545
- * In the client, you would typically create LSON values using:
1546
- *
1547
- * new LiveObject({ x: 0, y: 0 })
1548
- *
1549
- * But over HTTP, this has to be serialized somehow. The "Plain LSON" format
1550
- * is what's used in the POST /init-storage-new endpoint, to allow users to
1551
- * control which parts of their data structure should be considered "Live"
1552
- * objects, and which parts are "normal" objects.
1553
- *
1554
- * So if they have a structure like:
1555
- *
1556
- * { x: 0, y: 0 }
1557
- *
1558
- * And want to make it a Live object, they can serialize it by wrapping it in
1559
- * a special "annotation":
1560
- *
1561
- * {
1562
- * "liveblocksType": "LiveObject",
1563
- * "data": { x: 0, y: 0 },
1564
- * }
1565
- *
1566
- * This "Plain LSON" data format defines exactly those wrappings.
1567
- *
1568
- * To summarize:
1569
- *
1570
- * LSON value | Plain LSON equivalent
1571
- * ----------------------+----------------------------------------------
1572
- * 42 | 42
1573
- * [1, 2, 3] | [1, 2, 3]
1574
- * { x: 0, y: 0 } | { x: 0, y: 0 }
1575
- * ----------------------+----------------------------------------------
1576
- * new LiveList(...) | { liveblocksType: "LiveList", data: ... }
1577
- * new LiveMap(...) | { liveblocksType: "LiveMap", data: ... }
1578
- * new LiveObject(...) | { liveblocksType: "LiveObject", data: ... }
1579
- *
1580
- */
1581
-
1582
- declare type PlainLsonFields = Record<string, PlainLson>;
1583
- declare type PlainLsonObject = {
1584
- liveblocksType: "LiveObject";
1585
- data: PlainLsonFields;
1613
+ declare type JsonTreeNode = {
1614
+ readonly type: "Json";
1615
+ readonly id: string;
1616
+ readonly key: string;
1617
+ readonly payload: Json;
1586
1618
  };
1587
- declare type PlainLsonMap = {
1588
- liveblocksType: "LiveMap";
1589
- data: PlainLsonFields;
1619
+ declare type LiveTreeNode<TName extends `Live${string}` = `Live${string}`> = {
1620
+ readonly type: TName;
1621
+ readonly id: string;
1622
+ readonly key: string;
1623
+ readonly payload: LsonTreeNode[];
1590
1624
  };
1591
- declare type PlainLsonList = {
1592
- liveblocksType: "LiveList";
1593
- data: PlainLson[];
1625
+ declare type LsonTreeNode = LiveTreeNode | JsonTreeNode;
1626
+ declare type UserTreeNode = {
1627
+ readonly type: "User";
1628
+ readonly id: string;
1629
+ readonly key: string;
1630
+ readonly payload: {
1631
+ readonly connectionId: number;
1632
+ readonly id?: string;
1633
+ readonly info?: Json;
1634
+ readonly presence: JsonObject;
1635
+ readonly isReadOnly: boolean;
1636
+ };
1594
1637
  };
1595
- declare type PlainLson = PlainLsonObject | PlainLsonMap | PlainLsonList | Json;
1638
+ declare type TreeNode = LsonTreeNode | UserTreeNode;
1596
1639
 
1597
- declare enum WebsocketCloseCodes {
1598
- CLOSE_ABNORMAL = 1006,
1599
- INVALID_MESSAGE_FORMAT = 4000,
1600
- NOT_ALLOWED = 4001,
1601
- MAX_NUMBER_OF_MESSAGES_PER_SECONDS = 4002,
1602
- MAX_NUMBER_OF_CONCURRENT_CONNECTIONS = 4003,
1603
- MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP = 4004,
1604
- MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM = 4005,
1605
- CLOSE_WITHOUT_RETRY = 4999
1640
+ type DevToolsTreeNode_JsonTreeNode = JsonTreeNode;
1641
+ type DevToolsTreeNode_LiveTreeNode<TName extends `Live${string}` = `Live${string}`> = LiveTreeNode<TName>;
1642
+ type DevToolsTreeNode_LsonTreeNode = LsonTreeNode;
1643
+ type DevToolsTreeNode_UserTreeNode = UserTreeNode;
1644
+ type DevToolsTreeNode_TreeNode = TreeNode;
1645
+ declare namespace DevToolsTreeNode {
1646
+ export {
1647
+ DevToolsTreeNode_JsonTreeNode as JsonTreeNode,
1648
+ DevToolsTreeNode_LiveTreeNode as LiveTreeNode,
1649
+ DevToolsTreeNode_LsonTreeNode as LsonTreeNode,
1650
+ DevToolsTreeNode_UserTreeNode as UserTreeNode,
1651
+ DevToolsTreeNode_TreeNode as TreeNode,
1652
+ };
1606
1653
  }
1607
1654
 
1608
1655
  /**
@@ -1739,4 +1786,4 @@ declare type EnsureJson<T> = [
1739
1786
  [K in keyof T]: EnsureJson<T[K]>;
1740
1787
  };
1741
1788
 
1742
- export { AckOp, AppOnlyAuthToken, AuthToken, BaseUserMeta, BroadcastEventClientMsg, BroadcastOptions, BroadcastedEventServerMsg, Client, ClientMsg, ClientMsgCode, ConnectionStatus, CrdtType, CreateChildOp, CreateListOp, CreateMapOp, CreateObjectOp, CreateOp, CreateRegisterOp, CreateRootObjectOp, DeleteCrdtOp, DeleteObjectKeyOp, DevToolsTreeNode as DevTools, protocol as DevToolsMsg, EnsureJson, FetchStorageClientMsg, History, IdTuple, Immutable, InitialDocumentStateServerMsg, Json, JsonArray, JsonObject, JsonScalar, LiveList, LiveMap, LiveNode, LiveObject, LiveStructure, Lson, LsonObject, NodeMap, Op, OpCode, Others, ParentToChildNodeMap, PlainLson, PlainLsonFields, PlainLsonList, PlainLsonMap, PlainLsonObject, RejectedStorageOpServerMsg, Resolve, Room, RoomAuthToken, RoomInitializers, RoomStateServerMsg, SerializedChild, SerializedCrdt, SerializedList, SerializedMap, SerializedObject, SerializedRegister, SerializedRootObject, ServerMsg, ServerMsgCode, SetParentKeyOp, StorageStatus, StorageUpdate, ToImmutable, ToJson, UpdateObjectOp, UpdatePresenceClientMsg, UpdatePresenceServerMsg, UpdateStorageClientMsg, UpdateStorageServerMsg, User, UserJoinServerMsg, UserLeftServerMsg, WebsocketCloseCodes, asArrayWithLegacyMethods, asPos, assert, assertNever, b64decode, createClient, deprecate, deprecateIf, errorIf, freeze, isAppOnlyAuthToken, isAuthToken, isChildCrdt, isJsonArray, isJsonObject, isJsonScalar, isPlainObject, isRoomAuthToken, isRootCrdt, legacy_patchImmutableObject, lsonToJson, makePosition, nn, patchLiveObjectKey, shallow, throwUsageError, tryParseJson };
1789
+ export { AckOp, AppOnlyAuthToken, AuthToken, BaseUserMeta, BroadcastEventClientMsg, BroadcastOptions, BroadcastedEventServerMsg, Client, ClientMsg, ClientMsgCode, ConnectionStatus, CrdtType, CreateChildOp, CreateListOp, CreateMapOp, CreateObjectOp, CreateOp, CreateRegisterOp, CreateRootObjectOp, DeleteCrdtOp, DeleteObjectKeyOp, DevToolsTreeNode as DevTools, protocol as DevToolsMsg, EnsureJson, FetchStorageClientMsg, History, IWebSocket, IWebSocketCloseEvent, IWebSocketEvent, IWebSocketInstance, IWebSocketMessageEvent, IdTuple, Immutable, InitialDocumentStateServerMsg, Json, JsonArray, JsonObject, JsonScalar, LiveList, LiveListUpdate, LiveMap, LiveMapUpdate, LiveNode, LiveObject, LiveObjectUpdate, LiveStructure, Lson, LsonObject, NodeMap, Op, OpCode, Others, ParentToChildNodeMap, PlainLson, PlainLsonFields, PlainLsonList, PlainLsonMap, PlainLsonObject, RejectedStorageOpServerMsg, Resolve, Room, RoomAuthToken, RoomInitializers, RoomStateServerMsg, SerializedChild, SerializedCrdt, SerializedList, SerializedMap, SerializedObject, SerializedRegister, SerializedRootObject, ServerMsg, ServerMsgCode, SetParentKeyOp, StorageStatus, StorageUpdate, ToImmutable, ToJson, UpdateObjectOp, UpdatePresenceClientMsg, UpdatePresenceServerMsg, UpdateStorageClientMsg, UpdateStorageServerMsg, User, UserJoinServerMsg, UserLeftServerMsg, WebsocketCloseCodes, asArrayWithLegacyMethods, asPos, assert, assertNever, b64decode, createClient, deprecate, deprecateIf, errorIf, freeze, isAppOnlyAuthToken, isAuthToken, isChildCrdt, isJsonArray, isJsonObject, isJsonScalar, isPlainObject, isRoomAuthToken, isRootCrdt, legacy_patchImmutableObject, lsonToJson, makePosition, nn, patchLiveObjectKey, shallow, throwUsageError, toPlainLson, tryParseJson };