@abraca/dabra 0.1.7 → 0.2.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
@@ -768,6 +768,7 @@ interface CompleteHocuspocusProviderConfiguration {
768
768
  forceSyncInterval: false | number;
769
769
  onAuthenticated: (data: onAuthenticatedParameters) => void;
770
770
  onAuthenticationFailed: (data: onAuthenticationFailedParameters) => void;
771
+ onRateLimited: () => void;
771
772
  onOpen: (data: onOpenParameters) => void;
772
773
  onConnect: () => void;
773
774
  onStatus: (data: onStatusParameters) => void;
@@ -809,6 +810,7 @@ declare class HocuspocusProvider extends EventEmitter {
809
810
  forwardClose: (e: onCloseParameters) => this;
810
811
  forwardDisconnect: (e: onDisconnectParameters) => this;
811
812
  forwardDestroy: () => this;
813
+ forwardRateLimited: () => this;
812
814
  setConfiguration(configuration?: Partial<HocuspocusProviderConfiguration>): void;
813
815
  get document(): Y.Doc;
814
816
  get isAttached(): boolean;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@abraca/dabra",
3
- "version": "0.1.7",
3
+ "version": "0.2.0",
4
4
  "description": "abracadabra provider",
5
5
  "keywords": [
6
6
  "abracadabra",
@@ -78,6 +78,7 @@ export interface CompleteHocuspocusProviderConfiguration {
78
78
 
79
79
  onAuthenticated: (data: onAuthenticatedParameters) => void;
80
80
  onAuthenticationFailed: (data: onAuthenticationFailedParameters) => void;
81
+ onRateLimited: () => void;
81
82
  onOpen: (data: onOpenParameters) => void;
82
83
  onConnect: () => void;
83
84
  onStatus: (data: onStatusParameters) => void;
@@ -108,6 +109,7 @@ export class HocuspocusProvider extends EventEmitter {
108
109
  forceSyncInterval: false,
109
110
  onAuthenticated: () => null,
110
111
  onAuthenticationFailed: () => null,
112
+ onRateLimited: () => null,
111
113
  onOpen: () => null,
112
114
  onConnect: () => null,
113
115
  onMessage: () => null,
@@ -162,6 +164,7 @@ export class HocuspocusProvider extends EventEmitter {
162
164
 
163
165
  this.on("authenticated", this.configuration.onAuthenticated);
164
166
  this.on("authenticationFailed", this.configuration.onAuthenticationFailed);
167
+ this.on("rateLimited", this.configuration.onRateLimited);
165
168
 
166
169
  this.awareness?.on("update", () => {
167
170
  this.emit("awarenessUpdate", {
@@ -215,6 +218,8 @@ export class HocuspocusProvider extends EventEmitter {
215
218
 
216
219
  forwardDestroy = () => this.emit("destroy");
217
220
 
221
+ forwardRateLimited = () => this.emit("rateLimited");
222
+
218
223
  public setConfiguration(configuration: Partial<HocuspocusProviderConfiguration> = {}): void {
219
224
  if (!configuration.websocketProvider) {
220
225
  this.manageSocket = true;
@@ -492,6 +497,8 @@ export class HocuspocusProvider extends EventEmitter {
492
497
  this.configuration.websocketProvider.off("destroy", this.configuration.onDestroy);
493
498
  this.configuration.websocketProvider.off("destroy", this.forwardDestroy);
494
499
 
500
+ this.configuration.websocketProvider.off("rateLimited", this.forwardRateLimited);
501
+
495
502
  this.configuration.websocketProvider.detach(this);
496
503
 
497
504
  this._isAttached = false;
@@ -518,6 +525,8 @@ export class HocuspocusProvider extends EventEmitter {
518
525
  this.configuration.websocketProvider.on("destroy", this.configuration.onDestroy);
519
526
  this.configuration.websocketProvider.on("destroy", this.forwardDestroy);
520
527
 
528
+ this.configuration.websocketProvider.on("rateLimited", this.forwardRateLimited);
529
+
521
530
  this.configuration.websocketProvider.attach(this);
522
531
 
523
532
  this._isAttached = true;
@@ -500,13 +500,21 @@ export class HocuspocusProviderWebsocket extends EventEmitter {
500
500
  // Let’s update the connection status.
501
501
  this.status = WebSocketStatus.Disconnected;
502
502
  this.emit("status", { status: WebSocketStatus.Disconnected });
503
+
504
+ // Detect server-side rate-limit close (code 4429).
505
+ const isRateLimited = (event as any)?.code === 4429;
503
506
  this.emit("disconnect", { event });
507
+ if (isRateLimited) {
508
+ this.emit("rateLimited");
509
+ }
504
510
 
505
511
  // trigger connect if no retry is running and we want to have a connection
506
512
  if (!this.cancelWebsocketRetry && this.shouldConnect) {
513
+ // Apply a much longer delay for rate-limited closes to let the server window reset.
514
+ const delay = isRateLimited ? 60_000 : this.configuration.delay;
507
515
  setTimeout(() => {
508
516
  this.connect();
509
- }, this.configuration.delay);
517
+ }, delay);
510
518
  }
511
519
  }
512
520