@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.cjs CHANGED
@@ -12362,6 +12362,14 @@ function getLockManager() {
12362
12362
  const locks = nav?.locks;
12363
12363
  return locks && typeof locks.request === "function" ? locks : null;
12364
12364
  }
12365
+ function nodeKeyOf(baseUrl) {
12366
+ try {
12367
+ const { origin: origin2, pathname } = new URL(baseUrl);
12368
+ return origin2 === "null" ? baseUrl : origin2 + pathname.replace(/\/+$/, "");
12369
+ } catch {
12370
+ return baseUrl;
12371
+ }
12372
+ }
12365
12373
  function expiresAtFromJwt(token, fallbackMs) {
12366
12374
  try {
12367
12375
  const parts = token.split(".");
@@ -12397,8 +12405,9 @@ var MeroJs = class {
12397
12405
  ...config
12398
12406
  };
12399
12407
  this.tokenStore = config.tokenStore ?? null;
12408
+ this.nodeKey = nodeKeyOf(config.baseUrl);
12409
+ this.tokenData = this.readStoredTokens();
12400
12410
  if (this.tokenStore) {
12401
- this.tokenData = this.tokenStore.getTokens();
12402
12411
  this.storeHoldsTokens = this.tokenData !== null;
12403
12412
  if (typeof window !== "undefined") window.addEventListener("storage", this.onStorage);
12404
12413
  }
@@ -12419,7 +12428,7 @@ var MeroJs = class {
12419
12428
  onTokenRefresh: async (newToken) => {
12420
12429
  if (this.tokenData) {
12421
12430
  this.tokenData.access_token = newToken;
12422
- this.persistTokens(this.tokenData);
12431
+ this.persistTokens();
12423
12432
  }
12424
12433
  },
12425
12434
  onAuthRevoked: async () => {
@@ -12565,7 +12574,7 @@ var MeroJs = class {
12565
12574
  refresh_token: response.data.refresh_token,
12566
12575
  expires_at: expiresAtFromJwt(accessToken, Date.now() + 36e5)
12567
12576
  };
12568
- this.persistTokens(this.tokenData);
12577
+ this.persistTokens();
12569
12578
  return this.tokenData;
12570
12579
  } catch (error) {
12571
12580
  throw new Error(
@@ -12573,11 +12582,21 @@ var MeroJs = class {
12573
12582
  );
12574
12583
  }
12575
12584
  }
12576
- persistTokens(data) {
12577
- if (!this.tokenStore) return;
12578
- this.tokenStore.setTokens(data);
12585
+ persistTokens() {
12586
+ if (!this.tokenStore || !this.tokenData) return;
12587
+ this.tokenStore.setTokens({ ...this.tokenData, node_origin: this.nodeKey });
12579
12588
  this.storeHoldsTokens = this.tokenStore.getTokens() !== null;
12580
12589
  }
12590
+ /** The stored bundle, only if this node issued it; a legacy or foreign entry reads as empty. */
12591
+ readStoredTokens() {
12592
+ const stored = this.tokenStore?.getTokens();
12593
+ if (!stored || stored.node_origin !== this.nodeKey) return null;
12594
+ return {
12595
+ access_token: stored.access_token,
12596
+ refresh_token: stored.refresh_token,
12597
+ expires_at: stored.expires_at
12598
+ };
12599
+ }
12581
12600
  // Raw store read: an empty store is a logout, another node's bundle is not.
12582
12601
  signedOutElsewhere() {
12583
12602
  return this.storeHoldsTokens && !this.tokenStore?.getTokens();
@@ -12645,7 +12664,7 @@ var MeroJs = class {
12645
12664
  this.dropLocalTokens();
12646
12665
  throw new Error("Signed out in another tab");
12647
12666
  }
12648
- const stored = this.tokenStore?.getTokens() ?? null;
12667
+ const stored = this.readStoredTokens();
12649
12668
  if (stored?.refresh_token) {
12650
12669
  this.storeHoldsTokens = true;
12651
12670
  this.tokenData = stored;
@@ -12678,7 +12697,7 @@ var MeroJs = class {
12678
12697
  refresh_token: response.data.refresh_token,
12679
12698
  expires_at: expiresAtFromJwt(accessToken, Date.now() + 36e5)
12680
12699
  };
12681
- this.persistTokens(this.tokenData);
12700
+ this.persistTokens();
12682
12701
  return this.tokenData;
12683
12702
  } catch (error) {
12684
12703
  throw new Error(
@@ -12707,7 +12726,7 @@ var MeroJs = class {
12707
12726
  setTokenData(data) {
12708
12727
  const expiresAt = data.expires_at || expiresAtFromJwt(data.access_token, Date.now() + 36e5);
12709
12728
  this.tokenData = { ...data, expires_at: expiresAt };
12710
- this.persistTokens(this.tokenData);
12729
+ this.persistTokens();
12711
12730
  }
12712
12731
  /**
12713
12732
  * Get the current token data (for debugging)
@@ -12770,7 +12789,8 @@ var LocalStorageTokenStore = class {
12770
12789
  return {
12771
12790
  access_token: parsed.access_token,
12772
12791
  refresh_token: parsed.refresh_token,
12773
- expires_at: typeof parsed.expires_at === "number" ? parsed.expires_at : Date.now() + 36e5
12792
+ expires_at: typeof parsed.expires_at === "number" ? parsed.expires_at : Date.now() + 36e5,
12793
+ ...typeof parsed.node_origin === "string" && { node_origin: parsed.node_origin }
12774
12794
  };
12775
12795
  }
12776
12796
  return null;