@tinycloud/node-sdk 2.4.0-beta.15 → 2.4.0-beta.16

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.
@@ -1047,15 +1047,44 @@ declare class TinyCloudNode {
1047
1047
  * registered on the node.
1048
1048
  *
1049
1049
  * Calling this resolves `name` to the owner's owned-space URI
1050
- * (`tinycloud:pkh:eip155:<chain>:<addr>:<name>`) and hosts it via the
1051
- * host-SIWE delegation flow. The host SIWE is idempotent server-side, so it
1052
- * is safe to call whether or not the space already exists; do not gate it on
1053
- * a prior existence check. Must be called after {@link signIn}.
1050
+ * (`tinycloud:pkh:eip155:<chain>:<addr>:<name>`). It first consults the
1051
+ * account-space spaces registry (`account/spaces/{space_id}`, the canonical
1052
+ * KV source of truth, fronted by a best-effort SQLite index): if the space is
1053
+ * already registered/hosted it returns the URI WITHOUT submitting a host
1054
+ * delegation, avoiding a redundant host-SIWE signature prompt for owners who
1055
+ * already use the space. Only when the space is absent — or the registry
1056
+ * check fails for any reason (e.g. a cold SQLite index reporting
1057
+ * `no such table: spaces`) — does it fall through to {@link hostOwnedSpace}.
1058
+ *
1059
+ * The registry check is purely an optimization: any failure falls back to
1060
+ * hosting, and the host SIWE is idempotent server-side, so re-hosting an
1061
+ * existing space remains a safe no-op. Must be called after {@link signIn}.
1054
1062
  *
1055
1063
  * @param name - The owned space name (e.g. `"secrets"`).
1056
1064
  * @returns The hosted owned-space URI.
1057
1065
  */
1058
1066
  ensureOwnedSpaceHosted(name: string): Promise<string>;
1067
+ /**
1068
+ * Check whether an owned space is already registered/hosted by consulting the
1069
+ * account spaces registry.
1070
+ *
1071
+ * Source of truth is the canonical KV registry record
1072
+ * `account/spaces/{space_id}`, read here via `account.spaces.get(spaceId)`.
1073
+ * The KV path is used (rather than `syncAccessible()`) because it works under
1074
+ * a manifest/recap session with NO extra prompt: the composed manifest recap
1075
+ * already grants `tinycloud.kv get/list` on the account space `spaces/`
1076
+ * prefix, whereas `syncAccessible()` depends on `tinycloud.space/list`, which
1077
+ * a recap session does not hold. Before reading, it consults the fast SQLite
1078
+ * index (`account.index.spaces.list()`) as a best-effort short-circuit; on a
1079
+ * cold index (`no such table: spaces`) or any other index failure it falls
1080
+ * back to the canonical KV read.
1081
+ *
1082
+ * This is a best-effort optimization. ANY failure of the check path (missing
1083
+ * table, KV error, missing record, thrown exception) resolves to `false` so
1084
+ * the caller falls through to hosting — per the directive, "if it fails in any
1085
+ * way then create the space".
1086
+ */
1087
+ private isOwnedSpaceRegistered;
1059
1088
  /**
1060
1089
  * Restore a previously established session from stored delegation data.
1061
1090
  *
@@ -1047,15 +1047,44 @@ declare class TinyCloudNode {
1047
1047
  * registered on the node.
1048
1048
  *
1049
1049
  * Calling this resolves `name` to the owner's owned-space URI
1050
- * (`tinycloud:pkh:eip155:<chain>:<addr>:<name>`) and hosts it via the
1051
- * host-SIWE delegation flow. The host SIWE is idempotent server-side, so it
1052
- * is safe to call whether or not the space already exists; do not gate it on
1053
- * a prior existence check. Must be called after {@link signIn}.
1050
+ * (`tinycloud:pkh:eip155:<chain>:<addr>:<name>`). It first consults the
1051
+ * account-space spaces registry (`account/spaces/{space_id}`, the canonical
1052
+ * KV source of truth, fronted by a best-effort SQLite index): if the space is
1053
+ * already registered/hosted it returns the URI WITHOUT submitting a host
1054
+ * delegation, avoiding a redundant host-SIWE signature prompt for owners who
1055
+ * already use the space. Only when the space is absent — or the registry
1056
+ * check fails for any reason (e.g. a cold SQLite index reporting
1057
+ * `no such table: spaces`) — does it fall through to {@link hostOwnedSpace}.
1058
+ *
1059
+ * The registry check is purely an optimization: any failure falls back to
1060
+ * hosting, and the host SIWE is idempotent server-side, so re-hosting an
1061
+ * existing space remains a safe no-op. Must be called after {@link signIn}.
1054
1062
  *
1055
1063
  * @param name - The owned space name (e.g. `"secrets"`).
1056
1064
  * @returns The hosted owned-space URI.
1057
1065
  */
1058
1066
  ensureOwnedSpaceHosted(name: string): Promise<string>;
1067
+ /**
1068
+ * Check whether an owned space is already registered/hosted by consulting the
1069
+ * account spaces registry.
1070
+ *
1071
+ * Source of truth is the canonical KV registry record
1072
+ * `account/spaces/{space_id}`, read here via `account.spaces.get(spaceId)`.
1073
+ * The KV path is used (rather than `syncAccessible()`) because it works under
1074
+ * a manifest/recap session with NO extra prompt: the composed manifest recap
1075
+ * already grants `tinycloud.kv get/list` on the account space `spaces/`
1076
+ * prefix, whereas `syncAccessible()` depends on `tinycloud.space/list`, which
1077
+ * a recap session does not hold. Before reading, it consults the fast SQLite
1078
+ * index (`account.index.spaces.list()`) as a best-effort short-circuit; on a
1079
+ * cold index (`no such table: spaces`) or any other index failure it falls
1080
+ * back to the canonical KV read.
1081
+ *
1082
+ * This is a best-effort optimization. ANY failure of the check path (missing
1083
+ * table, KV error, missing record, thrown exception) resolves to `false` so
1084
+ * the caller falls through to hosting — per the directive, "if it fails in any
1085
+ * way then create the space".
1086
+ */
1087
+ private isOwnedSpaceRegistered;
1059
1088
  /**
1060
1089
  * Restore a previously established session from stored delegation data.
1061
1090
  *
package/dist/core.cjs CHANGED
@@ -2178,16 +2178,78 @@ var _TinyCloudNode = class _TinyCloudNode {
2178
2178
  * registered on the node.
2179
2179
  *
2180
2180
  * Calling this resolves `name` to the owner's owned-space URI
2181
- * (`tinycloud:pkh:eip155:<chain>:<addr>:<name>`) and hosts it via the
2182
- * host-SIWE delegation flow. The host SIWE is idempotent server-side, so it
2183
- * is safe to call whether or not the space already exists; do not gate it on
2184
- * a prior existence check. Must be called after {@link signIn}.
2181
+ * (`tinycloud:pkh:eip155:<chain>:<addr>:<name>`). It first consults the
2182
+ * account-space spaces registry (`account/spaces/{space_id}`, the canonical
2183
+ * KV source of truth, fronted by a best-effort SQLite index): if the space is
2184
+ * already registered/hosted it returns the URI WITHOUT submitting a host
2185
+ * delegation, avoiding a redundant host-SIWE signature prompt for owners who
2186
+ * already use the space. Only when the space is absent — or the registry
2187
+ * check fails for any reason (e.g. a cold SQLite index reporting
2188
+ * `no such table: spaces`) — does it fall through to {@link hostOwnedSpace}.
2189
+ *
2190
+ * The registry check is purely an optimization: any failure falls back to
2191
+ * hosting, and the host SIWE is idempotent server-side, so re-hosting an
2192
+ * existing space remains a safe no-op. Must be called after {@link signIn}.
2185
2193
  *
2186
2194
  * @param name - The owned space name (e.g. `"secrets"`).
2187
2195
  * @returns The hosted owned-space URI.
2188
2196
  */
2189
2197
  async ensureOwnedSpaceHosted(name) {
2190
- return this.hostOwnedSpace(name);
2198
+ if (!this.auth || !this.auth.tinyCloudSession) {
2199
+ throw new Error("Not signed in. Call signIn() first.");
2200
+ }
2201
+ const spaceId = this.ownedSpaceId(name);
2202
+ if (await this.isOwnedSpaceRegistered(spaceId)) {
2203
+ return spaceId;
2204
+ }
2205
+ const hosted = await this.hostOwnedSpace(name);
2206
+ try {
2207
+ await this.account.spaces.register({
2208
+ spaceId,
2209
+ name,
2210
+ ownerDid: this.did,
2211
+ type: "owned",
2212
+ permissions: ["*"],
2213
+ status: "active"
2214
+ });
2215
+ } catch {
2216
+ }
2217
+ return hosted;
2218
+ }
2219
+ /**
2220
+ * Check whether an owned space is already registered/hosted by consulting the
2221
+ * account spaces registry.
2222
+ *
2223
+ * Source of truth is the canonical KV registry record
2224
+ * `account/spaces/{space_id}`, read here via `account.spaces.get(spaceId)`.
2225
+ * The KV path is used (rather than `syncAccessible()`) because it works under
2226
+ * a manifest/recap session with NO extra prompt: the composed manifest recap
2227
+ * already grants `tinycloud.kv get/list` on the account space `spaces/`
2228
+ * prefix, whereas `syncAccessible()` depends on `tinycloud.space/list`, which
2229
+ * a recap session does not hold. Before reading, it consults the fast SQLite
2230
+ * index (`account.index.spaces.list()`) as a best-effort short-circuit; on a
2231
+ * cold index (`no such table: spaces`) or any other index failure it falls
2232
+ * back to the canonical KV read.
2233
+ *
2234
+ * This is a best-effort optimization. ANY failure of the check path (missing
2235
+ * table, KV error, missing record, thrown exception) resolves to `false` so
2236
+ * the caller falls through to hosting — per the directive, "if it fails in any
2237
+ * way then create the space".
2238
+ */
2239
+ async isOwnedSpaceRegistered(spaceId) {
2240
+ try {
2241
+ const indexed = await this.account.index.spaces.list();
2242
+ if (indexed.ok && indexed.data.some((space) => space.spaceId === spaceId)) {
2243
+ return true;
2244
+ }
2245
+ } catch {
2246
+ }
2247
+ try {
2248
+ const record = await this.account.spaces.get(spaceId);
2249
+ return record.ok;
2250
+ } catch {
2251
+ return false;
2252
+ }
2191
2253
  }
2192
2254
  /**
2193
2255
  * Restore a previously established session from stored delegation data.