@hocuspocus/provider 2.3.1 → 2.4.0-rc.1

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,
@@ -1845,7 +1843,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1845
1843
  if (this.status !== exports.WebSocketStatus.Connected) {
1846
1844
  return;
1847
1845
  }
1848
- // Don’t close then connection while waiting for the first message
1846
+ // Don’t close the connection while waiting for the first message
1849
1847
  if (!this.lastMessageReceived) {
1850
1848
  return;
1851
1849
  }
@@ -1854,7 +1852,8 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1854
1852
  return;
1855
1853
  }
1856
1854
  // No message received in a long time, not even your own
1857
- // Awareness updates, which are updated every 15 seconds.
1855
+ // Awareness updates, which are updated every 15 seconds
1856
+ // if awareness is enabled.
1858
1857
  this.closeTries += 1;
1859
1858
  // https://bugs.webkit.org/show_bug.cgi?id=247943
1860
1859
  if (this.closeTries > 2) {
@@ -2210,6 +2209,8 @@ class MessageReceiver {
2210
2209
  }
2211
2210
  }
2212
2211
  applyAwarenessMessage(provider) {
2212
+ if (!provider.awareness)
2213
+ return;
2213
2214
  const { message } = this;
2214
2215
  applyAwarenessUpdate(provider.awareness, message.readVarUint8Array(), provider);
2215
2216
  }
@@ -2218,6 +2219,8 @@ class MessageReceiver {
2218
2219
  common.readAuthMessage(message.decoder, provider.permissionDeniedHandler.bind(provider), provider.authenticatedHandler.bind(provider));
2219
2220
  }
2220
2221
  applyQueryAwarenessMessage(provider) {
2222
+ if (!provider.awareness)
2223
+ return;
2221
2224
  const { message } = this;
2222
2225
  message.writeVarUint(exports.MessageType.Awareness);
2223
2226
  message.writeVarUint8Array(encodeAwarenessUpdate(provider.awareness, Array.from(provider.awareness.getStates().keys())));
@@ -2375,8 +2378,15 @@ class UpdateMessage extends OutgoingMessage {
2375
2378
  }
2376
2379
  }
2377
2380
 
2381
+ class AwarenessError extends Error {
2382
+ constructor() {
2383
+ super(...arguments);
2384
+ this.code = 1001;
2385
+ }
2386
+ }
2378
2387
  class HocuspocusProvider extends EventEmitter {
2379
2388
  constructor(configuration) {
2389
+ var _a, _b, _c;
2380
2390
  super();
2381
2391
  this.configuration = {
2382
2392
  name: '',
@@ -2430,7 +2440,7 @@ class HocuspocusProvider extends EventEmitter {
2430
2440
  this.forwardDestroy = (e) => this.emit('destroy', e);
2431
2441
  this.setConfiguration(configuration);
2432
2442
  this.configuration.document = configuration.document ? configuration.document : new Y__namespace.Doc();
2433
- this.configuration.awareness = configuration.awareness ? configuration.awareness : new Awareness(this.document);
2443
+ this.configuration.awareness = configuration.awareness !== undefined ? configuration.awareness : new Awareness(this.document);
2434
2444
  this.on('open', this.configuration.onOpen);
2435
2445
  this.on('message', this.configuration.onMessage);
2436
2446
  this.on('outgoingMessage', this.configuration.onOutgoingMessage);
@@ -2454,14 +2464,14 @@ class HocuspocusProvider extends EventEmitter {
2454
2464
  this.configuration.websocketProvider.on('disconnect', this.forwardDisconnect);
2455
2465
  this.configuration.websocketProvider.on('destroy', this.configuration.onDestroy);
2456
2466
  this.configuration.websocketProvider.on('destroy', this.forwardDestroy);
2457
- this.awareness.on('update', () => {
2467
+ (_a = this.awareness) === null || _a === void 0 ? void 0 : _a.on('update', () => {
2458
2468
  this.emit('awarenessUpdate', { states: common.awarenessStatesToArray(this.awareness.getStates()) });
2459
2469
  });
2460
- this.awareness.on('change', () => {
2470
+ (_b = this.awareness) === null || _b === void 0 ? void 0 : _b.on('change', () => {
2461
2471
  this.emit('awarenessChange', { states: common.awarenessStatesToArray(this.awareness.getStates()) });
2462
2472
  });
2463
2473
  this.document.on('update', this.documentUpdateHandler.bind(this));
2464
- 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));
2465
2475
  this.registerEventListeners();
2466
2476
  if (this.configuration.forceSyncInterval) {
2467
2477
  this.intervals.forceSync = setInterval(this.forceSync.bind(this), this.configuration.forceSyncInterval);
@@ -2508,7 +2518,9 @@ class HocuspocusProvider extends EventEmitter {
2508
2518
  this.send(SyncStepOneMessage, { document: this.document, documentName: this.configuration.name });
2509
2519
  }
2510
2520
  pageUnload() {
2511
- removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
2521
+ if (this.awareness) {
2522
+ removeAwarenessStates(this.awareness, [this.document.clientID], 'window unload');
2523
+ }
2512
2524
  }
2513
2525
  registerEventListeners() {
2514
2526
  if (typeof window === 'undefined') {
@@ -2589,7 +2601,7 @@ class HocuspocusProvider extends EventEmitter {
2589
2601
  startSync() {
2590
2602
  this.incrementUnsyncedChanges();
2591
2603
  this.send(SyncStepOneMessage, { document: this.document, documentName: this.configuration.name });
2592
- if (this.awareness.getLocalState() !== null) {
2604
+ if (this.awareness && this.awareness.getLocalState() !== null) {
2593
2605
  this.send(AwarenessMessage, {
2594
2606
  awareness: this.awareness,
2595
2607
  clients: [this.document.clientID],
@@ -2622,16 +2634,21 @@ class HocuspocusProvider extends EventEmitter {
2622
2634
  this.isAuthenticated = false;
2623
2635
  this.synced = false;
2624
2636
  // update awareness (all users except local left)
2625
- 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
+ }
2626
2640
  }
2627
2641
  destroy() {
2642
+ var _a;
2628
2643
  this.emit('destroy');
2629
2644
  if (this.intervals.forceSync) {
2630
2645
  clearInterval(this.intervals.forceSync);
2631
2646
  }
2632
- removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy');
2647
+ if (this.awareness) {
2648
+ removeAwarenessStates(this.awareness, [this.document.clientID], 'provider destroy');
2649
+ }
2633
2650
  this.disconnect();
2634
- this.awareness.off('update', this.awarenessUpdateHandler);
2651
+ (_a = this.awareness) === null || _a === void 0 ? void 0 : _a.off('update', this.awarenessUpdateHandler);
2635
2652
  this.document.off('update', this.documentUpdateHandler);
2636
2653
  this.removeAllListeners();
2637
2654
  this.configuration.websocketProvider.off('connect', this.configuration.onConnect);
@@ -2688,17 +2705,21 @@ class HocuspocusProvider extends EventEmitter {
2688
2705
  this.broadcast(SyncStepOneMessage, { document: this.document });
2689
2706
  this.broadcast(SyncStepTwoMessage, { document: this.document });
2690
2707
  this.broadcast(QueryAwarenessMessage, { document: this.document });
2691
- 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
+ }
2692
2711
  });
2693
2712
  }
2694
2713
  disconnectBroadcastChannel() {
2695
2714
  // broadcast message with local awareness state set to null (indicating disconnect)
2696
- this.send(AwarenessMessage, {
2697
- awareness: this.awareness,
2698
- clients: [this.document.clientID],
2699
- states: new Map(),
2700
- documentName: this.configuration.name,
2701
- }, 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
+ }
2702
2723
  if (this.subscribedToBroadcastChannel) {
2703
2724
  unsubscribe(this.broadcastChannel, this.boundBroadcastChannelSubscriber);
2704
2725
  this.subscribedToBroadcastChannel = false;
@@ -2714,6 +2735,9 @@ class HocuspocusProvider extends EventEmitter {
2714
2735
  new MessageSender(Message, args).broadcast(this.broadcastChannel);
2715
2736
  }
2716
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
+ }
2717
2741
  this.awareness.setLocalStateField(key, value);
2718
2742
  }
2719
2743
  }
@@ -2769,6 +2793,7 @@ class TiptapCollabProvider extends HocuspocusProvider {
2769
2793
  }
2770
2794
  }
2771
2795
 
2796
+ exports.AwarenessError = AwarenessError;
2772
2797
  exports.HocuspocusProvider = HocuspocusProvider;
2773
2798
  exports.HocuspocusProviderWebsocket = HocuspocusProviderWebsocket;
2774
2799
  exports.TiptapCollabProvider = TiptapCollabProvider;