@liveblocks/core 1.1.6 → 1.1.8

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.
package/dist/index.d.ts CHANGED
@@ -557,6 +557,19 @@ declare type Resolve<T> = T extends (...args: unknown[]) => unknown ? T : {
557
557
  [K in keyof T]: T[K];
558
558
  };
559
559
 
560
+ declare type CustomAuthenticationResult = {
561
+ token: string;
562
+ error?: never;
563
+ } | {
564
+ token?: never;
565
+ error: "forbidden";
566
+ reason: string;
567
+ } | {
568
+ token?: never;
569
+ error: string;
570
+ reason: string;
571
+ };
572
+
560
573
  /**
561
574
  * This type is used by clients to define the metadata for a user.
562
575
  */
@@ -676,6 +689,212 @@ declare type Delegates<T extends BaseAuthResult> = {
676
689
  createSocket: (token: T) => IWebSocketInstance;
677
690
  };
678
691
 
692
+ declare type IdTuple<T> = [id: string, value: T];
693
+ declare enum CrdtType {
694
+ OBJECT = 0,
695
+ LIST = 1,
696
+ MAP = 2,
697
+ REGISTER = 3
698
+ }
699
+ declare type SerializedCrdt = SerializedRootObject | SerializedChild;
700
+ declare type SerializedChild = SerializedObject | SerializedList | SerializedMap | SerializedRegister;
701
+ declare type SerializedRootObject = {
702
+ readonly type: CrdtType.OBJECT;
703
+ readonly data: JsonObject;
704
+ readonly parentId?: never;
705
+ readonly parentKey?: never;
706
+ };
707
+ declare type SerializedObject = {
708
+ readonly type: CrdtType.OBJECT;
709
+ readonly parentId: string;
710
+ readonly parentKey: string;
711
+ readonly data: JsonObject;
712
+ };
713
+ declare type SerializedList = {
714
+ readonly type: CrdtType.LIST;
715
+ readonly parentId: string;
716
+ readonly parentKey: string;
717
+ };
718
+ declare type SerializedMap = {
719
+ readonly type: CrdtType.MAP;
720
+ readonly parentId: string;
721
+ readonly parentKey: string;
722
+ };
723
+ declare type SerializedRegister = {
724
+ readonly type: CrdtType.REGISTER;
725
+ readonly parentId: string;
726
+ readonly parentKey: string;
727
+ readonly data: Json;
728
+ };
729
+ declare function isRootCrdt(crdt: SerializedCrdt): crdt is SerializedRootObject;
730
+ declare function isChildCrdt(crdt: SerializedCrdt): crdt is SerializedChild;
731
+
732
+ declare enum ServerMsgCode {
733
+ UPDATE_PRESENCE = 100,
734
+ USER_JOINED = 101,
735
+ USER_LEFT = 102,
736
+ BROADCASTED_EVENT = 103,
737
+ ROOM_STATE = 104,
738
+ INITIAL_STORAGE_STATE = 200,
739
+ UPDATE_STORAGE = 201,
740
+ REJECT_STORAGE_OP = 299,
741
+ UPDATE_YDOC = 300
742
+ }
743
+ /**
744
+ * Messages that can be sent from the server to the client.
745
+ */
746
+ declare type ServerMsg<TPresence extends JsonObject, TUserMeta extends BaseUserMeta, TRoomEvent extends Json> = UpdatePresenceServerMsg<TPresence> | UserJoinServerMsg<TUserMeta> | UserLeftServerMsg | BroadcastedEventServerMsg<TRoomEvent> | RoomStateServerMsg<TUserMeta> | InitialDocumentStateServerMsg | UpdateStorageServerMsg | RejectedStorageOpServerMsg | YDocUpdate;
747
+ /**
748
+ * Sent by the WebSocket server and broadcasted to all clients to announce that
749
+ * a User updated their presence. For example, when a user moves their cursor.
750
+ *
751
+ * In most cases, the data payload will only include the fields from the
752
+ * Presence that have been changed since the last announcement. However, after
753
+ * a new user joins a room, a "full presence" will be announced so the newly
754
+ * connected user will get each other's user full presence at least once. In
755
+ * those cases, the `targetActor` field indicates the newly connected client,
756
+ * so all other existing clients can ignore this broadcasted message.
757
+ */
758
+ declare type UpdatePresenceServerMsg<TPresence extends JsonObject> = {
759
+ readonly type: ServerMsgCode.UPDATE_PRESENCE;
760
+ /**
761
+ * The User whose Presence has changed.
762
+ */
763
+ readonly actor: number;
764
+ /**
765
+ * When set, signifies that this is a Full Presence™ update, not a patch.
766
+ *
767
+ * The numeric value itself no longer has specific meaning. Historically,
768
+ * this field was intended so that clients could ignore these broadcasted
769
+ * full presence messages, but it turned out that getting a full presence
770
+ * "keyframe" from time to time was useful.
771
+ *
772
+ * So nowadays, the presence (pun intended) of this `targetActor` field
773
+ * is a backward-compatible way of expressing that the `data` contains
774
+ * all presence fields, and isn't a partial "patch".
775
+ */
776
+ readonly targetActor: number;
777
+ /**
778
+ * The partial or full Presence of a User. If the `targetActor` field is set,
779
+ * this will be the full Presence, otherwise it only contain the fields that
780
+ * have changed since the last broadcast.
781
+ */
782
+ readonly data: TPresence;
783
+ } | {
784
+ readonly type: ServerMsgCode.UPDATE_PRESENCE;
785
+ /**
786
+ * The User whose Presence has changed.
787
+ */
788
+ readonly actor: number;
789
+ /**
790
+ * Not set for partial presence updates.
791
+ */
792
+ readonly targetActor?: undefined;
793
+ /**
794
+ * A partial Presence patch to apply to the User. It will only contain the
795
+ * fields that have changed since the last broadcast.
796
+ */
797
+ readonly data: Partial<TPresence>;
798
+ };
799
+ /**
800
+ * Sent by the WebSocket server and broadcasted to all clients to announce that
801
+ * a new User has joined the Room.
802
+ */
803
+ declare type UserJoinServerMsg<TUserMeta extends BaseUserMeta> = {
804
+ readonly type: ServerMsgCode.USER_JOINED;
805
+ readonly actor: number;
806
+ /**
807
+ * The id of the User that has been set in the authentication endpoint.
808
+ * Useful to get additional information about the connected user.
809
+ */
810
+ readonly id: TUserMeta["id"];
811
+ /**
812
+ * Additional user information that has been set in the authentication
813
+ * endpoint.
814
+ */
815
+ readonly info: TUserMeta["info"];
816
+ /**
817
+ * Permissions that the user has in the Room.
818
+ */
819
+ readonly scopes: string[];
820
+ };
821
+ /**
822
+ * Sent by the WebSocket server and broadcasted to all clients to announce that
823
+ * a new User has left the Room.
824
+ */
825
+ declare type UserLeftServerMsg = {
826
+ readonly type: ServerMsgCode.USER_LEFT;
827
+ readonly actor: number;
828
+ };
829
+ /**
830
+ * Sent by the WebSocket server when the ydoc is updated or when requested based on stateVector passed.
831
+ * Contains a base64 encoded update
832
+ */
833
+ declare type YDocUpdate = {
834
+ readonly type: ServerMsgCode.UPDATE_YDOC;
835
+ readonly update: string;
836
+ readonly isSync: boolean;
837
+ };
838
+ /**
839
+ * Sent by the WebSocket server and broadcasted to all clients to announce that
840
+ * a User broadcasted an Event to everyone in the Room.
841
+ */
842
+ declare type BroadcastedEventServerMsg<TRoomEvent extends Json> = {
843
+ readonly type: ServerMsgCode.BROADCASTED_EVENT;
844
+ /**
845
+ * The User who broadcasted the Event.
846
+ */
847
+ readonly actor: number;
848
+ /**
849
+ * The arbitrary payload of the Event. This can be any JSON value. Clients
850
+ * will have to manually verify/decode this event.
851
+ */
852
+ readonly event: TRoomEvent;
853
+ };
854
+ /**
855
+ * Sent by the WebSocket server to a single client in response to the client
856
+ * joining the Room, to provide the initial state of the Room. The payload
857
+ * includes a list of all other Users that already are in the Room.
858
+ */
859
+ declare type RoomStateServerMsg<TUserMeta extends BaseUserMeta> = {
860
+ readonly type: ServerMsgCode.ROOM_STATE;
861
+ readonly users: {
862
+ readonly [actor: number]: TUserMeta & {
863
+ scopes: string[];
864
+ };
865
+ };
866
+ };
867
+ /**
868
+ * Sent by the WebSocket server to a single client in response to the client
869
+ * joining the Room, to provide the initial Storage state of the Room. The
870
+ * payload includes the entire Storage document.
871
+ */
872
+ declare type InitialDocumentStateServerMsg = {
873
+ readonly type: ServerMsgCode.INITIAL_STORAGE_STATE;
874
+ readonly items: IdTuple<SerializedCrdt>[];
875
+ };
876
+ /**
877
+ * Sent by the WebSocket server and broadcasted to all clients to announce that
878
+ * a change occurred in the Storage document.
879
+ *
880
+ * The payload of this message contains a list of Ops (aka incremental
881
+ * mutations to make to the initially loaded document).
882
+ */
883
+ declare type UpdateStorageServerMsg = {
884
+ readonly type: ServerMsgCode.UPDATE_STORAGE;
885
+ readonly ops: Op[];
886
+ };
887
+ /**
888
+ * Sent by the WebSocket server to the client to indicate that certain opIds
889
+ * have been received but were rejected because they caused mutations that are
890
+ * incompatible with the Room's schema.
891
+ */
892
+ declare type RejectedStorageOpServerMsg = {
893
+ readonly type: ServerMsgCode.REJECT_STORAGE_OP;
894
+ readonly opIds: string[];
895
+ readonly reason: string;
896
+ };
897
+
679
898
  declare type ReadonlyArrayWithLegacyMethods<T> = readonly T[] & {
680
899
  /**
681
900
  * @deprecated Prefer the normal .length property on arrays.
@@ -1112,7 +1331,8 @@ declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUs
1112
1331
  connectionId: number;
1113
1332
  event: TRoomEvent;
1114
1333
  }>;
1115
- readonly me: Observable<TPresence>;
1334
+ readonly self: Observable<User<TPresence, TUserMeta>>;
1335
+ readonly myPresence: Observable<TPresence>;
1116
1336
  readonly others: Observable<{
1117
1337
  others: Others<TPresence, TUserMeta>;
1118
1338
  event: OthersEvent<TPresence, TUserMeta>;
@@ -1127,7 +1347,7 @@ declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUs
1127
1347
  */
1128
1348
  readonly storageDidLoad: Observable<void>;
1129
1349
  readonly storageStatus: Observable<StorageStatus>;
1130
- readonly ydoc: Observable<string>;
1350
+ readonly ydoc: Observable<YDocUpdate>;
1131
1351
  };
1132
1352
  /**
1133
1353
  * Batches modifications made during the given function.
@@ -1212,9 +1432,7 @@ declare type Client = {
1212
1432
  */
1213
1433
  leave(roomId: string): void;
1214
1434
  };
1215
- declare type AuthEndpoint = string | ((room: string) => Promise<{
1216
- token: string;
1217
- }>);
1435
+ declare type AuthEndpoint = string | ((room: string) => Promise<CustomAuthenticationResult>);
1218
1436
  /**
1219
1437
  * The authentication endpoint that is called to ensure that the current user has access to a room.
1220
1438
  * Can be an url or a callback if you need to add additional headers.
@@ -1504,211 +1722,6 @@ declare type UpdateYDocClientMsg = {
1504
1722
  readonly update: string;
1505
1723
  };
1506
1724
 
1507
- declare type IdTuple<T> = [id: string, value: T];
1508
- declare enum CrdtType {
1509
- OBJECT = 0,
1510
- LIST = 1,
1511
- MAP = 2,
1512
- REGISTER = 3
1513
- }
1514
- declare type SerializedCrdt = SerializedRootObject | SerializedChild;
1515
- declare type SerializedChild = SerializedObject | SerializedList | SerializedMap | SerializedRegister;
1516
- declare type SerializedRootObject = {
1517
- readonly type: CrdtType.OBJECT;
1518
- readonly data: JsonObject;
1519
- readonly parentId?: never;
1520
- readonly parentKey?: never;
1521
- };
1522
- declare type SerializedObject = {
1523
- readonly type: CrdtType.OBJECT;
1524
- readonly parentId: string;
1525
- readonly parentKey: string;
1526
- readonly data: JsonObject;
1527
- };
1528
- declare type SerializedList = {
1529
- readonly type: CrdtType.LIST;
1530
- readonly parentId: string;
1531
- readonly parentKey: string;
1532
- };
1533
- declare type SerializedMap = {
1534
- readonly type: CrdtType.MAP;
1535
- readonly parentId: string;
1536
- readonly parentKey: string;
1537
- };
1538
- declare type SerializedRegister = {
1539
- readonly type: CrdtType.REGISTER;
1540
- readonly parentId: string;
1541
- readonly parentKey: string;
1542
- readonly data: Json;
1543
- };
1544
- declare function isRootCrdt(crdt: SerializedCrdt): crdt is SerializedRootObject;
1545
- declare function isChildCrdt(crdt: SerializedCrdt): crdt is SerializedChild;
1546
-
1547
- declare enum ServerMsgCode {
1548
- UPDATE_PRESENCE = 100,
1549
- USER_JOINED = 101,
1550
- USER_LEFT = 102,
1551
- BROADCASTED_EVENT = 103,
1552
- ROOM_STATE = 104,
1553
- INITIAL_STORAGE_STATE = 200,
1554
- UPDATE_STORAGE = 201,
1555
- REJECT_STORAGE_OP = 299,
1556
- UPDATE_YDOC = 300
1557
- }
1558
- /**
1559
- * Messages that can be sent from the server to the client.
1560
- */
1561
- declare type ServerMsg<TPresence extends JsonObject, TUserMeta extends BaseUserMeta, TRoomEvent extends Json> = UpdatePresenceServerMsg<TPresence> | UserJoinServerMsg<TUserMeta> | UserLeftServerMsg | BroadcastedEventServerMsg<TRoomEvent> | RoomStateServerMsg<TUserMeta> | InitialDocumentStateServerMsg | UpdateStorageServerMsg | RejectedStorageOpServerMsg | YDocUpdate;
1562
- /**
1563
- * Sent by the WebSocket server and broadcasted to all clients to announce that
1564
- * a User updated their presence. For example, when a user moves their cursor.
1565
- *
1566
- * In most cases, the data payload will only include the fields from the
1567
- * Presence that have been changed since the last announcement. However, after
1568
- * a new user joins a room, a "full presence" will be announced so the newly
1569
- * connected user will get each other's user full presence at least once. In
1570
- * those cases, the `targetActor` field indicates the newly connected client,
1571
- * so all other existing clients can ignore this broadcasted message.
1572
- */
1573
- declare type UpdatePresenceServerMsg<TPresence extends JsonObject> = {
1574
- readonly type: ServerMsgCode.UPDATE_PRESENCE;
1575
- /**
1576
- * The User whose Presence has changed.
1577
- */
1578
- readonly actor: number;
1579
- /**
1580
- * When set, signifies that this is a Full Presence™ update, not a patch.
1581
- *
1582
- * The numeric value itself no longer has specific meaning. Historically,
1583
- * this field was intended so that clients could ignore these broadcasted
1584
- * full presence messages, but it turned out that getting a full presence
1585
- * "keyframe" from time to time was useful.
1586
- *
1587
- * So nowadays, the presence (pun intended) of this `targetActor` field
1588
- * is a backward-compatible way of expressing that the `data` contains
1589
- * all presence fields, and isn't a partial "patch".
1590
- */
1591
- readonly targetActor: number;
1592
- /**
1593
- * The partial or full Presence of a User. If the `targetActor` field is set,
1594
- * this will be the full Presence, otherwise it only contain the fields that
1595
- * have changed since the last broadcast.
1596
- */
1597
- readonly data: TPresence;
1598
- } | {
1599
- readonly type: ServerMsgCode.UPDATE_PRESENCE;
1600
- /**
1601
- * The User whose Presence has changed.
1602
- */
1603
- readonly actor: number;
1604
- /**
1605
- * Not set for partial presence updates.
1606
- */
1607
- readonly targetActor?: undefined;
1608
- /**
1609
- * A partial Presence patch to apply to the User. It will only contain the
1610
- * fields that have changed since the last broadcast.
1611
- */
1612
- readonly data: Partial<TPresence>;
1613
- };
1614
- /**
1615
- * Sent by the WebSocket server and broadcasted to all clients to announce that
1616
- * a new User has joined the Room.
1617
- */
1618
- declare type UserJoinServerMsg<TUserMeta extends BaseUserMeta> = {
1619
- readonly type: ServerMsgCode.USER_JOINED;
1620
- readonly actor: number;
1621
- /**
1622
- * The id of the User that has been set in the authentication endpoint.
1623
- * Useful to get additional information about the connected user.
1624
- */
1625
- readonly id: TUserMeta["id"];
1626
- /**
1627
- * Additional user information that has been set in the authentication
1628
- * endpoint.
1629
- */
1630
- readonly info: TUserMeta["info"];
1631
- /**
1632
- * Permissions that the user has in the Room.
1633
- */
1634
- readonly scopes: string[];
1635
- };
1636
- /**
1637
- * Sent by the WebSocket server and broadcasted to all clients to announce that
1638
- * a new User has left the Room.
1639
- */
1640
- declare type UserLeftServerMsg = {
1641
- readonly type: ServerMsgCode.USER_LEFT;
1642
- readonly actor: number;
1643
- };
1644
- /**
1645
- * Sent by the WebSocket server when the ydoc is updated or when requested based on stateVector passed.
1646
- * Contains a base64 encoded update
1647
- */
1648
- declare type YDocUpdate = {
1649
- readonly type: ServerMsgCode.UPDATE_YDOC;
1650
- readonly update: string;
1651
- };
1652
- /**
1653
- * Sent by the WebSocket server and broadcasted to all clients to announce that
1654
- * a User broadcasted an Event to everyone in the Room.
1655
- */
1656
- declare type BroadcastedEventServerMsg<TRoomEvent extends Json> = {
1657
- readonly type: ServerMsgCode.BROADCASTED_EVENT;
1658
- /**
1659
- * The User who broadcasted the Event.
1660
- */
1661
- readonly actor: number;
1662
- /**
1663
- * The arbitrary payload of the Event. This can be any JSON value. Clients
1664
- * will have to manually verify/decode this event.
1665
- */
1666
- readonly event: TRoomEvent;
1667
- };
1668
- /**
1669
- * Sent by the WebSocket server to a single client in response to the client
1670
- * joining the Room, to provide the initial state of the Room. The payload
1671
- * includes a list of all other Users that already are in the Room.
1672
- */
1673
- declare type RoomStateServerMsg<TUserMeta extends BaseUserMeta> = {
1674
- readonly type: ServerMsgCode.ROOM_STATE;
1675
- readonly users: {
1676
- readonly [actor: number]: TUserMeta & {
1677
- scopes: string[];
1678
- };
1679
- };
1680
- };
1681
- /**
1682
- * Sent by the WebSocket server to a single client in response to the client
1683
- * joining the Room, to provide the initial Storage state of the Room. The
1684
- * payload includes the entire Storage document.
1685
- */
1686
- declare type InitialDocumentStateServerMsg = {
1687
- readonly type: ServerMsgCode.INITIAL_STORAGE_STATE;
1688
- readonly items: IdTuple<SerializedCrdt>[];
1689
- };
1690
- /**
1691
- * Sent by the WebSocket server and broadcasted to all clients to announce that
1692
- * a change occurred in the Storage document.
1693
- *
1694
- * The payload of this message contains a list of Ops (aka incremental
1695
- * mutations to make to the initially loaded document).
1696
- */
1697
- declare type UpdateStorageServerMsg = {
1698
- readonly type: ServerMsgCode.UPDATE_STORAGE;
1699
- readonly ops: Op[];
1700
- };
1701
- /**
1702
- * Sent by the WebSocket server to the client to indicate that certain opIds
1703
- * have been received but were rejected because they caused mutations that are
1704
- * incompatible with the Room's schema.
1705
- */
1706
- declare type RejectedStorageOpServerMsg = {
1707
- readonly type: ServerMsgCode.REJECT_STORAGE_OP;
1708
- readonly opIds: string[];
1709
- readonly reason: string;
1710
- };
1711
-
1712
1725
  /**
1713
1726
  * Lookup table for nodes (= SerializedCrdt values) by their IDs.
1714
1727
  */
@@ -1884,4 +1897,4 @@ declare type EnsureJson<T> = [
1884
1897
  [K in keyof T]: EnsureJson<T[K]>;
1885
1898
  };
1886
1899
 
1887
- export { AckOp, BaseAuthResult, BaseUserMeta, BroadcastEventClientMsg, BroadcastOptions, BroadcastedEventServerMsg, Client, ClientMsg, ClientMsgCode, CrdtType, CreateChildOp, CreateListOp, CreateMapOp, CreateObjectOp, CreateOp, CreateRegisterOp, CreateRootObjectOp, Delegates, DeleteCrdtOp, DeleteObjectKeyOp, DevToolsTreeNode as DevTools, protocol as DevToolsMsg, EnsureJson, FetchStorageClientMsg, FetchYDocClientMsg, History, IWebSocket, IWebSocketCloseEvent, IWebSocketEvent, IWebSocketInstance, IWebSocketMessageEvent, IdTuple, Immutable, InitialDocumentStateServerMsg, Json, JsonArray, JsonObject, JsonScalar, LegacyConnectionStatus, LiveList, LiveListUpdate, LiveMap, LiveMapUpdate, LiveNode, LiveObject, LiveObjectUpdate, LiveStructure, LostConnectionEvent, Lson, LsonObject, NodeMap, Op, OpCode, Others, ParentToChildNodeMap, PlainLson, PlainLsonFields, PlainLsonList, PlainLsonMap, PlainLsonObject, RejectedStorageOpServerMsg, Resolve, Room, RoomInitializers, RoomStateServerMsg, SerializedChild, SerializedCrdt, SerializedList, SerializedMap, SerializedObject, SerializedRegister, SerializedRootObject, ServerMsg, ServerMsgCode, SetParentKeyOp, Status, StorageStatus, StorageUpdate, ToImmutable, ToJson, UpdateObjectOp, UpdatePresenceClientMsg, UpdatePresenceServerMsg, UpdateStorageClientMsg, UpdateStorageServerMsg, UpdateYDocClientMsg, User, UserJoinServerMsg, UserLeftServerMsg, WebsocketCloseCodes, asArrayWithLegacyMethods, asPos, assert, assertNever, b64decode, createClient, deprecate, deprecateIf, detectDupes, errorIf, freeze, isChildCrdt, isJsonArray, isJsonObject, isJsonScalar, isPlainObject, isRootCrdt, legacy_patchImmutableObject, lsonToJson, makePosition, nn, patchLiveObjectKey, shallow, throwUsageError, toPlainLson, tryParseJson, withTimeout };
1900
+ export { AckOp, BaseAuthResult, BaseUserMeta, BroadcastEventClientMsg, BroadcastOptions, BroadcastedEventServerMsg, Client, ClientMsg, ClientMsgCode, CrdtType, CreateChildOp, CreateListOp, CreateMapOp, CreateObjectOp, CreateOp, CreateRegisterOp, CreateRootObjectOp, CustomAuthenticationResult, Delegates, DeleteCrdtOp, DeleteObjectKeyOp, DevToolsTreeNode as DevTools, protocol as DevToolsMsg, EnsureJson, FetchStorageClientMsg, FetchYDocClientMsg, History, IWebSocket, IWebSocketCloseEvent, IWebSocketEvent, IWebSocketInstance, IWebSocketMessageEvent, IdTuple, Immutable, InitialDocumentStateServerMsg, Json, JsonArray, JsonObject, JsonScalar, LegacyConnectionStatus, LiveList, LiveListUpdate, LiveMap, LiveMapUpdate, LiveNode, LiveObject, LiveObjectUpdate, LiveStructure, LostConnectionEvent, Lson, LsonObject, NodeMap, Op, OpCode, Others, ParentToChildNodeMap, PlainLson, PlainLsonFields, PlainLsonList, PlainLsonMap, PlainLsonObject, RejectedStorageOpServerMsg, Resolve, Room, RoomInitializers, RoomStateServerMsg, SerializedChild, SerializedCrdt, SerializedList, SerializedMap, SerializedObject, SerializedRegister, SerializedRootObject, ServerMsg, ServerMsgCode, SetParentKeyOp, Status, StorageStatus, StorageUpdate, ToImmutable, ToJson, UpdateObjectOp, UpdatePresenceClientMsg, UpdatePresenceServerMsg, UpdateStorageClientMsg, UpdateStorageServerMsg, UpdateYDocClientMsg, User, UserJoinServerMsg, UserLeftServerMsg, WebsocketCloseCodes, asArrayWithLegacyMethods, asPos, assert, assertNever, b64decode, createClient, deprecate, deprecateIf, detectDupes, errorIf, freeze, isChildCrdt, isJsonArray, isJsonObject, isJsonScalar, isPlainObject, isRootCrdt, legacy_patchImmutableObject, lsonToJson, makePosition, nn, patchLiveObjectKey, shallow, throwUsageError, toPlainLson, tryParseJson, withTimeout };
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/version.ts
2
2
  var PKG_NAME = "@liveblocks/core";
3
- var PKG_VERSION = "1.1.6";
3
+ var PKG_VERSION = "1.1.8";
4
4
  var PKG_FORMAT = "cjs";
5
5
 
6
6
  // src/dupe-detection.ts
@@ -195,7 +195,7 @@ function startSyncStream(room) {
195
195
  // Any time storage updates, send the new storage root
196
196
  room.events.storage.subscribe(() => partialSyncStorage(room)),
197
197
  // Any time "me" or "others" updates, send the new values accordingly
198
- room.events.me.subscribe(() => partialSyncMe(room)),
198
+ room.events.self.subscribe(() => partialSyncMe(room)),
199
199
  room.events.others.subscribe(() => partialSyncOthers(room))
200
200
  ]);
201
201
  }
@@ -3893,16 +3893,11 @@ function isJsonObject(data) {
3893
3893
  }
3894
3894
 
3895
3895
  // src/protocol/AuthToken.ts
3896
- function isTokenExpired(token) {
3897
- const now = Date.now() / 1e3;
3898
- const valid = now <= token.exp - 300 && now >= token.iat - 300;
3899
- return !valid;
3900
- }
3901
3896
  function isStringList(value) {
3902
3897
  return Array.isArray(value) && value.every((i) => typeof i === "string");
3903
3898
  }
3904
3899
  function isMinimalTokenPayload(data) {
3905
- return isPlainObject(data) && typeof data.iat === "number" && typeof data.exp === "number" && typeof data.actor === "number" && (data.id === void 0 || typeof data.id === "string") && isStringList(data.scopes);
3900
+ return isPlainObject(data) && typeof data.actor === "number" && (data.id === void 0 || typeof data.id === "string") && isStringList(data.scopes);
3906
3901
  }
3907
3902
  function parseAuthToken(rawTokenString) {
3908
3903
  const tokenParts = rawTokenString.split(".");
@@ -4243,6 +4238,7 @@ function createRoom(options, config) {
4243
4238
  batchUpdates(() => {
4244
4239
  eventHub.status.notify(newStatus);
4245
4240
  eventHub.connection.notify(newToLegacyStatus(newStatus));
4241
+ notifySelfChanged(doNotBatchUpdates);
4246
4242
  });
4247
4243
  }
4248
4244
  let _connectionLossTimerId;
@@ -4366,7 +4362,8 @@ function createRoom(options, config) {
4366
4362
  // New/recommended API
4367
4363
  lostConnection: makeEventSource(),
4368
4364
  customEvent: makeEventSource(),
4369
- me: makeEventSource(),
4365
+ self: makeEventSource(),
4366
+ myPresence: makeEventSource(),
4370
4367
  others: makeEventSource(),
4371
4368
  error: makeEventSource(),
4372
4369
  storage: makeEventSource(),
@@ -4380,15 +4377,16 @@ function createRoom(options, config) {
4380
4377
  if (config.unstable_fallbackToHTTP) {
4381
4378
  const size = new TextEncoder().encode(message).length;
4382
4379
  if (size > MAX_MESSAGE_SIZE && _optionalChain([managedSocket, 'access', _117 => _117.token, 'optionalAccess', _118 => _118.raw]) && config.httpSendEndpoint) {
4383
- if (isTokenExpired(managedSocket.token.parsed)) {
4384
- return managedSocket.reconnect();
4385
- }
4386
4380
  void httpSend(
4387
4381
  message,
4388
4382
  managedSocket.token.raw,
4389
4383
  config.httpSendEndpoint,
4390
4384
  _optionalChain([config, 'access', _119 => _119.polyfills, 'optionalAccess', _120 => _120.fetch])
4391
- );
4385
+ ).then((resp) => {
4386
+ if (!resp.ok && resp.status === 403) {
4387
+ managedSocket.reconnect();
4388
+ }
4389
+ });
4392
4390
  warn(
4393
4391
  "Message was too large for websockets and sent over HTTP instead"
4394
4392
  );
@@ -4410,6 +4408,16 @@ function createRoom(options, config) {
4410
4408
  } : null;
4411
4409
  }
4412
4410
  );
4411
+ let _lastSelf;
4412
+ function notifySelfChanged(batchedUpdatesWrapper) {
4413
+ const currSelf = self.current;
4414
+ if (currSelf !== null && currSelf !== _lastSelf) {
4415
+ batchedUpdatesWrapper(() => {
4416
+ eventHub.self.notify(currSelf);
4417
+ });
4418
+ _lastSelf = currSelf;
4419
+ }
4420
+ }
4413
4421
  const selfAsTreeNode = new DerivedRef(
4414
4422
  self,
4415
4423
  (me) => me !== null ? userToTreeNode("Me", me) : null
@@ -4468,12 +4476,14 @@ function createRoom(options, config) {
4468
4476
  }
4469
4477
  }
4470
4478
  if (presence) {
4471
- eventHub.me.notify(context.me.current);
4479
+ notifySelfChanged(doNotBatchUpdates);
4480
+ eventHub.myPresence.notify(context.me.current);
4472
4481
  }
4473
4482
  if (storageUpdates.size > 0) {
4474
4483
  const updates = Array.from(storageUpdates.values());
4475
4484
  eventHub.storage.notify(updates);
4476
4485
  }
4486
+ notifyStorageStatus();
4477
4487
  });
4478
4488
  }
4479
4489
  function getConnectionId() {
@@ -4549,7 +4559,6 @@ function createRoom(options, config) {
4549
4559
  }
4550
4560
  }
4551
4561
  }
4552
- notifyStorageStatus();
4553
4562
  return {
4554
4563
  ops,
4555
4564
  reverse: output.reverse,
@@ -4793,7 +4802,7 @@ function createRoom(options, config) {
4793
4802
  break;
4794
4803
  }
4795
4804
  case 300 /* UPDATE_YDOC */: {
4796
- eventHub.ydoc.notify(message.update);
4805
+ eventHub.ydoc.notify(message);
4797
4806
  break;
4798
4807
  }
4799
4808
  case 104 /* ROOM_STATE */: {
@@ -5101,7 +5110,8 @@ ${Array.from(traces).join("\n\n")}`
5101
5110
  lostConnection: eventHub.lostConnection.observable,
5102
5111
  customEvent: eventHub.customEvent.observable,
5103
5112
  others: eventHub.others.observable,
5104
- me: eventHub.me.observable,
5113
+ self: eventHub.self.observable,
5114
+ myPresence: eventHub.myPresence.observable,
5105
5115
  error: eventHub.error.observable,
5106
5116
  storage: eventHub.storage.observable,
5107
5117
  history: eventHub.history.observable,
@@ -5207,7 +5217,7 @@ function makeClassicSubscribeFn(events) {
5207
5217
  callback
5208
5218
  );
5209
5219
  case "my-presence":
5210
- return events.me.subscribe(callback);
5220
+ return events.myPresence.subscribe(callback);
5211
5221
  case "others": {
5212
5222
  const cb = callback;
5213
5223
  return events.others.subscribe(
@@ -5315,12 +5325,25 @@ function makeAuthDelegateForRoom(roomId, authentication, fetchPolyfill) {
5315
5325
  } else if (authentication.type === "custom") {
5316
5326
  return async () => {
5317
5327
  const response = await authentication.callback(roomId);
5318
- if (!response || !response.token) {
5328
+ if (!response || typeof response !== "object") {
5329
+ throw new Error(
5330
+ 'We expect the authentication callback to return a token, but it does not. Hint: the return value should look like: { token: "..." }'
5331
+ );
5332
+ }
5333
+ if (typeof response.token === "string") {
5334
+ return parseAuthToken(response.token);
5335
+ } else if (typeof response.error === "string") {
5336
+ const reason = `Authentication failed: ${"reason" in response && typeof response.reason === "string" ? response.reason : "Forbidden"}`;
5337
+ if (response.error === "forbidden") {
5338
+ throw new StopRetrying(reason);
5339
+ } else {
5340
+ throw new Error(reason);
5341
+ }
5342
+ } else {
5319
5343
  throw new Error(
5320
5344
  'We expect the authentication callback to return a token, but it does not. Hint: the return value should look like: { token: "..." }'
5321
5345
  );
5322
5346
  }
5323
- return parseAuthToken(response.token);
5324
5347
  };
5325
5348
  } else {
5326
5349
  throw new Error("Internal error. Unexpected authentication type");