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.mjs CHANGED
@@ -7394,6 +7394,8 @@ var WebSocketChannel = class {
7394
7394
  // resetting the reconnection attempt counter.
7395
7395
  reconnectStabilityTimeoutId = null;
7396
7396
  requestQueue = [];
7397
+ /** Abort handles for the requests currently on the wire, one per pending `sendReceive`. */
7398
+ inFlight = /* @__PURE__ */ new Set();
7397
7399
  events = new EventEmitter();
7398
7400
  openListener = (ev) => {
7399
7401
  this.scheduleReconnectAttemptsReset();
@@ -7477,12 +7479,19 @@ var WebSocketChannel = class {
7477
7479
  });
7478
7480
  }
7479
7481
  const sendId = this.send(method, params);
7482
+ const socket = this.websocket;
7480
7483
  return new Promise((resolve, reject) => {
7481
- let timeoutId;
7482
- if (!this.websocket || this.websocket.readyState !== ws_default.OPEN) {
7484
+ if (socket.readyState !== ws_default.OPEN) {
7483
7485
  reject(new WebSocketNotConnectedError("WebSocket not available or not connected."));
7484
7486
  return;
7485
7487
  }
7488
+ let timeoutId;
7489
+ const settle = () => {
7490
+ clearTimeout(timeoutId);
7491
+ socket.removeEventListener("message", messageHandler);
7492
+ socket.removeEventListener("error", errorHandler);
7493
+ this.inFlight.delete(abort);
7494
+ };
7486
7495
  const messageHandler = (event) => {
7487
7496
  if (!isString(event.data)) {
7488
7497
  logger.warn("WebSocket received non-string message data:", event.data);
@@ -7497,34 +7506,35 @@ var WebSocketChannel = class {
7497
7506
  );
7498
7507
  return;
7499
7508
  }
7500
- if (message.id === sendId) {
7501
- clearTimeout(timeoutId);
7502
- this.websocket.removeEventListener("message", messageHandler);
7503
- this.websocket.removeEventListener("error", errorHandler);
7504
- if ("result" in message) {
7505
- resolve(message.result);
7506
- } else {
7507
- reject(
7508
- new Error(`Error on ${method} (id: ${sendId}): ${JSON.stringify(message.error)}`)
7509
- );
7510
- }
7509
+ if (message.id !== sendId) return;
7510
+ settle();
7511
+ if ("result" in message) {
7512
+ resolve(message.result);
7513
+ } else {
7514
+ reject(new Error(`Error on ${method} (id: ${sendId}): ${JSON.stringify(message.error)}`));
7511
7515
  }
7512
7516
  };
7513
7517
  const errorHandler = (event) => {
7514
- clearTimeout(timeoutId);
7515
- this.websocket.removeEventListener("message", messageHandler);
7516
- this.websocket.removeEventListener("error", errorHandler);
7518
+ settle();
7517
7519
  reject(
7518
7520
  new Error(
7519
7521
  `WebSocket error during ${method} (id: ${sendId}): ${event.type || "Unknown error"}`
7520
7522
  )
7521
7523
  );
7522
7524
  };
7523
- this.websocket.addEventListener("message", messageHandler);
7524
- this.websocket.addEventListener("error", errorHandler);
7525
+ const abort = (reason) => {
7526
+ settle();
7527
+ reject(
7528
+ new WebSocketNotConnectedError(
7529
+ `Request ${method} (id: ${sendId}) went unanswered: ${reason}`
7530
+ )
7531
+ );
7532
+ };
7533
+ socket.addEventListener("message", messageHandler);
7534
+ socket.addEventListener("error", errorHandler);
7535
+ this.inFlight.add(abort);
7525
7536
  timeoutId = setTimeout(() => {
7526
- this.websocket.removeEventListener("message", messageHandler);
7527
- this.websocket.removeEventListener("error", errorHandler);
7537
+ settle();
7528
7538
  reject(
7529
7539
  new TimeoutError(
7530
7540
  `Request ${method} (id: ${sendId}) timed out after ${this.requestTimeout}ms`
@@ -7579,6 +7589,9 @@ var WebSocketChannel = class {
7579
7589
  this.reconnectStabilityTimeoutId = null;
7580
7590
  }
7581
7591
  this.userInitiatedClose = true;
7592
+ this.isReconnecting = false;
7593
+ this._rejectRequestQueue("the connection was closed by the user");
7594
+ this._rejectInFlight("the connection was closed by the user");
7582
7595
  this.websocket.close(code, reason);
7583
7596
  }
7584
7597
  /**
@@ -7652,6 +7665,39 @@ var WebSocketChannel = class {
7652
7665
  this.sendReceive(method, params).then(resolve).catch(reject);
7653
7666
  });
7654
7667
  }
7668
+ /**
7669
+ * Reject every request still waiting in the queue.
7670
+ *
7671
+ * A queued request carries no timeout of its own: the `requestTimeout` timer is only
7672
+ * armed once the request is actually put on the wire. So once the channel reaches a state
7673
+ * where the queue can never be flushed — reconnection gave up, or the user closed the
7674
+ * connection — the queued promises would stay pending forever, and no caller-side timeout
7675
+ * could rescue them. Settling them here is the only way out.
7676
+ */
7677
+ _rejectRequestQueue(reason) {
7678
+ if (this.requestQueue.length === 0) return;
7679
+ const pending = this.requestQueue;
7680
+ this.requestQueue = [];
7681
+ logger.info(`WebSocket: Rejecting ${pending.length} queued request(s). Reason: ${reason}.`);
7682
+ pending.forEach(({ method, reject }) => {
7683
+ reject(new WebSocketNotConnectedError(`Request ${method} was never sent: ${reason}`));
7684
+ });
7685
+ }
7686
+ /**
7687
+ * Settle every request already on the wire.
7688
+ *
7689
+ * The counterpart of `_rejectRequestQueue`, for requests past the queue. Their only other
7690
+ * exit is the `requestTimeout` timer, so without this the caller waits the whole timeout —
7691
+ * 60s by default — for a reply that can no longer arrive, and that pending timer keeps the
7692
+ * Node event loop alive for just as long.
7693
+ */
7694
+ _rejectInFlight(reason) {
7695
+ if (this.inFlight.size === 0) return;
7696
+ const pending = Array.from(this.inFlight);
7697
+ this.inFlight.clear();
7698
+ logger.info(`WebSocket: Rejecting ${pending.length} in-flight request(s). Reason: ${reason}.`);
7699
+ pending.forEach((abort) => abort(reason));
7700
+ }
7655
7701
  async _restoreSubscriptions() {
7656
7702
  const oldSubscriptions = Array.from(this.activeSubscriptions.values());
7657
7703
  this.activeSubscriptions.clear();
@@ -7695,6 +7741,9 @@ var WebSocketChannel = class {
7695
7741
  if (this.reconnectAttempts >= this.reconnectOptions.retries) {
7696
7742
  logger.error("WebSocket: Maximum reconnection retries reached. Giving up.");
7697
7743
  this.isReconnecting = false;
7744
+ this._rejectRequestQueue(
7745
+ `reconnection gave up after ${this.reconnectOptions.retries} attempts`
7746
+ );
7698
7747
  return;
7699
7748
  }
7700
7749
  this.reconnectAttempts += 1;
@@ -7702,18 +7751,24 @@ var WebSocketChannel = class {
7702
7751
  `WebSocket: Connection lost. Attempting to reconnect... (${this.reconnectAttempts}/${this.reconnectOptions.retries})`
7703
7752
  );
7704
7753
  this.reconnect();
7754
+ let attemptSettled = false;
7755
+ const scheduleRetry = () => {
7756
+ if (attemptSettled || !this.isReconnecting) return;
7757
+ attemptSettled = true;
7758
+ const delay = this.reconnectOptions.exponential ? this.reconnectOptions.delay * 2 ** (this.reconnectAttempts - 1) : this.reconnectOptions.delay;
7759
+ logger.info(`WebSocket: Reconnect attempt failed. Retrying in ${delay}ms.`);
7760
+ this.reconnectTimeoutId = setTimeout(tryReconnect, delay);
7761
+ };
7705
7762
  this.websocket.onopen = async () => {
7706
7763
  logger.info("WebSocket: Reconnection successful.");
7764
+ attemptSettled = true;
7707
7765
  this.isReconnecting = false;
7708
7766
  await this._restoreSubscriptions();
7709
7767
  this._processRequestQueue();
7710
7768
  this.events.emit("open", new Event("open"));
7711
7769
  };
7712
- this.websocket.onerror = () => {
7713
- const delay = this.reconnectOptions.exponential ? this.reconnectOptions.delay * 2 ** (this.reconnectAttempts - 1) : this.reconnectOptions.delay;
7714
- logger.info(`WebSocket: Reconnect attempt failed. Retrying in ${delay}ms.`);
7715
- this.reconnectTimeoutId = setTimeout(tryReconnect, delay);
7716
- };
7770
+ this.websocket.onerror = scheduleRetry;
7771
+ this.websocket.addEventListener("close", scheduleRetry);
7717
7772
  };
7718
7773
  tryReconnect();
7719
7774
  }
@@ -7722,6 +7777,7 @@ var WebSocketChannel = class {
7722
7777
  this.websocket.removeEventListener("close", this.closeListener);
7723
7778
  this.websocket.removeEventListener("message", this.messageListener);
7724
7779
  this.websocket.removeEventListener("error", this.errorListener);
7780
+ this._rejectInFlight("the connection was closed");
7725
7781
  this.events.emit("close", ev);
7726
7782
  if (!this.userInitiatedClose) {
7727
7783
  this._startReconnect();