nostr-tools 2.23.9 → 2.23.12

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/lib/cjs/nip46.js CHANGED
@@ -372,12 +372,14 @@ var AbstractRelay = class {
372
372
  openSubs = /* @__PURE__ */ new Map();
373
373
  enablePing;
374
374
  enableReconnect;
375
+ idleTimeout = 0;
375
376
  idleSince = Date.now();
376
377
  ongoingOperations = 0;
377
378
  reconnectTimeoutHandle;
378
379
  pingIntervalHandle;
379
380
  reconnectAttempts = 0;
380
381
  skipReconnection = false;
382
+ idleTimeoutHandle;
381
383
  connectionPromise;
382
384
  openCountRequests = /* @__PURE__ */ new Map();
383
385
  openEventPublishes = /* @__PURE__ */ new Map();
@@ -393,6 +395,8 @@ var AbstractRelay = class {
393
395
  this._WebSocket = opts.websocketImplementation || WebSocket;
394
396
  this.enablePing = opts.enablePing;
395
397
  this.enableReconnect = opts.enableReconnect || false;
398
+ if (opts.idleTimeout)
399
+ this.idleTimeout = opts.idleTimeout;
396
400
  }
397
401
  static async connect(url, opts) {
398
402
  const relay = new AbstractRelay(url, opts);
@@ -416,6 +420,22 @@ var AbstractRelay = class {
416
420
  get connected() {
417
421
  return this._connected;
418
422
  }
423
+ clearIdleTimeout() {
424
+ if (this.idleTimeoutHandle) {
425
+ clearTimeout(this.idleTimeoutHandle);
426
+ this.idleTimeoutHandle = void 0;
427
+ }
428
+ }
429
+ scheduleIdleClose() {
430
+ this.clearIdleTimeout();
431
+ if (this.idleTimeout > 0) {
432
+ this.idleTimeoutHandle = setTimeout(() => {
433
+ if (this.ongoingOperations === 0 && this.idleSince) {
434
+ this.close();
435
+ }
436
+ }, this.idleTimeout);
437
+ }
438
+ }
419
439
  async reconnect() {
420
440
  const backoff = this.resubscribeBackoff[Math.min(this.reconnectAttempts, this.resubscribeBackoff.length - 1)];
421
441
  this.reconnectAttempts++;
@@ -434,6 +454,7 @@ var AbstractRelay = class {
434
454
  this._connected = false;
435
455
  this.connectionPromise = void 0;
436
456
  this.idleSince = void 0;
457
+ this.clearIdleTimeout();
437
458
  if (this.enableReconnect && !this.skipReconnection) {
438
459
  this.reconnect();
439
460
  } else {
@@ -591,6 +612,7 @@ var AbstractRelay = class {
591
612
  }
592
613
  async publish(event) {
593
614
  this.idleSince = void 0;
615
+ this.clearIdleTimeout();
594
616
  this.ongoingOperations++;
595
617
  const ret = new Promise((resolve, reject) => {
596
618
  const timeout = setTimeout(() => {
@@ -612,8 +634,10 @@ var AbstractRelay = class {
612
634
  }
613
635
  }
614
636
  this.ongoingOperations--;
615
- if (this.ongoingOperations === 0)
637
+ if (this.ongoingOperations === 0) {
616
638
  this.idleSince = Date.now();
639
+ this.scheduleIdleClose();
640
+ }
617
641
  return ret;
618
642
  }
619
643
  async count(filters, params) {
@@ -639,6 +663,7 @@ var AbstractRelay = class {
639
663
  subscribe(filters, params) {
640
664
  if (params.label !== "<forced-ping>") {
641
665
  this.idleSince = void 0;
666
+ this.clearIdleTimeout();
642
667
  this.ongoingOperations++;
643
668
  }
644
669
  const sub = this.prepareSubscription(filters, params);
@@ -668,6 +693,7 @@ var AbstractRelay = class {
668
693
  this.closeAllSubscriptions("relay connection closed by us");
669
694
  this._connected = false;
670
695
  this.idleSince = void 0;
696
+ this.clearIdleTimeout();
671
697
  this.onclose?.();
672
698
  if (this.ws?.readyState === this._WebSocket.OPEN) {
673
699
  this.ws?.close();
@@ -845,8 +871,10 @@ var Subscription = class {
845
871
  }
846
872
  this.relay.openSubs.delete(this.id);
847
873
  this.relay.ongoingOperations--;
848
- if (this.relay.ongoingOperations === 0)
874
+ if (this.relay.ongoingOperations === 0) {
849
875
  this.relay.idleSince = Date.now();
876
+ this.relay.scheduleIdleClose();
877
+ }
850
878
  this.onclose?.(reason);
851
879
  }
852
880
  };
@@ -922,6 +950,7 @@ var AbstractSimplePool = class {
922
950
  verifyEvent;
923
951
  enablePing;
924
952
  enableReconnect;
953
+ idleTimeout = 2e4;
925
954
  automaticallyAuth;
926
955
  trustedRelayURLs = /* @__PURE__ */ new Set();
927
956
  onRelayConnectionFailure;
@@ -934,6 +963,8 @@ var AbstractSimplePool = class {
934
963
  this._WebSocket = opts.websocketImplementation;
935
964
  this.enablePing = opts.enablePing;
936
965
  this.enableReconnect = opts.enableReconnect || false;
966
+ if (opts.idleTimeout)
967
+ this.idleTimeout = opts.idleTimeout;
937
968
  this.automaticallyAuth = opts.automaticallyAuth;
938
969
  this.onRelayConnectionFailure = opts.onRelayConnectionFailure;
939
970
  this.onRelayConnectionSuccess = opts.onRelayConnectionSuccess;
@@ -948,7 +979,8 @@ var AbstractSimplePool = class {
948
979
  verifyEvent: this.trustedRelayURLs.has(url) ? alwaysTrue : this.verifyEvent,
949
980
  websocketImplementation: this._WebSocket,
950
981
  enablePing: this.enablePing,
951
- enableReconnect: this.enableReconnect
982
+ enableReconnect: this.enableReconnect,
983
+ idleTimeout: this.idleTimeout
952
984
  });
953
985
  relay.onclose = () => {
954
986
  this.relays.delete(url);