@unicitylabs/sphere-sdk 0.6.10-dev.7 → 0.6.11

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.cjs CHANGED
@@ -594,6 +594,18 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
594
594
  return waitForConnection();
595
595
  }
596
596
  isConnecting = true;
597
+ if (ws) {
598
+ try {
599
+ ws.onopen = null;
600
+ ws.onclose = null;
601
+ ws.onerror = null;
602
+ ws.onmessage = null;
603
+ ws.close();
604
+ } catch {
605
+ }
606
+ ws = null;
607
+ }
608
+ const epoch = ++connectionEpoch;
597
609
  return new Promise((resolve, reject) => {
598
610
  let hasResolved = false;
599
611
  try {
@@ -605,6 +617,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
605
617
  return;
606
618
  }
607
619
  ws.onopen = () => {
620
+ if (epoch !== connectionEpoch) return;
608
621
  isConnected = true;
609
622
  isConnecting = false;
610
623
  reconnectAttempts = 0;
@@ -618,6 +631,7 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
618
631
  connectionCallbacks.length = 0;
619
632
  };
620
633
  ws.onclose = () => {
634
+ if (epoch !== connectionEpoch) return;
621
635
  isConnected = false;
622
636
  isBlockSubscribed = false;
623
637
  stopPingTimer();
@@ -626,16 +640,6 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
626
640
  req.reject(new Error("WebSocket connection closed"));
627
641
  });
628
642
  Object.keys(pending).forEach((key) => delete pending[Number(key)]);
629
- if (intentionalClose) {
630
- intentionalClose = false;
631
- isConnecting = false;
632
- reconnectAttempts = 0;
633
- if (!hasResolved) {
634
- hasResolved = true;
635
- reject(new Error("WebSocket connection closed intentionally"));
636
- }
637
- return;
638
- }
639
643
  if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
640
644
  logger.error("L1", "Max reconnect attempts reached. Giving up.");
641
645
  isConnecting = false;
@@ -657,7 +661,8 @@ function connect(endpoint = DEFAULT_ENDPOINT) {
657
661
  "L1",
658
662
  `WebSocket closed unexpectedly. Reconnecting in ${delay}ms (attempt ${reconnectAttempts}/${MAX_RECONNECT_ATTEMPTS})...`
659
663
  );
660
- setTimeout(() => {
664
+ reconnectTimer = setTimeout(() => {
665
+ reconnectTimer = null;
661
666
  connect(endpoint).then(() => {
662
667
  if (!hasResolved) {
663
668
  hasResolved = true;
@@ -804,8 +809,16 @@ async function getCurrentBlockHeight() {
804
809
  }
805
810
  function disconnect() {
806
811
  stopPingTimer();
812
+ if (reconnectTimer) {
813
+ clearTimeout(reconnectTimer);
814
+ reconnectTimer = null;
815
+ }
816
+ connectionEpoch++;
807
817
  if (ws) {
808
- intentionalClose = true;
818
+ ws.onopen = null;
819
+ ws.onclose = null;
820
+ ws.onerror = null;
821
+ ws.onmessage = null;
809
822
  ws.close();
810
823
  ws = null;
811
824
  }
@@ -813,18 +826,21 @@ function disconnect() {
813
826
  isConnecting = false;
814
827
  reconnectAttempts = 0;
815
828
  isBlockSubscribed = false;
829
+ const disconnectError = new Error("WebSocket disconnected");
816
830
  Object.values(pending).forEach((req) => {
817
831
  if (req.timeoutId) clearTimeout(req.timeoutId);
832
+ req.reject(disconnectError);
818
833
  });
819
834
  Object.keys(pending).forEach((key) => delete pending[Number(key)]);
820
835
  connectionCallbacks.forEach((cb) => {
821
836
  if (cb.timeoutId) clearTimeout(cb.timeoutId);
837
+ cb.reject(disconnectError);
822
838
  });
823
839
  connectionCallbacks.length = 0;
824
840
  blockSubscribers.length = 0;
825
841
  lastBlockHeader = null;
826
842
  }
827
- var DEFAULT_ENDPOINT, ws, isConnected, isConnecting, requestId, intentionalClose, reconnectAttempts, isBlockSubscribed, lastBlockHeader, pingTimer, pending, blockSubscribers, connectionCallbacks, MAX_RECONNECT_ATTEMPTS, BASE_DELAY, MAX_DELAY, RPC_TIMEOUT, CONNECTION_TIMEOUT, PING_INTERVAL;
843
+ var DEFAULT_ENDPOINT, ws, isConnected, isConnecting, requestId, reconnectAttempts, isBlockSubscribed, lastBlockHeader, pingTimer, reconnectTimer, connectionEpoch, pending, blockSubscribers, connectionCallbacks, MAX_RECONNECT_ATTEMPTS, BASE_DELAY, MAX_DELAY, RPC_TIMEOUT, CONNECTION_TIMEOUT, PING_INTERVAL;
828
844
  var init_network = __esm({
829
845
  "l1/network.ts"() {
830
846
  "use strict";
@@ -836,11 +852,12 @@ var init_network = __esm({
836
852
  isConnected = false;
837
853
  isConnecting = false;
838
854
  requestId = 0;
839
- intentionalClose = false;
840
855
  reconnectAttempts = 0;
841
856
  isBlockSubscribed = false;
842
857
  lastBlockHeader = null;
843
858
  pingTimer = null;
859
+ reconnectTimer = null;
860
+ connectionEpoch = 0;
844
861
  pending = {};
845
862
  blockSubscribers = [];
846
863
  connectionCallbacks = [];
@@ -5491,9 +5508,7 @@ var L1PaymentsModule = class {
5491
5508
  }
5492
5509
  }
5493
5510
  destroy() {
5494
- if (isWebSocketConnected()) {
5495
- disconnect();
5496
- }
5511
+ disconnect();
5497
5512
  this._initialized = false;
5498
5513
  this._identity = void 0;
5499
5514
  this._addresses = [];
@@ -5504,9 +5519,7 @@ var L1PaymentsModule = class {
5504
5519
  */
5505
5520
  disable() {
5506
5521
  this._disabled = true;
5507
- if (isWebSocketConnected()) {
5508
- disconnect();
5509
- }
5522
+ disconnect();
5510
5523
  }
5511
5524
  /**
5512
5525
  * Re-enable this module. Connection will be established lazily on next operation.
@@ -15812,7 +15825,7 @@ var MarketModule = class {
15812
15825
  const MAX_ATTEMPTS = 10;
15813
15826
  let ws2 = null;
15814
15827
  let reconnectAttempts2 = 0;
15815
- let reconnectTimer = null;
15828
+ let reconnectTimer2 = null;
15816
15829
  let destroyed = false;
15817
15830
  function connect2() {
15818
15831
  if (destroyed) return;
@@ -15844,14 +15857,14 @@ var MarketModule = class {
15844
15857
  const delay = Math.min(BASE_DELAY2 * Math.pow(2, reconnectAttempts2), MAX_DELAY2);
15845
15858
  reconnectAttempts2++;
15846
15859
  logger.debug("Market", `Feed WebSocket reconnecting in ${delay}ms (attempt ${reconnectAttempts2}/${MAX_ATTEMPTS})`);
15847
- reconnectTimer = setTimeout(connect2, delay);
15860
+ reconnectTimer2 = setTimeout(connect2, delay);
15848
15861
  }
15849
15862
  connect2();
15850
15863
  return () => {
15851
15864
  destroyed = true;
15852
- if (reconnectTimer) {
15853
- clearTimeout(reconnectTimer);
15854
- reconnectTimer = null;
15865
+ if (reconnectTimer2) {
15866
+ clearTimeout(reconnectTimer2);
15867
+ reconnectTimer2 = null;
15855
15868
  }
15856
15869
  ws2?.close();
15857
15870
  ws2 = null;
@@ -18567,7 +18580,7 @@ var Sphere = class _Sphere {
18567
18580
  if (!this._transport.discoverAddresses) {
18568
18581
  throw new SphereError("Transport provider does not support address discovery", "INVALID_CONFIG");
18569
18582
  }
18570
- const includeL1Scan = options.includeL1Scan ?? true;
18583
+ const includeL1Scan = (options.includeL1Scan ?? true) && !!this._l1Config;
18571
18584
  const transportResult = await discoverAddressesImpl(
18572
18585
  (index) => {
18573
18586
  const addrInfo = this._deriveAddressInternal(index, false);