@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.
@@ -2423,11 +2423,14 @@ var AwarenessError = class extends Error {
2423
2423
  this.code = 1001;
2424
2424
  }
2425
2425
  };
2426
- var AbracadabraBaseProvider = class extends EventEmitter {
2426
+ var AbracadabraBaseProvider = class AbracadabraBaseProvider extends EventEmitter {
2427
2427
  /** Current WebSocket connection status. */
2428
2428
  get connectionStatus() {
2429
2429
  return this.configuration.websocketProvider.status;
2430
2430
  }
2431
+ static {
2432
+ this.MAX_RESUBSCRIBE_ATTEMPTS = 6;
2433
+ }
2431
2434
  get rpc() {
2432
2435
  if (!this._rpc) this._rpc = new RpcClient(this);
2433
2436
  return this._rpc;
@@ -2479,6 +2482,8 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2479
2482
  this.authorizedScope = void 0;
2480
2483
  this.manageSocket = false;
2481
2484
  this._isAttached = false;
2485
+ this._resubscribeAttempts = 0;
2486
+ this._resubscribeTimer = null;
2482
2487
  this.intervals = { forceSync: null };
2483
2488
  this.boundDocumentUpdateHandler = this.documentUpdateHandler.bind(this);
2484
2489
  this.boundAwarenessUpdateHandler = this.awarenessUpdateHandler.bind(this);
@@ -2591,6 +2596,7 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2591
2596
  }
2592
2597
  documentUpdateHandler(update, origin) {
2593
2598
  if (origin === this) return;
2599
+ if (!this.isAuthenticated) return;
2594
2600
  this.incrementUnsyncedChanges();
2595
2601
  this.send(UpdateMessage, {
2596
2602
  update,
@@ -2598,6 +2604,7 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2598
2604
  });
2599
2605
  }
2600
2606
  awarenessUpdateHandler({ added, updated, removed }, origin) {
2607
+ if (!this.isAuthenticated) return;
2601
2608
  const changedClients = added.concat(updated).concat(removed);
2602
2609
  this.send(AwarenessMessage, {
2603
2610
  awareness: this.awareness,
@@ -2721,6 +2728,7 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2721
2728
  this.isAuthenticated = false;
2722
2729
  this.synced = false;
2723
2730
  this._hasEverAuthenticated = false;
2731
+ this.clearResubscribe();
2724
2732
  if (this.awareness) removeAwarenessStates(this.awareness, Array.from(this.awareness.getStates().keys()).filter((client) => client !== this.document.clientID), this);
2725
2733
  }
2726
2734
  destroy() {
@@ -2755,6 +2763,7 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2755
2763
  this.configuration.websocketProvider.off("rateLimited", this.forwardRateLimited);
2756
2764
  this.configuration.websocketProvider.detach(this);
2757
2765
  this._isAttached = false;
2766
+ this.clearResubscribe();
2758
2767
  }
2759
2768
  attach() {
2760
2769
  if (this._isAttached) return;
@@ -2780,11 +2789,47 @@ var AbracadabraBaseProvider = class extends EventEmitter {
2780
2789
  if (this.manageSocket && !this._hasEverAuthenticated) try {
2781
2790
  this.configuration.websocketProvider.disconnect();
2782
2791
  } catch {}
2792
+ if (!this.manageSocket && !this.isSynced && this._isAttached && this._resubscribeAttempts < AbracadabraBaseProvider.MAX_RESUBSCRIBE_ATTEMPTS && AbracadabraBaseProvider.isTransientSubscribeDenial(reason)) this.scheduleResubscribe();
2793
+ }
2794
+ /**
2795
+ * Whether a permission-denied reason reflects a *transient* server condition
2796
+ * a retry can clear (a freshly-created doc not yet visible on this replica, or
2797
+ * a dropped subscribe-ordering) rather than a permanent authorization failure.
2798
+ */
2799
+ static isTransientSubscribeDenial(reason) {
2800
+ const r = reason.toLowerCase();
2801
+ return r.includes("not found") || r.includes("not subscribed");
2802
+ }
2803
+ /**
2804
+ * Schedule one backed-off replay of the initial subscribe handshake. A single
2805
+ * timer is in flight at a time; each denial that slips past the guard above
2806
+ * arms the next attempt until {@link MAX_RESUBSCRIBE_ATTEMPTS} is reached.
2807
+ */
2808
+ scheduleResubscribe() {
2809
+ if (this._resubscribeTimer) return;
2810
+ const attempt = this._resubscribeAttempts++;
2811
+ const delay = Math.min(4e3, 250 * 2 ** attempt);
2812
+ this._resubscribeTimer = setTimeout(() => {
2813
+ this._resubscribeTimer = null;
2814
+ if (!this._isAttached || this.isSynced || this.connectionStatus !== WebSocketStatus.Connected) return;
2815
+ this.sendToken().then(() => {
2816
+ if (this._isAttached && !this.isAuthenticated) this.startSync();
2817
+ }).catch(() => {});
2818
+ }, delay);
2819
+ }
2820
+ /** Cancel any pending resubscribe and reset the attempt budget. */
2821
+ clearResubscribe() {
2822
+ if (this._resubscribeTimer) {
2823
+ clearTimeout(this._resubscribeTimer);
2824
+ this._resubscribeTimer = null;
2825
+ }
2826
+ this._resubscribeAttempts = 0;
2783
2827
  }
2784
2828
  authenticatedHandler(scope) {
2785
2829
  this.isAuthenticated = true;
2786
2830
  this._hasEverAuthenticated = true;
2787
2831
  this.authorizedScope = scope;
2832
+ this.clearResubscribe();
2788
2833
  this.emit("authenticated", { scope });
2789
2834
  }
2790
2835
  setAwarenessField(key, value) {
@@ -15968,6 +16013,7 @@ var E2EAbracadabraProvider = class E2EAbracadabraProvider extends AbracadabraPro
15968
16013
  this.compactionInFlightTimeout = null;
15969
16014
  this.compactionDebounceTimer = null;
15970
16015
  this.destroyed = false;
16016
+ this._pendingPreAuthUpdates = [];
15971
16017
  this.docKeyManager = configuration.docKeyManager;
15972
16018
  this.keystore = configuration.keystore;
15973
16019
  this.e2eClient = configuration.client;
@@ -16048,10 +16094,27 @@ var E2EAbracadabraProvider = class E2EAbracadabraProvider extends AbracadabraPro
16048
16094
  /** Encrypt local updates before sending over WebSocket. */
16049
16095
  documentUpdateHandler(update, origin) {
16050
16096
  if (origin === this) return;
16097
+ if (!this.isAuthenticated) {
16098
+ this._pendingPreAuthUpdates.push(update);
16099
+ return;
16100
+ }
16051
16101
  this._encryptAndSend(update).catch((e) => {
16052
16102
  console.error("[E2EAbracadabraProvider] failed to encrypt update:", e);
16053
16103
  });
16054
16104
  }
16105
+ /**
16106
+ * Flush updates buffered while unauthenticated once the server confirms the
16107
+ * doc is subscribed, then resume normal live sends.
16108
+ */
16109
+ authenticatedHandler(scope) {
16110
+ super.authenticatedHandler(scope);
16111
+ if (this._pendingPreAuthUpdates.length === 0) return;
16112
+ const pending = this._pendingPreAuthUpdates;
16113
+ this._pendingPreAuthUpdates = [];
16114
+ for (const update of pending) this._encryptAndSend(update).catch((e) => {
16115
+ console.error("[E2EAbracadabraProvider] failed to flush pre-auth update:", e);
16116
+ });
16117
+ }
16055
16118
  async _encryptAndSend(update) {
16056
16119
  const key = await this.ensureDocKey();
16057
16120
  if (!key) {