@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.
@@ -1642,8 +1642,6 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1642
1642
  url: '',
1643
1643
  // @ts-ignore
1644
1644
  document: undefined,
1645
- // @ts-ignore
1646
- awareness: undefined,
1647
1645
  WebSocketPolyfill: undefined,
1648
1646
  parameters: {},
1649
1647
  connect: true,
@@ -1693,6 +1691,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1693
1691
  this.receivedOnOpenPayload = undefined;
1694
1692
  this.receivedOnStatusPayload = undefined;
1695
1693
  this.boundConnect = this.connect.bind(this);
1694
+ this.closeTries = 0;
1696
1695
  this.setConfiguration(configuration);
1697
1696
  this.configuration.WebSocketPolyfill = configuration.WebSocketPolyfill ? configuration.WebSocketPolyfill : WebSocket;
1698
1697
  this.on('open', this.configuration.onOpen);
@@ -1844,7 +1843,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1844
1843
  if (this.status !== exports.WebSocketStatus.Connected) {
1845
1844
  return;
1846
1845
  }
1847
- // Don’t close then connection while waiting for the first message
1846
+ // Don’t close the connection while waiting for the first message
1848
1847
  if (!this.lastMessageReceived) {
1849
1848
  return;
1850
1849
  }
@@ -1853,9 +1852,23 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1853
1852
  return;
1854
1853
  }
1855
1854
  // No message received in a long time, not even your own
1856
- // Awareness updates, which are updated every 15 seconds.
1857
- (_a = this.webSocket) === null || _a === void 0 ? void 0 : _a.close();
1858
- this.messageQueue = [];
1855
+ // Awareness updates, which are updated every 15 seconds
1856
+ // if awareness is enabled.
1857
+ this.closeTries += 1;
1858
+ // https://bugs.webkit.org/show_bug.cgi?id=247943
1859
+ if (this.closeTries > 2) {
1860
+ this.onClose({
1861
+ event: {
1862
+ code: 4408,
1863
+ reason: 'forced',
1864
+ },
1865
+ });
1866
+ this.closeTries = 0;
1867
+ }
1868
+ else {
1869
+ (_a = this.webSocket) === null || _a === void 0 ? void 0 : _a.close();
1870
+ this.messageQueue = [];
1871
+ }
1859
1872
  }
1860
1873
  registerEventListeners() {
1861
1874
  if (typeof window === 'undefined') {
@@ -1897,6 +1910,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1897
1910
  }
1898
1911
  }
1899
1912
  onClose({ event }) {
1913
+ this.closeTries = 0;
1900
1914
  this.webSocket = null;
1901
1915
  if (this.status === exports.WebSocketStatus.Connected) {
1902
1916
  this.status = exports.WebSocketStatus.Disconnected;
@@ -2195,6 +2209,8 @@ class MessageReceiver {
2195
2209
  }
2196
2210
  }
2197
2211
  applyAwarenessMessage(provider) {
2212
+ if (!provider.awareness)
2213
+ return;
2198
2214
  const { message } = this;
2199
2215
  applyAwarenessUpdate(provider.awareness, message.readVarUint8Array(), provider);
2200
2216
  }
@@ -2203,6 +2219,8 @@ class MessageReceiver {
2203
2219
  common.readAuthMessage(message.decoder, provider.permissionDeniedHandler.bind(provider), provider.authenticatedHandler.bind(provider));
2204
2220
  }
2205
2221
  applyQueryAwarenessMessage(provider) {
2222
+ if (!provider.awareness)
2223
+ return;
2206
2224
  const { message } = this;
2207
2225
  message.writeVarUint(exports.MessageType.Awareness);
2208
2226
  message.writeVarUint8Array(encodeAwarenessUpdate(provider.awareness, Array.from(provider.awareness.getStates().keys())));
@@ -2360,8 +2378,15 @@ class UpdateMessage extends OutgoingMessage {
2360
2378
  }
2361
2379
  }
2362
2380
 
2381
+ class AwarenessError extends Error {
2382
+ constructor() {
2383
+ super(...arguments);
2384
+ this.code = 1001;
2385
+ }
2386
+ }
2363
2387
  class HocuspocusProvider extends EventEmitter {
2364
2388
  constructor(configuration) {
2389
+ var _a, _b, _c;
2365
2390
  super();
2366
2391
  this.configuration = {
2367
2392
  name: '',
@@ -2415,7 +2440,7 @@ class HocuspocusProvider extends EventEmitter {
2415
2440
  this.forwardDestroy = (e) => this.emit('destroy', e);
2416
2441
  this.setConfiguration(configuration);
2417
2442
  this.configuration.document = configuration.document ? configuration.document : new Y__namespace.Doc();
2418
- this.configuration.awareness = configuration.awareness ? configuration.awareness : new Awareness(this.document);
2443
+ this.configuration.awareness = configuration.awareness !== undefined ? configuration.awareness : new Awareness(this.document);
2419
2444
  this.on('open', this.configuration.onOpen);
2420
2445
  this.on('message', this.configuration.onMessage);
2421
2446
  this.on('outgoingMessage', this.configuration.onOutgoingMessage);
@@ -2439,14 +2464,14 @@ class HocuspocusProvider extends EventEmitter {
2439
2464
  this.configuration.websocketProvider.on('disconnect', this.forwardDisconnect);
2440
2465
  this.configuration.websocketProvider.on('destroy', this.configuration.onDestroy);
2441
2466
  this.configuration.websocketProvider.on('destroy', this.forwardDestroy);
2442
- this.awareness.on('update', () => {
2467
+ (_a = this.awareness) === null || _a === void 0 ? void 0 : _a.on('update', () => {
2443
2468
  this.emit('awarenessUpdate', { states: common.awarenessStatesToArray(this.awareness.getStates()) });
2444
2469
  });
2445
- this.awareness.on('change', () => {
2470
+ (_b = this.awareness) === null || _b === void 0 ? void 0 : _b.on('change', () => {
2446
2471
  this.emit('awarenessChange', { states: common.awarenessStatesToArray(this.awareness.getStates()) });
2447
2472
  });
2448
2473
  this.document.on('update', this.documentUpdateHandler.bind(this));
2449
- this.awareness.on('update', this.awarenessUpdateHandler.bind(this));
2474
+ (_c = this.awareness) === null || _c === void 0 ? void 0 : _c.on('update', this.awarenessUpdateHandler.bind(this));
2450
2475
  this.registerEventListeners();
2451
2476
  if (this.configuration.forceSyncInterval) {
2452
2477
  this.intervals.forceSync = setInterval(this.forceSync.bind(this), this.configuration.forceSyncInterval);
@@ -2493,7 +2518,9 @@ class HocuspocusProvider extends EventEmitter {
2493
2518
  this.send(SyncStepOneMessage, { document: this.document, documentName: this.configuration.name });
2494
2519
  }
2495
2520
  pageUnload() {
2496
- removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
2521
+ if (this.awareness) {
2522
+ removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
2523
+ }
2497
2524
  }
2498
2525
  registerEventListeners() {
2499
2526
  if (typeof window === 'undefined') {
@@ -2574,7 +2601,7 @@ class HocuspocusProvider extends EventEmitter {
2574
2601
  startSync() {
2575
2602
  this.incrementUnsyncedChanges();
2576
2603
  this.send(SyncStepOneMessage, { document: this.document, documentName: this.configuration.name });
2577
- if (this.awareness.getLocalState() !== null) {
2604
+ if (this.awareness && this.awareness.getLocalState() !== null) {
2578
2605
  this.send(AwarenessMessage, {
2579
2606
  awareness: this.awareness,
2580
2607
  clients: [this.document.clientID],
@@ -2607,16 +2634,21 @@ class HocuspocusProvider extends EventEmitter {
2607
2634
  this.isAuthenticated = false;
2608
2635
  this.synced = false;
2609
2636
  // update awareness (all users except local left)
2610
- removeAwarenessStates(this.awareness, Array.from(this.awareness.getStates().keys()).filter(client => client !== this.document.clientID), this);
2637
+ if (this.awareness) {
2638
+ removeAwarenessStates(this.awareness, Array.from(this.awareness.getStates().keys()).filter(client => client !== this.document.clientID), this);
2639
+ }
2611
2640
  }
2612
2641
  destroy() {
2642
+ var _a;
2613
2643
  this.emit('destroy');
2614
2644
  if (this.intervals.forceSync) {
2615
2645
  clearInterval(this.intervals.forceSync);
2616
2646
  }
2617
- removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy');
2647
+ if (this.awareness) {
2648
+ removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy');
2649
+ }
2618
2650
  this.disconnect();
2619
- this.awareness.off('update', this.awarenessUpdateHandler);
2651
+ (_a = this.awareness) === null || _a === void 0 ? void 0 : _a.off('update', this.awarenessUpdateHandler);
2620
2652
  this.document.off('update', this.documentUpdateHandler);
2621
2653
  this.removeAllListeners();
2622
2654
  this.configuration.websocketProvider.off('connect', this.configuration.onConnect);
@@ -2673,17 +2705,21 @@ class HocuspocusProvider extends EventEmitter {
2673
2705
  this.broadcast(SyncStepOneMessage, { document: this.document });
2674
2706
  this.broadcast(SyncStepTwoMessage, { document: this.document });
2675
2707
  this.broadcast(QueryAwarenessMessage, { document: this.document });
2676
- this.broadcast(AwarenessMessage, { awareness: this.awareness, clients: [this.document.clientID], document: this.document });
2708
+ if (this.awareness) {
2709
+ this.broadcast(AwarenessMessage, { awareness: this.awareness, clients: [this.document.clientID], document: this.document });
2710
+ }
2677
2711
  });
2678
2712
  }
2679
2713
  disconnectBroadcastChannel() {
2680
2714
  // broadcast message with local awareness state set to null (indicating disconnect)
2681
- this.send(AwarenessMessage, {
2682
- awareness: this.awareness,
2683
- clients: [this.document.clientID],
2684
- states: new Map(),
2685
- documentName: this.configuration.name,
2686
- }, true);
2715
+ if (this.awareness) {
2716
+ this.send(AwarenessMessage, {
2717
+ awareness: this.awareness,
2718
+ clients: [this.document.clientID],
2719
+ states: new Map(),
2720
+ documentName: this.configuration.name,
2721
+ }, true);
2722
+ }
2687
2723
  if (this.subscribedToBroadcastChannel) {
2688
2724
  unsubscribe(this.broadcastChannel, this.boundBroadcastChannelSubscriber);
2689
2725
  this.subscribedToBroadcastChannel = false;
@@ -2699,6 +2735,9 @@ class HocuspocusProvider extends EventEmitter {
2699
2735
  new MessageSender(Message, args).broadcast(this.broadcastChannel);
2700
2736
  }
2701
2737
  setAwarenessField(key, value) {
2738
+ if (!this.awareness) {
2739
+ 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.`);
2740
+ }
2702
2741
  this.awareness.setLocalStateField(key, value);
2703
2742
  }
2704
2743
  }
@@ -2754,6 +2793,7 @@ class TiptapCollabProvider extends HocuspocusProvider {
2754
2793
  }
2755
2794
  }
2756
2795
 
2796
+ exports.AwarenessError = AwarenessError;
2757
2797
  exports.HocuspocusProvider = HocuspocusProvider;
2758
2798
  exports.HocuspocusProviderWebsocket = HocuspocusProviderWebsocket;
2759
2799
  exports.TiptapCollabProvider = TiptapCollabProvider;