@tinycloud/sdk-core 3.3.0-beta.22 → 3.3.0-beta.23

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.
@@ -185,7 +185,8 @@ var DelegationErrorCodes = {
185
185
  INVALID_TOKEN: "INVALID_TOKEN",
186
186
  KV_SERVICE_UNAVAILABLE: "KV_SERVICE_UNAVAILABLE",
187
187
  DATA_FETCH_FAILED: "DATA_FETCH_FAILED",
188
- VALIDATION_ERROR: "VALIDATION_ERROR"
188
+ VALIDATION_ERROR: "VALIDATION_ERROR",
189
+ UNSUPPORTED_OPERATION: "UNSUPPORTED_OPERATION"
189
190
  };
190
191
  var DelegationSchema = import_zod.z.object({
191
192
  /** Content identifier (CID) of the delegation */
@@ -534,26 +535,8 @@ var CreateDelegationWasmResultSchema = import_zod.z.object({
534
535
  resources: import_zod.z.array(DelegatedResourceSchema)
535
536
  });
536
537
 
537
- // src/expiry.ts
538
- var EPHEMERAL_MS = 60 * 60 * 1e3;
539
- var SIGNED_READ_URL_MS = 5 * 60 * 1e3;
540
- var SESSION_MS = 30 * 24 * 60 * 60 * 1e3;
541
- var SHARE_MS = 7 * 24 * 60 * 60 * 1e3;
542
- var APP_MS = 30 * 24 * 60 * 60 * 1e3;
543
- var MAX_MS = 10 * 365 * 24 * 60 * 60 * 1e3;
544
- var EXPIRY = {
545
- EPHEMERAL_MS,
546
- SIGNED_READ_URL_MS,
547
- SESSION_MS,
548
- SHARE_MS,
549
- APP_MS,
550
- MAX_MS
551
- };
552
- var DEFAULT_SIGNED_READ_URL_EXPIRY_MS = EXPIRY.SIGNED_READ_URL_MS;
553
-
554
538
  // src/delegations/DelegationManager.ts
555
539
  var DelegationAction = {
556
- CREATE: "tinycloud.delegation/create",
557
540
  REVOKE: "tinycloud.delegation/revoke",
558
541
  STATUS: "tinycloud.delegation/status",
559
542
  LIST: "tinycloud.delegation/list",
@@ -569,6 +552,21 @@ function createError(code3, message, cause, meta) {
569
552
  meta
570
553
  };
571
554
  }
555
+ async function readInvokedJson(response, action) {
556
+ const text = await response.text();
557
+ if (text.trim() === "") {
558
+ return {
559
+ ok: false,
560
+ error: createError(
561
+ DelegationErrorCodes.UNSUPPORTED_OPERATION,
562
+ `The node answered ${action} with an empty ${response.status} response; it does not support this operation`,
563
+ void 0,
564
+ { status: response.status, action }
565
+ )
566
+ };
567
+ }
568
+ return { ok: true, data: JSON.parse(text) };
569
+ }
572
570
  var DelegationManager = class {
573
571
  /**
574
572
  * Creates a new DelegationManager instance.
@@ -609,114 +607,6 @@ var DelegationManager = class {
609
607
  body
610
608
  });
611
609
  }
612
- /**
613
- * Creates a new delegation.
614
- *
615
- * Delegates specific permissions to another DID for a given path.
616
- * The delegatee can then use these permissions to access resources
617
- * within the specified scope.
618
- *
619
- * @param params - Parameters for the delegation
620
- * @returns Result containing the created Delegation or an error
621
- *
622
- * @example
623
- * ```typescript
624
- * const result = await manager.create({
625
- * delegateDID: bob.did,
626
- * path: "documents/shared/",
627
- * actions: ["tinycloud.kv/get", "tinycloud.kv/put"],
628
- * expiry: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 days
629
- * });
630
- * ```
631
- */
632
- async create(params) {
633
- if (!params.delegateDID) {
634
- return {
635
- ok: false,
636
- error: createError(
637
- DelegationErrorCodes.INVALID_INPUT,
638
- "delegateDID is required"
639
- )
640
- };
641
- }
642
- if (!params.path) {
643
- return {
644
- ok: false,
645
- error: createError(
646
- DelegationErrorCodes.INVALID_INPUT,
647
- "path is required"
648
- )
649
- };
650
- }
651
- if (!params.actions || params.actions.length === 0) {
652
- return {
653
- ok: false,
654
- error: createError(
655
- DelegationErrorCodes.INVALID_INPUT,
656
- "at least one action is required"
657
- )
658
- };
659
- }
660
- try {
661
- const body = JSON.stringify({
662
- delegateDID: params.delegateDID,
663
- path: params.path,
664
- actions: params.actions,
665
- expiry: params.expiry?.toISOString(),
666
- disableSubDelegation: params.disableSubDelegation ?? false,
667
- statement: params.statement
668
- });
669
- const response = await this.invokeOperation(
670
- params.path,
671
- DelegationAction.CREATE,
672
- body
673
- );
674
- if (!response.ok) {
675
- const errorText = await response.text();
676
- return {
677
- ok: false,
678
- error: createError(
679
- DelegationErrorCodes.CREATION_FAILED,
680
- `Failed to create delegation: ${response.status} - ${errorText}`,
681
- void 0,
682
- { status: response.status, path: params.path }
683
- )
684
- };
685
- }
686
- const apiResponse = await response.json();
687
- const delegation = {
688
- cid: apiResponse.cid ?? "",
689
- delegateDID: params.delegateDID,
690
- spaceId: this.session.spaceId,
691
- path: params.path,
692
- actions: params.actions,
693
- expiry: params.expiry ?? new Date(Date.now() + EXPIRY.SHARE_MS),
694
- isRevoked: false,
695
- allowSubDelegation: !(params.disableSubDelegation ?? false),
696
- createdAt: /* @__PURE__ */ new Date()
697
- };
698
- return { ok: true, data: delegation };
699
- } catch (error) {
700
- if (error instanceof Error && error.name === "AbortError") {
701
- return {
702
- ok: false,
703
- error: createError(
704
- DelegationErrorCodes.ABORTED,
705
- "Request aborted",
706
- error
707
- )
708
- };
709
- }
710
- return {
711
- ok: false,
712
- error: createError(
713
- DelegationErrorCodes.NETWORK_ERROR,
714
- `Network error during delegation creation: ${String(error)}`,
715
- error instanceof Error ? error : void 0
716
- )
717
- };
718
- }
719
- }
720
610
  /**
721
611
  * Revokes an existing delegation.
722
612
  *
@@ -945,19 +835,33 @@ var DelegationManager = class {
945
835
  };
946
836
  }
947
837
  try {
948
- const invocationHeaders = this.invokeAny(this.session, [{
949
- spaceId: accountSpaceId,
950
- service: "delegation",
951
- path: "",
952
- action: DelegationAction.LIST
953
- }]);
954
- const headers = Object.fromEntries(new Headers(invocationHeaders).entries());
955
- headers["content-type"] = "application/json";
956
- const response = await this.fetchFn(`${this.host}/delegation/query`, {
957
- method: "POST",
958
- headers,
959
- body: JSON.stringify(validated.data)
960
- });
838
+ let response;
839
+ for (const spaceId of /* @__PURE__ */ new Set([accountSpaceId, this.session.spaceId])) {
840
+ const headers = Object.fromEntries(new Headers(this.invokeAny(this.session, [{
841
+ spaceId,
842
+ service: "delegation",
843
+ path: "",
844
+ action: DelegationAction.LIST
845
+ }])).entries());
846
+ headers["content-type"] = "application/json";
847
+ response = await this.fetchFn(`${this.host}/delegation/query`, {
848
+ method: "POST",
849
+ headers,
850
+ body: JSON.stringify(validated.data)
851
+ });
852
+ if (response.status !== 403) break;
853
+ }
854
+ if (!response || response.status === 403) {
855
+ return {
856
+ ok: false,
857
+ error: createError(
858
+ DelegationErrorCodes.PERMISSION_DENIED,
859
+ "This session does not hold tinycloud.delegation/list on the account space or its own space",
860
+ void 0,
861
+ { status: 403 }
862
+ )
863
+ };
864
+ }
961
865
  if (!response.ok) {
962
866
  const errorText = await response.text();
963
867
  return {
@@ -992,75 +896,6 @@ var DelegationManager = class {
992
896
  };
993
897
  }
994
898
  }
995
- /**
996
- * Lists all delegations for the current session's space.
997
- *
998
- * Returns both delegations created by the current user (as delegator)
999
- * and delegations granted to the current user (as delegatee).
1000
- *
1001
- * @returns Result containing an array of Delegations or an error
1002
- *
1003
- * @example
1004
- * ```typescript
1005
- * const result = await manager.list();
1006
- * if (result.ok) {
1007
- * for (const delegation of result.data) {
1008
- * console.log(`${delegation.cid}: ${delegation.path} -> ${delegation.delegateDID}`);
1009
- * }
1010
- * }
1011
- * ```
1012
- */
1013
- async list() {
1014
- try {
1015
- const response = await this.invokeOperation("", DelegationAction.LIST);
1016
- if (!response.ok) {
1017
- const errorText = await response.text();
1018
- return {
1019
- ok: false,
1020
- error: createError(
1021
- DelegationErrorCodes.NETWORK_ERROR,
1022
- `Failed to list delegations: ${response.status} - ${errorText}`,
1023
- void 0,
1024
- { status: response.status }
1025
- )
1026
- };
1027
- }
1028
- const data = await response.json();
1029
- const delegations = data.map((item) => ({
1030
- cid: item.cid,
1031
- delegateDID: item.delegateDID,
1032
- delegatorDID: item.delegatorDID,
1033
- spaceId: item.spaceId,
1034
- path: item.path,
1035
- actions: item.actions,
1036
- expiry: new Date(item.expiry),
1037
- isRevoked: item.isRevoked,
1038
- createdAt: item.createdAt ? new Date(item.createdAt) : void 0,
1039
- parentCid: item.parentCid,
1040
- allowSubDelegation: item.allowSubDelegation
1041
- }));
1042
- return { ok: true, data: delegations };
1043
- } catch (error) {
1044
- if (error instanceof Error && error.name === "AbortError") {
1045
- return {
1046
- ok: false,
1047
- error: createError(
1048
- DelegationErrorCodes.ABORTED,
1049
- "Request aborted",
1050
- error
1051
- )
1052
- };
1053
- }
1054
- return {
1055
- ok: false,
1056
- error: createError(
1057
- DelegationErrorCodes.NETWORK_ERROR,
1058
- `Network error during delegation list: ${String(error)}`,
1059
- error instanceof Error ? error : void 0
1060
- )
1061
- };
1062
- }
1063
- }
1064
899
  /**
1065
900
  * Gets the full delegation chain for a given delegation.
1066
901
  *
@@ -1119,7 +954,9 @@ var DelegationManager = class {
1119
954
  )
1120
955
  };
1121
956
  }
1122
- const data = await response.json();
957
+ const parsed = await readInvokedJson(response, DelegationAction.GET);
958
+ if (!parsed.ok) return parsed;
959
+ const data = parsed.data;
1123
960
  const chain = data.chain.map((item) => ({
1124
961
  cid: item.cid,
1125
962
  delegateDID: item.delegateDID,
@@ -1216,8 +1053,9 @@ var DelegationManager = class {
1216
1053
  )
1217
1054
  };
1218
1055
  }
1219
- const data = await response.json();
1220
- return { ok: true, data: data.allowed };
1056
+ const body2 = await readInvokedJson(response, DelegationAction.CHECK);
1057
+ if (!body2.ok) return body2;
1058
+ return { ok: true, data: body2.data.allowed };
1221
1059
  } catch (error) {
1222
1060
  if (error instanceof Error && error.name === "AbortError") {
1223
1061
  return {
@@ -1305,14 +1143,6 @@ var SharingServiceConfigSchema = import_zod2.z.object({
1305
1143
  (val) => val !== null && typeof val === "object",
1306
1144
  { message: "Expected an ICapabilityKeyRegistry object" }
1307
1145
  ),
1308
- /**
1309
- * Delegation manager for creating delegations.
1310
- * Required for generate(), optional for receive().
1311
- */
1312
- delegationManager: import_zod2.z.unknown().refine(
1313
- (val) => val === void 0 || val !== null && typeof val === "object",
1314
- { message: "Expected a DelegationManager object or undefined" }
1315
- ).optional(),
1316
1146
  /** Factory for creating KV service instances */
1317
1147
  createKVService: import_zod2.z.unknown().refine(
1318
1148
  (val) => typeof val === "function",
@@ -5413,6 +5243,23 @@ var ShareRecipientClient = class {
5413
5243
  }
5414
5244
  };
5415
5245
 
5246
+ // src/expiry.ts
5247
+ var EPHEMERAL_MS = 60 * 60 * 1e3;
5248
+ var SIGNED_READ_URL_MS = 5 * 60 * 1e3;
5249
+ var SESSION_MS = 30 * 24 * 60 * 60 * 1e3;
5250
+ var SHARE_MS = 7 * 24 * 60 * 60 * 1e3;
5251
+ var APP_MS = 30 * 24 * 60 * 60 * 1e3;
5252
+ var MAX_MS = 10 * 365 * 24 * 60 * 60 * 1e3;
5253
+ var EXPIRY = {
5254
+ EPHEMERAL_MS,
5255
+ SIGNED_READ_URL_MS,
5256
+ SESSION_MS,
5257
+ SHARE_MS,
5258
+ APP_MS,
5259
+ MAX_MS
5260
+ };
5261
+ var DEFAULT_SIGNED_READ_URL_EXPIRY_MS = EXPIRY.SIGNED_READ_URL_MS;
5262
+
5416
5263
  // src/delegations/SharingService.ts
5417
5264
  function spaceIdsEqual(left, right) {
5418
5265
  const normalizeOwnerAddress = (space) => {
@@ -5511,7 +5358,6 @@ var SharingService = class {
5511
5358
  this.fetchFn = config.fetch ?? globalThis.fetch.bind(globalThis);
5512
5359
  this.keyProvider = config.keyProvider;
5513
5360
  this.registry = config.registry;
5514
- this.delegationManager = config.delegationManager;
5515
5361
  this.createKVService = config.createKVService;
5516
5362
  this.baseUrl = (config.baseUrl ?? "").replace(/\/$/, "");
5517
5363
  this.createDelegationFn = config.createDelegation;
@@ -5537,16 +5383,13 @@ var SharingService = class {
5537
5383
  }
5538
5384
  /**
5539
5385
  * Updates the service configuration.
5540
- * Used to add full capabilities (session, delegationManager, createDelegation, createDelegationWasm) after signIn.
5386
+ * Used to add full capabilities (session, createDelegation, createDelegationWasm) after signIn.
5541
5387
  */
5542
5388
  updateConfig(config) {
5543
5389
  this.assertActiveFn?.();
5544
5390
  if (config.session !== void 0) {
5545
5391
  this.session = config.session;
5546
5392
  }
5547
- if (config.delegationManager !== void 0) {
5548
- this.delegationManager = config.delegationManager;
5549
- }
5550
5393
  if (config.createDelegation !== void 0) {
5551
5394
  this.createDelegationFn = config.createDelegation;
5552
5395
  }
@@ -5585,12 +5428,12 @@ var SharingService = class {
5585
5428
  )
5586
5429
  };
5587
5430
  }
5588
- if (!this.createDelegationWasmFn && !this.createDelegationFn && !this.delegationManager) {
5431
+ if (!this.createDelegationWasmFn && !this.createDelegationFn) {
5589
5432
  return {
5590
5433
  ok: false,
5591
5434
  error: createError2(
5592
5435
  DelegationErrorCodes.NOT_INITIALIZED,
5593
- "DelegationManager, createDelegation, or createDelegationWasm function required for generating sharing links."
5436
+ "createDelegation or createDelegationWasm function required for generating sharing links."
5594
5437
  )
5595
5438
  };
5596
5439
  }
@@ -5942,7 +5785,7 @@ var SharingService = class {
5942
5785
  expiry,
5943
5786
  disableSubDelegation: false
5944
5787
  };
5945
- const delegationResult = this.createDelegationFn ? await this.createDelegationFn(delegationParams) : await this.delegationManager.create(delegationParams);
5788
+ const delegationResult = await this.createDelegationFn(delegationParams);
5946
5789
  const delegationCreationActiveError = this.retiredGraphError();
5947
5790
  if (delegationCreationActiveError) return { ok: false, error: delegationCreationActiveError };
5948
5791
  if (!delegationResult.ok) {