@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.
- package/dist/delegations/index.cjs +71 -228
- package/dist/delegations/index.cjs.map +1 -1
- package/dist/delegations/index.d.cts +3 -10
- package/dist/delegations/index.d.ts +3 -10
- package/dist/delegations/index.js +71 -228
- package/dist/delegations/index.js.map +1 -1
- package/dist/index.cjs +71 -228
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +71 -228
- package/dist/index.js.map +1 -1
- package/dist/{recipient-types.schema-CPDoAVh4.d.cts → recipient-types.schema-DWLT3lo3.d.cts} +10 -61
- package/dist/{recipient-types.schema-CPDoAVh4.d.ts → recipient-types.schema-DWLT3lo3.d.ts} +10 -61
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -6683,7 +6683,8 @@ var DelegationErrorCodes = {
|
|
|
6683
6683
|
INVALID_TOKEN: "INVALID_TOKEN",
|
|
6684
6684
|
KV_SERVICE_UNAVAILABLE: "KV_SERVICE_UNAVAILABLE",
|
|
6685
6685
|
DATA_FETCH_FAILED: "DATA_FETCH_FAILED",
|
|
6686
|
-
VALIDATION_ERROR: "VALIDATION_ERROR"
|
|
6686
|
+
VALIDATION_ERROR: "VALIDATION_ERROR",
|
|
6687
|
+
UNSUPPORTED_OPERATION: "UNSUPPORTED_OPERATION"
|
|
6687
6688
|
};
|
|
6688
6689
|
var DelegationSchema = import_zod4.z.object({
|
|
6689
6690
|
/** Content identifier (CID) of the delegation */
|
|
@@ -7032,26 +7033,8 @@ var CreateDelegationWasmResultSchema = import_zod4.z.object({
|
|
|
7032
7033
|
resources: import_zod4.z.array(DelegatedResourceSchema)
|
|
7033
7034
|
});
|
|
7034
7035
|
|
|
7035
|
-
// src/expiry.ts
|
|
7036
|
-
var EPHEMERAL_MS = 60 * 60 * 1e3;
|
|
7037
|
-
var SIGNED_READ_URL_MS = 5 * 60 * 1e3;
|
|
7038
|
-
var SESSION_MS = 30 * 24 * 60 * 60 * 1e3;
|
|
7039
|
-
var SHARE_MS = 7 * 24 * 60 * 60 * 1e3;
|
|
7040
|
-
var APP_MS = 30 * 24 * 60 * 60 * 1e3;
|
|
7041
|
-
var MAX_MS = 10 * 365 * 24 * 60 * 60 * 1e3;
|
|
7042
|
-
var EXPIRY = {
|
|
7043
|
-
EPHEMERAL_MS,
|
|
7044
|
-
SIGNED_READ_URL_MS,
|
|
7045
|
-
SESSION_MS,
|
|
7046
|
-
SHARE_MS,
|
|
7047
|
-
APP_MS,
|
|
7048
|
-
MAX_MS
|
|
7049
|
-
};
|
|
7050
|
-
var DEFAULT_SIGNED_READ_URL_EXPIRY_MS = EXPIRY.SIGNED_READ_URL_MS;
|
|
7051
|
-
|
|
7052
7036
|
// src/delegations/DelegationManager.ts
|
|
7053
7037
|
var DelegationAction = {
|
|
7054
|
-
CREATE: "tinycloud.delegation/create",
|
|
7055
7038
|
REVOKE: "tinycloud.delegation/revoke",
|
|
7056
7039
|
STATUS: "tinycloud.delegation/status",
|
|
7057
7040
|
LIST: "tinycloud.delegation/list",
|
|
@@ -7067,6 +7050,21 @@ function createError(code3, message, cause, meta) {
|
|
|
7067
7050
|
meta
|
|
7068
7051
|
};
|
|
7069
7052
|
}
|
|
7053
|
+
async function readInvokedJson(response, action) {
|
|
7054
|
+
const text3 = await response.text();
|
|
7055
|
+
if (text3.trim() === "") {
|
|
7056
|
+
return {
|
|
7057
|
+
ok: false,
|
|
7058
|
+
error: createError(
|
|
7059
|
+
DelegationErrorCodes.UNSUPPORTED_OPERATION,
|
|
7060
|
+
`The node answered ${action} with an empty ${response.status} response; it does not support this operation`,
|
|
7061
|
+
void 0,
|
|
7062
|
+
{ status: response.status, action }
|
|
7063
|
+
)
|
|
7064
|
+
};
|
|
7065
|
+
}
|
|
7066
|
+
return { ok: true, data: JSON.parse(text3) };
|
|
7067
|
+
}
|
|
7070
7068
|
var DelegationManager = class {
|
|
7071
7069
|
/**
|
|
7072
7070
|
* Creates a new DelegationManager instance.
|
|
@@ -7107,114 +7105,6 @@ var DelegationManager = class {
|
|
|
7107
7105
|
body
|
|
7108
7106
|
});
|
|
7109
7107
|
}
|
|
7110
|
-
/**
|
|
7111
|
-
* Creates a new delegation.
|
|
7112
|
-
*
|
|
7113
|
-
* Delegates specific permissions to another DID for a given path.
|
|
7114
|
-
* The delegatee can then use these permissions to access resources
|
|
7115
|
-
* within the specified scope.
|
|
7116
|
-
*
|
|
7117
|
-
* @param params - Parameters for the delegation
|
|
7118
|
-
* @returns Result containing the created Delegation or an error
|
|
7119
|
-
*
|
|
7120
|
-
* @example
|
|
7121
|
-
* ```typescript
|
|
7122
|
-
* const result = await manager.create({
|
|
7123
|
-
* delegateDID: bob.did,
|
|
7124
|
-
* path: "documents/shared/",
|
|
7125
|
-
* actions: ["tinycloud.kv/get", "tinycloud.kv/put"],
|
|
7126
|
-
* expiry: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 days
|
|
7127
|
-
* });
|
|
7128
|
-
* ```
|
|
7129
|
-
*/
|
|
7130
|
-
async create(params) {
|
|
7131
|
-
if (!params.delegateDID) {
|
|
7132
|
-
return {
|
|
7133
|
-
ok: false,
|
|
7134
|
-
error: createError(
|
|
7135
|
-
DelegationErrorCodes.INVALID_INPUT,
|
|
7136
|
-
"delegateDID is required"
|
|
7137
|
-
)
|
|
7138
|
-
};
|
|
7139
|
-
}
|
|
7140
|
-
if (!params.path) {
|
|
7141
|
-
return {
|
|
7142
|
-
ok: false,
|
|
7143
|
-
error: createError(
|
|
7144
|
-
DelegationErrorCodes.INVALID_INPUT,
|
|
7145
|
-
"path is required"
|
|
7146
|
-
)
|
|
7147
|
-
};
|
|
7148
|
-
}
|
|
7149
|
-
if (!params.actions || params.actions.length === 0) {
|
|
7150
|
-
return {
|
|
7151
|
-
ok: false,
|
|
7152
|
-
error: createError(
|
|
7153
|
-
DelegationErrorCodes.INVALID_INPUT,
|
|
7154
|
-
"at least one action is required"
|
|
7155
|
-
)
|
|
7156
|
-
};
|
|
7157
|
-
}
|
|
7158
|
-
try {
|
|
7159
|
-
const body = JSON.stringify({
|
|
7160
|
-
delegateDID: params.delegateDID,
|
|
7161
|
-
path: params.path,
|
|
7162
|
-
actions: params.actions,
|
|
7163
|
-
expiry: params.expiry?.toISOString(),
|
|
7164
|
-
disableSubDelegation: params.disableSubDelegation ?? false,
|
|
7165
|
-
statement: params.statement
|
|
7166
|
-
});
|
|
7167
|
-
const response = await this.invokeOperation(
|
|
7168
|
-
params.path,
|
|
7169
|
-
DelegationAction.CREATE,
|
|
7170
|
-
body
|
|
7171
|
-
);
|
|
7172
|
-
if (!response.ok) {
|
|
7173
|
-
const errorText = await response.text();
|
|
7174
|
-
return {
|
|
7175
|
-
ok: false,
|
|
7176
|
-
error: createError(
|
|
7177
|
-
DelegationErrorCodes.CREATION_FAILED,
|
|
7178
|
-
`Failed to create delegation: ${response.status} - ${errorText}`,
|
|
7179
|
-
void 0,
|
|
7180
|
-
{ status: response.status, path: params.path }
|
|
7181
|
-
)
|
|
7182
|
-
};
|
|
7183
|
-
}
|
|
7184
|
-
const apiResponse = await response.json();
|
|
7185
|
-
const delegation = {
|
|
7186
|
-
cid: apiResponse.cid ?? "",
|
|
7187
|
-
delegateDID: params.delegateDID,
|
|
7188
|
-
spaceId: this.session.spaceId,
|
|
7189
|
-
path: params.path,
|
|
7190
|
-
actions: params.actions,
|
|
7191
|
-
expiry: params.expiry ?? new Date(Date.now() + EXPIRY.SHARE_MS),
|
|
7192
|
-
isRevoked: false,
|
|
7193
|
-
allowSubDelegation: !(params.disableSubDelegation ?? false),
|
|
7194
|
-
createdAt: /* @__PURE__ */ new Date()
|
|
7195
|
-
};
|
|
7196
|
-
return { ok: true, data: delegation };
|
|
7197
|
-
} catch (error) {
|
|
7198
|
-
if (error instanceof Error && error.name === "AbortError") {
|
|
7199
|
-
return {
|
|
7200
|
-
ok: false,
|
|
7201
|
-
error: createError(
|
|
7202
|
-
DelegationErrorCodes.ABORTED,
|
|
7203
|
-
"Request aborted",
|
|
7204
|
-
error
|
|
7205
|
-
)
|
|
7206
|
-
};
|
|
7207
|
-
}
|
|
7208
|
-
return {
|
|
7209
|
-
ok: false,
|
|
7210
|
-
error: createError(
|
|
7211
|
-
DelegationErrorCodes.NETWORK_ERROR,
|
|
7212
|
-
`Network error during delegation creation: ${String(error)}`,
|
|
7213
|
-
error instanceof Error ? error : void 0
|
|
7214
|
-
)
|
|
7215
|
-
};
|
|
7216
|
-
}
|
|
7217
|
-
}
|
|
7218
7108
|
/**
|
|
7219
7109
|
* Revokes an existing delegation.
|
|
7220
7110
|
*
|
|
@@ -7443,19 +7333,33 @@ var DelegationManager = class {
|
|
|
7443
7333
|
};
|
|
7444
7334
|
}
|
|
7445
7335
|
try {
|
|
7446
|
-
|
|
7447
|
-
|
|
7448
|
-
|
|
7449
|
-
|
|
7450
|
-
|
|
7451
|
-
|
|
7452
|
-
|
|
7453
|
-
|
|
7454
|
-
|
|
7455
|
-
|
|
7456
|
-
|
|
7457
|
-
|
|
7458
|
-
|
|
7336
|
+
let response;
|
|
7337
|
+
for (const spaceId of /* @__PURE__ */ new Set([accountSpaceId, this.session.spaceId])) {
|
|
7338
|
+
const headers = Object.fromEntries(new Headers(this.invokeAny(this.session, [{
|
|
7339
|
+
spaceId,
|
|
7340
|
+
service: "delegation",
|
|
7341
|
+
path: "",
|
|
7342
|
+
action: DelegationAction.LIST
|
|
7343
|
+
}])).entries());
|
|
7344
|
+
headers["content-type"] = "application/json";
|
|
7345
|
+
response = await this.fetchFn(`${this.host}/delegation/query`, {
|
|
7346
|
+
method: "POST",
|
|
7347
|
+
headers,
|
|
7348
|
+
body: JSON.stringify(validated.data)
|
|
7349
|
+
});
|
|
7350
|
+
if (response.status !== 403) break;
|
|
7351
|
+
}
|
|
7352
|
+
if (!response || response.status === 403) {
|
|
7353
|
+
return {
|
|
7354
|
+
ok: false,
|
|
7355
|
+
error: createError(
|
|
7356
|
+
DelegationErrorCodes.PERMISSION_DENIED,
|
|
7357
|
+
"This session does not hold tinycloud.delegation/list on the account space or its own space",
|
|
7358
|
+
void 0,
|
|
7359
|
+
{ status: 403 }
|
|
7360
|
+
)
|
|
7361
|
+
};
|
|
7362
|
+
}
|
|
7459
7363
|
if (!response.ok) {
|
|
7460
7364
|
const errorText = await response.text();
|
|
7461
7365
|
return {
|
|
@@ -7490,75 +7394,6 @@ var DelegationManager = class {
|
|
|
7490
7394
|
};
|
|
7491
7395
|
}
|
|
7492
7396
|
}
|
|
7493
|
-
/**
|
|
7494
|
-
* Lists all delegations for the current session's space.
|
|
7495
|
-
*
|
|
7496
|
-
* Returns both delegations created by the current user (as delegator)
|
|
7497
|
-
* and delegations granted to the current user (as delegatee).
|
|
7498
|
-
*
|
|
7499
|
-
* @returns Result containing an array of Delegations or an error
|
|
7500
|
-
*
|
|
7501
|
-
* @example
|
|
7502
|
-
* ```typescript
|
|
7503
|
-
* const result = await manager.list();
|
|
7504
|
-
* if (result.ok) {
|
|
7505
|
-
* for (const delegation of result.data) {
|
|
7506
|
-
* console.log(`${delegation.cid}: ${delegation.path} -> ${delegation.delegateDID}`);
|
|
7507
|
-
* }
|
|
7508
|
-
* }
|
|
7509
|
-
* ```
|
|
7510
|
-
*/
|
|
7511
|
-
async list() {
|
|
7512
|
-
try {
|
|
7513
|
-
const response = await this.invokeOperation("", DelegationAction.LIST);
|
|
7514
|
-
if (!response.ok) {
|
|
7515
|
-
const errorText = await response.text();
|
|
7516
|
-
return {
|
|
7517
|
-
ok: false,
|
|
7518
|
-
error: createError(
|
|
7519
|
-
DelegationErrorCodes.NETWORK_ERROR,
|
|
7520
|
-
`Failed to list delegations: ${response.status} - ${errorText}`,
|
|
7521
|
-
void 0,
|
|
7522
|
-
{ status: response.status }
|
|
7523
|
-
)
|
|
7524
|
-
};
|
|
7525
|
-
}
|
|
7526
|
-
const data = await response.json();
|
|
7527
|
-
const delegations = data.map((item) => ({
|
|
7528
|
-
cid: item.cid,
|
|
7529
|
-
delegateDID: item.delegateDID,
|
|
7530
|
-
delegatorDID: item.delegatorDID,
|
|
7531
|
-
spaceId: item.spaceId,
|
|
7532
|
-
path: item.path,
|
|
7533
|
-
actions: item.actions,
|
|
7534
|
-
expiry: new Date(item.expiry),
|
|
7535
|
-
isRevoked: item.isRevoked,
|
|
7536
|
-
createdAt: item.createdAt ? new Date(item.createdAt) : void 0,
|
|
7537
|
-
parentCid: item.parentCid,
|
|
7538
|
-
allowSubDelegation: item.allowSubDelegation
|
|
7539
|
-
}));
|
|
7540
|
-
return { ok: true, data: delegations };
|
|
7541
|
-
} catch (error) {
|
|
7542
|
-
if (error instanceof Error && error.name === "AbortError") {
|
|
7543
|
-
return {
|
|
7544
|
-
ok: false,
|
|
7545
|
-
error: createError(
|
|
7546
|
-
DelegationErrorCodes.ABORTED,
|
|
7547
|
-
"Request aborted",
|
|
7548
|
-
error
|
|
7549
|
-
)
|
|
7550
|
-
};
|
|
7551
|
-
}
|
|
7552
|
-
return {
|
|
7553
|
-
ok: false,
|
|
7554
|
-
error: createError(
|
|
7555
|
-
DelegationErrorCodes.NETWORK_ERROR,
|
|
7556
|
-
`Network error during delegation list: ${String(error)}`,
|
|
7557
|
-
error instanceof Error ? error : void 0
|
|
7558
|
-
)
|
|
7559
|
-
};
|
|
7560
|
-
}
|
|
7561
|
-
}
|
|
7562
7397
|
/**
|
|
7563
7398
|
* Gets the full delegation chain for a given delegation.
|
|
7564
7399
|
*
|
|
@@ -7617,7 +7452,9 @@ var DelegationManager = class {
|
|
|
7617
7452
|
)
|
|
7618
7453
|
};
|
|
7619
7454
|
}
|
|
7620
|
-
const
|
|
7455
|
+
const parsed = await readInvokedJson(response, DelegationAction.GET);
|
|
7456
|
+
if (!parsed.ok) return parsed;
|
|
7457
|
+
const data = parsed.data;
|
|
7621
7458
|
const chain = data.chain.map((item) => ({
|
|
7622
7459
|
cid: item.cid,
|
|
7623
7460
|
delegateDID: item.delegateDID,
|
|
@@ -7714,8 +7551,9 @@ var DelegationManager = class {
|
|
|
7714
7551
|
)
|
|
7715
7552
|
};
|
|
7716
7553
|
}
|
|
7717
|
-
const
|
|
7718
|
-
|
|
7554
|
+
const body2 = await readInvokedJson(response, DelegationAction.CHECK);
|
|
7555
|
+
if (!body2.ok) return body2;
|
|
7556
|
+
return { ok: true, data: body2.data.allowed };
|
|
7719
7557
|
} catch (error) {
|
|
7720
7558
|
if (error instanceof Error && error.name === "AbortError") {
|
|
7721
7559
|
return {
|
|
@@ -7803,14 +7641,6 @@ var SharingServiceConfigSchema = import_zod5.z.object({
|
|
|
7803
7641
|
(val) => val !== null && typeof val === "object",
|
|
7804
7642
|
{ message: "Expected an ICapabilityKeyRegistry object" }
|
|
7805
7643
|
),
|
|
7806
|
-
/**
|
|
7807
|
-
* Delegation manager for creating delegations.
|
|
7808
|
-
* Required for generate(), optional for receive().
|
|
7809
|
-
*/
|
|
7810
|
-
delegationManager: import_zod5.z.unknown().refine(
|
|
7811
|
-
(val) => val === void 0 || val !== null && typeof val === "object",
|
|
7812
|
-
{ message: "Expected a DelegationManager object or undefined" }
|
|
7813
|
-
).optional(),
|
|
7814
7644
|
/** Factory for creating KV service instances */
|
|
7815
7645
|
createKVService: import_zod5.z.unknown().refine(
|
|
7816
7646
|
(val) => typeof val === "function",
|
|
@@ -11081,6 +10911,23 @@ var ShareRecipientClient = class {
|
|
|
11081
10911
|
}
|
|
11082
10912
|
};
|
|
11083
10913
|
|
|
10914
|
+
// src/expiry.ts
|
|
10915
|
+
var EPHEMERAL_MS = 60 * 60 * 1e3;
|
|
10916
|
+
var SIGNED_READ_URL_MS = 5 * 60 * 1e3;
|
|
10917
|
+
var SESSION_MS = 30 * 24 * 60 * 60 * 1e3;
|
|
10918
|
+
var SHARE_MS = 7 * 24 * 60 * 60 * 1e3;
|
|
10919
|
+
var APP_MS = 30 * 24 * 60 * 60 * 1e3;
|
|
10920
|
+
var MAX_MS = 10 * 365 * 24 * 60 * 60 * 1e3;
|
|
10921
|
+
var EXPIRY = {
|
|
10922
|
+
EPHEMERAL_MS,
|
|
10923
|
+
SIGNED_READ_URL_MS,
|
|
10924
|
+
SESSION_MS,
|
|
10925
|
+
SHARE_MS,
|
|
10926
|
+
APP_MS,
|
|
10927
|
+
MAX_MS
|
|
10928
|
+
};
|
|
10929
|
+
var DEFAULT_SIGNED_READ_URL_EXPIRY_MS = EXPIRY.SIGNED_READ_URL_MS;
|
|
10930
|
+
|
|
11084
10931
|
// src/delegations/SharingService.ts
|
|
11085
10932
|
function spaceIdsEqual(left, right) {
|
|
11086
10933
|
const normalizeOwnerAddress = (space) => {
|
|
@@ -11179,7 +11026,6 @@ var SharingService = class {
|
|
|
11179
11026
|
this.fetchFn = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
11180
11027
|
this.keyProvider = config.keyProvider;
|
|
11181
11028
|
this.registry = config.registry;
|
|
11182
|
-
this.delegationManager = config.delegationManager;
|
|
11183
11029
|
this.createKVService = config.createKVService;
|
|
11184
11030
|
this.baseUrl = (config.baseUrl ?? "").replace(/\/$/, "");
|
|
11185
11031
|
this.createDelegationFn = config.createDelegation;
|
|
@@ -11205,16 +11051,13 @@ var SharingService = class {
|
|
|
11205
11051
|
}
|
|
11206
11052
|
/**
|
|
11207
11053
|
* Updates the service configuration.
|
|
11208
|
-
* Used to add full capabilities (session,
|
|
11054
|
+
* Used to add full capabilities (session, createDelegation, createDelegationWasm) after signIn.
|
|
11209
11055
|
*/
|
|
11210
11056
|
updateConfig(config) {
|
|
11211
11057
|
this.assertActiveFn?.();
|
|
11212
11058
|
if (config.session !== void 0) {
|
|
11213
11059
|
this.session = config.session;
|
|
11214
11060
|
}
|
|
11215
|
-
if (config.delegationManager !== void 0) {
|
|
11216
|
-
this.delegationManager = config.delegationManager;
|
|
11217
|
-
}
|
|
11218
11061
|
if (config.createDelegation !== void 0) {
|
|
11219
11062
|
this.createDelegationFn = config.createDelegation;
|
|
11220
11063
|
}
|
|
@@ -11253,12 +11096,12 @@ var SharingService = class {
|
|
|
11253
11096
|
)
|
|
11254
11097
|
};
|
|
11255
11098
|
}
|
|
11256
|
-
if (!this.createDelegationWasmFn && !this.createDelegationFn
|
|
11099
|
+
if (!this.createDelegationWasmFn && !this.createDelegationFn) {
|
|
11257
11100
|
return {
|
|
11258
11101
|
ok: false,
|
|
11259
11102
|
error: createError2(
|
|
11260
11103
|
DelegationErrorCodes.NOT_INITIALIZED,
|
|
11261
|
-
"
|
|
11104
|
+
"createDelegation or createDelegationWasm function required for generating sharing links."
|
|
11262
11105
|
)
|
|
11263
11106
|
};
|
|
11264
11107
|
}
|
|
@@ -11610,7 +11453,7 @@ var SharingService = class {
|
|
|
11610
11453
|
expiry,
|
|
11611
11454
|
disableSubDelegation: false
|
|
11612
11455
|
};
|
|
11613
|
-
const delegationResult =
|
|
11456
|
+
const delegationResult = await this.createDelegationFn(delegationParams);
|
|
11614
11457
|
const delegationCreationActiveError = this.retiredGraphError();
|
|
11615
11458
|
if (delegationCreationActiveError) return { ok: false, error: delegationCreationActiveError };
|
|
11616
11459
|
if (!delegationResult.ok) {
|