@liveblocks/core 1.6.0 → 1.7.0-stream2

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.mts CHANGED
@@ -999,12 +999,10 @@ declare type FetchStorageClientMsg = {
999
999
  declare type FetchYDocClientMsg = {
1000
1000
  readonly type: ClientMsgCode.FETCH_YDOC;
1001
1001
  readonly vector: string;
1002
- readonly guid?: string;
1003
1002
  };
1004
1003
  declare type UpdateYDocClientMsg = {
1005
1004
  readonly type: ClientMsgCode.UPDATE_YDOC;
1006
1005
  readonly update: string;
1007
- readonly guid?: string;
1008
1006
  };
1009
1007
 
1010
1008
  declare type IdTuple<T> = [id: string, value: T];
@@ -1196,7 +1194,6 @@ declare type YDocUpdateServerMsg = {
1196
1194
  readonly update: string;
1197
1195
  readonly isSync: boolean;
1198
1196
  readonly stateVector: string | null;
1199
- readonly guid?: string;
1200
1197
  };
1201
1198
  /**
1202
1199
  * Sent by the WebSocket server and broadcasted to all clients to announce that
@@ -1689,11 +1686,11 @@ declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUs
1689
1686
  *
1690
1687
  * @param {string} data the doc update to send to the server, base64 encoded uint8array
1691
1688
  */
1692
- updateYDoc(data: string, guid?: string): void;
1689
+ updateYDoc(data: string): void;
1693
1690
  /**
1694
1691
  * Sends a request for the current document from liveblocks server
1695
1692
  */
1696
- fetchYDoc(stateVector: string, guid?: string): void;
1693
+ fetchYDoc(stateVector: string): void;
1697
1694
  /**
1698
1695
  * Broadcasts an event to other users in the room. Event broadcasted to the room can be listened with {@link Room.subscribe}("event").
1699
1696
  * @param {any} event the event to broadcast. Should be serializable to JSON
@@ -1890,6 +1887,7 @@ declare type ClientOptions = {
1890
1887
  backgroundKeepAliveTimeout?: number;
1891
1888
  polyfills?: Polyfills;
1892
1889
  unstable_fallbackToHTTP?: boolean;
1890
+ unstable_streamData?: boolean;
1893
1891
  /**
1894
1892
  * @deprecated Use `polyfills: { fetch: ... }` instead.
1895
1893
  * This option will be removed in a future release.
package/dist/index.d.ts CHANGED
@@ -999,12 +999,10 @@ declare type FetchStorageClientMsg = {
999
999
  declare type FetchYDocClientMsg = {
1000
1000
  readonly type: ClientMsgCode.FETCH_YDOC;
1001
1001
  readonly vector: string;
1002
- readonly guid?: string;
1003
1002
  };
1004
1003
  declare type UpdateYDocClientMsg = {
1005
1004
  readonly type: ClientMsgCode.UPDATE_YDOC;
1006
1005
  readonly update: string;
1007
- readonly guid?: string;
1008
1006
  };
1009
1007
 
1010
1008
  declare type IdTuple<T> = [id: string, value: T];
@@ -1196,7 +1194,6 @@ declare type YDocUpdateServerMsg = {
1196
1194
  readonly update: string;
1197
1195
  readonly isSync: boolean;
1198
1196
  readonly stateVector: string | null;
1199
- readonly guid?: string;
1200
1197
  };
1201
1198
  /**
1202
1199
  * Sent by the WebSocket server and broadcasted to all clients to announce that
@@ -1689,11 +1686,11 @@ declare type Room<TPresence extends JsonObject, TStorage extends LsonObject, TUs
1689
1686
  *
1690
1687
  * @param {string} data the doc update to send to the server, base64 encoded uint8array
1691
1688
  */
1692
- updateYDoc(data: string, guid?: string): void;
1689
+ updateYDoc(data: string): void;
1693
1690
  /**
1694
1691
  * Sends a request for the current document from liveblocks server
1695
1692
  */
1696
- fetchYDoc(stateVector: string, guid?: string): void;
1693
+ fetchYDoc(stateVector: string): void;
1697
1694
  /**
1698
1695
  * Broadcasts an event to other users in the room. Event broadcasted to the room can be listened with {@link Room.subscribe}("event").
1699
1696
  * @param {any} event the event to broadcast. Should be serializable to JSON
@@ -1890,6 +1887,7 @@ declare type ClientOptions = {
1890
1887
  backgroundKeepAliveTimeout?: number;
1891
1888
  polyfills?: Polyfills;
1892
1889
  unstable_fallbackToHTTP?: boolean;
1890
+ unstable_streamData?: boolean;
1893
1891
  /**
1894
1892
  * @deprecated Use `polyfills: { fetch: ... }` instead.
1895
1893
  * This option will be removed in a future release.
package/dist/index.js CHANGED
@@ -6,7 +6,7 @@ var __export = (target, all) => {
6
6
 
7
7
  // src/version.ts
8
8
  var PKG_NAME = "@liveblocks/core";
9
- var PKG_VERSION = "1.6.0";
9
+ var PKG_VERSION = "1.7.0-stream2";
10
10
  var PKG_FORMAT = "cjs";
11
11
 
12
12
  // src/dupe-detection.ts
@@ -4977,12 +4977,27 @@ function createRoom(options, config) {
4977
4977
  ydoc: makeEventSource(),
4978
4978
  comments: makeEventSource()
4979
4979
  };
4980
+ async function streamFetch(authTokenOrPublicApiKey, roomId) {
4981
+ const url = new URL(
4982
+ `/v2/c/rooms/${encodeURIComponent(roomId)}/storage`,
4983
+ config.baseUrl
4984
+ ).toString();
4985
+ const fetcher = _optionalChain([config, 'access', _121 => _121.polyfills, 'optionalAccess', _122 => _122.fetch]) || /* istanbul ignore next */
4986
+ fetch;
4987
+ return fetcher(url.toString(), {
4988
+ method: "GET",
4989
+ headers: {
4990
+ "Content-Type": "application/json",
4991
+ Authorization: `Bearer ${authTokenOrPublicApiKey}`
4992
+ }
4993
+ });
4994
+ }
4980
4995
  async function httpSend(authTokenOrPublicApiKey, roomId, nonce, messages) {
4981
4996
  const url = new URL(
4982
4997
  `/v2/c/rooms/${encodeURIComponent(roomId)}/send-message`,
4983
4998
  config.baseUrl
4984
4999
  ).toString();
4985
- const fetcher = _optionalChain([config, 'access', _121 => _121.polyfills, 'optionalAccess', _122 => _122.fetch]) || /* istanbul ignore next */
5000
+ const fetcher = _optionalChain([config, 'access', _123 => _123.polyfills, 'optionalAccess', _124 => _124.fetch]) || /* istanbul ignore next */
4986
5001
  fetch;
4987
5002
  return fetcher(url, {
4988
5003
  method: "POST",
@@ -4995,7 +5010,7 @@ function createRoom(options, config) {
4995
5010
  }
4996
5011
  function sendMessages(messages) {
4997
5012
  const serializedPayload = JSON.stringify(messages);
4998
- const nonce = _optionalChain([context, 'access', _123 => _123.dynamicSessionInfo, 'access', _124 => _124.current, 'optionalAccess', _125 => _125.nonce]);
5013
+ const nonce = _optionalChain([context, 'access', _125 => _125.dynamicSessionInfo, 'access', _126 => _126.current, 'optionalAccess', _127 => _127.nonce]);
4999
5014
  if (config.unstable_fallbackToHTTP && managedSocket.authValue && nonce) {
5000
5015
  const size = new TextEncoder().encode(serializedPayload).length;
5001
5016
  if (size > MAX_SOCKET_MESSAGE_SIZE) {
@@ -5260,7 +5275,7 @@ function createRoom(options, config) {
5260
5275
  }
5261
5276
  context.myPresence.patch(patch);
5262
5277
  if (context.activeBatch) {
5263
- if (_optionalChain([options2, 'optionalAccess', _126 => _126.addToHistory])) {
5278
+ if (_optionalChain([options2, 'optionalAccess', _128 => _128.addToHistory])) {
5264
5279
  context.activeBatch.reverseOps.unshift({
5265
5280
  type: "presence",
5266
5281
  data: oldValues
@@ -5270,7 +5285,7 @@ function createRoom(options, config) {
5270
5285
  } else {
5271
5286
  flushNowOrSoon();
5272
5287
  batchUpdates(() => {
5273
- if (_optionalChain([options2, 'optionalAccess', _127 => _127.addToHistory])) {
5288
+ if (_optionalChain([options2, 'optionalAccess', _129 => _129.addToHistory])) {
5274
5289
  addToUndoStack(
5275
5290
  [{ type: "presence", data: oldValues }],
5276
5291
  doNotBatchUpdates
@@ -5447,12 +5462,7 @@ function createRoom(options, config) {
5447
5462
  break;
5448
5463
  }
5449
5464
  case 200 /* INITIAL_STORAGE_STATE */: {
5450
- const unacknowledgedOps = new Map(context.unacknowledgedOps);
5451
- createOrUpdateRootFromMessage(message, doNotBatchUpdates);
5452
- applyAndSendOps(unacknowledgedOps, doNotBatchUpdates);
5453
- _optionalChain([_resolveStoragePromise, 'optionalCall', _128 => _128()]);
5454
- notifyStorageStatus();
5455
- eventHub.storageDidLoad.notify();
5465
+ processInitialStorage(message);
5456
5466
  break;
5457
5467
  }
5458
5468
  case 201 /* UPDATE_STORAGE */: {
@@ -5473,7 +5483,7 @@ function createRoom(options, config) {
5473
5483
  if (process.env.NODE_ENV !== "production") {
5474
5484
  const traces = /* @__PURE__ */ new Set();
5475
5485
  for (const opId of message.opIds) {
5476
- const trace = _optionalChain([context, 'access', _129 => _129.opStackTraces, 'optionalAccess', _130 => _130.get, 'call', _131 => _131(opId)]);
5486
+ const trace = _optionalChain([context, 'access', _130 => _130.opStackTraces, 'optionalAccess', _131 => _131.get, 'call', _132 => _132(opId)]);
5477
5487
  if (trace) {
5478
5488
  traces.add(trace);
5479
5489
  }
@@ -5570,11 +5580,10 @@ ${Array.from(traces).join("\n\n")}`
5570
5580
  }
5571
5581
  return messages;
5572
5582
  }
5573
- function updateYDoc(update, guid) {
5583
+ function updateYDoc(update) {
5574
5584
  const clientMsg = {
5575
5585
  type: 301 /* UPDATE_YDOC */,
5576
- update,
5577
- guid
5586
+ update
5578
5587
  };
5579
5588
  context.buffer.messages.push(clientMsg);
5580
5589
  eventHub.ydoc.notify(clientMsg);
@@ -5598,9 +5607,30 @@ ${Array.from(traces).join("\n\n")}`
5598
5607
  }
5599
5608
  let _getStorage$ = null;
5600
5609
  let _resolveStoragePromise = null;
5610
+ function processInitialStorage(message) {
5611
+ const unacknowledgedOps = new Map(context.unacknowledgedOps);
5612
+ createOrUpdateRootFromMessage(message, doNotBatchUpdates);
5613
+ applyAndSendOps(unacknowledgedOps, doNotBatchUpdates);
5614
+ _optionalChain([_resolveStoragePromise, 'optionalCall', _133 => _133()]);
5615
+ notifyStorageStatus();
5616
+ eventHub.storageDidLoad.notify();
5617
+ }
5618
+ async function streamStorage() {
5619
+ if (!managedSocket.authValue) {
5620
+ return;
5621
+ }
5622
+ const result = await streamFetch(
5623
+ managedSocket.authValue.type === "public" ? managedSocket.authValue.publicApiKey : managedSocket.authValue.token.raw,
5624
+ config.roomId
5625
+ );
5626
+ const items = await result.json();
5627
+ processInitialStorage({ type: 200 /* INITIAL_STORAGE_STATE */, items });
5628
+ }
5601
5629
  function refreshStorage(options2) {
5602
5630
  const messages = context.buffer.messages;
5603
- if (!messages.some((msg) => msg.type === 200 /* FETCH_STORAGE */)) {
5631
+ if (config.unstable_streamData) {
5632
+ void streamStorage();
5633
+ } else if (!messages.some((msg) => msg.type === 200 /* FETCH_STORAGE */)) {
5604
5634
  messages.push({ type: 200 /* FETCH_STORAGE */ });
5605
5635
  }
5606
5636
  if (options2.flush) {
@@ -5637,14 +5667,13 @@ ${Array.from(traces).join("\n\n")}`
5637
5667
  root: nn(context.root)
5638
5668
  };
5639
5669
  }
5640
- function fetchYDoc(vector, guid) {
5670
+ function fetchYDoc(vector) {
5641
5671
  if (!context.buffer.messages.find((m) => {
5642
- return m.type === 300 /* FETCH_YDOC */ && m.vector === vector && m.guid === guid;
5672
+ return m.type === 300 /* FETCH_YDOC */ && m.vector === vector;
5643
5673
  })) {
5644
5674
  context.buffer.messages.push({
5645
5675
  type: 300 /* FETCH_YDOC */,
5646
- vector,
5647
- guid
5676
+ vector
5648
5677
  });
5649
5678
  }
5650
5679
  flushNowOrSoon();
@@ -5784,7 +5813,7 @@ ${Array.from(traces).join("\n\n")}`
5784
5813
  /* NOTE: Exposing __internal here only to allow testing implementation details in unit tests */
5785
5814
  __internal: {
5786
5815
  get presenceBuffer() {
5787
- return deepClone(_nullishCoalesce(_optionalChain([context, 'access', _132 => _132.buffer, 'access', _133 => _133.presenceUpdates, 'optionalAccess', _134 => _134.data]), () => ( null)));
5816
+ return deepClone(_nullishCoalesce(_optionalChain([context, 'access', _134 => _134.buffer, 'access', _135 => _135.presenceUpdates, 'optionalAccess', _136 => _136.data]), () => ( null)));
5788
5817
  },
5789
5818
  // prettier-ignore
5790
5819
  get undoStack() {
@@ -5926,7 +5955,7 @@ function makeClassicSubscribeFn(events) {
5926
5955
  }
5927
5956
  if (isLiveNode(first)) {
5928
5957
  const node = first;
5929
- if (_optionalChain([options, 'optionalAccess', _135 => _135.isDeep])) {
5958
+ if (_optionalChain([options, 'optionalAccess', _137 => _137.isDeep])) {
5930
5959
  const storageCallback = second;
5931
5960
  return subscribeToLiveStructureDeeply(node, storageCallback);
5932
5961
  } else {
@@ -6051,14 +6080,15 @@ function createClient(options) {
6051
6080
  createSocket: makeCreateSocketDelegateForRoom(
6052
6081
  roomId,
6053
6082
  baseUrl,
6054
- _optionalChain([clientOptions, 'access', _136 => _136.polyfills, 'optionalAccess', _137 => _137.WebSocket])
6083
+ _optionalChain([clientOptions, 'access', _138 => _138.polyfills, 'optionalAccess', _139 => _139.WebSocket])
6055
6084
  ),
6056
6085
  authenticate: makeAuthDelegateForRoom(roomId, authManager)
6057
6086
  })),
6058
6087
  enableDebugLogging: clientOptions.enableDebugLogging,
6059
- unstable_batchedUpdates: _optionalChain([options2, 'optionalAccess', _138 => _138.unstable_batchedUpdates]),
6088
+ unstable_batchedUpdates: _optionalChain([options2, 'optionalAccess', _140 => _140.unstable_batchedUpdates]),
6060
6089
  baseUrl,
6061
- unstable_fallbackToHTTP: !!clientOptions.unstable_fallbackToHTTP
6090
+ unstable_fallbackToHTTP: !!clientOptions.unstable_fallbackToHTTP,
6091
+ unstable_streamData: !!clientOptions.unstable_streamData
6062
6092
  }
6063
6093
  );
6064
6094
  const newRoomInfo = {
@@ -6071,7 +6101,7 @@ function createClient(options) {
6071
6101
  const shouldConnect = _nullishCoalesce(_nullishCoalesce(options2.autoConnect, () => ( options2.shouldInitiallyConnect)), () => ( true));
6072
6102
  if (shouldConnect) {
6073
6103
  if (typeof atob === "undefined") {
6074
- if (_optionalChain([clientOptions, 'access', _139 => _139.polyfills, 'optionalAccess', _140 => _140.atob]) === void 0) {
6104
+ if (_optionalChain([clientOptions, 'access', _141 => _141.polyfills, 'optionalAccess', _142 => _142.atob]) === void 0) {
6075
6105
  throw new Error(
6076
6106
  "You need to polyfill atob to use the client in your environment. Please follow the instructions at https://liveblocks.io/docs/errors/liveblocks-client/atob-polyfill"
6077
6107
  );
@@ -6087,11 +6117,11 @@ function createClient(options) {
6087
6117
  return room;
6088
6118
  }
6089
6119
  function getRoom(roomId) {
6090
- const room = _optionalChain([roomsById, 'access', _141 => _141.get, 'call', _142 => _142(roomId), 'optionalAccess', _143 => _143.room]);
6120
+ const room = _optionalChain([roomsById, 'access', _143 => _143.get, 'call', _144 => _144(roomId), 'optionalAccess', _145 => _145.room]);
6091
6121
  return room ? room : null;
6092
6122
  }
6093
6123
  function forceLeave(roomId) {
6094
- const unsubs = _nullishCoalesce(_optionalChain([roomsById, 'access', _144 => _144.get, 'call', _145 => _145(roomId), 'optionalAccess', _146 => _146.unsubs]), () => ( /* @__PURE__ */ new Set()));
6124
+ const unsubs = _nullishCoalesce(_optionalChain([roomsById, 'access', _146 => _146.get, 'call', _147 => _147(roomId), 'optionalAccess', _148 => _148.unsubs]), () => ( /* @__PURE__ */ new Set()));
6095
6125
  for (const unsub of unsubs) {
6096
6126
  unsub();
6097
6127
  }
@@ -6373,12 +6403,12 @@ function legacy_patchImmutableNode(state, path, update) {
6373
6403
  }
6374
6404
  const newState = Object.assign({}, state);
6375
6405
  for (const key in update.updates) {
6376
- if (_optionalChain([update, 'access', _147 => _147.updates, 'access', _148 => _148[key], 'optionalAccess', _149 => _149.type]) === "update") {
6406
+ if (_optionalChain([update, 'access', _149 => _149.updates, 'access', _150 => _150[key], 'optionalAccess', _151 => _151.type]) === "update") {
6377
6407
  const val = update.node.get(key);
6378
6408
  if (val !== void 0) {
6379
6409
  newState[key] = lsonToJson(val);
6380
6410
  }
6381
- } else if (_optionalChain([update, 'access', _150 => _150.updates, 'access', _151 => _151[key], 'optionalAccess', _152 => _152.type]) === "delete") {
6411
+ } else if (_optionalChain([update, 'access', _152 => _152.updates, 'access', _153 => _153[key], 'optionalAccess', _154 => _154.type]) === "delete") {
6382
6412
  delete newState[key];
6383
6413
  }
6384
6414
  }
@@ -6439,12 +6469,12 @@ function legacy_patchImmutableNode(state, path, update) {
6439
6469
  }
6440
6470
  const newState = Object.assign({}, state);
6441
6471
  for (const key in update.updates) {
6442
- if (_optionalChain([update, 'access', _153 => _153.updates, 'access', _154 => _154[key], 'optionalAccess', _155 => _155.type]) === "update") {
6472
+ if (_optionalChain([update, 'access', _155 => _155.updates, 'access', _156 => _156[key], 'optionalAccess', _157 => _157.type]) === "update") {
6443
6473
  const value = update.node.get(key);
6444
6474
  if (value !== void 0) {
6445
6475
  newState[key] = lsonToJson(value);
6446
6476
  }
6447
- } else if (_optionalChain([update, 'access', _156 => _156.updates, 'access', _157 => _157[key], 'optionalAccess', _158 => _158.type]) === "delete") {
6477
+ } else if (_optionalChain([update, 'access', _158 => _158.updates, 'access', _159 => _159[key], 'optionalAccess', _160 => _160.type]) === "delete") {
6448
6478
  delete newState[key];
6449
6479
  }
6450
6480
  }
@@ -6534,7 +6564,7 @@ function createCacheItem(key, asyncFunction, options) {
6534
6564
  let previousState = { isLoading: false };
6535
6565
  const eventSource2 = makeEventSource();
6536
6566
  function notify() {
6537
- const isEqual = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _159 => _159.isStateEqual]), () => ( isShallowEqual));
6567
+ const isEqual = _nullishCoalesce(_optionalChain([options, 'optionalAccess', _161 => _161.isStateEqual]), () => ( isShallowEqual));
6538
6568
  if (!isEqual(previousState, state)) {
6539
6569
  previousState = state;
6540
6570
  eventSource2.notify(state);
@@ -6602,7 +6632,7 @@ function createAsyncCache(asyncFunction, options) {
6602
6632
  return create(key).get();
6603
6633
  }
6604
6634
  function getState(key) {
6605
- return _optionalChain([cache, 'access', _160 => _160.get, 'call', _161 => _161(key), 'optionalAccess', _162 => _162.getState, 'call', _163 => _163()]);
6635
+ return _optionalChain([cache, 'access', _162 => _162.get, 'call', _163 => _163(key), 'optionalAccess', _164 => _164.getState, 'call', _165 => _165()]);
6606
6636
  }
6607
6637
  function revalidate(key) {
6608
6638
  return create(key).revalidate();