@calimero-network/mero-js 24.0.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.cjs CHANGED
@@ -3021,6 +3021,7 @@ function isGroupMigrationEvent(event) {
3021
3021
  // src/events/sse.ts
3022
3022
  var TERMINAL_AUTH_ERRORS2 = /* @__PURE__ */ new Set(["token_reuse", "token_revoked"]);
3023
3023
  var TERMINAL_AUTH_STATUSES = /* @__PURE__ */ new Set([401, 403]);
3024
+ var SUBSCRIPTION_TIMEOUT_MS = 1e4;
3024
3025
  function isTerminalAuthFailure(response) {
3025
3026
  const authError = response.headers.get("x-auth-error");
3026
3027
  if (!authError) return null;
@@ -3205,15 +3206,21 @@ var SseClient = class {
3205
3206
  try {
3206
3207
  const msg = JSON.parse(jsonStr);
3207
3208
  if (msg.type === "connect" && msg.session_id) {
3208
- this.sessionId = msg.session_id;
3209
- this.emit("connect", msg.session_id);
3209
+ const sessionId = msg.session_id;
3210
+ this.sessionId = sessionId;
3210
3211
  this.presence.clear();
3211
- if (this.subscribedContextIds.size > 0 || this.subscribedGroupIds.size > 0) {
3212
- this.sendSubscription("subscribe", {
3213
- contextIds: [...this.subscribedContextIds],
3214
- groupIds: [...this.subscribedGroupIds]
3215
- });
3212
+ if (this.subscribedContextIds.size === 0 && this.subscribedGroupIds.size === 0) {
3213
+ this.emit("connect", sessionId);
3214
+ return;
3216
3215
  }
3216
+ void this.postSubscription("subscribe", {
3217
+ contextIds: [...this.subscribedContextIds],
3218
+ groupIds: [...this.subscribedGroupIds]
3219
+ }).then((failure) => {
3220
+ if (this.closed || this.sessionId !== sessionId) return;
3221
+ this.emit("connect", sessionId);
3222
+ if (failure) this.emit("error", failure);
3223
+ });
3217
3224
  return;
3218
3225
  }
3219
3226
  if (!msg.result) return;
@@ -3302,6 +3309,16 @@ var SseClient = class {
3302
3309
  }
3303
3310
  }
3304
3311
  async sendSubscription(method, ids) {
3312
+ const failure = await this.postSubscription(method, ids);
3313
+ if (failure) this.emit("error", failure);
3314
+ }
3315
+ /** Sends one subscription change and returns why it failed, or `null`. */
3316
+ async postSubscription(method, ids) {
3317
+ const timeout = new AbortController();
3318
+ const timer = setTimeout(
3319
+ () => timeout.abort(new DOMException(`SSE ${method} timed out`, "TimeoutError")),
3320
+ SUBSCRIPTION_TIMEOUT_MS
3321
+ );
3305
3322
  try {
3306
3323
  const params = {};
3307
3324
  if (ids.contextIds && ids.contextIds.length > 0) params.contextIds = ids.contextIds;
@@ -3314,31 +3331,32 @@ var SseClient = class {
3314
3331
  ...await this.authorize({ method: "POST", path: new URL(url).pathname, body }),
3315
3332
  "Content-Type": "application/json"
3316
3333
  },
3317
- body
3334
+ body,
3335
+ signal: timeout.signal
3318
3336
  });
3319
3337
  if (!response.ok) {
3320
3338
  const terminal = isTerminalAuthFailure(response);
3321
3339
  const bodyText = await response.text().catch(() => void 0);
3322
- this.emit(
3323
- "error",
3324
- terminal ? new AuthRevokedError(
3325
- terminal,
3326
- response.status,
3327
- response.statusText,
3328
- `${this.baseUrl}/sse/subscription`,
3329
- response.headers,
3330
- bodyText
3331
- ) : new HTTPError(
3332
- response.status,
3333
- response.statusText,
3334
- `${this.baseUrl}/sse/subscription`,
3335
- response.headers,
3336
- bodyText
3337
- )
3340
+ return terminal ? new AuthRevokedError(
3341
+ terminal,
3342
+ response.status,
3343
+ response.statusText,
3344
+ `${this.baseUrl}/sse/subscription`,
3345
+ response.headers,
3346
+ bodyText
3347
+ ) : new HTTPError(
3348
+ response.status,
3349
+ response.statusText,
3350
+ `${this.baseUrl}/sse/subscription`,
3351
+ response.headers,
3352
+ bodyText
3338
3353
  );
3339
3354
  }
3355
+ return null;
3340
3356
  } catch (err) {
3341
- this.emit("error", err instanceof Error ? err : new Error(`SSE ${method} failed`));
3357
+ return err instanceof Error ? err : new Error(`SSE ${method} failed`);
3358
+ } finally {
3359
+ clearTimeout(timer);
3342
3360
  }
3343
3361
  }
3344
3362
  forceReconnect() {
@@ -3817,7 +3835,8 @@ async function mintDeviceId(account, nonce2) {
3817
3835
  throw new Error(`nonce must be 16 bytes, got ${nonce2.length}`);
3818
3836
  }
3819
3837
  const accountBytes = fromHex(account, "account", 32);
3820
- return hex(await domainHash(DEVICE_ID_DOMAIN, [accountBytes, nonce2]));
3838
+ const binding = await domainHash(DEVICE_ID_DOMAIN, [accountBytes, nonce2]);
3839
+ return hex(concat(nonce2, binding.subarray(0, 16)));
3821
3840
  }
3822
3841
  async function deviceCertPayload(input) {
3823
3842
  return domainHash(CERT_DOMAIN, [
@@ -11212,6 +11231,20 @@ function teeRoleOf(row) {
11212
11231
  function pickExecutingRelay(relays) {
11213
11232
  return relays.find((r) => r.canExecute && r.relayUrl !== null && r.executorAccount !== null) ?? null;
11214
11233
  }
11234
+ function joinOf(row) {
11235
+ const raw = row.join;
11236
+ if (!raw || typeof raw !== "object" || Array.isArray(raw)) return null;
11237
+ const join = raw;
11238
+ if (typeof join.state !== "string") return null;
11239
+ const refusals = Array.isArray(join.refusals) ? join.refusals.filter((r) => !!r && typeof r === "object").map((r) => ({ peer: String(r.peer ?? ""), reason: String(r.reason ?? "") })) : [];
11240
+ return {
11241
+ state: join.state,
11242
+ attempts: typeof join.attempts === "number" ? join.attempts : 0,
11243
+ refusals,
11244
+ error: typeof join.error === "string" ? join.error : null,
11245
+ reportedAt: typeof join.reported_at === "string" ? join.reported_at : null
11246
+ };
11247
+ }
11215
11248
  var ACCOUNT_HEX = /^[0-9a-f]{64}$/;
11216
11249
  function accountHexOrNull(value) {
11217
11250
  return typeof value === "string" && ACCOUNT_HEX.test(value) ? value : null;
@@ -11451,7 +11484,10 @@ var CloudClient = class {
11451
11484
  authorshipReady && relayUrl !== null && executorAccount !== null
11452
11485
  ),
11453
11486
  lastSeenAt: row.last_seen_at ?? null,
11454
- confirmedAt: row.confirmed_at ?? null
11487
+ confirmedAt: row.confirmed_at ?? null,
11488
+ admission: typeof row.admission === "string" ? row.admission : null,
11489
+ admissionHint: typeof row.admission_hint === "string" ? row.admission_hint : null,
11490
+ join: joinOf(row)
11455
11491
  };
11456
11492
  });
11457
11493
  }