@calimero-network/mero-js 25.0.6 → 25.0.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
@@ -12077,6 +12077,14 @@ function getLockManager() {
12077
12077
  const locks = nav?.locks;
12078
12078
  return locks && typeof locks.request === "function" ? locks : null;
12079
12079
  }
12080
+ function nodeKeyOf(baseUrl) {
12081
+ try {
12082
+ const { origin: origin2, pathname } = new URL(baseUrl);
12083
+ return origin2 === "null" ? baseUrl : origin2 + pathname.replace(/\/+$/, "");
12084
+ } catch {
12085
+ return baseUrl;
12086
+ }
12087
+ }
12080
12088
  function expiresAtFromJwt(token, fallbackMs) {
12081
12089
  try {
12082
12090
  const parts = token.split(".");
@@ -12112,8 +12120,9 @@ var MeroJs = class {
12112
12120
  ...config
12113
12121
  };
12114
12122
  this.tokenStore = config.tokenStore ?? null;
12123
+ this.nodeKey = nodeKeyOf(config.baseUrl);
12124
+ this.tokenData = this.readStoredTokens();
12115
12125
  if (this.tokenStore) {
12116
- this.tokenData = this.tokenStore.getTokens();
12117
12126
  this.storeHoldsTokens = this.tokenData !== null;
12118
12127
  if (typeof window !== "undefined") window.addEventListener("storage", this.onStorage);
12119
12128
  }
@@ -12134,7 +12143,7 @@ var MeroJs = class {
12134
12143
  onTokenRefresh: async (newToken) => {
12135
12144
  if (this.tokenData) {
12136
12145
  this.tokenData.access_token = newToken;
12137
- this.persistTokens(this.tokenData);
12146
+ this.persistTokens();
12138
12147
  }
12139
12148
  },
12140
12149
  onAuthRevoked: async () => {
@@ -12280,7 +12289,7 @@ var MeroJs = class {
12280
12289
  refresh_token: response.data.refresh_token,
12281
12290
  expires_at: expiresAtFromJwt(accessToken, Date.now() + 36e5)
12282
12291
  };
12283
- this.persistTokens(this.tokenData);
12292
+ this.persistTokens();
12284
12293
  return this.tokenData;
12285
12294
  } catch (error) {
12286
12295
  throw new Error(
@@ -12288,11 +12297,21 @@ var MeroJs = class {
12288
12297
  );
12289
12298
  }
12290
12299
  }
12291
- persistTokens(data) {
12292
- if (!this.tokenStore) return;
12293
- this.tokenStore.setTokens(data);
12300
+ persistTokens() {
12301
+ if (!this.tokenStore || !this.tokenData) return;
12302
+ this.tokenStore.setTokens({ ...this.tokenData, node_origin: this.nodeKey });
12294
12303
  this.storeHoldsTokens = this.tokenStore.getTokens() !== null;
12295
12304
  }
12305
+ /** The stored bundle, only if this node issued it; a legacy or foreign entry reads as empty. */
12306
+ readStoredTokens() {
12307
+ const stored = this.tokenStore?.getTokens();
12308
+ if (!stored || stored.node_origin !== this.nodeKey) return null;
12309
+ return {
12310
+ access_token: stored.access_token,
12311
+ refresh_token: stored.refresh_token,
12312
+ expires_at: stored.expires_at
12313
+ };
12314
+ }
12296
12315
  // Raw store read: an empty store is a logout, another node's bundle is not.
12297
12316
  signedOutElsewhere() {
12298
12317
  return this.storeHoldsTokens && !this.tokenStore?.getTokens();
@@ -12360,7 +12379,7 @@ var MeroJs = class {
12360
12379
  this.dropLocalTokens();
12361
12380
  throw new Error("Signed out in another tab");
12362
12381
  }
12363
- const stored = this.tokenStore?.getTokens() ?? null;
12382
+ const stored = this.readStoredTokens();
12364
12383
  if (stored?.refresh_token) {
12365
12384
  this.storeHoldsTokens = true;
12366
12385
  this.tokenData = stored;
@@ -12393,7 +12412,7 @@ var MeroJs = class {
12393
12412
  refresh_token: response.data.refresh_token,
12394
12413
  expires_at: expiresAtFromJwt(accessToken, Date.now() + 36e5)
12395
12414
  };
12396
- this.persistTokens(this.tokenData);
12415
+ this.persistTokens();
12397
12416
  return this.tokenData;
12398
12417
  } catch (error) {
12399
12418
  throw new Error(
@@ -12422,7 +12441,7 @@ var MeroJs = class {
12422
12441
  setTokenData(data) {
12423
12442
  const expiresAt = data.expires_at || expiresAtFromJwt(data.access_token, Date.now() + 36e5);
12424
12443
  this.tokenData = { ...data, expires_at: expiresAt };
12425
- this.persistTokens(this.tokenData);
12444
+ this.persistTokens();
12426
12445
  }
12427
12446
  /**
12428
12447
  * Get the current token data (for debugging)
@@ -12485,7 +12504,8 @@ var LocalStorageTokenStore = class {
12485
12504
  return {
12486
12505
  access_token: parsed.access_token,
12487
12506
  refresh_token: parsed.refresh_token,
12488
- expires_at: typeof parsed.expires_at === "number" ? parsed.expires_at : Date.now() + 36e5
12507
+ expires_at: typeof parsed.expires_at === "number" ? parsed.expires_at : Date.now() + 36e5,
12508
+ ...typeof parsed.node_origin === "string" && { node_origin: parsed.node_origin }
12489
12509
  };
12490
12510
  }
12491
12511
  return null;