@abraca/dabra 2.35.1 → 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.
package/dist/index.d.ts CHANGED
@@ -2707,6 +2707,23 @@ declare class AbracadabraBaseProvider extends EventEmitter {
2707
2707
  authorizedScope: AuthorizedScope | undefined;
2708
2708
  manageSocket: boolean;
2709
2709
  private _isAttached;
2710
+ /**
2711
+ * Bounded retry of a multiplexed child's INITIAL subscribe handshake after a
2712
+ * *transient* server denial. On a clustered server a just-created doc's row
2713
+ * (or its permission cascade) can be briefly invisible on the replica this
2714
+ * socket landed on; a dropped "send AUTH for this document first" ordering
2715
+ * yields the same denial. Before this, a child gave up permanently on the
2716
+ * first denial and was left with a blank body until a full reload re-ran the
2717
+ * handshake — exactly the "open a freshly-created doc, see only the title,
2718
+ * blinking dot, never syncs until reload" report. We instead replay the
2719
+ * subscribe a few times with backoff; the instant the server catches up the
2720
+ * child authenticates and syncs, no reconnect. Only ever fires for the
2721
+ * INITIAL subscribe (never a mid-session write denial) and is bounded, so a
2722
+ * genuinely forbidden/absent doc still settles into give-up, just later.
2723
+ */
2724
+ private static readonly MAX_RESUBSCRIBE_ATTEMPTS;
2725
+ private _resubscribeAttempts;
2726
+ private _resubscribeTimer;
2710
2727
  intervals: {
2711
2728
  forceSync: ReturnType<typeof setInterval> | null;
2712
2729
  };
@@ -2806,6 +2823,20 @@ declare class AbracadabraBaseProvider extends EventEmitter {
2806
2823
  detach(): void;
2807
2824
  attach(): void;
2808
2825
  permissionDeniedHandler(reason: string): void;
2826
+ /**
2827
+ * Whether a permission-denied reason reflects a *transient* server condition
2828
+ * a retry can clear (a freshly-created doc not yet visible on this replica, or
2829
+ * a dropped subscribe-ordering) rather than a permanent authorization failure.
2830
+ */
2831
+ private static isTransientSubscribeDenial;
2832
+ /**
2833
+ * Schedule one backed-off replay of the initial subscribe handshake. A single
2834
+ * timer is in flight at a time; each denial that slips past the guard above
2835
+ * arms the next attempt until {@link MAX_RESUBSCRIBE_ATTEMPTS} is reached.
2836
+ */
2837
+ private scheduleResubscribe;
2838
+ /** Cancel any pending resubscribe and reset the attempt budget. */
2839
+ private clearResubscribe;
2809
2840
  authenticatedHandler(scope: string): void;
2810
2841
  setAwarenessField(key: string, value: any): void;
2811
2842
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abraca/dabra",
3
- "version": "2.35.1",
3
+ "version": "2.36.0",
4
4
  "description": "abracadabra provider",
5
5
  "keywords": [
6
6
  "abracadabra",
@@ -41,7 +41,7 @@
41
41
  "yjs": "^13.6.8"
42
42
  },
43
43
  "devDependencies": {
44
- "@abraca/schema": "2.35.1"
44
+ "@abraca/schema": "2.36.0"
45
45
  },
46
46
  "scripts": {
47
47
  "test": "node --no-warnings --conditions=source --experimental-transform-types --test 'tests/*.test.ts'"
@@ -35,8 +35,10 @@ import type {
35
35
  onStatusParameters,
36
36
  onSyncedParameters,
37
37
  onUnsyncedChangesParameters,
38
- WebSocketStatus,
39
38
  } from "./types.ts";
39
+ // Value import (not `import type`): `WebSocketStatus` is a runtime string enum
40
+ // read in scheduleResubscribe's connection check, not just a type annotation.
41
+ import { WebSocketStatus } from "./types.ts";
40
42
 
41
43
  export type AbracadabraBaseProviderConfiguration = Required<
42
44
  Pick<CompleteAbracadabraBaseProviderConfiguration, "name">
@@ -178,6 +180,24 @@ export class AbracadabraBaseProvider extends EventEmitter {
178
180
 
179
181
  private _isAttached = false;
180
182
 
183
+ /**
184
+ * Bounded retry of a multiplexed child's INITIAL subscribe handshake after a
185
+ * *transient* server denial. On a clustered server a just-created doc's row
186
+ * (or its permission cascade) can be briefly invisible on the replica this
187
+ * socket landed on; a dropped "send AUTH for this document first" ordering
188
+ * yields the same denial. Before this, a child gave up permanently on the
189
+ * first denial and was left with a blank body until a full reload re-ran the
190
+ * handshake — exactly the "open a freshly-created doc, see only the title,
191
+ * blinking dot, never syncs until reload" report. We instead replay the
192
+ * subscribe a few times with backoff; the instant the server catches up the
193
+ * child authenticates and syncs, no reconnect. Only ever fires for the
194
+ * INITIAL subscribe (never a mid-session write denial) and is bounded, so a
195
+ * genuinely forbidden/absent doc still settles into give-up, just later.
196
+ */
197
+ private static readonly MAX_RESUBSCRIBE_ATTEMPTS = 6;
198
+ private _resubscribeAttempts = 0;
199
+ private _resubscribeTimer: ReturnType<typeof setTimeout> | null = null;
200
+
181
201
  intervals: { forceSync: ReturnType<typeof setInterval> | null } = {
182
202
  forceSync: null,
183
203
  };
@@ -640,6 +660,9 @@ export class AbracadabraBaseProvider extends EventEmitter {
640
660
  // permissionDeniedHandler can correctly identify a fresh-connection
641
661
  // denial as a config error.
642
662
  this._hasEverAuthenticated = false;
663
+ // The socket's own reconnect re-runs onOpen → subscribe for every doc, so
664
+ // any in-flight initial-subscribe retry from this lifetime is moot.
665
+ this.clearResubscribe();
643
666
 
644
667
  // update awareness (all users except local left)
645
668
  if (this.awareness) {
@@ -714,6 +737,7 @@ export class AbracadabraBaseProvider extends EventEmitter {
714
737
  this.configuration.websocketProvider.detach(this);
715
738
 
716
739
  this._isAttached = false;
740
+ this.clearResubscribe();
717
741
  }
718
742
 
719
743
  attach() {
@@ -786,12 +810,92 @@ export class AbracadabraBaseProvider extends EventEmitter {
786
810
  }
787
811
  catch { /* WS may already be torn down — best effort */ }
788
812
  }
813
+
814
+ // Transient-denial recovery (see _resubscribeAttempts doc). Fires only for
815
+ // a multiplexed child (shared socket; the root is handled above) that has
816
+ // NOT yet completed its initial sync, and only for denials whose reason is
817
+ // transient — "document not found" (a just-created doc whose row/permission
818
+ // is briefly invisible on the replica this socket hit) or "not subscribed"
819
+ // (a dropped AUTH-ordering). We deliberately do NOT key off
820
+ // `_hasEverAuthenticated`: the server sends AUTHENTICATED *before* it runs
821
+ // the initial sync, so a not-found denial lands after that flag is already
822
+ // set. `!isSynced` is the true "never got my content" signal and also
823
+ // excludes mid-session write denials (those docs are already synced).
824
+ // Permanent denials ("permission denied"/"write permission denied") are
825
+ // NOT matched, so a genuinely forbidden doc still gives up immediately.
826
+ if (
827
+ !this.manageSocket &&
828
+ !this.isSynced &&
829
+ this._isAttached &&
830
+ this._resubscribeAttempts < AbracadabraBaseProvider.MAX_RESUBSCRIBE_ATTEMPTS &&
831
+ AbracadabraBaseProvider.isTransientSubscribeDenial(reason)
832
+ ) {
833
+ this.scheduleResubscribe();
834
+ }
835
+ }
836
+
837
+ /**
838
+ * Whether a permission-denied reason reflects a *transient* server condition
839
+ * a retry can clear (a freshly-created doc not yet visible on this replica, or
840
+ * a dropped subscribe-ordering) rather than a permanent authorization failure.
841
+ */
842
+ private static isTransientSubscribeDenial(reason: string): boolean {
843
+ const r = reason.toLowerCase();
844
+ return r.includes("not found") || r.includes("not subscribed");
845
+ }
846
+
847
+ /**
848
+ * Schedule one backed-off replay of the initial subscribe handshake. A single
849
+ * timer is in flight at a time; each denial that slips past the guard above
850
+ * arms the next attempt until {@link MAX_RESUBSCRIBE_ATTEMPTS} is reached.
851
+ */
852
+ private scheduleResubscribe() {
853
+ if (this._resubscribeTimer) return;
854
+ const attempt = this._resubscribeAttempts++;
855
+ // 250, 500, 1000, 2000, 4000, 4000 ms — recovers within ~750ms for a
856
+ // typical sub-second replica lag, ~11s total before final give-up.
857
+ const delay = Math.min(4000, 250 * 2 ** attempt);
858
+ this._resubscribeTimer = setTimeout(() => {
859
+ this._resubscribeTimer = null;
860
+ // Bail if anything changed under us: detached, already recovered, or the
861
+ // socket dropped (its reconnect re-runs onOpen for every doc anyway).
862
+ // `isSynced` — not the auth flags — is the success signal: the server
863
+ // sends AUTHENTICATED *before* the initial-sync attempt, so a not-found
864
+ // denial leaves isAuthenticated/_hasEverAuthenticated set yet unsynced;
865
+ // bailing on those would (and did) skip the very retry we scheduled.
866
+ if (
867
+ !this._isAttached ||
868
+ this.isSynced ||
869
+ this.connectionStatus !== WebSocketStatus.Connected
870
+ ) {
871
+ return;
872
+ }
873
+ // Replay onOpen's subscribe: re-send AUTH (server re-runs subscribe_doc,
874
+ // now that the doc/permission may be visible) then SyncStep1.
875
+ void this.sendToken()
876
+ .then(() => {
877
+ if (this._isAttached && !this.isAuthenticated) this.startSync();
878
+ })
879
+ .catch(() => { /* a fresh denial re-enters permissionDeniedHandler */ });
880
+ }, delay);
881
+ }
882
+
883
+ /** Cancel any pending resubscribe and reset the attempt budget. */
884
+ private clearResubscribe() {
885
+ if (this._resubscribeTimer) {
886
+ clearTimeout(this._resubscribeTimer);
887
+ this._resubscribeTimer = null;
888
+ }
889
+ this._resubscribeAttempts = 0;
789
890
  }
790
891
 
791
892
  authenticatedHandler(scope: string) {
792
893
  this.isAuthenticated = true;
793
894
  this._hasEverAuthenticated = true;
794
895
  this.authorizedScope = scope as AuthorizedScope;
896
+ // Initial subscribe succeeded (possibly via a resubscribe retry) — stop
897
+ // retrying and restore the budget for a future socket lifetime.
898
+ this.clearResubscribe();
795
899
 
796
900
  this.emit("authenticated", { scope });
797
901
  }