@hocuspocus/provider 2.3.0 → 2.4.0-rc.0

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.
@@ -1618,8 +1618,6 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1618
1618
  url: '',
1619
1619
  // @ts-ignore
1620
1620
  document: undefined,
1621
- // @ts-ignore
1622
- awareness: undefined,
1623
1621
  WebSocketPolyfill: undefined,
1624
1622
  parameters: {},
1625
1623
  connect: true,
@@ -1669,6 +1667,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1669
1667
  this.receivedOnOpenPayload = undefined;
1670
1668
  this.receivedOnStatusPayload = undefined;
1671
1669
  this.boundConnect = this.connect.bind(this);
1670
+ this.closeTries = 0;
1672
1671
  this.setConfiguration(configuration);
1673
1672
  this.configuration.WebSocketPolyfill = configuration.WebSocketPolyfill ? configuration.WebSocketPolyfill : WebSocket;
1674
1673
  this.on('open', this.configuration.onOpen);
@@ -1820,7 +1819,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1820
1819
  if (this.status !== WebSocketStatus.Connected) {
1821
1820
  return;
1822
1821
  }
1823
- // Don’t close then connection while waiting for the first message
1822
+ // Don’t close the connection while waiting for the first message
1824
1823
  if (!this.lastMessageReceived) {
1825
1824
  return;
1826
1825
  }
@@ -1829,9 +1828,23 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1829
1828
  return;
1830
1829
  }
1831
1830
  // No message received in a long time, not even your own
1832
- // Awareness updates, which are updated every 15 seconds.
1833
- (_a = this.webSocket) === null || _a === void 0 ? void 0 : _a.close();
1834
- this.messageQueue = [];
1831
+ // Awareness updates, which are updated every 15 seconds
1832
+ // if awareness is enabled.
1833
+ this.closeTries += 1;
1834
+ // https://bugs.webkit.org/show_bug.cgi?id=247943
1835
+ if (this.closeTries > 2) {
1836
+ this.onClose({
1837
+ event: {
1838
+ code: 4408,
1839
+ reason: 'forced',
1840
+ },
1841
+ });
1842
+ this.closeTries = 0;
1843
+ }
1844
+ else {
1845
+ (_a = this.webSocket) === null || _a === void 0 ? void 0 : _a.close();
1846
+ this.messageQueue = [];
1847
+ }
1835
1848
  }
1836
1849
  registerEventListeners() {
1837
1850
  if (typeof window === 'undefined') {
@@ -1873,6 +1886,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1873
1886
  }
1874
1887
  }
1875
1888
  onClose({ event }) {
1889
+ this.closeTries = 0;
1876
1890
  this.webSocket = null;
1877
1891
  if (this.status === WebSocketStatus.Connected) {
1878
1892
  this.status = WebSocketStatus.Disconnected;
@@ -2171,6 +2185,8 @@ class MessageReceiver {
2171
2185
  }
2172
2186
  }
2173
2187
  applyAwarenessMessage(provider) {
2188
+ if (!provider.awareness)
2189
+ return;
2174
2190
  const { message } = this;
2175
2191
  applyAwarenessUpdate(provider.awareness, message.readVarUint8Array(), provider);
2176
2192
  }
@@ -2179,6 +2195,8 @@ class MessageReceiver {
2179
2195
  readAuthMessage(message.decoder, provider.permissionDeniedHandler.bind(provider), provider.authenticatedHandler.bind(provider));
2180
2196
  }
2181
2197
  applyQueryAwarenessMessage(provider) {
2198
+ if (!provider.awareness)
2199
+ return;
2182
2200
  const { message } = this;
2183
2201
  message.writeVarUint(MessageType.Awareness);
2184
2202
  message.writeVarUint8Array(encodeAwarenessUpdate(provider.awareness, Array.from(provider.awareness.getStates().keys())));
@@ -2336,8 +2354,15 @@ class UpdateMessage extends OutgoingMessage {
2336
2354
  }
2337
2355
  }
2338
2356
 
2357
+ class AwarenessError extends Error {
2358
+ constructor() {
2359
+ super(...arguments);
2360
+ this.code = 1001;
2361
+ }
2362
+ }
2339
2363
  class HocuspocusProvider extends EventEmitter {
2340
2364
  constructor(configuration) {
2365
+ var _a, _b, _c;
2341
2366
  super();
2342
2367
  this.configuration = {
2343
2368
  name: '',
@@ -2391,7 +2416,7 @@ class HocuspocusProvider extends EventEmitter {
2391
2416
  this.forwardDestroy = (e) => this.emit('destroy', e);
2392
2417
  this.setConfiguration(configuration);
2393
2418
  this.configuration.document = configuration.document ? configuration.document : new Y.Doc();
2394
- this.configuration.awareness = configuration.awareness ? configuration.awareness : new Awareness(this.document);
2419
+ this.configuration.awareness = configuration.awareness !== undefined ? configuration.awareness : new Awareness(this.document);
2395
2420
  this.on('open', this.configuration.onOpen);
2396
2421
  this.on('message', this.configuration.onMessage);
2397
2422
  this.on('outgoingMessage', this.configuration.onOutgoingMessage);
@@ -2415,14 +2440,14 @@ class HocuspocusProvider extends EventEmitter {
2415
2440
  this.configuration.websocketProvider.on('disconnect', this.forwardDisconnect);
2416
2441
  this.configuration.websocketProvider.on('destroy', this.configuration.onDestroy);
2417
2442
  this.configuration.websocketProvider.on('destroy', this.forwardDestroy);
2418
- this.awareness.on('update', () => {
2443
+ (_a = this.awareness) === null || _a === void 0 ? void 0 : _a.on('update', () => {
2419
2444
  this.emit('awarenessUpdate', { states: awarenessStatesToArray(this.awareness.getStates()) });
2420
2445
  });
2421
- this.awareness.on('change', () => {
2446
+ (_b = this.awareness) === null || _b === void 0 ? void 0 : _b.on('change', () => {
2422
2447
  this.emit('awarenessChange', { states: awarenessStatesToArray(this.awareness.getStates()) });
2423
2448
  });
2424
2449
  this.document.on('update', this.documentUpdateHandler.bind(this));
2425
- this.awareness.on('update', this.awarenessUpdateHandler.bind(this));
2450
+ (_c = this.awareness) === null || _c === void 0 ? void 0 : _c.on('update', this.awarenessUpdateHandler.bind(this));
2426
2451
  this.registerEventListeners();
2427
2452
  if (this.configuration.forceSyncInterval) {
2428
2453
  this.intervals.forceSync = setInterval(this.forceSync.bind(this), this.configuration.forceSyncInterval);
@@ -2469,7 +2494,9 @@ class HocuspocusProvider extends EventEmitter {
2469
2494
  this.send(SyncStepOneMessage, { document: this.document, documentName: this.configuration.name });
2470
2495
  }
2471
2496
  pageUnload() {
2472
- removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
2497
+ if (this.awareness) {
2498
+ removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
2499
+ }
2473
2500
  }
2474
2501
  registerEventListeners() {
2475
2502
  if (typeof window === 'undefined') {
@@ -2550,7 +2577,7 @@ class HocuspocusProvider extends EventEmitter {
2550
2577
  startSync() {
2551
2578
  this.incrementUnsyncedChanges();
2552
2579
  this.send(SyncStepOneMessage, { document: this.document, documentName: this.configuration.name });
2553
- if (this.awareness.getLocalState() !== null) {
2580
+ if (this.awareness && this.awareness.getLocalState() !== null) {
2554
2581
  this.send(AwarenessMessage, {
2555
2582
  awareness: this.awareness,
2556
2583
  clients: [this.document.clientID],
@@ -2583,16 +2610,21 @@ class HocuspocusProvider extends EventEmitter {
2583
2610
  this.isAuthenticated = false;
2584
2611
  this.synced = false;
2585
2612
  // update awareness (all users except local left)
2586
- removeAwarenessStates(this.awareness, Array.from(this.awareness.getStates().keys()).filter(client => client !== this.document.clientID), this);
2613
+ if (this.awareness) {
2614
+ removeAwarenessStates(this.awareness, Array.from(this.awareness.getStates().keys()).filter(client => client !== this.document.clientID), this);
2615
+ }
2587
2616
  }
2588
2617
  destroy() {
2618
+ var _a;
2589
2619
  this.emit('destroy');
2590
2620
  if (this.intervals.forceSync) {
2591
2621
  clearInterval(this.intervals.forceSync);
2592
2622
  }
2593
- removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy');
2623
+ if (this.awareness) {
2624
+ removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy');
2625
+ }
2594
2626
  this.disconnect();
2595
- this.awareness.off('update', this.awarenessUpdateHandler);
2627
+ (_a = this.awareness) === null || _a === void 0 ? void 0 : _a.off('update', this.awarenessUpdateHandler);
2596
2628
  this.document.off('update', this.documentUpdateHandler);
2597
2629
  this.removeAllListeners();
2598
2630
  this.configuration.websocketProvider.off('connect', this.configuration.onConnect);
@@ -2649,17 +2681,21 @@ class HocuspocusProvider extends EventEmitter {
2649
2681
  this.broadcast(SyncStepOneMessage, { document: this.document });
2650
2682
  this.broadcast(SyncStepTwoMessage, { document: this.document });
2651
2683
  this.broadcast(QueryAwarenessMessage, { document: this.document });
2652
- this.broadcast(AwarenessMessage, { awareness: this.awareness, clients: [this.document.clientID], document: this.document });
2684
+ if (this.awareness) {
2685
+ this.broadcast(AwarenessMessage, { awareness: this.awareness, clients: [this.document.clientID], document: this.document });
2686
+ }
2653
2687
  });
2654
2688
  }
2655
2689
  disconnectBroadcastChannel() {
2656
2690
  // broadcast message with local awareness state set to null (indicating disconnect)
2657
- this.send(AwarenessMessage, {
2658
- awareness: this.awareness,
2659
- clients: [this.document.clientID],
2660
- states: new Map(),
2661
- documentName: this.configuration.name,
2662
- }, true);
2691
+ if (this.awareness) {
2692
+ this.send(AwarenessMessage, {
2693
+ awareness: this.awareness,
2694
+ clients: [this.document.clientID],
2695
+ states: new Map(),
2696
+ documentName: this.configuration.name,
2697
+ }, true);
2698
+ }
2663
2699
  if (this.subscribedToBroadcastChannel) {
2664
2700
  unsubscribe(this.broadcastChannel, this.boundBroadcastChannelSubscriber);
2665
2701
  this.subscribedToBroadcastChannel = false;
@@ -2675,6 +2711,9 @@ class HocuspocusProvider extends EventEmitter {
2675
2711
  new MessageSender(Message, args).broadcast(this.broadcastChannel);
2676
2712
  }
2677
2713
  setAwarenessField(key, value) {
2714
+ if (!this.awareness) {
2715
+ throw new AwarenessError(`Cannot set awareness field "${key}" to ${JSON.stringify(value)}. You have disabled Awareness for this provider by explicitly passing awareness: null in the provider configuration.`);
2716
+ }
2678
2717
  this.awareness.setLocalStateField(key, value);
2679
2718
  }
2680
2719
  }
@@ -2730,5 +2769,5 @@ class TiptapCollabProvider extends HocuspocusProvider {
2730
2769
  }
2731
2770
  }
2732
2771
 
2733
- export { HocuspocusProvider, HocuspocusProviderWebsocket, MessageType, TiptapCollabProvider, TiptapCollabProviderWebsocket, WebSocketStatus };
2772
+ export { AwarenessError, HocuspocusProvider, HocuspocusProviderWebsocket, MessageType, TiptapCollabProvider, TiptapCollabProviderWebsocket, WebSocketStatus };
2734
2773
  //# sourceMappingURL=hocuspocus-provider.esm.js.map