@tinycloud/node-sdk 2.3.0 → 2.4.0-beta.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
@@ -18987,6 +18987,52 @@ var _TinyCloudNode = class _TinyCloudNode {
18987
18987
  );
18988
18988
  }
18989
18989
  }
18990
+ /**
18991
+ * Host one of this user's owned spaces by name (e.g. `"applications"`).
18992
+ *
18993
+ * Resolves the name to the owned space URI
18994
+ * (`tinycloud:pkh:eip155:<chain>:<addr>:<name>`) and registers it on the
18995
+ * server via the host-SIWE delegation flow, so subsequent KV/SQL writes to
18996
+ * that space succeed instead of returning `404 - Space not found`. The
18997
+ * caller is the root authority of their own owned spaces, so no additional
18998
+ * delegation is required.
18999
+ *
19000
+ * Unlike {@link ensureOwnedSpaceHosted}, this always submits the host
19001
+ * delegation rather than inferring hosting from session activation: a space
19002
+ * the current session has never referenced is reported neither as
19003
+ * `activated` nor `skipped`, so activation-based detection would wrongly
19004
+ * skip the host. The host SIWE is idempotent server-side, so re-hosting an
19005
+ * existing space is a safe no-op. Must be called after {@link signIn}.
19006
+ *
19007
+ * @param name - The owned space name (e.g. `"applications"`).
19008
+ * @returns The hosted space URI.
19009
+ */
19010
+ async hostOwnedSpace(name) {
19011
+ if (!this.auth || !this.auth.tinyCloudSession) {
19012
+ throw new Error("Not signed in. Call signIn() first.");
19013
+ }
19014
+ const spaceId = this.ownedSpaceId(name);
19015
+ const host = this.hosts[0] ?? this.config.host;
19016
+ if (!host) {
19017
+ throw new Error("Owned space hosting requires a TinyCloud host");
19018
+ }
19019
+ const hosted = await this.auth.hostOwnedSpace(
19020
+ spaceId
19021
+ );
19022
+ if (!hosted) {
19023
+ throw new Error(`Failed to host owned space: ${spaceId}`);
19024
+ }
19025
+ const activation = await (0, import_sdk_core5.activateSessionWithHost)(
19026
+ host,
19027
+ this.auth.tinyCloudSession.delegationHeader
19028
+ );
19029
+ if (!activation.success || activation.skipped?.includes(spaceId)) {
19030
+ throw new Error(
19031
+ `Failed to activate session for owned space ${spaceId}: ${activation.error ?? "space was skipped"}`
19032
+ );
19033
+ }
19034
+ return spaceId;
19035
+ }
18990
19036
  /**
18991
19037
  * Restore a previously established session from stored delegation data.
18992
19038
  *
@@ -20967,6 +21013,59 @@ var _TinyCloudNode = class _TinyCloudNode {
20967
21013
  actions
20968
21014
  }));
20969
21015
  }
21016
+ /**
21017
+ * Build the abilities/rawAbilities maps for a wallet-mode activation
21018
+ * sub-delegation from the FULL resource set of a received delegation.
21019
+ *
21020
+ * Each entry in `delegation.resources[]` is one `(service, space, path,
21021
+ * actions)` grant; the flat top-level `path`/`actions` mirror only the
21022
+ * first resource. We must reconstruct every grant so the activated
21023
+ * session carries all of them (e.g. both `tinycloud.kv/get` and
21024
+ * `tinycloud.encryption/decrypt`) — not just the primary one.
21025
+ *
21026
+ * Encryption resources are raw network URNs (space-independent) and go
21027
+ * into `rawAbilities`. All other resources are space-scoped and go into
21028
+ * `abilities` keyed by short service name. The activation `prepareSession`
21029
+ * call uses a single `spaceId` (`delegation.spaceId`), so every
21030
+ * non-encryption resource must target that same space — which is exactly
21031
+ * what the multi-resource issuance path enforces. A resource targeting a
21032
+ * different space cannot be activated in one call, so we fail loudly
21033
+ * rather than silently dropping it.
21034
+ *
21035
+ * @internal
21036
+ */
21037
+ buildActivationAbilities(delegation) {
21038
+ var _a, _b, _c;
21039
+ const resources = delegation.resources !== void 0 && delegation.resources.length > 0 ? delegation.resources : this.flatDelegationResources(delegation);
21040
+ const abilities = {};
21041
+ const rawAbilities = {};
21042
+ const addActions = (target, actions) => {
21043
+ const seen = new Set(target);
21044
+ for (const action of actions) {
21045
+ if (!seen.has(action)) {
21046
+ target.push(action);
21047
+ seen.add(action);
21048
+ }
21049
+ }
21050
+ };
21051
+ for (const resource of resources) {
21052
+ const service = this.invocationServiceName(resource.service);
21053
+ if (this.isEncryptionNetworkOperation(service, resource.path)) {
21054
+ rawAbilities[_a = resource.path] ?? (rawAbilities[_a] = []);
21055
+ addActions(rawAbilities[resource.path], resource.actions);
21056
+ continue;
21057
+ }
21058
+ if (resource.space !== delegation.spaceId) {
21059
+ throw new Error(
21060
+ `useDelegation: resource targets space '${resource.space}' but the delegation activates space '${delegation.spaceId}'. Multi-space delegations cannot be activated in a single useDelegation call.`
21061
+ );
21062
+ }
21063
+ abilities[service] ?? (abilities[service] = {});
21064
+ (_b = abilities[service])[_c = resource.path] ?? (_b[_c] = []);
21065
+ addActions(abilities[service][resource.path], resource.actions);
21066
+ }
21067
+ return { abilities, rawAbilities };
21068
+ }
20970
21069
  selectInvocationSession(fallback, service, path, action) {
20971
21070
  const grant = this.findGrantForOperation({
20972
21071
  spaceId: fallback.spaceId,
@@ -21313,26 +21412,7 @@ var _TinyCloudNode = class _TinyCloudNode {
21313
21412
  throw new Error("Not signed in. Call signIn() first.");
21314
21413
  }
21315
21414
  const jwk = mySession.jwk;
21316
- const abilities = {};
21317
- const kvActions = delegation.actions.filter((a) => a.startsWith("tinycloud.kv/"));
21318
- const sqlActions = delegation.actions.filter((a) => a.startsWith("tinycloud.sql/"));
21319
- const duckdbActions = delegation.actions.filter((a) => a.startsWith("tinycloud.duckdb/"));
21320
- const encryptionActions = delegation.actions.filter(
21321
- (a) => a.startsWith("tinycloud.encryption/")
21322
- );
21323
- const rawAbilities = {};
21324
- if (kvActions.length > 0) {
21325
- abilities.kv = { [delegation.path]: kvActions };
21326
- }
21327
- if (sqlActions.length > 0) {
21328
- abilities.sql = { [delegation.path]: sqlActions };
21329
- }
21330
- if (duckdbActions.length > 0) {
21331
- abilities.duckdb = { [delegation.path]: duckdbActions };
21332
- }
21333
- if (encryptionActions.length > 0 && delegation.path.startsWith("urn:tinycloud:encryption:")) {
21334
- rawAbilities[delegation.path] = encryptionActions;
21335
- }
21415
+ const { abilities, rawAbilities } = this.buildActivationAbilities(delegation);
21336
21416
  const now = /* @__PURE__ */ new Date();
21337
21417
  const maxExpiry = new Date(now.getTime() + 60 * 60 * 1e3);
21338
21418
  const expirationTime = delegation.expiry < maxExpiry ? delegation.expiry : maxExpiry;