@calimero-network/mero-js 25.3.2 → 25.3.3

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
@@ -3028,7 +3028,8 @@ var SseClient = class {
3028
3028
  return;
3029
3029
  }
3030
3030
  this.closed = false;
3031
- this.abortController = new AbortController();
3031
+ const controller = new AbortController();
3032
+ this.abortController = controller;
3032
3033
  try {
3033
3034
  const url2 = `${this.baseUrl}/sse`;
3034
3035
  const headers = {
@@ -3038,7 +3039,7 @@ var SseClient = class {
3038
3039
  const proofAttached = carriesProof(headers);
3039
3040
  const response = await this.fetchImpl(url2, {
3040
3041
  headers,
3041
- signal: this.abortController.signal,
3042
+ signal: controller.signal,
3042
3043
  ...proofAttached && { redirect: "manual" }
3043
3044
  });
3044
3045
  if (proofAttached) refuseProofRedirect(response, url2);
@@ -3066,21 +3067,31 @@ var SseClient = class {
3066
3067
  if (!response.body) {
3067
3068
  throw new Error("SSE response has no body");
3068
3069
  }
3069
- this.readStream(response.body).catch((err) => {
3070
- if (this.closed) return;
3070
+ this.readStream(response.body, controller).catch((err) => {
3071
+ if (this.superseded(controller)) return;
3071
3072
  const error = err instanceof Error ? err : new Error(String(err));
3072
3073
  this.emit("error", error);
3073
3074
  this.scheduleReconnect();
3074
3075
  });
3075
3076
  } catch (err) {
3076
- if (this.closed) return;
3077
+ if (this.superseded(controller)) return;
3077
3078
  const error = err instanceof Error ? err : new Error(String(err));
3078
3079
  this.emit("error", error);
3079
3080
  if (error instanceof AuthRevokedError) return;
3080
3081
  this.scheduleReconnect();
3081
3082
  }
3082
3083
  }
3083
- async readStream(body) {
3084
+ /**
3085
+ * Whether `controller`'s connection is no longer the one this client holds:
3086
+ * closed, or replaced by a later connect. Its abort lands asynchronously,
3087
+ * after a `close()` + `connect()` has already cleared `closed`, so `closed`
3088
+ * alone cannot tell; reporting it, and reconnecting for it, would abort the
3089
+ * live connection, whose own abort would do the same, without end.
3090
+ */
3091
+ superseded(controller) {
3092
+ return this.closed || controller.signal.aborted || this.abortController !== controller;
3093
+ }
3094
+ async readStream(body, controller) {
3084
3095
  const reader = body.getReader();
3085
3096
  const decoder = new TextDecoder();
3086
3097
  let buffer = "";
@@ -3108,11 +3119,11 @@ var SseClient = class {
3108
3119
  }
3109
3120
  }
3110
3121
  } catch (err) {
3111
- if (this.closed) return;
3122
+ if (this.superseded(controller)) return;
3112
3123
  const error = err instanceof Error ? err : new Error(String(err));
3113
3124
  this.emit("error", error);
3114
3125
  }
3115
- if (!this.closed) {
3126
+ if (!this.superseded(controller)) {
3116
3127
  this.scheduleReconnect();
3117
3128
  }
3118
3129
  }