@tinycloud/node-sdk 2.4.0-beta.10 → 2.4.0-beta.12

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
@@ -17475,12 +17475,48 @@ var NodeUserAuthorization = class {
17475
17475
  * `siwe` is the load-bearing one because `extractSiweExpiration` returns
17476
17476
  * undefined for missing SIWEs and the SDK then treats the session as
17477
17477
  * expired-at-epoch-zero.
17478
+ *
17479
+ * @param hosts - The TinyCloud hosts this session was created against,
17480
+ * as persisted in {@link PersistedSessionData.tinycloudHosts}. When
17481
+ * present (and non-empty) they are adopted directly so the restored
17482
+ * session resolves to the same node as the original sign-in without
17483
+ * re-running registry/fallback resolution. When absent (old session)
17484
+ * hosts are resolved lazily on the first host-needing call via
17485
+ * {@link ensureTinyCloudHosts}.
17478
17486
  */
17479
- setRestoredTinyCloudSession(session) {
17487
+ setRestoredTinyCloudSession(session, hosts) {
17480
17488
  const address = (0, import_sdk_core.canonicalizeAddress)(session.address);
17481
17489
  this._tinyCloudSession = { ...session, address };
17482
17490
  this._address = address;
17483
17491
  this._chainId = session.chainId;
17492
+ if ((!this.tinycloudHosts || this.tinycloudHosts.length === 0) && hosts && hosts.length > 0) {
17493
+ this.tinycloudHosts = [...hosts];
17494
+ }
17495
+ }
17496
+ /**
17497
+ * Ensure `tinycloudHosts` are resolved before a host-needing call.
17498
+ *
17499
+ * Fresh sign-in resolves hosts up front; a restored session may not have
17500
+ * (old persisted sessions predate {@link PersistedSessionData.tinycloudHosts}).
17501
+ * This guard makes a restored session resolve the node exactly like a fresh
17502
+ * sign-in — persisted hosts are preferred (already set by
17503
+ * {@link setRestoredTinyCloudSession}), otherwise the registry/fallback
17504
+ * resolution runs lazily here. Idempotent: {@link resolveTinyCloudHostsForSignIn}
17505
+ * early-returns when hosts are already set.
17506
+ *
17507
+ * Throws if hosts are unset and the restored session has no address/chainId
17508
+ * to resolve from — a real failure that must surface, not be masked.
17509
+ */
17510
+ async ensureTinyCloudHosts() {
17511
+ if (this.tinycloudHosts && this.tinycloudHosts.length > 0) {
17512
+ return;
17513
+ }
17514
+ if (this._address === void 0 || this._chainId === void 0) {
17515
+ throw new Error(
17516
+ "Cannot resolve TinyCloud hosts: no address/chainId available. Sign in or restore a session first."
17517
+ );
17518
+ }
17519
+ await this.resolveTinyCloudHostsForSignIn(this._address, this._chainId);
17484
17520
  }
17485
17521
  async resolveTinyCloudHostsForSignIn(address, chainId) {
17486
17522
  if (this.tinycloudHosts && this.tinycloudHosts.length > 0) {
@@ -17677,6 +17713,7 @@ var NodeUserAuthorization = class {
17677
17713
  if (!this._tinyCloudSession || !this._address || !this._chainId) {
17678
17714
  throw new Error("Must be signed in to host space");
17679
17715
  }
17716
+ await this.ensureTinyCloudHosts();
17680
17717
  const host = this.primaryTinyCloudHost;
17681
17718
  const spaceId = targetSpaceId ?? this._tinyCloudSession.spaceId;
17682
17719
  const peerId = await (0, import_sdk_core.fetchPeerId)(host, spaceId);
@@ -17719,6 +17756,7 @@ var NodeUserAuthorization = class {
17719
17756
  if (!this._tinyCloudSession) {
17720
17757
  throw new Error("Must be signed in to ensure space exists");
17721
17758
  }
17759
+ await this.ensureTinyCloudHosts();
17722
17760
  const host = this.primaryTinyCloudHost;
17723
17761
  const primarySpaceId = this._tinyCloudSession.spaceId;
17724
17762
  const result = await (0, import_sdk_core.activateSessionWithHost)(
@@ -17900,7 +17938,8 @@ var NodeUserAuthorization = class {
17900
17938
  },
17901
17939
  expiresAt: expirationTime.toISOString(),
17902
17940
  createdAt: now.toISOString(),
17903
- version: "1.0"
17941
+ version: "1.0",
17942
+ tinycloudHosts: this.tinycloudHosts
17904
17943
  };
17905
17944
  await this.sessionStorage.save(address, persistedData);
17906
17945
  this._session = clientSession;
@@ -18067,7 +18106,8 @@ var NodeUserAuthorization = class {
18067
18106
  },
18068
18107
  expiresAt,
18069
18108
  createdAt,
18070
- version: "1.0"
18109
+ version: "1.0",
18110
+ tinycloudHosts: this.tinycloudHosts
18071
18111
  };
18072
18112
  await this.sessionStorage.save(address, persistedData);
18073
18113
  this._session = clientSession;
@@ -19195,6 +19235,14 @@ var _TinyCloudNode = class _TinyCloudNode {
19195
19235
  if (sessionData.chainId) {
19196
19236
  this._chainId = sessionData.chainId;
19197
19237
  }
19238
+ const resolvedHost = await this.resolveRestoredHost(
19239
+ sessionData.tinycloudHosts,
19240
+ restoredAddress,
19241
+ sessionData.chainId
19242
+ );
19243
+ if (resolvedHost) {
19244
+ this.config.host = resolvedHost;
19245
+ }
19198
19246
  this._serviceContext = new import_sdk_core6.ServiceContext({
19199
19247
  invoke: this.invokeWithRuntimePermissions,
19200
19248
  invokeAny: this.invokeAnyWithRuntimePermissions,
@@ -19240,12 +19288,45 @@ var _TinyCloudNode = class _TinyCloudNode {
19240
19288
  signature: sessionData.signature ?? ""
19241
19289
  };
19242
19290
  if (this.auth) {
19243
- this.auth.setRestoredTinyCloudSession(tcSession);
19291
+ this.auth.setRestoredTinyCloudSession(
19292
+ tcSession,
19293
+ this.config.host ? [this.config.host] : void 0
19294
+ );
19244
19295
  } else {
19245
19296
  this._restoredTcSession = tcSession;
19246
19297
  }
19247
19298
  }
19248
19299
  }
19300
+ /**
19301
+ * Resolve the host a restored session should target.
19302
+ *
19303
+ * Mirrors fresh sign-in host resolution but for the restore path:
19304
+ * an explicit/pinned host always wins, then the hosts the session was
19305
+ * persisted with, then a lazy registry/fallback resolution for sessions
19306
+ * that predate the persisted `tinycloudHosts` field. Returns `undefined`
19307
+ * only when there's nothing to resolve from (no explicit host, no
19308
+ * persisted hosts, and no address/chainId) — in which case the existing
19309
+ * `config.host` (default) is left in place.
19310
+ *
19311
+ * Resolution failures are surfaced, not swallowed: a genuinely broken
19312
+ * registry lookup throws rather than silently falling back to a wrong host.
19313
+ */
19314
+ async resolveRestoredHost(persistedHosts, address, chainId) {
19315
+ if (this.explicitHost) {
19316
+ return this.explicitHost;
19317
+ }
19318
+ if (persistedHosts && persistedHosts.length > 0) {
19319
+ return persistedHosts[0];
19320
+ }
19321
+ if (address === void 0 || chainId === void 0) {
19322
+ return void 0;
19323
+ }
19324
+ const resolved = await (0, import_sdk_core6.resolveTinyCloudHosts)((0, import_sdk_core6.pkhDid)(address, chainId), {
19325
+ registryUrl: this.config.tinycloudRegistryUrl,
19326
+ fallbackHosts: this.config.tinycloudFallbackHosts
19327
+ });
19328
+ return resolved.hosts[0];
19329
+ }
19249
19330
  /**
19250
19331
  * Resolve the currently-active TinyCloudSession, preferring the auth
19251
19332
  * layer's value (wallet mode) and falling back to the node-level