@calimero-network/mero-js 24.1.0 → 24.1.1

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
@@ -2739,6 +2739,7 @@ function isGroupMigrationEvent(event) {
2739
2739
  // src/events/sse.ts
2740
2740
  var TERMINAL_AUTH_ERRORS2 = /* @__PURE__ */ new Set(["token_reuse", "token_revoked"]);
2741
2741
  var TERMINAL_AUTH_STATUSES = /* @__PURE__ */ new Set([401, 403]);
2742
+ var SUBSCRIPTION_TIMEOUT_MS = 1e4;
2742
2743
  function isTerminalAuthFailure(response) {
2743
2744
  const authError = response.headers.get("x-auth-error");
2744
2745
  if (!authError) return null;
@@ -2923,15 +2924,21 @@ var SseClient = class {
2923
2924
  try {
2924
2925
  const msg = JSON.parse(jsonStr);
2925
2926
  if (msg.type === "connect" && msg.session_id) {
2926
- this.sessionId = msg.session_id;
2927
- this.emit("connect", msg.session_id);
2927
+ const sessionId = msg.session_id;
2928
+ this.sessionId = sessionId;
2928
2929
  this.presence.clear();
2929
- if (this.subscribedContextIds.size > 0 || this.subscribedGroupIds.size > 0) {
2930
- this.sendSubscription("subscribe", {
2931
- contextIds: [...this.subscribedContextIds],
2932
- groupIds: [...this.subscribedGroupIds]
2933
- });
2930
+ if (this.subscribedContextIds.size === 0 && this.subscribedGroupIds.size === 0) {
2931
+ this.emit("connect", sessionId);
2932
+ return;
2934
2933
  }
2934
+ void this.postSubscription("subscribe", {
2935
+ contextIds: [...this.subscribedContextIds],
2936
+ groupIds: [...this.subscribedGroupIds]
2937
+ }).then((failure) => {
2938
+ if (this.closed || this.sessionId !== sessionId) return;
2939
+ this.emit("connect", sessionId);
2940
+ if (failure) this.emit("error", failure);
2941
+ });
2935
2942
  return;
2936
2943
  }
2937
2944
  if (!msg.result) return;
@@ -3020,6 +3027,16 @@ var SseClient = class {
3020
3027
  }
3021
3028
  }
3022
3029
  async sendSubscription(method, ids) {
3030
+ const failure = await this.postSubscription(method, ids);
3031
+ if (failure) this.emit("error", failure);
3032
+ }
3033
+ /** Sends one subscription change and returns why it failed, or `null`. */
3034
+ async postSubscription(method, ids) {
3035
+ const timeout = new AbortController();
3036
+ const timer = setTimeout(
3037
+ () => timeout.abort(new DOMException(`SSE ${method} timed out`, "TimeoutError")),
3038
+ SUBSCRIPTION_TIMEOUT_MS
3039
+ );
3023
3040
  try {
3024
3041
  const params = {};
3025
3042
  if (ids.contextIds && ids.contextIds.length > 0) params.contextIds = ids.contextIds;
@@ -3032,31 +3049,32 @@ var SseClient = class {
3032
3049
  ...await this.authorize({ method: "POST", path: new URL(url).pathname, body }),
3033
3050
  "Content-Type": "application/json"
3034
3051
  },
3035
- body
3052
+ body,
3053
+ signal: timeout.signal
3036
3054
  });
3037
3055
  if (!response.ok) {
3038
3056
  const terminal = isTerminalAuthFailure(response);
3039
3057
  const bodyText = await response.text().catch(() => void 0);
3040
- this.emit(
3041
- "error",
3042
- terminal ? new AuthRevokedError(
3043
- terminal,
3044
- response.status,
3045
- response.statusText,
3046
- `${this.baseUrl}/sse/subscription`,
3047
- response.headers,
3048
- bodyText
3049
- ) : new HTTPError(
3050
- response.status,
3051
- response.statusText,
3052
- `${this.baseUrl}/sse/subscription`,
3053
- response.headers,
3054
- bodyText
3055
- )
3058
+ return terminal ? new AuthRevokedError(
3059
+ terminal,
3060
+ response.status,
3061
+ response.statusText,
3062
+ `${this.baseUrl}/sse/subscription`,
3063
+ response.headers,
3064
+ bodyText
3065
+ ) : new HTTPError(
3066
+ response.status,
3067
+ response.statusText,
3068
+ `${this.baseUrl}/sse/subscription`,
3069
+ response.headers,
3070
+ bodyText
3056
3071
  );
3057
3072
  }
3073
+ return null;
3058
3074
  } catch (err) {
3059
- this.emit("error", err instanceof Error ? err : new Error(`SSE ${method} failed`));
3075
+ return err instanceof Error ? err : new Error(`SSE ${method} failed`);
3076
+ } finally {
3077
+ clearTimeout(timer);
3060
3078
  }
3061
3079
  }
3062
3080
  forceReconnect() {
@@ -3535,7 +3553,8 @@ async function mintDeviceId(account, nonce2) {
3535
3553
  throw new Error(`nonce must be 16 bytes, got ${nonce2.length}`);
3536
3554
  }
3537
3555
  const accountBytes = fromHex(account, "account", 32);
3538
- return hex(await domainHash(DEVICE_ID_DOMAIN, [accountBytes, nonce2]));
3556
+ const binding = await domainHash(DEVICE_ID_DOMAIN, [accountBytes, nonce2]);
3557
+ return hex(concat(nonce2, binding.subarray(0, 16)));
3539
3558
  }
3540
3559
  async function deviceCertPayload(input) {
3541
3560
  return domainHash(CERT_DOMAIN, [