@liveblocks/core 1.0.9 → 1.0.10

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
@@ -1115,6 +1115,7 @@ declare type AuthEndpoint = string | ((room: string) => Promise<{
1115
1115
  declare type ClientOptions = {
1116
1116
  throttle?: number;
1117
1117
  polyfills?: Polyfills;
1118
+ unstable_fallbackToHTTP?: boolean;
1118
1119
  /**
1119
1120
  * Backward-compatible way to set `polyfills.fetch`.
1120
1121
  */
package/dist/index.js CHANGED
@@ -117,7 +117,7 @@ var onMessageFromPanel = eventSource.observable;
117
117
  // src/devtools/index.ts
118
118
  var VERSION = true ? (
119
119
  /* istanbul ignore next */
120
- "1.0.9"
120
+ "1.0.10"
121
121
  ) : "dev";
122
122
  var _devtoolsSetupHasRun = false;
123
123
  function setupDevTools(getAllRooms) {
@@ -2955,7 +2955,8 @@ function hasJwtMeta(data) {
2955
2955
  }
2956
2956
  function isTokenExpired(token) {
2957
2957
  const now = Date.now() / 1e3;
2958
- return now > token.exp - 300 || now < token.iat + 300;
2958
+ const valid = now <= token.exp - 300 && now >= token.iat - 300;
2959
+ return !valid;
2959
2960
  }
2960
2961
  function isStringList(value) {
2961
2962
  return Array.isArray(value) && value.every((i) => typeof i === "string");
@@ -3257,6 +3258,7 @@ var BACKOFF_RETRY_DELAYS = [250, 500, 1e3, 2e3, 4e3, 8e3, 1e4];
3257
3258
  var BACKOFF_RETRY_DELAYS_SLOW = [2e3, 3e4, 6e4, 3e5];
3258
3259
  var HEARTBEAT_INTERVAL = 3e4;
3259
3260
  var PONG_TIMEOUT = 2e3;
3261
+ var MAX_MESSAGE_SIZE = 1024 * 1024 - 128;
3260
3262
  function makeIdFactory(connectionId) {
3261
3263
  let count = 0;
3262
3264
  return () => `${connectionId}:${count++}`;
@@ -3405,14 +3407,33 @@ function createRoom(options, config) {
3405
3407
  }
3406
3408
  },
3407
3409
  send(messageOrMessages) {
3410
+ var _a2, _b;
3408
3411
  if (context.socket === null) {
3409
3412
  throw new Error("Can't send message if socket is null");
3410
3413
  }
3411
3414
  if (context.socket.readyState === context.socket.OPEN) {
3412
- context.socket.send(JSON.stringify(messageOrMessages));
3415
+ const message = JSON.stringify(messageOrMessages);
3416
+ if (config.unstable_fallbackToHTTP) {
3417
+ const size = new TextEncoder().encode(message).length;
3418
+ if (size > MAX_MESSAGE_SIZE && ((_a2 = context.token) == null ? void 0 : _a2.raw) && config.httpSendEndpoint) {
3419
+ if (isTokenExpired(context.token.parsed)) {
3420
+ return reconnect();
3421
+ }
3422
+ void httpSend(
3423
+ message,
3424
+ context.token.raw,
3425
+ config.httpSendEndpoint,
3426
+ (_b = config.polyfills) == null ? void 0 : _b.fetch
3427
+ );
3428
+ warn(
3429
+ "Message was too large for websockets and sent over HTTP instead"
3430
+ );
3431
+ return;
3432
+ }
3433
+ }
3434
+ context.socket.send(message);
3413
3435
  }
3414
3436
  },
3415
- scheduleFlush: (delay) => setTimeout(tryFlushing, delay),
3416
3437
  scheduleReconnect: (delay) => setTimeout(handleConnect, delay),
3417
3438
  startHeartbeatInterval: () => setInterval(heartbeat, HEARTBEAT_INTERVAL),
3418
3439
  schedulePongTimeout: () => setTimeout(pongTimeout, PONG_TIMEOUT)
@@ -4099,7 +4120,8 @@ ${Array.from(traces).join("\n\n")}`
4099
4120
  };
4100
4121
  } else {
4101
4122
  clearTimeout(context.timers.flush);
4102
- context.timers.flush = effects.scheduleFlush(
4123
+ context.timers.flush = setTimeout(
4124
+ tryFlushing,
4103
4125
  config.throttleDelay - elapsedMillis
4104
4126
  );
4105
4127
  }
@@ -4503,11 +4525,25 @@ function prepareCreateWebSocket(liveblocksServer, WebSocketPolyfill) {
4503
4525
  // @ts-ignore (__PACKAGE_VERSION__ will be injected by the build script)
4504
4526
  true ? (
4505
4527
  /* istanbul ignore next */
4506
- "1.0.9"
4528
+ "1.0.10"
4507
4529
  ) : "dev"}`
4508
4530
  );
4509
4531
  };
4510
4532
  }
4533
+ function httpSend(message, token, endpoint, fetchPolyfill) {
4534
+ return __async(this, null, function* () {
4535
+ const fetcher = fetchPolyfill || /* istanbul ignore next */
4536
+ fetch;
4537
+ return fetcher(endpoint, {
4538
+ method: "POST",
4539
+ headers: {
4540
+ "Content-Type": "application/json",
4541
+ Authorization: `Bearer ${token}`
4542
+ },
4543
+ body: message
4544
+ });
4545
+ });
4546
+ }
4511
4547
  function prepareAuthEndpoint(roomId, authentication, fetchPolyfill) {
4512
4548
  if (authentication.type === "public") {
4513
4549
  if (typeof window === "undefined" && fetchPolyfill === void 0) {
@@ -4628,7 +4664,12 @@ function createClient(options) {
4628
4664
  polyfills: clientOptions.polyfills,
4629
4665
  unstable_batchedUpdates: options2 == null ? void 0 : options2.unstable_batchedUpdates,
4630
4666
  liveblocksServer: getServerFromClientOptions(clientOptions),
4631
- authentication: prepareAuthentication(clientOptions, roomId)
4667
+ authentication: prepareAuthentication(clientOptions, roomId),
4668
+ httpSendEndpoint: buildLiveblocksHttpSendEndpoint(
4669
+ clientOptions,
4670
+ roomId
4671
+ ),
4672
+ unstable_fallbackToHTTP: !!clientOptions.unstable_fallbackToHTTP
4632
4673
  }
4633
4674
  );
4634
4675
  rooms.set(roomId, newRoom);
@@ -4732,6 +4773,14 @@ function prepareAuthentication(clientOptions, roomId) {
4732
4773
  "Invalid Liveblocks client options. For more information: https://liveblocks.io/docs/api-reference/liveblocks-client#createClient"
4733
4774
  );
4734
4775
  }
4776
+ function buildLiveblocksHttpSendEndpoint(options, roomId) {
4777
+ if (options.httpSendEndpoint) {
4778
+ return options.httpSendEndpoint.replace("{roomId}", roomId);
4779
+ }
4780
+ return `https://api.liveblocks.io/v2/rooms/${encodeURIComponent(
4781
+ roomId
4782
+ )}/send-message`;
4783
+ }
4735
4784
  function buildLiveblocksPublicAuthorizeEndpoint(options, roomId) {
4736
4785
  if (options.publicAuthorizeEndpoint) {
4737
4786
  return options.publicAuthorizeEndpoint.replace("{roomId}", roomId);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@liveblocks/core",
3
- "version": "1.0.9",
3
+ "version": "1.0.10",
4
4
  "description": "Shared code and foundational internals for Liveblocks",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",