@hocuspocus/provider 2.6.1 → 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.
@@ -964,6 +964,20 @@ const _readVarStringNative = decoder =>
964
964
  /* c8 ignore next */
965
965
  const readVarString = utf8TextDecoder ? _readVarStringNative : _readVarStringPolyfill;
966
966
 
967
+ /**
968
+ * Look ahead and read varString without incrementing position
969
+ *
970
+ * @function
971
+ * @param {Decoder} decoder
972
+ * @return {string}
973
+ */
974
+ const peekVarString = decoder => {
975
+ const pos = decoder.pos;
976
+ const s = readVarString(decoder);
977
+ decoder.pos = pos;
978
+ return s
979
+ };
980
+
967
981
  /**
968
982
  * Utility functions to work with buffers (Uint8Array).
969
983
  *
@@ -1610,6 +1624,38 @@ var WebSocketStatus;
1610
1624
  WebSocketStatus["Disconnected"] = "disconnected";
1611
1625
  })(WebSocketStatus || (WebSocketStatus = {}));
1612
1626
 
1627
+ class IncomingMessage {
1628
+ constructor(data) {
1629
+ this.data = data;
1630
+ this.encoder = createEncoder();
1631
+ this.decoder = createDecoder(new Uint8Array(this.data));
1632
+ }
1633
+ peekVarString() {
1634
+ return peekVarString(this.decoder);
1635
+ }
1636
+ readVarUint() {
1637
+ return readVarUint(this.decoder);
1638
+ }
1639
+ readVarString() {
1640
+ return readVarString(this.decoder);
1641
+ }
1642
+ readVarUint8Array() {
1643
+ return readVarUint8Array(this.decoder);
1644
+ }
1645
+ writeVarUint(type) {
1646
+ return writeVarUint(this.encoder, type);
1647
+ }
1648
+ writeVarString(string) {
1649
+ return writeVarString(this.encoder, string);
1650
+ }
1651
+ writeVarUint8Array(data) {
1652
+ return writeVarUint8Array(this.encoder, data);
1653
+ }
1654
+ length() {
1655
+ return length(this.encoder);
1656
+ }
1657
+ }
1658
+
1613
1659
  class HocuspocusProviderWebsocket extends EventEmitter {
1614
1660
  constructor(configuration) {
1615
1661
  super();
@@ -1652,6 +1698,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1652
1698
  onAwarenessUpdate: () => null,
1653
1699
  onAwarenessChange: () => null,
1654
1700
  quiet: false,
1701
+ providerMap: new Map(),
1655
1702
  };
1656
1703
  this.subscribedToBroadcastChannel = false;
1657
1704
  this.webSocket = null;
@@ -1704,6 +1751,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1704
1751
  this.receivedOnStatusPayload = data;
1705
1752
  }
1706
1753
  attach(provider) {
1754
+ this.configuration.providerMap.set(provider.configuration.name, provider);
1707
1755
  if (this.status === WebSocketStatus.Disconnected && this.shouldConnect) {
1708
1756
  this.connect();
1709
1757
  }
@@ -1715,7 +1763,7 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1715
1763
  }
1716
1764
  }
1717
1765
  detach(provider) {
1718
- // tell the server to remove the listener
1766
+ this.configuration.providerMap.delete(provider.configuration.name);
1719
1767
  }
1720
1768
  setConfiguration(configuration = {}) {
1721
1769
  this.configuration = { ...this.configuration, ...configuration };
@@ -1824,8 +1872,12 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1824
1872
  });
1825
1873
  }
1826
1874
  onMessage(event) {
1875
+ var _a;
1827
1876
  this.resolveConnectionAttempt();
1828
1877
  this.lastMessageReceived = getUnixTime();
1878
+ const message = new IncomingMessage(event.data);
1879
+ const documentName = message.peekVarString();
1880
+ (_a = this.configuration.providerMap.get(documentName)) === null || _a === void 0 ? void 0 : _a.onMessage(event);
1829
1881
  }
1830
1882
  resolveConnectionAttempt() {
1831
1883
  if (this.connectionAttempt) {
@@ -1977,35 +2029,6 @@ class HocuspocusProviderWebsocket extends EventEmitter {
1977
2029
  }
1978
2030
  }
1979
2031
 
1980
- class IncomingMessage {
1981
- constructor(data) {
1982
- this.data = data;
1983
- this.encoder = createEncoder();
1984
- this.decoder = createDecoder(new Uint8Array(this.data));
1985
- }
1986
- readVarUint() {
1987
- return readVarUint(this.decoder);
1988
- }
1989
- readVarString() {
1990
- return readVarString(this.decoder);
1991
- }
1992
- readVarUint8Array() {
1993
- return readVarUint8Array(this.decoder);
1994
- }
1995
- writeVarUint(type) {
1996
- return writeVarUint(this.encoder, type);
1997
- }
1998
- writeVarString(string) {
1999
- return writeVarString(this.encoder, string);
2000
- }
2001
- writeVarUint8Array(data) {
2002
- return writeVarUint8Array(this.encoder, data);
2003
- }
2004
- length() {
2005
- return length(this.encoder);
2006
- }
2007
- }
2008
-
2009
2032
  /**
2010
2033
  * @module sync-protocol
2011
2034
  */
@@ -2308,8 +2331,6 @@ class QueryAwarenessMessage extends OutgoingMessage {
2308
2331
  this.description = 'Queries awareness states';
2309
2332
  }
2310
2333
  get(args) {
2311
- console.log('queryAwareness: writing string docName', args.documentName);
2312
- console.log(this.encoder.cpos);
2313
2334
  writeVarString(this.encoder, args.documentName);
2314
2335
  writeVarUint(this.encoder, this.type);
2315
2336
  return this.encoder;
@@ -2431,7 +2452,6 @@ class HocuspocusProvider extends EventEmitter {
2431
2452
  this.boundBroadcastChannelSubscriber = this.broadcastChannelSubscriber.bind(this);
2432
2453
  this.boundPageUnload = this.pageUnload.bind(this);
2433
2454
  this.boundOnOpen = this.onOpen.bind(this);
2434
- this.boundOnMessage = this.onMessage.bind(this);
2435
2455
  this.boundOnClose = this.onClose.bind(this);
2436
2456
  this.boundOnStatus = this.onStatus.bind(this);
2437
2457
  this.forwardConnect = (e) => this.emit('connect', e);
@@ -2456,7 +2476,6 @@ class HocuspocusProvider extends EventEmitter {
2456
2476
  this.configuration.websocketProvider.on('connect', this.forwardConnect);
2457
2477
  this.configuration.websocketProvider.on('open', this.boundOnOpen);
2458
2478
  this.configuration.websocketProvider.on('open', this.forwardOpen);
2459
- this.configuration.websocketProvider.on('message', this.boundOnMessage);
2460
2479
  this.configuration.websocketProvider.on('close', this.boundOnClose);
2461
2480
  this.configuration.websocketProvider.on('close', this.configuration.onClose);
2462
2481
  this.configuration.websocketProvider.on('close', this.forwardClose);
@@ -2587,9 +2606,17 @@ class HocuspocusProvider extends EventEmitter {
2587
2606
  async onOpen(event) {
2588
2607
  this.isAuthenticated = false;
2589
2608
  this.emit('open', { event });
2609
+ let token;
2610
+ try {
2611
+ token = await this.getToken();
2612
+ }
2613
+ catch (error) {
2614
+ this.permissionDeniedHandler(`Failed to get token: ${error}`);
2615
+ return;
2616
+ }
2590
2617
  if (this.isAuthenticationRequired) {
2591
2618
  this.send(AuthenticationMessage, {
2592
- token: await this.getToken(),
2619
+ token,
2593
2620
  documentName: this.configuration.name,
2594
2621
  });
2595
2622
  }
@@ -2627,9 +2654,6 @@ class HocuspocusProvider extends EventEmitter {
2627
2654
  onMessage(event) {
2628
2655
  const message = new IncomingMessage(event.data);
2629
2656
  const documentName = message.readVarString();
2630
- if (documentName !== this.configuration.name) {
2631
- return; // message is meant for another provider
2632
- }
2633
2657
  message.writeVarString(documentName);
2634
2658
  this.emit('message', { event, message: new IncomingMessage(event.data) });
2635
2659
  new MessageReceiver(message).apply(this, true);
@@ -2659,7 +2683,6 @@ class HocuspocusProvider extends EventEmitter {
2659
2683
  this.configuration.websocketProvider.off('connect', this.forwardConnect);
2660
2684
  this.configuration.websocketProvider.off('open', this.boundOnOpen);
2661
2685
  this.configuration.websocketProvider.off('open', this.forwardOpen);
2662
- this.configuration.websocketProvider.off('message', this.boundOnMessage);
2663
2686
  this.configuration.websocketProvider.off('close', this.boundOnClose);
2664
2687
  this.configuration.websocketProvider.off('close', this.configuration.onClose);
2665
2688
  this.configuration.websocketProvider.off('close', this.forwardClose);