@hocuspocus/provider 2.6.0 → 2.7.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.
@@ -988,6 +988,20 @@ const _readVarStringNative = decoder =>
988
988
  /* c8 ignore next */
989
989
  const readVarString = utf8TextDecoder ? _readVarStringNative : _readVarStringPolyfill;
990
990
 
991
+ /**
992
+ * Look ahead and read varString without incrementing position
993
+ *
994
+ * @function
995
+ * @param {Decoder} decoder
996
+ * @return {string}
997
+ */
998
+ const peekVarString = decoder => {
999
+ const pos = decoder.pos;
1000
+ const s = readVarString(decoder);
1001
+ decoder.pos = pos;
1002
+ return s
1003
+ };
1004
+
991
1005
  /**
992
1006
  * Utility functions to work with buffers (Uint8Array).
993
1007
  *
@@ -1634,6 +1648,38 @@ exports.WebSocketStatus = void 0;
1634
1648
  WebSocketStatus["Disconnected"] = "disconnected";
1635
1649
  })(exports.WebSocketStatus || (exports.WebSocketStatus = {}));
1636
1650
 
1651
+ class IncomingMessage {
1652
+ constructor(data) {
1653
+ this.data = data;
1654
+ this.encoder = createEncoder();
1655
+ this.decoder = createDecoder(new Uint8Array(this.data));
1656
+ }
1657
+ peekVarString() {
1658
+ return peekVarString(this.decoder);
1659
+ }
1660
+ readVarUint() {
1661
+ return readVarUint(this.decoder);
1662
+ }
1663
+ readVarString() {
1664
+ return readVarString(this.decoder);
1665
+ }
1666
+ readVarUint8Array() {
1667
+ return readVarUint8Array(this.decoder);
1668
+ }
1669
+ writeVarUint(type) {
1670
+ return writeVarUint(this.encoder, type);
1671
+ }
1672
+ writeVarString(string) {
1673
+ return writeVarString(this.encoder, string);
1674
+ }
1675
+ writeVarUint8Array(data) {
1676
+ return writeVarUint8Array(this.encoder, data);
1677
+ }
1678
+ length() {
1679
+ return length(this.encoder);
1680
+ }
1681
+ }
1682
+
1637
1683
  class HocuspocusProviderWebsocket extends EventEmitter {
1638
1684
  constructor(configuration) {
1639
1685
  super();
@@ -1676,6 +1722,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1676
1722
  onAwarenessUpdate: () => null,
1677
1723
  onAwarenessChange: () => null,
1678
1724
  quiet: false,
1725
+ providerMap: new Map(),
1679
1726
  };
1680
1727
  this.subscribedToBroadcastChannel = false;
1681
1728
  this.webSocket = null;
@@ -1728,6 +1775,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1728
1775
  this.receivedOnStatusPayload = data;
1729
1776
  }
1730
1777
  attach(provider) {
1778
+ this.configuration.providerMap.set(provider.configuration.name, provider);
1731
1779
  if (this.status === exports.WebSocketStatus.Disconnected && this.shouldConnect) {
1732
1780
  this.connect();
1733
1781
  }
@@ -1739,7 +1787,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1739
1787
  }
1740
1788
  }
1741
1789
  detach(provider) {
1742
- // tell the server to remove the listener
1790
+ this.configuration.providerMap.delete(provider.configuration.name);
1743
1791
  }
1744
1792
  setConfiguration(configuration = {}) {
1745
1793
  this.configuration = { ...this.configuration, ...configuration };
@@ -1848,8 +1896,12 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1848
1896
  });
1849
1897
  }
1850
1898
  onMessage(event) {
1899
+ var _a;
1851
1900
  this.resolveConnectionAttempt();
1852
1901
  this.lastMessageReceived = getUnixTime();
1902
+ const message = new IncomingMessage(event.data);
1903
+ const documentName = message.peekVarString();
1904
+ (_a = this.configuration.providerMap.get(documentName)) === null || _a === void 0 ? void 0 : _a.onMessage(event);
1853
1905
  }
1854
1906
  resolveConnectionAttempt() {
1855
1907
  if (this.connectionAttempt) {
@@ -2001,35 +2053,6 @@ class HocuspocusProviderWebsocket extends EventEmitter {
2001
2053
  }
2002
2054
  }
2003
2055
 
2004
- class IncomingMessage {
2005
- constructor(data) {
2006
- this.data = data;
2007
- this.encoder = createEncoder();
2008
- this.decoder = createDecoder(new Uint8Array(this.data));
2009
- }
2010
- readVarUint() {
2011
- return readVarUint(this.decoder);
2012
- }
2013
- readVarString() {
2014
- return readVarString(this.decoder);
2015
- }
2016
- readVarUint8Array() {
2017
- return readVarUint8Array(this.decoder);
2018
- }
2019
- writeVarUint(type) {
2020
- return writeVarUint(this.encoder, type);
2021
- }
2022
- writeVarString(string) {
2023
- return writeVarString(this.encoder, string);
2024
- }
2025
- writeVarUint8Array(data) {
2026
- return writeVarUint8Array(this.encoder, data);
2027
- }
2028
- length() {
2029
- return length(this.encoder);
2030
- }
2031
- }
2032
-
2033
2056
  /**
2034
2057
  * @module sync-protocol
2035
2058
  */
@@ -2332,8 +2355,6 @@ class QueryAwarenessMessage extends OutgoingMessage {
2332
2355
  this.description = 'Queries awareness states';
2333
2356
  }
2334
2357
  get(args) {
2335
- console.log('queryAwareness: writing string docName', args.documentName);
2336
- console.log(this.encoder.cpos);
2337
2358
  writeVarString(this.encoder, args.documentName);
2338
2359
  writeVarUint(this.encoder, this.type);
2339
2360
  return this.encoder;
@@ -2455,7 +2476,6 @@ class HocuspocusProvider extends EventEmitter {
2455
2476
  this.boundBroadcastChannelSubscriber = this.broadcastChannelSubscriber.bind(this);
2456
2477
  this.boundPageUnload = this.pageUnload.bind(this);
2457
2478
  this.boundOnOpen = this.onOpen.bind(this);
2458
- this.boundOnMessage = this.onMessage.bind(this);
2459
2479
  this.boundOnClose = this.onClose.bind(this);
2460
2480
  this.boundOnStatus = this.onStatus.bind(this);
2461
2481
  this.forwardConnect = (e) => this.emit('connect', e);
@@ -2480,7 +2500,6 @@ class HocuspocusProvider extends EventEmitter {
2480
2500
  this.configuration.websocketProvider.on('connect', this.forwardConnect);
2481
2501
  this.configuration.websocketProvider.on('open', this.boundOnOpen);
2482
2502
  this.configuration.websocketProvider.on('open', this.forwardOpen);
2483
- this.configuration.websocketProvider.on('message', this.boundOnMessage);
2484
2503
  this.configuration.websocketProvider.on('close', this.boundOnClose);
2485
2504
  this.configuration.websocketProvider.on('close', this.configuration.onClose);
2486
2505
  this.configuration.websocketProvider.on('close', this.forwardClose);
@@ -2611,9 +2630,17 @@ class HocuspocusProvider extends EventEmitter {
2611
2630
  async onOpen(event) {
2612
2631
  this.isAuthenticated = false;
2613
2632
  this.emit('open', { event });
2633
+ let token;
2634
+ try {
2635
+ token = await this.getToken();
2636
+ }
2637
+ catch (error) {
2638
+ this.permissionDeniedHandler(`Failed to get token: ${error}`);
2639
+ return;
2640
+ }
2614
2641
  if (this.isAuthenticationRequired) {
2615
2642
  this.send(AuthenticationMessage, {
2616
- token: await this.getToken(),
2643
+ token,
2617
2644
  documentName: this.configuration.name,
2618
2645
  });
2619
2646
  }
@@ -2651,9 +2678,6 @@ class HocuspocusProvider extends EventEmitter {
2651
2678
  onMessage(event) {
2652
2679
  const message = new IncomingMessage(event.data);
2653
2680
  const documentName = message.readVarString();
2654
- if (documentName !== this.configuration.name) {
2655
- return; // message is meant for another provider
2656
- }
2657
2681
  message.writeVarString(documentName);
2658
2682
  this.emit('message', { event, message: new IncomingMessage(event.data) });
2659
2683
  new MessageReceiver(message).apply(this, true);
@@ -2683,7 +2707,6 @@ class HocuspocusProvider extends EventEmitter {
2683
2707
  this.configuration.websocketProvider.off('connect', this.forwardConnect);
2684
2708
  this.configuration.websocketProvider.off('open', this.boundOnOpen);
2685
2709
  this.configuration.websocketProvider.off('open', this.forwardOpen);
2686
- this.configuration.websocketProvider.off('message', this.boundOnMessage);
2687
2710
  this.configuration.websocketProvider.off('close', this.boundOnClose);
2688
2711
  this.configuration.websocketProvider.off('close', this.configuration.onClose);
2689
2712
  this.configuration.websocketProvider.off('close', this.forwardClose);
@@ -2730,11 +2753,16 @@ class HocuspocusProvider extends EventEmitter {
2730
2753
  this.subscribedToBroadcastChannel = true;
2731
2754
  }
2732
2755
  this.mux(() => {
2733
- this.broadcast(SyncStepOneMessage, { document: this.document });
2734
- this.broadcast(SyncStepTwoMessage, { document: this.document });
2735
- this.broadcast(QueryAwarenessMessage, { document: this.document });
2756
+ this.broadcast(SyncStepOneMessage, { document: this.document, documentName: this.configuration.name });
2757
+ this.broadcast(SyncStepTwoMessage, { document: this.document, documentName: this.configuration.name });
2758
+ this.broadcast(QueryAwarenessMessage, { document: this.document, documentName: this.configuration.name });
2736
2759
  if (this.awareness) {
2737
- this.broadcast(AwarenessMessage, { awareness: this.awareness, clients: [this.document.clientID], document: this.document });
2760
+ this.broadcast(AwarenessMessage, {
2761
+ awareness: this.awareness,
2762
+ clients: [this.document.clientID],
2763
+ document: this.document,
2764
+ documentName: this.configuration.name,
2765
+ });
2738
2766
  }
2739
2767
  });
2740
2768
  }