@abraca/dabra 2.35.0 → 2.36.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.
@@ -2453,11 +2453,14 @@ var AwarenessError = class extends Error {
2453
2453
  this.code = 1001;
2454
2454
  }
2455
2455
  };
2456
- var AbracadabraBaseProvider = class extends EventEmitter {
2456
+ var AbracadabraBaseProvider = class AbracadabraBaseProvider extends EventEmitter {
2457
2457
  /** Current WebSocket connection status. */
2458
2458
  get connectionStatus() {
2459
2459
  return this.configuration.websocketProvider.status;
2460
2460
  }
2461
+ static {
2462
+ this.MAX_RESUBSCRIBE_ATTEMPTS = 6;
2463
+ }
2461
2464
  get rpc() {
2462
2465
  if (!this._rpc) this._rpc = new RpcClient(this);
2463
2466
  return this._rpc;
@@ -2509,6 +2512,8 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2509
2512
  this.authorizedScope = void 0;
2510
2513
  this.manageSocket = false;
2511
2514
  this._isAttached = false;
2515
+ this._resubscribeAttempts = 0;
2516
+ this._resubscribeTimer = null;
2512
2517
  this.intervals = { forceSync: null };
2513
2518
  this.boundDocumentUpdateHandler = this.documentUpdateHandler.bind(this);
2514
2519
  this.boundAwarenessUpdateHandler = this.awarenessUpdateHandler.bind(this);
@@ -2621,6 +2626,7 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2621
2626
  }
2622
2627
  documentUpdateHandler(update, origin) {
2623
2628
  if (origin === this) return;
2629
+ if (!this.isAuthenticated) return;
2624
2630
  this.incrementUnsyncedChanges();
2625
2631
  this.send(UpdateMessage, {
2626
2632
  update,
@@ -2628,6 +2634,7 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2628
2634
  });
2629
2635
  }
2630
2636
  awarenessUpdateHandler({ added, updated, removed }, origin) {
2637
+ if (!this.isAuthenticated) return;
2631
2638
  const changedClients = added.concat(updated).concat(removed);
2632
2639
  this.send(AwarenessMessage, {
2633
2640
  awareness: this.awareness,
@@ -2751,6 +2758,7 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2751
2758
  this.isAuthenticated = false;
2752
2759
  this.synced = false;
2753
2760
  this._hasEverAuthenticated = false;
2761
+ this.clearResubscribe();
2754
2762
  if (this.awareness) removeAwarenessStates(this.awareness, Array.from(this.awareness.getStates().keys()).filter((client) => client !== this.document.clientID), this);
2755
2763
  }
2756
2764
  destroy() {
@@ -2785,6 +2793,7 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2785
2793
  this.configuration.websocketProvider.off("rateLimited", this.forwardRateLimited);
2786
2794
  this.configuration.websocketProvider.detach(this);
2787
2795
  this._isAttached = false;
2796
+ this.clearResubscribe();
2788
2797
  }
2789
2798
  attach() {
2790
2799
  if (this._isAttached) return;
@@ -2810,11 +2819,47 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2810
2819
  if (this.manageSocket && !this._hasEverAuthenticated) try {
2811
2820
  this.configuration.websocketProvider.disconnect();
2812
2821
  } catch {}
2822
+ if (!this.manageSocket && !this.isSynced && this._isAttached && this._resubscribeAttempts < AbracadabraBaseProvider.MAX_RESUBSCRIBE_ATTEMPTS && AbracadabraBaseProvider.isTransientSubscribeDenial(reason)) this.scheduleResubscribe();
2823
+ }
2824
+ /**
2825
+ * Whether a permission-denied reason reflects a *transient* server condition
2826
+ * a retry can clear (a freshly-created doc not yet visible on this replica, or
2827
+ * a dropped subscribe-ordering) rather than a permanent authorization failure.
2828
+ */
2829
+ static isTransientSubscribeDenial(reason) {
2830
+ const r = reason.toLowerCase();
2831
+ return r.includes("not found") || r.includes("not subscribed");
2832
+ }
2833
+ /**
2834
+ * Schedule one backed-off replay of the initial subscribe handshake. A single
2835
+ * timer is in flight at a time; each denial that slips past the guard above
2836
+ * arms the next attempt until {@link MAX_RESUBSCRIBE_ATTEMPTS} is reached.
2837
+ */
2838
+ scheduleResubscribe() {
2839
+ if (this._resubscribeTimer) return;
2840
+ const attempt = this._resubscribeAttempts++;
2841
+ const delay = Math.min(4e3, 250 * 2 ** attempt);
2842
+ this._resubscribeTimer = setTimeout(() => {
2843
+ this._resubscribeTimer = null;
2844
+ if (!this._isAttached || this.isSynced || this.connectionStatus !== WebSocketStatus.Connected) return;
2845
+ this.sendToken().then(() => {
2846
+ if (this._isAttached && !this.isAuthenticated) this.startSync();
2847
+ }).catch(() => {});
2848
+ }, delay);
2849
+ }
2850
+ /** Cancel any pending resubscribe and reset the attempt budget. */
2851
+ clearResubscribe() {
2852
+ if (this._resubscribeTimer) {
2853
+ clearTimeout(this._resubscribeTimer);
2854
+ this._resubscribeTimer = null;
2855
+ }
2856
+ this._resubscribeAttempts = 0;
2813
2857
  }
2814
2858
  authenticatedHandler(scope) {
2815
2859
  this.isAuthenticated = true;
2816
2860
  this._hasEverAuthenticated = true;
2817
2861
  this.authorizedScope = scope;
2862
+ this.clearResubscribe();
2818
2863
  this.emit("authenticated", { scope });
2819
2864
  }
2820
2865
  setAwarenessField(key, value) {
@@ -16007,6 +16052,7 @@ var E2EAbracadabraProvider = class E2EAbracadabraProvider extends AbracadabraPro
16007
16052
  this.compactionInFlightTimeout = null;
16008
16053
  this.compactionDebounceTimer = null;
16009
16054
  this.destroyed = false;
16055
+ this._pendingPreAuthUpdates = [];
16010
16056
  this.docKeyManager = configuration.docKeyManager;
16011
16057
  this.keystore = configuration.keystore;
16012
16058
  this.e2eClient = configuration.client;
@@ -16087,10 +16133,27 @@ var E2EAbracadabraProvider = class E2EAbracadabraProvider extends AbracadabraPro
16087
16133
  /** Encrypt local updates before sending over WebSocket. */
16088
16134
  documentUpdateHandler(update, origin) {
16089
16135
  if (origin === this) return;
16136
+ if (!this.isAuthenticated) {
16137
+ this._pendingPreAuthUpdates.push(update);
16138
+ return;
16139
+ }
16090
16140
  this._encryptAndSend(update).catch((e) => {
16091
16141
  console.error("[E2EAbracadabraProvider] failed to encrypt update:", e);
16092
16142
  });
16093
16143
  }
16144
+ /**
16145
+ * Flush updates buffered while unauthenticated once the server confirms the
16146
+ * doc is subscribed, then resume normal live sends.
16147
+ */
16148
+ authenticatedHandler(scope) {
16149
+ super.authenticatedHandler(scope);
16150
+ if (this._pendingPreAuthUpdates.length === 0) return;
16151
+ const pending = this._pendingPreAuthUpdates;
16152
+ this._pendingPreAuthUpdates = [];
16153
+ for (const update of pending) this._encryptAndSend(update).catch((e) => {
16154
+ console.error("[E2EAbracadabraProvider] failed to flush pre-auth update:", e);
16155
+ });
16156
+ }
16094
16157
  async _encryptAndSend(update) {
16095
16158
  const key = await this.ensureDocKey();
16096
16159
  if (!key) {