@liveblocks/client 0.16.11 → 0.16.14

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/index.js CHANGED
@@ -324,7 +324,7 @@ function makeStateMachine(state, context, mockedEffects) {
324
324
  }
325
325
  function onMessage(event) {
326
326
  if ("pong" !== event.data) {
327
- var text, data, messages = (text = event.data, void 0 === (data = LiveObject.parseJson(text)) ? null : LiveObject.isJsonArray(data) ? LiveObject.compact(data.map((function(item) {
327
+ var text, data, messages = (text = event.data, void 0 === (data = LiveObject.tryParseJson(text)) ? null : LiveObject.isJsonArray(data) ? LiveObject.compact(data.map((function(item) {
328
328
  return parseServerMessage(item);
329
329
  }))) : LiveObject.compact([ parseServerMessage(data) ]));
330
330
  if (null !== messages && 0 !== messages.length) {
@@ -758,7 +758,7 @@ var LiveblocksError = function(_Error) {
758
758
  function parseToken(token) {
759
759
  var tokenParts = token.split(".");
760
760
  if (3 !== tokenParts.length) throw new Error("Authentication error. Liveblocks could not parse the response of your authentication endpoint");
761
- var data = LiveObject.parseJson(atob(tokenParts[1]));
761
+ var data = LiveObject.tryParseJson(LiveObject.b64decode(tokenParts[1]));
762
762
  if (void 0 !== data && LiveObject.isJsonObject(data) && "number" == typeof data.actor && (void 0 === data.id || "string" == typeof data.id)) return {
763
763
  actor: data.actor,
764
764
  id: data.id,
package/index.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import { A as AbstractCrdt, r as remove, S as ServerMsgCode, m as mergeStorageUpdates, W as WebsocketCloseCodes, C as ClientMsgCode, i as isTokenValid, a as isSameNodeOrChildOf, L as LiveObject, g as getTreesDiffOperations, O as OpCode, b as LiveList, p as parseJson, c as isJsonArray, d as compact, e as isJsonObject, f as isRootCrdt, h as deprecateIf } from "./shared.mjs";
1
+ import { A as AbstractCrdt, r as remove, S as ServerMsgCode, m as mergeStorageUpdates, W as WebsocketCloseCodes, C as ClientMsgCode, i as isTokenValid, a as isSameNodeOrChildOf, L as LiveObject, g as getTreesDiffOperations, O as OpCode, b as LiveList, t as tryParseJson, c as isJsonArray, d as compact, e as b64decode, f as isJsonObject, h as isRootCrdt, j as deprecateIf } from "./shared.mjs";
2
2
 
3
- export { b as LiveList, j as LiveMap, L as LiveObject } from "./shared.mjs";
3
+ export { b as LiveList, k as LiveMap, L as LiveObject } from "./shared.mjs";
4
4
 
5
5
  const BACKOFF_RETRY_DELAYS = [ 250, 500, 1e3, 2e3, 4e3, 8e3, 1e4 ], BACKOFF_RETRY_DELAYS_SLOW = [ 2e3, 3e4, 6e4, 3e5 ];
6
6
 
@@ -302,7 +302,7 @@ function makeStateMachine(state, context, mockedEffects) {
302
302
  function onMessage(event) {
303
303
  if ("pong" === event.data) return void clearTimeout(state.timeoutHandles.pongTimeout);
304
304
  const messages = function(text) {
305
- const data = parseJson(text);
305
+ const data = tryParseJson(text);
306
306
  return void 0 === data ? null : isJsonArray(data) ? compact(data.map((item => parseServerMessage(item)))) : compact([ parseServerMessage(data) ]);
307
307
  }(event.data);
308
308
  if (null === messages || 0 === messages.length) return;
@@ -710,7 +710,7 @@ class LiveblocksError extends Error {
710
710
  function parseToken(token) {
711
711
  const tokenParts = token.split(".");
712
712
  if (3 !== tokenParts.length) throw new Error("Authentication error. Liveblocks could not parse the response of your authentication endpoint");
713
- const data = parseJson(atob(tokenParts[1]));
713
+ const data = tryParseJson(b64decode(tokenParts[1]));
714
714
  if (void 0 !== data && isJsonObject(data) && "number" == typeof data.actor && (void 0 === data.id || "string" == typeof data.id)) return {
715
715
  actor: data.actor,
716
716
  id: data.id,
package/internal.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { d as JsonObject, J as Json, g as Resolve, e as Lson, A as AbstractCrdt, f as LsonObject, L as LiveObject, S as StorageUpdate } from './shared';
2
- export { g as Resolve, h as RoomInitializers, p as parseJson } from './shared';
2
+ export { g as Resolve, h as RoomInitializers } from './shared';
3
3
 
4
4
  declare type IdTuple<T> = [id: string, value: T];
5
5
  /**
@@ -132,12 +132,29 @@ declare type UpdateStorageServerMsg = {
132
132
  type: ServerMsgCode.UPDATE_STORAGE;
133
133
  ops: Op[];
134
134
  };
135
+ /**
136
+ * Messages that can be sent from the client to the server.
137
+ */
138
+ declare type ClientMsg<TPresence extends JsonObject> = BroadcastEventClientMsg | UpdatePresenceClientMsg<TPresence> | UpdateStorageClientMsg | FetchStorageClientMsg;
135
139
  declare enum ClientMsgCode {
136
140
  UPDATE_PRESENCE = 100,
137
141
  BROADCAST_EVENT = 103,
138
142
  FETCH_STORAGE = 200,
139
143
  UPDATE_STORAGE = 201
140
144
  }
145
+ declare type BroadcastEventClientMsg = {
146
+ type: ClientMsgCode.BROADCAST_EVENT;
147
+ event: Json;
148
+ };
149
+ declare type UpdatePresenceClientMsg<TPresence extends JsonObject> = {
150
+ type: ClientMsgCode.UPDATE_PRESENCE;
151
+ data: TPresence;
152
+ targetActor?: number;
153
+ };
154
+ declare type UpdateStorageClientMsg = {
155
+ type: ClientMsgCode.UPDATE_STORAGE;
156
+ ops: Op[];
157
+ };
141
158
  declare type FetchStorageClientMsg = {
142
159
  type: ClientMsgCode.FETCH_STORAGE;
143
160
  };
@@ -327,5 +344,14 @@ declare function comparePosition(posA: string, posB: string): number;
327
344
  * 🍒 case.
328
345
  */
329
346
  declare function assertNever(_value: never, errmsg: string): never;
347
+ /**
348
+ * Alternative to JSON.parse() that will not throw in production. If the passed
349
+ * string cannot be parsed, this will return `undefined`.
350
+ */
351
+ declare function tryParseJson(rawMessage: string): Json | undefined;
352
+ /**
353
+ * Decode base64 string.
354
+ */
355
+ declare function b64decode(b64value: string): string;
330
356
 
331
- export { ClientMsgCode, CrdtType, CreateChildOp, CreateListOp, CreateMapOp, CreateObjectOp, CreateRegisterOp, CreateRootObjectOp, DeleteCrdtOp, DeleteObjectKeyOp, FetchStorageClientMsg, IdTuple, InitialDocumentStateServerMsg, NodeMap, Op, OpCode, ParentToChildNodeMap, RoomStateServerMsg, SerializedChild, SerializedCrdt, SerializedList, SerializedMap, SerializedObject, SerializedRegister, SerializedRootObject, ServerMsg, ServerMsgCode, SetParentKeyOp, UpdateObjectOp, UserJoinServerMsg, WebsocketCloseCodes, assertNever, comparePosition, deprecate, deprecateIf, errorIf, isChildCrdt, isRootCrdt, lsonToJson, makePosition, patchImmutableObject, patchLiveObjectKey, throwUsageError };
357
+ export { BroadcastEventClientMsg, ClientMsg, ClientMsgCode, CrdtType, CreateChildOp, CreateListOp, CreateMapOp, CreateObjectOp, CreateRegisterOp, CreateRootObjectOp, DeleteCrdtOp, DeleteObjectKeyOp, FetchStorageClientMsg, IdTuple, InitialDocumentStateServerMsg, NodeMap, Op, OpCode, ParentToChildNodeMap, RoomStateServerMsg, SerializedChild, SerializedCrdt, SerializedList, SerializedMap, SerializedObject, SerializedRegister, SerializedRootObject, ServerMsg, ServerMsgCode, SetParentKeyOp, UpdateObjectOp, UpdatePresenceClientMsg, UpdateStorageClientMsg, UserJoinServerMsg, WebsocketCloseCodes, assertNever, b64decode, comparePosition, deprecate, deprecateIf, errorIf, isChildCrdt, isRootCrdt, lsonToJson, makePosition, patchImmutableObject, patchLiveObjectKey, throwUsageError, tryParseJson };
package/internal.js CHANGED
@@ -165,12 +165,13 @@ Object.defineProperty(exports, "ClientMsgCode", {
165
165
  get: function() {
166
166
  return LiveObject.WebsocketCloseCodes;
167
167
  }
168
- }), exports.assertNever = LiveObject.assertNever, exports.comparePosition = LiveObject.comparePosition,
169
- exports.deprecate = LiveObject.deprecate, exports.deprecateIf = LiveObject.deprecateIf,
170
- exports.errorIf = LiveObject.errorIf, exports.isChildCrdt = LiveObject.isChildCrdt,
171
- exports.isRootCrdt = LiveObject.isRootCrdt, exports.makePosition = LiveObject.makePosition,
172
- exports.parseJson = LiveObject.parseJson, exports.throwUsageError = LiveObject.throwUsageError,
173
- exports.lsonToJson = lsonToJson, exports.patchImmutableObject = function(state, updates) {
168
+ }), exports.assertNever = LiveObject.assertNever, exports.b64decode = LiveObject.b64decode,
169
+ exports.comparePosition = LiveObject.comparePosition, exports.deprecate = LiveObject.deprecate,
170
+ exports.deprecateIf = LiveObject.deprecateIf, exports.errorIf = LiveObject.errorIf,
171
+ exports.isChildCrdt = LiveObject.isChildCrdt, exports.isRootCrdt = LiveObject.isRootCrdt,
172
+ exports.makePosition = LiveObject.makePosition, exports.throwUsageError = LiveObject.throwUsageError,
173
+ exports.tryParseJson = LiveObject.tryParseJson, exports.lsonToJson = lsonToJson,
174
+ exports.patchImmutableObject = function(state, updates) {
174
175
  return updates.reduce((function(state, update) {
175
176
  return function(state, update) {
176
177
  var path = function(node) {
package/internal.mjs CHANGED
@@ -1,6 +1,6 @@
1
- import { L as LiveObject, b as LiveList, j as LiveMap, k as LiveRegister, A as AbstractCrdt, l as findNonSerializableValue } from "./shared.mjs";
1
+ import { L as LiveObject, b as LiveList, k as LiveMap, l as LiveRegister, A as AbstractCrdt, n as findNonSerializableValue } from "./shared.mjs";
2
2
 
3
- export { C as ClientMsgCode, q as CrdtType, O as OpCode, S as ServerMsgCode, W as WebsocketCloseCodes, w as assertNever, s as comparePosition, n as deprecate, h as deprecateIf, o as errorIf, v as isChildCrdt, f as isRootCrdt, u as makePosition, p as parseJson, t as throwUsageError } from "./shared.mjs";
3
+ export { C as ClientMsgCode, s as CrdtType, O as OpCode, S as ServerMsgCode, W as WebsocketCloseCodes, x as assertNever, e as b64decode, u as comparePosition, o as deprecate, j as deprecateIf, p as errorIf, w as isChildCrdt, h as isRootCrdt, v as makePosition, q as throwUsageError, t as tryParseJson } from "./shared.mjs";
4
4
 
5
5
  function lsonObjectToJson(obj) {
6
6
  const result = {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/client",
3
- "version": "0.16.11",
3
+ "version": "0.16.14",
4
4
  "description": "A client that lets you interact with Liveblocks servers.",
5
5
  "main": "./index.js",
6
6
  "module": "./index.mjs",
package/shared.d.ts CHANGED
@@ -14,11 +14,6 @@ declare type JsonArray = Json[];
14
14
  declare type JsonObject = {
15
15
  [key: string]: Json | undefined;
16
16
  };
17
- /**
18
- * Alternative to JSON.parse() that will not throw in production. If the passed
19
- * string cannot be parsed, this will return `undefined`.
20
- */
21
- declare function parseJson(rawMessage: string): Json | undefined;
22
17
 
23
18
  /**
24
19
  * The LiveMap class is similar to a JavaScript Map that is synchronized on all clients.
@@ -785,4 +780,4 @@ declare class LiveObject<O extends LsonObject = LsonObject> extends AbstractCrdt
785
780
  update(overrides: Partial<O>): void;
786
781
  }
787
782
 
788
- export { AbstractCrdt as A, BroadcastOptions as B, ClientOptions as C, History as H, Json as J, LiveObject as L, Others as O, Presence as P, Room as R, StorageUpdate as S, User as U, Client as a, LiveMap as b, LiveList as c, JsonObject as d, Lson as e, LsonObject as f, Resolve as g, RoomInitializers as h, parseJson as p };
783
+ export { AbstractCrdt as A, BroadcastOptions as B, ClientOptions as C, History as H, Json as J, LiveObject as L, Others as O, Presence as P, Room as R, StorageUpdate as S, User as U, Client as a, LiveMap as b, LiveList as c, JsonObject as d, Lson as e, LsonObject as f, Resolve as g, RoomInitializers as h };
package/shared.js CHANGED
@@ -197,14 +197,6 @@ var AbstractCrdt = function() {
197
197
  } ]), AbstractCrdt;
198
198
  }();
199
199
 
200
- function parseJson(rawMessage) {
201
- try {
202
- return JSON.parse(rawMessage);
203
- } catch (e) {
204
- return;
205
- }
206
- }
207
-
208
200
  function isJsonArray(data) {
209
201
  return Array.isArray(data);
210
202
  }
@@ -962,6 +954,14 @@ function entries(obj) {
962
954
  return Object.entries(obj);
963
955
  }
964
956
 
957
+ function tryParseJson(rawMessage) {
958
+ try {
959
+ return JSON.parse(rawMessage);
960
+ } catch (e) {
961
+ return;
962
+ }
963
+ }
964
+
965
965
  var LiveObject = function(_AbstractCrdt) {
966
966
  function LiveObject(obj) {
967
967
  var _this;
@@ -1271,6 +1271,15 @@ exports._extends = _extends, exports._inheritsLoose = _inheritsLoose, exports._o
1271
1271
  return target;
1272
1272
  }, exports._wrapNativeSuper = _wrapNativeSuper, exports.assertNever = function(_value, errmsg) {
1273
1273
  throw new Error(errmsg);
1274
+ }, exports.b64decode = function(b64value) {
1275
+ try {
1276
+ var formattedValue = b64value.replace(/-/g, "+").replace(/_/g, "/");
1277
+ return decodeURIComponent(atob(formattedValue).split("").map((function(c) {
1278
+ return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
1279
+ })).join(""));
1280
+ } catch (err) {
1281
+ return atob(b64value);
1282
+ }
1274
1283
  }, exports.compact = function(items) {
1275
1284
  return items.filter((function(item) {
1276
1285
  return null != item;
@@ -1364,7 +1373,7 @@ exports.isRootCrdt = function(crdt) {
1364
1373
  }, exports.isTokenValid = function(token) {
1365
1374
  var tokenParts = token.split(".");
1366
1375
  if (3 !== tokenParts.length) return !1;
1367
- var data = parseJson(atob(tokenParts[1]));
1376
+ var data = tryParseJson(atob(tokenParts[1]));
1368
1377
  return !(void 0 === data || !isJsonObject(data) || "number" != typeof data.exp) && !(Date.now() / 1e3 > data.exp - 300);
1369
1378
  }, exports.makePosition = makePosition, exports.mergeStorageUpdates = function(first, second) {
1370
1379
  return first ? "LiveObject" === first.type && "LiveObject" === second.type ? function(first, second) {
@@ -1388,9 +1397,9 @@ exports.isRootCrdt = function(crdt) {
1388
1397
  updates: first.updates.concat(second.updates)
1389
1398
  });
1390
1399
  }(first, second) : second : second;
1391
- }, exports.parseJson = parseJson, exports.remove = function(array, item) {
1400
+ }, exports.remove = function(array, item) {
1392
1401
  for (var i = 0; i < array.length; i++) if (array[i] === item) {
1393
1402
  array.splice(i, 1);
1394
1403
  break;
1395
1404
  }
1396
- }, exports.throwUsageError = throwUsageError;
1405
+ }, exports.throwUsageError = throwUsageError, exports.tryParseJson = tryParseJson;
package/shared.mjs CHANGED
@@ -70,14 +70,6 @@ class AbstractCrdt {
70
70
  }
71
71
  }
72
72
 
73
- function parseJson(rawMessage) {
74
- try {
75
- return JSON.parse(rawMessage);
76
- } catch (e) {
77
- return;
78
- }
79
- }
80
-
81
73
  function isJsonArray(data) {
82
74
  return Array.isArray(data);
83
75
  }
@@ -950,7 +942,7 @@ function findNonSerializableValue(value, path = "") {
950
942
  function isTokenValid(token) {
951
943
  const tokenParts = token.split(".");
952
944
  if (3 !== tokenParts.length) return !1;
953
- const data = parseJson(atob(tokenParts[1]));
945
+ const data = tryParseJson(atob(tokenParts[1]));
954
946
  if (void 0 === data || !isJsonObject(data) || "number" != typeof data.exp) return !1;
955
947
  return !(Date.now() / 1e3 > data.exp - 300);
956
948
  }
@@ -959,6 +951,25 @@ function entries(obj) {
959
951
  return Object.entries(obj);
960
952
  }
961
953
 
954
+ function tryParseJson(rawMessage) {
955
+ try {
956
+ return JSON.parse(rawMessage);
957
+ } catch (e) {
958
+ return;
959
+ }
960
+ }
961
+
962
+ function b64decode(b64value) {
963
+ try {
964
+ const formattedValue = b64value.replace(/-/g, "+").replace(/_/g, "/");
965
+ return decodeURIComponent(atob(formattedValue).split("").map((function(c) {
966
+ return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
967
+ })).join(""));
968
+ } catch (err) {
969
+ return atob(b64value);
970
+ }
971
+ }
972
+
962
973
  class LiveObject extends AbstractCrdt {
963
974
  constructor(obj = {}) {
964
975
  super(), this._propToLastUpdate = new Map;
@@ -1262,4 +1273,4 @@ class LiveObject extends AbstractCrdt {
1262
1273
  }
1263
1274
  }
1264
1275
 
1265
- export { AbstractCrdt as A, ClientMsgCode as C, LiveObject as L, OpCode as O, ServerMsgCode as S, WebsocketCloseCodes as W, isSameNodeOrChildOf as a, LiveList as b, isJsonArray as c, compact as d, isJsonObject as e, isRootCrdt as f, getTreesDiffOperations as g, deprecateIf as h, isTokenValid as i, LiveMap as j, LiveRegister as k, findNonSerializableValue as l, mergeStorageUpdates as m, deprecate as n, errorIf as o, parseJson as p, CrdtType as q, remove as r, comparePosition as s, throwUsageError as t, makePosition as u, isChildCrdt as v, assertNever as w };
1276
+ export { AbstractCrdt as A, ClientMsgCode as C, LiveObject as L, OpCode as O, ServerMsgCode as S, WebsocketCloseCodes as W, isSameNodeOrChildOf as a, LiveList as b, isJsonArray as c, compact as d, b64decode as e, isJsonObject as f, getTreesDiffOperations as g, isRootCrdt as h, isTokenValid as i, deprecateIf as j, LiveMap as k, LiveRegister as l, mergeStorageUpdates as m, findNonSerializableValue as n, deprecate as o, errorIf as p, throwUsageError as q, remove as r, CrdtType as s, tryParseJson as t, comparePosition as u, makePosition as v, isChildCrdt as w, assertNever as x };