starknet 10.6.5 → 10.6.7

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.js CHANGED
@@ -7578,6 +7578,8 @@ var WebSocketChannel = class {
7578
7578
  // resetting the reconnection attempt counter.
7579
7579
  reconnectStabilityTimeoutId = null;
7580
7580
  requestQueue = [];
7581
+ /** Abort handles for the requests currently on the wire, one per pending `sendReceive`. */
7582
+ inFlight = /* @__PURE__ */ new Set();
7581
7583
  events = new EventEmitter();
7582
7584
  openListener = (ev) => {
7583
7585
  this.scheduleReconnectAttemptsReset();
@@ -7661,12 +7663,19 @@ var WebSocketChannel = class {
7661
7663
  });
7662
7664
  }
7663
7665
  const sendId = this.send(method, params);
7666
+ const socket = this.websocket;
7664
7667
  return new Promise((resolve, reject) => {
7665
- let timeoutId;
7666
- if (!this.websocket || this.websocket.readyState !== ws_default.OPEN) {
7668
+ if (socket.readyState !== ws_default.OPEN) {
7667
7669
  reject(new WebSocketNotConnectedError("WebSocket not available or not connected."));
7668
7670
  return;
7669
7671
  }
7672
+ let timeoutId;
7673
+ const settle = () => {
7674
+ clearTimeout(timeoutId);
7675
+ socket.removeEventListener("message", messageHandler);
7676
+ socket.removeEventListener("error", errorHandler);
7677
+ this.inFlight.delete(abort);
7678
+ };
7670
7679
  const messageHandler = (event) => {
7671
7680
  if (!isString(event.data)) {
7672
7681
  logger.warn("WebSocket received non-string message data:", event.data);
@@ -7681,34 +7690,35 @@ var WebSocketChannel = class {
7681
7690
  );
7682
7691
  return;
7683
7692
  }
7684
- if (message.id === sendId) {
7685
- clearTimeout(timeoutId);
7686
- this.websocket.removeEventListener("message", messageHandler);
7687
- this.websocket.removeEventListener("error", errorHandler);
7688
- if ("result" in message) {
7689
- resolve(message.result);
7690
- } else {
7691
- reject(
7692
- new Error(`Error on ${method} (id: ${sendId}): ${JSON.stringify(message.error)}`)
7693
- );
7694
- }
7693
+ if (message.id !== sendId) return;
7694
+ settle();
7695
+ if ("result" in message) {
7696
+ resolve(message.result);
7697
+ } else {
7698
+ reject(new Error(`Error on ${method} (id: ${sendId}): ${JSON.stringify(message.error)}`));
7695
7699
  }
7696
7700
  };
7697
7701
  const errorHandler = (event) => {
7698
- clearTimeout(timeoutId);
7699
- this.websocket.removeEventListener("message", messageHandler);
7700
- this.websocket.removeEventListener("error", errorHandler);
7702
+ settle();
7701
7703
  reject(
7702
7704
  new Error(
7703
7705
  `WebSocket error during ${method} (id: ${sendId}): ${event.type || "Unknown error"}`
7704
7706
  )
7705
7707
  );
7706
7708
  };
7707
- this.websocket.addEventListener("message", messageHandler);
7708
- this.websocket.addEventListener("error", errorHandler);
7709
+ const abort = (reason) => {
7710
+ settle();
7711
+ reject(
7712
+ new WebSocketNotConnectedError(
7713
+ `Request ${method} (id: ${sendId}) went unanswered: ${reason}`
7714
+ )
7715
+ );
7716
+ };
7717
+ socket.addEventListener("message", messageHandler);
7718
+ socket.addEventListener("error", errorHandler);
7719
+ this.inFlight.add(abort);
7709
7720
  timeoutId = setTimeout(() => {
7710
- this.websocket.removeEventListener("message", messageHandler);
7711
- this.websocket.removeEventListener("error", errorHandler);
7721
+ settle();
7712
7722
  reject(
7713
7723
  new TimeoutError(
7714
7724
  `Request ${method} (id: ${sendId}) timed out after ${this.requestTimeout}ms`
@@ -7763,6 +7773,9 @@ var WebSocketChannel = class {
7763
7773
  this.reconnectStabilityTimeoutId = null;
7764
7774
  }
7765
7775
  this.userInitiatedClose = true;
7776
+ this.isReconnecting = false;
7777
+ this._rejectRequestQueue("the connection was closed by the user");
7778
+ this._rejectInFlight("the connection was closed by the user");
7766
7779
  this.websocket.close(code, reason);
7767
7780
  }
7768
7781
  /**
@@ -7836,6 +7849,39 @@ var WebSocketChannel = class {
7836
7849
  this.sendReceive(method, params).then(resolve).catch(reject);
7837
7850
  });
7838
7851
  }
7852
+ /**
7853
+ * Reject every request still waiting in the queue.
7854
+ *
7855
+ * A queued request carries no timeout of its own: the `requestTimeout` timer is only
7856
+ * armed once the request is actually put on the wire. So once the channel reaches a state
7857
+ * where the queue can never be flushed — reconnection gave up, or the user closed the
7858
+ * connection — the queued promises would stay pending forever, and no caller-side timeout
7859
+ * could rescue them. Settling them here is the only way out.
7860
+ */
7861
+ _rejectRequestQueue(reason) {
7862
+ if (this.requestQueue.length === 0) return;
7863
+ const pending = this.requestQueue;
7864
+ this.requestQueue = [];
7865
+ logger.info(`WebSocket: Rejecting ${pending.length} queued request(s). Reason: ${reason}.`);
7866
+ pending.forEach(({ method, reject }) => {
7867
+ reject(new WebSocketNotConnectedError(`Request ${method} was never sent: ${reason}`));
7868
+ });
7869
+ }
7870
+ /**
7871
+ * Settle every request already on the wire.
7872
+ *
7873
+ * The counterpart of `_rejectRequestQueue`, for requests past the queue. Their only other
7874
+ * exit is the `requestTimeout` timer, so without this the caller waits the whole timeout —
7875
+ * 60s by default — for a reply that can no longer arrive, and that pending timer keeps the
7876
+ * Node event loop alive for just as long.
7877
+ */
7878
+ _rejectInFlight(reason) {
7879
+ if (this.inFlight.size === 0) return;
7880
+ const pending = Array.from(this.inFlight);
7881
+ this.inFlight.clear();
7882
+ logger.info(`WebSocket: Rejecting ${pending.length} in-flight request(s). Reason: ${reason}.`);
7883
+ pending.forEach((abort) => abort(reason));
7884
+ }
7839
7885
  async _restoreSubscriptions() {
7840
7886
  const oldSubscriptions = Array.from(this.activeSubscriptions.values());
7841
7887
  this.activeSubscriptions.clear();
@@ -7879,6 +7925,9 @@ var WebSocketChannel = class {
7879
7925
  if (this.reconnectAttempts >= this.reconnectOptions.retries) {
7880
7926
  logger.error("WebSocket: Maximum reconnection retries reached. Giving up.");
7881
7927
  this.isReconnecting = false;
7928
+ this._rejectRequestQueue(
7929
+ `reconnection gave up after ${this.reconnectOptions.retries} attempts`
7930
+ );
7882
7931
  return;
7883
7932
  }
7884
7933
  this.reconnectAttempts += 1;
@@ -7886,18 +7935,24 @@ var WebSocketChannel = class {
7886
7935
  `WebSocket: Connection lost. Attempting to reconnect... (${this.reconnectAttempts}/${this.reconnectOptions.retries})`
7887
7936
  );
7888
7937
  this.reconnect();
7938
+ let attemptSettled = false;
7939
+ const scheduleRetry = () => {
7940
+ if (attemptSettled || !this.isReconnecting) return;
7941
+ attemptSettled = true;
7942
+ const delay = this.reconnectOptions.exponential ? this.reconnectOptions.delay * 2 ** (this.reconnectAttempts - 1) : this.reconnectOptions.delay;
7943
+ logger.info(`WebSocket: Reconnect attempt failed. Retrying in ${delay}ms.`);
7944
+ this.reconnectTimeoutId = setTimeout(tryReconnect, delay);
7945
+ };
7889
7946
  this.websocket.onopen = async () => {
7890
7947
  logger.info("WebSocket: Reconnection successful.");
7948
+ attemptSettled = true;
7891
7949
  this.isReconnecting = false;
7892
7950
  await this._restoreSubscriptions();
7893
7951
  this._processRequestQueue();
7894
7952
  this.events.emit("open", new Event("open"));
7895
7953
  };
7896
- this.websocket.onerror = () => {
7897
- const delay = this.reconnectOptions.exponential ? this.reconnectOptions.delay * 2 ** (this.reconnectAttempts - 1) : this.reconnectOptions.delay;
7898
- logger.info(`WebSocket: Reconnect attempt failed. Retrying in ${delay}ms.`);
7899
- this.reconnectTimeoutId = setTimeout(tryReconnect, delay);
7900
- };
7954
+ this.websocket.onerror = scheduleRetry;
7955
+ this.websocket.addEventListener("close", scheduleRetry);
7901
7956
  };
7902
7957
  tryReconnect();
7903
7958
  }
@@ -7906,6 +7961,7 @@ var WebSocketChannel = class {
7906
7961
  this.websocket.removeEventListener("close", this.closeListener);
7907
7962
  this.websocket.removeEventListener("message", this.messageListener);
7908
7963
  this.websocket.removeEventListener("error", this.errorListener);
7964
+ this._rejectInFlight("the connection was closed");
7909
7965
  this.events.emit("close", ev);
7910
7966
  if (!this.userInitiatedClose) {
7911
7967
  this._startReconnect();