@tinycloud/sdk-core 2.2.0-beta.6 → 2.2.0-beta.9

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
@@ -63,6 +63,7 @@ __export(index_exports, {
63
63
  SERVICE_SHORT_TO_LONG: () => SERVICE_SHORT_TO_LONG,
64
64
  SQLAction: () => import_sdk_services4.SQLAction,
65
65
  SQLService: () => import_sdk_services4.SQLService,
66
+ SecretsService: () => import_sdk_services4.SecretsService,
66
67
  ServiceContext: () => import_sdk_services4.ServiceContext,
67
68
  SessionExpiredError: () => SessionExpiredError,
68
69
  SharingService: () => SharingService,
@@ -74,6 +75,7 @@ __export(index_exports, {
74
75
  SpaceService: () => SpaceService,
75
76
  TinyCloud: () => TinyCloud,
76
77
  UnsupportedFeatureError: () => UnsupportedFeatureError,
78
+ VAULT_PERMISSION_SERVICE: () => VAULT_PERMISSION_SERVICE,
77
79
  VaultHeaders: () => import_sdk_services4.VaultHeaders,
78
80
  VaultPublicSpaceKVActions: () => import_sdk_services4.VaultPublicSpaceKVActions,
79
81
  VersionCheckError: () => VersionCheckError,
@@ -92,6 +94,8 @@ __export(index_exports, {
92
94
  defaultSpaceCreationHandler: () => defaultSpaceCreationHandler,
93
95
  err: () => import_sdk_services4.err,
94
96
  expandActionShortNames: () => expandActionShortNames,
97
+ expandPermissionEntries: () => expandPermissionEntries,
98
+ expandPermissionEntry: () => expandPermissionEntry,
95
99
  fetchLocationRecord: () => fetchLocationRecord,
96
100
  fetchPeerId: () => fetchPeerId,
97
101
  httpUrlToMultiaddr: () => httpUrlToMultiaddr,
@@ -269,6 +273,7 @@ var Space = class {
269
273
  this._id = config.id;
270
274
  this._name = config.name;
271
275
  this._kv = config.createKV(config.id);
276
+ this._vault = config.createVault(config.id);
272
277
  this._delegations = config.createDelegations(config.id);
273
278
  this._sharing = config.createSharing(config.id);
274
279
  this._getInfo = config.getInfo;
@@ -291,6 +296,12 @@ var Space = class {
291
296
  get kv() {
292
297
  return this._kv;
293
298
  }
299
+ /**
300
+ * Data Vault operations scoped to this space.
301
+ */
302
+ get vault() {
303
+ return this._vault;
304
+ }
294
305
  /**
295
306
  * Delegation operations scoped to this space.
296
307
  */
@@ -674,6 +685,8 @@ var SpaceConfigSchema = import_zod4.z.object({
674
685
  name: import_zod4.z.string(),
675
686
  /** Factory function to create a space-scoped KV service */
676
687
  createKV: import_zod4.z.function(),
688
+ /** Factory function to create a space-scoped Data Vault service */
689
+ createVault: import_zod4.z.function(),
677
690
  /** Factory function to create space-scoped delegations */
678
691
  createDelegations: import_zod4.z.function(),
679
692
  /** Factory function to create space-scoped sharing */
@@ -694,6 +707,8 @@ var SpaceServiceConfigSchema = import_zod4.z.object({
694
707
  capabilityRegistry: import_zod4.z.unknown().optional(),
695
708
  /** Factory function to create a space-scoped KV service */
696
709
  createKVService: import_zod4.z.function().optional(),
710
+ /** Factory function to create a space-scoped Data Vault service */
711
+ createVaultService: import_zod4.z.function().optional(),
697
712
  /** User's PKH DID (derived from address or provided explicitly) */
698
713
  userDid: import_zod4.z.string().optional(),
699
714
  /** Optional SharingService for v2 sharing links (client-side) */
@@ -935,6 +950,7 @@ var SpaceService = class {
935
950
  this.fetchFn = config.fetch ?? globalThis.fetch.bind(globalThis);
936
951
  this.capabilityRegistry = config.capabilityRegistry;
937
952
  this.createKVServiceFn = config.createKVService;
953
+ this.createVaultServiceFn = config.createVaultService;
938
954
  this._userDid = config.userDid;
939
955
  this.sharingService = config.sharingService;
940
956
  this.createDelegationFn = config.createDelegation;
@@ -949,6 +965,7 @@ var SpaceService = class {
949
965
  if (config.fetch) this.fetchFn = config.fetch;
950
966
  if (config.capabilityRegistry) this.capabilityRegistry = config.capabilityRegistry;
951
967
  if (config.createKVService) this.createKVServiceFn = config.createKVService;
968
+ if (config.createVaultService) this.createVaultServiceFn = config.createVaultService;
952
969
  if (config.userDid !== void 0) this._userDid = config.userDid;
953
970
  if (config.sharingService) this.sharingService = config.sharingService;
954
971
  if (config.createDelegation) this.createDelegationFn = config.createDelegation;
@@ -1231,6 +1248,7 @@ var SpaceService = class {
1231
1248
  id: spaceId,
1232
1249
  name,
1233
1250
  createKV: this.createSpaceScopedKV.bind(this),
1251
+ createVault: this.createSpaceScopedVault.bind(this),
1234
1252
  createDelegations: this.createSpaceScopedDelegations.bind(this),
1235
1253
  createSharing: this.createSpaceScopedSharing.bind(this),
1236
1254
  getInfo: this.getSpaceInfo.bind(this)
@@ -1360,6 +1378,21 @@ var SpaceService = class {
1360
1378
  }
1361
1379
  });
1362
1380
  }
1381
+ /**
1382
+ * Create a space-scoped Data Vault service.
1383
+ */
1384
+ createSpaceScopedVault(spaceId) {
1385
+ if (this.createVaultServiceFn) {
1386
+ return this.createVaultServiceFn(spaceId);
1387
+ }
1388
+ return new Proxy({}, {
1389
+ get: () => {
1390
+ throw new Error(
1391
+ "Vault service factory not configured. Provide createVaultService in SpaceServiceConfig."
1392
+ );
1393
+ }
1394
+ });
1395
+ }
1363
1396
  /**
1364
1397
  * Create space-scoped delegation operations.
1365
1398
  */
@@ -2765,6 +2798,9 @@ var DEFAULT_MANIFEST_VERSION = 1;
2765
2798
  var DEFAULT_MANIFEST_SPACE = "applications";
2766
2799
  var ACCOUNT_REGISTRY_SPACE = "account";
2767
2800
  var ACCOUNT_REGISTRY_PATH = "applications/";
2801
+ var SECRETS_SPACE = "secrets";
2802
+ var SECRET_NAME_RE = /^[A-Z][A-Z0-9_]*$/;
2803
+ var VAULT_PERMISSION_SERVICE = "tinycloud.vault";
2768
2804
  var SERVICE_SHORT_TO_LONG = Object.freeze({
2769
2805
  kv: "tinycloud.kv",
2770
2806
  sql: "tinycloud.sql",
@@ -2847,6 +2883,20 @@ function expandActionShortNames(service, actions) {
2847
2883
  return `${service}/${a}`;
2848
2884
  });
2849
2885
  }
2886
+ function expandPermissionEntry(entry) {
2887
+ if (entry.service !== VAULT_PERMISSION_SERVICE) {
2888
+ return [
2889
+ {
2890
+ ...entry,
2891
+ actions: expandActionShortNames(entry.service, entry.actions)
2892
+ }
2893
+ ];
2894
+ }
2895
+ return expandVaultPermissionEntry(entry);
2896
+ }
2897
+ function expandPermissionEntries(entries) {
2898
+ return entries.flatMap(expandPermissionEntry);
2899
+ }
2850
2900
  function applyPrefix(prefix, path, skipPrefix) {
2851
2901
  if (skipPrefix) {
2852
2902
  return path;
@@ -2918,8 +2968,39 @@ function validateManifest(input) {
2918
2968
  (p, i) => validatePermissionEntry(p, `permissions[${i}]`)
2919
2969
  );
2920
2970
  }
2971
+ if (m.secrets !== void 0) {
2972
+ validateManifestSecrets(m.secrets);
2973
+ }
2921
2974
  return m;
2922
2975
  }
2976
+ function validateManifestSecrets(secrets) {
2977
+ if (secrets === null || typeof secrets !== "object" || Array.isArray(secrets)) {
2978
+ throw new ManifestValidationError("manifest.secrets must be an object");
2979
+ }
2980
+ for (const [name, spec] of Object.entries(secrets)) {
2981
+ if (!SECRET_NAME_RE.test(name)) {
2982
+ throw new ManifestValidationError(
2983
+ `manifest.secrets.${name} must match ${SECRET_NAME_RE.source}`
2984
+ );
2985
+ }
2986
+ const actions = secretActionsFromSpec(name, spec);
2987
+ if (actions.length === 0) {
2988
+ throw new ManifestValidationError(
2989
+ `manifest.secrets.${name} actions must be non-empty`
2990
+ );
2991
+ }
2992
+ for (const action of actions) {
2993
+ if (typeof action !== "string" || action.length === 0) {
2994
+ throw new ManifestValidationError(
2995
+ `manifest.secrets.${name} actions must be non-empty strings`
2996
+ );
2997
+ }
2998
+ }
2999
+ if (spec !== null && typeof spec === "object" && !Array.isArray(spec) && spec.expiry !== void 0) {
3000
+ parseExpiry(spec.expiry);
3001
+ }
3002
+ }
3003
+ }
2923
3004
  function validatePermissionEntry(p, path) {
2924
3005
  if (p === null || typeof p !== "object") {
2925
3006
  throw new ManifestValidationError(`${path} must be an object`);
@@ -2943,6 +3024,16 @@ function validatePermissionEntry(p, path) {
2943
3024
  `${path}.actions must be a non-empty array`
2944
3025
  );
2945
3026
  }
3027
+ for (const action of entry.actions) {
3028
+ if (typeof action !== "string" || action.length === 0) {
3029
+ throw new ManifestValidationError(
3030
+ `${path}.actions must contain non-empty strings`
3031
+ );
3032
+ }
3033
+ if (entry.service === VAULT_PERMISSION_SERVICE) {
3034
+ vaultActionExpansion(action);
3035
+ }
3036
+ }
2946
3037
  if (entry.expiry !== void 0) {
2947
3038
  parseExpiry(entry.expiry);
2948
3039
  }
@@ -2985,9 +3076,14 @@ function resolveManifest(input) {
2985
3076
  const tier = normalizeDefaults(manifest.defaults);
2986
3077
  const defaultEntries = defaultEntriesForTier(tier);
2987
3078
  const explicitEntries = manifest.permissions ?? [];
2988
- const allEntries = [...defaultEntries, ...explicitEntries];
3079
+ const secretEntries = secretEntriesForManifest(manifest.secrets);
3080
+ const allEntries = [
3081
+ ...defaultEntries,
3082
+ ...explicitEntries,
3083
+ ...secretEntries
3084
+ ];
2989
3085
  const resources = withCapabilitiesReadForSpaces(
2990
- allEntries.map((entry) => resolveEntry(entry, prefix, expiryMs, space))
3086
+ allEntries.flatMap((entry) => resolveEntry(entry, prefix, expiryMs, space))
2991
3087
  );
2992
3088
  const additionalDelegates = manifest.did === void 0 ? [] : [
2993
3089
  {
@@ -3007,25 +3103,177 @@ function resolveManifest(input) {
3007
3103
  additionalDelegates
3008
3104
  };
3009
3105
  }
3106
+ function normalizeSecretActions(actions) {
3107
+ const out = [];
3108
+ const seen = /* @__PURE__ */ new Set();
3109
+ const add = (action) => {
3110
+ if (!seen.has(action)) {
3111
+ out.push(action);
3112
+ seen.add(action);
3113
+ }
3114
+ };
3115
+ for (const action of actions) {
3116
+ if (action === "read") {
3117
+ add("get");
3118
+ continue;
3119
+ }
3120
+ if (action === "write") {
3121
+ add("put");
3122
+ continue;
3123
+ }
3124
+ if (action === "delete") {
3125
+ add("del");
3126
+ continue;
3127
+ }
3128
+ if (action === "get" || action === "put" || action === "del" || action === "list" || action === "metadata") {
3129
+ add(action);
3130
+ continue;
3131
+ }
3132
+ if (action === "tinycloud.kv/get" || action === "tinycloud.kv/put" || action === "tinycloud.kv/del" || action === "tinycloud.kv/list" || action === "tinycloud.kv/metadata") {
3133
+ add(action);
3134
+ continue;
3135
+ }
3136
+ throw new ManifestValidationError(
3137
+ `unknown secret action ${JSON.stringify(action)}; expected read, write, delete, list, or metadata`
3138
+ );
3139
+ }
3140
+ return out;
3141
+ }
3142
+ function secretActionsFromSpec(name, spec) {
3143
+ if (spec === true) {
3144
+ return ["read"];
3145
+ }
3146
+ if (typeof spec === "string") {
3147
+ return [spec];
3148
+ }
3149
+ if (Array.isArray(spec)) {
3150
+ return spec;
3151
+ }
3152
+ if (spec === null || typeof spec !== "object") {
3153
+ throw new ManifestValidationError(
3154
+ `manifest.secrets.${name} must be true, a string action, an actions array, or an object`
3155
+ );
3156
+ }
3157
+ if (spec.actions === void 0) {
3158
+ return ["read"];
3159
+ }
3160
+ if (typeof spec.actions === "string") {
3161
+ return [spec.actions];
3162
+ }
3163
+ if (Array.isArray(spec.actions)) {
3164
+ return spec.actions;
3165
+ }
3166
+ throw new ManifestValidationError(
3167
+ `manifest.secrets.${name}.actions must be a string or array`
3168
+ );
3169
+ }
3170
+ function secretEntriesForManifest(secrets) {
3171
+ if (secrets === void 0) {
3172
+ return [];
3173
+ }
3174
+ const entries = [];
3175
+ for (const [name, spec] of Object.entries(secrets)) {
3176
+ const actions = secretActionsFromSpec(name, spec);
3177
+ const extra = spec !== true && typeof spec === "object" && !Array.isArray(spec) ? spec : {};
3178
+ for (const base of ["keys", "vault"]) {
3179
+ entries.push({
3180
+ service: "tinycloud.kv",
3181
+ space: SECRETS_SPACE,
3182
+ path: `${base}/secrets/${name}`,
3183
+ actions: normalizeSecretActions(actions),
3184
+ skipPrefix: true,
3185
+ ...extra.expiry !== void 0 ? { expiry: extra.expiry } : {},
3186
+ ...extra.description !== void 0 ? { description: extra.description } : {}
3187
+ });
3188
+ }
3189
+ }
3190
+ return entries;
3191
+ }
3010
3192
  function resolveEntry(entry, prefix, _inheritedExpiryMs, inheritedSpace) {
3011
3193
  const resolvedPath = applyPrefix(
3012
3194
  prefix,
3013
3195
  entry.path,
3014
3196
  entry.skipPrefix === true
3015
3197
  );
3016
- const resolvedActions = expandActionShortNames(entry.service, entry.actions);
3017
3198
  const entryExpiryMs = entry.expiry !== void 0 ? parseExpiry(entry.expiry) : void 0;
3018
- return {
3019
- service: entry.service,
3199
+ return expandPermissionEntry({
3200
+ ...entry,
3020
3201
  space: entry.space ?? inheritedSpace,
3021
3202
  path: resolvedPath,
3022
- actions: resolvedActions,
3203
+ skipPrefix: true
3204
+ }).map((expanded) => ({
3205
+ service: expanded.service,
3206
+ space: expanded.space ?? inheritedSpace,
3207
+ path: expanded.path,
3208
+ actions: expanded.actions,
3023
3209
  // Only populate `expiryMs` when the entry had its own expiry override.
3024
3210
  // When absent, callers use the parent (delegation or manifest) expiry
3025
3211
  // which is carried on ResolvedDelegate.expiryMs / ResolvedCapabilities.expiryMs.
3026
3212
  ...entryExpiryMs !== void 0 ? { expiryMs: entryExpiryMs } : {},
3027
3213
  ...entry.description !== void 0 ? { description: entry.description } : {}
3028
- };
3214
+ }));
3215
+ }
3216
+ function expandVaultPermissionEntry(entry) {
3217
+ const byBase = /* @__PURE__ */ new Map();
3218
+ for (const action of entry.actions) {
3219
+ const expansion = vaultActionExpansion(action);
3220
+ for (const base of expansion.bases) {
3221
+ const actions = byBase.get(base) ?? [];
3222
+ if (!actions.includes(expansion.action)) {
3223
+ actions.push(expansion.action);
3224
+ }
3225
+ byBase.set(base, actions);
3226
+ }
3227
+ }
3228
+ return [...byBase.entries()].map(([base, actions]) => ({
3229
+ ...entry,
3230
+ service: "tinycloud.kv",
3231
+ path: vaultKVPath(base, entry.path),
3232
+ actions,
3233
+ skipPrefix: true
3234
+ }));
3235
+ }
3236
+ function vaultActionExpansion(action) {
3237
+ const normalized = normalizeVaultAction(action);
3238
+ if (normalized === "read" || normalized === "get") {
3239
+ return { bases: ["keys", "vault"], action: "tinycloud.kv/get" };
3240
+ }
3241
+ if (normalized === "write" || normalized === "put") {
3242
+ return { bases: ["keys", "vault"], action: "tinycloud.kv/put" };
3243
+ }
3244
+ if (normalized === "delete" || normalized === "del") {
3245
+ return { bases: ["keys", "vault"], action: "tinycloud.kv/del" };
3246
+ }
3247
+ if (normalized === "list") {
3248
+ return { bases: ["vault"], action: "tinycloud.kv/list" };
3249
+ }
3250
+ if (normalized === "head") {
3251
+ return { bases: ["vault"], action: "tinycloud.kv/get" };
3252
+ }
3253
+ if (normalized === "metadata") {
3254
+ return { bases: ["vault"], action: "tinycloud.kv/metadata" };
3255
+ }
3256
+ throw new ManifestValidationError(
3257
+ `unknown vault action ${JSON.stringify(action)}; expected read, write, delete, get, put, del, list, head, or metadata`
3258
+ );
3259
+ }
3260
+ function normalizeVaultAction(action) {
3261
+ if (action.startsWith(`${VAULT_PERMISSION_SERVICE}/`)) {
3262
+ return action.slice(`${VAULT_PERMISSION_SERVICE}/`.length);
3263
+ }
3264
+ if (action.startsWith("tinycloud.kv/")) {
3265
+ return action.slice("tinycloud.kv/".length);
3266
+ }
3267
+ if (action.includes("/")) {
3268
+ throw new ManifestValidationError(
3269
+ `unknown vault action ${JSON.stringify(action)}; expected a tinycloud.vault or tinycloud.kv action`
3270
+ );
3271
+ }
3272
+ return action;
3273
+ }
3274
+ function vaultKVPath(base, path) {
3275
+ const normalized = path.startsWith("/") ? path.slice(1) : path;
3276
+ return `${base}/${normalized}`;
3029
3277
  }
3030
3278
  function cloneResourceCapability(entry) {
3031
3279
  return {
@@ -4883,6 +5131,7 @@ function parseRecapCapabilities(parseWasm, siwe) {
4883
5131
  SERVICE_SHORT_TO_LONG,
4884
5132
  SQLAction,
4885
5133
  SQLService,
5134
+ SecretsService,
4886
5135
  ServiceContext,
4887
5136
  SessionExpiredError,
4888
5137
  SharingService,
@@ -4894,6 +5143,7 @@ function parseRecapCapabilities(parseWasm, siwe) {
4894
5143
  SpaceService,
4895
5144
  TinyCloud,
4896
5145
  UnsupportedFeatureError,
5146
+ VAULT_PERMISSION_SERVICE,
4897
5147
  VaultHeaders,
4898
5148
  VaultPublicSpaceKVActions,
4899
5149
  VersionCheckError,
@@ -4912,6 +5162,8 @@ function parseRecapCapabilities(parseWasm, siwe) {
4912
5162
  defaultSpaceCreationHandler,
4913
5163
  err,
4914
5164
  expandActionShortNames,
5165
+ expandPermissionEntries,
5166
+ expandPermissionEntry,
4915
5167
  fetchLocationRecord,
4916
5168
  fetchPeerId,
4917
5169
  httpUrlToMultiaddr,