@tinycloud/sdk-core 2.3.0 → 2.4.0-beta.10
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 +2216 -1355
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +200 -59
- package/dist/index.d.ts +200 -59
- package/dist/index.js +2212 -1348
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/index.cjs
CHANGED
|
@@ -32,57 +32,58 @@ var index_exports = {};
|
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
ACCOUNT_REGISTRY_PATH: () => ACCOUNT_REGISTRY_PATH,
|
|
34
34
|
ACCOUNT_REGISTRY_SPACE: () => ACCOUNT_REGISTRY_SPACE,
|
|
35
|
+
AccountService: () => AccountService,
|
|
35
36
|
AutoApproveSpaceCreationHandler: () => AutoApproveSpaceCreationHandler,
|
|
36
37
|
CapabilityKeyRegistry: () => CapabilityKeyRegistry,
|
|
37
38
|
CapabilityKeyRegistryErrorCodes: () => CapabilityKeyRegistryErrorCodes,
|
|
38
39
|
ClientSessionSchema: () => ClientSessionSchema,
|
|
39
40
|
CloudLocationResolutionError: () => CloudLocationResolutionError,
|
|
40
|
-
DECRYPT_ACTION: () =>
|
|
41
|
-
DECRYPT_FACT_TYPE: () =>
|
|
42
|
-
DECRYPT_RESULT_TYPE: () =>
|
|
41
|
+
DECRYPT_ACTION: () => import_sdk_services7.DECRYPT_ACTION,
|
|
42
|
+
DECRYPT_FACT_TYPE: () => import_sdk_services7.DECRYPT_FACT_TYPE,
|
|
43
|
+
DECRYPT_RESULT_TYPE: () => import_sdk_services7.DECRYPT_RESULT_TYPE,
|
|
43
44
|
DEFAULT_DEFAULTS: () => DEFAULT_DEFAULTS,
|
|
44
|
-
DEFAULT_ENCRYPTION_ALG: () =>
|
|
45
|
+
DEFAULT_ENCRYPTION_ALG: () => import_sdk_services7.DEFAULT_ENCRYPTION_ALG,
|
|
45
46
|
DEFAULT_EXPIRY: () => DEFAULT_EXPIRY,
|
|
46
|
-
DEFAULT_KEY_VERSION: () =>
|
|
47
|
+
DEFAULT_KEY_VERSION: () => import_sdk_services7.DEFAULT_KEY_VERSION,
|
|
47
48
|
DEFAULT_MANIFEST_SPACE: () => DEFAULT_MANIFEST_SPACE,
|
|
48
49
|
DEFAULT_MANIFEST_VERSION: () => DEFAULT_MANIFEST_VERSION,
|
|
49
50
|
DEFAULT_SIGNED_READ_URL_EXPIRY_MS: () => DEFAULT_SIGNED_READ_URL_EXPIRY_MS,
|
|
50
51
|
DEFAULT_TINYCLOUD_FALLBACK_HOST: () => DEFAULT_TINYCLOUD_FALLBACK_HOST,
|
|
51
52
|
DEFAULT_TINYCLOUD_LOCATION_REGISTRY_URL: () => DEFAULT_TINYCLOUD_LOCATION_REGISTRY_URL,
|
|
52
|
-
DataVaultService: () =>
|
|
53
|
-
DatabaseHandle: () =>
|
|
53
|
+
DataVaultService: () => import_sdk_services7.DataVaultService,
|
|
54
|
+
DatabaseHandle: () => import_sdk_services7.DatabaseHandle,
|
|
54
55
|
DelegationErrorCodes: () => DelegationErrorCodes,
|
|
55
56
|
DelegationManager: () => DelegationManager,
|
|
56
|
-
DuckDbAction: () =>
|
|
57
|
-
DuckDbDatabaseHandle: () =>
|
|
58
|
-
DuckDbService: () =>
|
|
57
|
+
DuckDbAction: () => import_sdk_services7.DuckDbAction,
|
|
58
|
+
DuckDbDatabaseHandle: () => import_sdk_services7.DuckDbDatabaseHandle,
|
|
59
|
+
DuckDbService: () => import_sdk_services7.DuckDbService,
|
|
59
60
|
ENCRYPTION_MANIFEST_SPACE: () => ENCRYPTION_MANIFEST_SPACE,
|
|
60
|
-
ENCRYPTION_NETWORK_URN_PREFIX: () =>
|
|
61
|
+
ENCRYPTION_NETWORK_URN_PREFIX: () => import_sdk_services7.ENCRYPTION_NETWORK_URN_PREFIX,
|
|
61
62
|
ENCRYPTION_PERMISSION_SERVICE: () => ENCRYPTION_PERMISSION_SERVICE,
|
|
62
|
-
ENCRYPTION_SERVICE: () =>
|
|
63
|
-
ENCRYPTION_SERVICE_SHORT: () =>
|
|
64
|
-
ENVELOPE_VERSION: () =>
|
|
63
|
+
ENCRYPTION_SERVICE: () => import_sdk_services7.ENCRYPTION_SERVICE,
|
|
64
|
+
ENCRYPTION_SERVICE_SHORT: () => import_sdk_services7.ENCRYPTION_SERVICE_SHORT,
|
|
65
|
+
ENVELOPE_VERSION: () => import_sdk_services7.ENVELOPE_VERSION,
|
|
65
66
|
EXPIRY: () => EXPIRY,
|
|
66
|
-
EncryptionService: () =>
|
|
67
|
+
EncryptionService: () => import_sdk_services7.EncryptionService,
|
|
67
68
|
EnsDataSchema: () => EnsDataSchema,
|
|
68
|
-
ErrorCodes: () =>
|
|
69
|
-
HooksService: () =>
|
|
69
|
+
ErrorCodes: () => import_sdk_services7.ErrorCodes,
|
|
70
|
+
HooksService: () => import_sdk_services7.HooksService,
|
|
70
71
|
IdentityParseError: () => IdentityParseError,
|
|
71
|
-
KVService: () =>
|
|
72
|
+
KVService: () => import_sdk_services7.KVService,
|
|
72
73
|
LocationRecordValidationError: () => LocationRecordValidationError,
|
|
73
74
|
ManifestValidationError: () => ManifestValidationError,
|
|
74
|
-
NETWORK_NAME_PATTERN: () =>
|
|
75
|
-
NetworkIdError: () =>
|
|
75
|
+
NETWORK_NAME_PATTERN: () => import_sdk_services7.NETWORK_NAME_PATTERN,
|
|
76
|
+
NetworkIdError: () => import_sdk_services7.NetworkIdError,
|
|
76
77
|
PermissionNotInManifestError: () => PermissionNotInManifestError,
|
|
77
|
-
PrefixedKVService: () =>
|
|
78
|
+
PrefixedKVService: () => import_sdk_services7.PrefixedKVService,
|
|
78
79
|
ProtocolMismatchError: () => ProtocolMismatchError,
|
|
79
|
-
SECRET_NAME_RE: () =>
|
|
80
|
+
SECRET_NAME_RE: () => import_sdk_services7.SECRET_NAME_RE,
|
|
80
81
|
SERVICE_LONG_TO_SHORT: () => SERVICE_LONG_TO_SHORT,
|
|
81
82
|
SERVICE_SHORT_TO_LONG: () => SERVICE_SHORT_TO_LONG,
|
|
82
|
-
SQLAction: () =>
|
|
83
|
-
SQLService: () =>
|
|
84
|
-
SecretsService: () =>
|
|
85
|
-
ServiceContext: () =>
|
|
83
|
+
SQLAction: () => import_sdk_services7.SQLAction,
|
|
84
|
+
SQLService: () => import_sdk_services7.SQLService,
|
|
85
|
+
SecretsService: () => import_sdk_services7.SecretsService,
|
|
86
|
+
ServiceContext: () => import_sdk_services7.ServiceContext,
|
|
86
87
|
SessionExpiredError: () => SessionExpiredError,
|
|
87
88
|
SharingService: () => SharingService,
|
|
88
89
|
SilentNotificationHandler: () => SilentNotificationHandler,
|
|
@@ -94,75 +95,75 @@ __export(index_exports, {
|
|
|
94
95
|
TinyCloud: () => TinyCloud,
|
|
95
96
|
UnsupportedFeatureError: () => UnsupportedFeatureError,
|
|
96
97
|
VAULT_PERMISSION_SERVICE: () => VAULT_PERMISSION_SERVICE,
|
|
97
|
-
VaultHeaders: () =>
|
|
98
|
-
VaultPublicSpaceKVActions: () =>
|
|
98
|
+
VaultHeaders: () => import_sdk_services7.VaultHeaders,
|
|
99
|
+
VaultPublicSpaceKVActions: () => import_sdk_services7.VaultPublicSpaceKVActions,
|
|
99
100
|
VersionCheckError: () => VersionCheckError,
|
|
100
101
|
activateSessionWithHost: () => activateSessionWithHost,
|
|
101
102
|
addressStorageKey: () => addressStorageKey,
|
|
102
103
|
applyPrefix: () => applyPrefix,
|
|
103
|
-
buildCanonicalDecryptRequest: () =>
|
|
104
|
-
buildDecryptAttenuation: () =>
|
|
105
|
-
buildDecryptFacts: () =>
|
|
106
|
-
buildDecryptInvocation: () =>
|
|
107
|
-
buildNetworkId: () =>
|
|
104
|
+
buildCanonicalDecryptRequest: () => import_sdk_services7.buildCanonicalDecryptRequest,
|
|
105
|
+
buildDecryptAttenuation: () => import_sdk_services7.buildDecryptAttenuation,
|
|
106
|
+
buildDecryptFacts: () => import_sdk_services7.buildDecryptFacts,
|
|
107
|
+
buildDecryptInvocation: () => import_sdk_services7.buildDecryptInvocation,
|
|
108
|
+
buildNetworkId: () => import_sdk_services7.buildNetworkId,
|
|
108
109
|
buildSpaceUri: () => buildSpaceUri,
|
|
109
|
-
canonicalHashHex: () =>
|
|
110
|
+
canonicalHashHex: () => import_sdk_services7.canonicalHashHex,
|
|
110
111
|
canonicalLocationPayload: () => canonicalLocationPayload,
|
|
111
|
-
canonicalSignedResponse: () =>
|
|
112
|
+
canonicalSignedResponse: () => import_sdk_services7.canonicalSignedResponse,
|
|
112
113
|
canonicalizeAddress: () => canonicalizeAddress,
|
|
113
114
|
canonicalizeDid: () => canonicalizeDid,
|
|
114
115
|
canonicalizeDidUrl: () => canonicalizeDidUrl,
|
|
115
|
-
canonicalizeEncryptionJson: () =>
|
|
116
|
+
canonicalizeEncryptionJson: () => import_sdk_services7.canonicalizeEncryptionJson,
|
|
116
117
|
canonicalizeNetworkId: () => canonicalizeNetworkId,
|
|
117
|
-
canonicalizeSecretScope: () =>
|
|
118
|
-
checkDecryptInvocationInput: () =>
|
|
118
|
+
canonicalizeSecretScope: () => import_sdk_services7.canonicalizeSecretScope,
|
|
119
|
+
checkDecryptInvocationInput: () => import_sdk_services7.checkDecryptInvocationInput,
|
|
119
120
|
checkNodeInfo: () => checkNodeInfo,
|
|
120
121
|
composeManifestRequest: () => composeManifestRequest,
|
|
121
122
|
createCapabilityKeyRegistry: () => createCapabilityKeyRegistry,
|
|
122
123
|
createSharingService: () => createSharingService,
|
|
123
124
|
createSpaceService: () => createSpaceService,
|
|
124
|
-
createVaultCrypto: () =>
|
|
125
|
-
decryptEnvelopeWithKey: () =>
|
|
126
|
-
defaultRetryPolicy: () =>
|
|
125
|
+
createVaultCrypto: () => import_sdk_services7.createVaultCrypto,
|
|
126
|
+
decryptEnvelopeWithKey: () => import_sdk_services7.decryptEnvelopeWithKey,
|
|
127
|
+
defaultRetryPolicy: () => import_sdk_services7.defaultRetryPolicy,
|
|
127
128
|
defaultSignStrategy: () => defaultSignStrategy,
|
|
128
129
|
defaultSpaceCreationHandler: () => defaultSpaceCreationHandler,
|
|
129
|
-
deriveSignedReceiverKey: () =>
|
|
130
|
+
deriveSignedReceiverKey: () => import_sdk_services7.deriveSignedReceiverKey,
|
|
130
131
|
didCacheKey: () => didCacheKey,
|
|
131
132
|
didEquals: () => didEquals,
|
|
132
|
-
discoverNetwork: () =>
|
|
133
|
-
encryptToNetwork: () =>
|
|
134
|
-
encryptionBase64Decode: () =>
|
|
135
|
-
encryptionBase64Encode: () =>
|
|
136
|
-
encryptionError: () =>
|
|
137
|
-
encryptionUtf8Decode: () =>
|
|
138
|
-
encryptionUtf8Encode: () =>
|
|
139
|
-
ensureNetworkUsableForDecrypt: () =>
|
|
140
|
-
err: () =>
|
|
133
|
+
discoverNetwork: () => import_sdk_services7.discoverNetwork,
|
|
134
|
+
encryptToNetwork: () => import_sdk_services7.encryptToNetwork,
|
|
135
|
+
encryptionBase64Decode: () => import_sdk_services7.base64Decode,
|
|
136
|
+
encryptionBase64Encode: () => import_sdk_services7.base64Encode,
|
|
137
|
+
encryptionError: () => import_sdk_services7.encryptionError,
|
|
138
|
+
encryptionUtf8Decode: () => import_sdk_services7.utf8Decode,
|
|
139
|
+
encryptionUtf8Encode: () => import_sdk_services7.utf8Encode,
|
|
140
|
+
ensureNetworkUsableForDecrypt: () => import_sdk_services7.ensureNetworkUsableForDecrypt,
|
|
141
|
+
err: () => import_sdk_services7.err,
|
|
141
142
|
expandActionShortNames: () => expandActionShortNames,
|
|
142
143
|
expandPermissionEntries: () => expandPermissionEntries,
|
|
143
144
|
expandPermissionEntry: () => expandPermissionEntry,
|
|
144
145
|
fetchLocationRecord: () => fetchLocationRecord,
|
|
145
146
|
fetchPeerId: () => fetchPeerId,
|
|
146
|
-
generateRandomReceiverKey: () =>
|
|
147
|
-
hexDecode: () =>
|
|
148
|
-
hexEncode: () =>
|
|
147
|
+
generateRandomReceiverKey: () => import_sdk_services7.generateRandomReceiverKey,
|
|
148
|
+
hexDecode: () => import_sdk_services7.hexDecode,
|
|
149
|
+
hexEncode: () => import_sdk_services7.hexEncode,
|
|
149
150
|
httpUrlToMultiaddr: () => httpUrlToMultiaddr,
|
|
150
151
|
isCapabilitySubset: () => isCapabilitySubset,
|
|
151
152
|
isEvmAddress: () => isEvmAddress,
|
|
152
|
-
isNetworkId: () =>
|
|
153
|
+
isNetworkId: () => import_sdk_services7.isNetworkId,
|
|
153
154
|
loadManifest: () => loadManifest,
|
|
154
155
|
locationPayloadForRecord: () => locationPayloadForRecord,
|
|
155
156
|
makePkhSpaceId: () => makePkhSpaceId,
|
|
156
157
|
makePublicSpaceId: () => makePublicSpaceId,
|
|
157
158
|
manifestAbilitiesUnion: () => manifestAbilitiesUnion,
|
|
158
159
|
multiaddrToHttpUrl: () => multiaddrToHttpUrl,
|
|
159
|
-
networkDiscoveryKey: () =>
|
|
160
|
+
networkDiscoveryKey: () => import_sdk_services7.networkDiscoveryKey,
|
|
160
161
|
normalizeDefaults: () => normalizeDefaults,
|
|
161
|
-
ok: () =>
|
|
162
|
-
openWrappedKey: () =>
|
|
162
|
+
ok: () => import_sdk_services7.ok,
|
|
163
|
+
openWrappedKey: () => import_sdk_services7.openWrappedKey,
|
|
163
164
|
parseCanonicalNetworkId: () => parseCanonicalNetworkId,
|
|
164
165
|
parseExpiry: () => parseExpiry,
|
|
165
|
-
parseNetworkId: () =>
|
|
166
|
+
parseNetworkId: () => import_sdk_services7.parseNetworkId,
|
|
166
167
|
parsePkhDid: () => parsePkhDid,
|
|
167
168
|
parseRecapCapabilities: () => parseRecapCapabilities,
|
|
168
169
|
parseSpaceUri: () => parseSpaceUri,
|
|
@@ -171,21 +172,21 @@ __export(index_exports, {
|
|
|
171
172
|
principalDidEquals: () => principalDidEquals,
|
|
172
173
|
resolveCloudLocation: () => resolveCloudLocation,
|
|
173
174
|
resolveManifest: () => resolveManifest,
|
|
174
|
-
resolveSecretListPrefix: () =>
|
|
175
|
-
resolveSecretPath: () =>
|
|
175
|
+
resolveSecretListPrefix: () => import_sdk_services7.resolveSecretListPrefix,
|
|
176
|
+
resolveSecretPath: () => import_sdk_services7.resolveSecretPath,
|
|
176
177
|
resolveTinyCloudHosts: () => resolveTinyCloudHosts,
|
|
177
178
|
resourceCapabilitiesToAbilitiesMap: () => resourceCapabilitiesToAbilitiesMap,
|
|
178
179
|
resourceCapabilitiesToSpaceAbilitiesMap: () => resourceCapabilitiesToSpaceAbilitiesMap,
|
|
179
|
-
serviceError: () =>
|
|
180
|
+
serviceError: () => import_sdk_services7.serviceError,
|
|
180
181
|
signLocationRecord: () => signLocationRecord,
|
|
181
182
|
submitHostDelegation: () => submitHostDelegation,
|
|
182
183
|
validateClientSession: () => validateClientSession,
|
|
183
|
-
validateEnvelope: () =>
|
|
184
|
+
validateEnvelope: () => import_sdk_services7.validateEnvelope,
|
|
184
185
|
validateLocationRecord: () => validateLocationRecord,
|
|
185
186
|
validateLocationRecordPayload: () => validateLocationRecordPayload,
|
|
186
187
|
validateManifest: () => validateManifest,
|
|
187
188
|
validatePersistedSessionData: () => validatePersistedSessionData,
|
|
188
|
-
verifyDecryptResponse: () =>
|
|
189
|
+
verifyDecryptResponse: () => import_sdk_services7.verifyDecryptResponse,
|
|
189
190
|
verifyDidKeyEd25519Signature: () => verifyDidKeyEd25519Signature,
|
|
190
191
|
verifyLocationRecord: () => verifyLocationRecord
|
|
191
192
|
});
|
|
@@ -1150,6 +1151,7 @@ var SpaceService = class {
|
|
|
1150
1151
|
this._userDid = config.userDid;
|
|
1151
1152
|
this.sharingService = config.sharingService;
|
|
1152
1153
|
this.createDelegationFn = config.createDelegation;
|
|
1154
|
+
this.onSpaceRegisteredFn = config.onSpaceRegistered;
|
|
1153
1155
|
}
|
|
1154
1156
|
/**
|
|
1155
1157
|
* Update the service configuration.
|
|
@@ -1165,6 +1167,7 @@ var SpaceService = class {
|
|
|
1165
1167
|
if (config.userDid !== void 0) this._userDid = config.userDid;
|
|
1166
1168
|
if (config.sharingService) this.sharingService = config.sharingService;
|
|
1167
1169
|
if (config.createDelegation) this.createDelegationFn = config.createDelegation;
|
|
1170
|
+
if (config.onSpaceRegistered) this.onSpaceRegisteredFn = config.onSpaceRegistered;
|
|
1168
1171
|
this.spaceCache.clear();
|
|
1169
1172
|
this.infoCache.clear();
|
|
1170
1173
|
}
|
|
@@ -1215,6 +1218,9 @@ var SpaceService = class {
|
|
|
1215
1218
|
spaces.push(...delegatedSpaces);
|
|
1216
1219
|
}
|
|
1217
1220
|
const uniqueSpaces = this.deduplicateSpaces(spaces);
|
|
1221
|
+
for (const space of uniqueSpaces) {
|
|
1222
|
+
this.notifySpaceRegistered(space);
|
|
1223
|
+
}
|
|
1218
1224
|
return (0, import_sdk_services2.ok)(uniqueSpaces);
|
|
1219
1225
|
} catch (error) {
|
|
1220
1226
|
return (0, import_sdk_services2.err)(
|
|
@@ -1412,6 +1418,7 @@ var SpaceService = class {
|
|
|
1412
1418
|
permissions: ["*"]
|
|
1413
1419
|
};
|
|
1414
1420
|
this.infoCache.set(spaceInfo.id, { info: spaceInfo, cachedAt: Date.now() });
|
|
1421
|
+
this.notifySpaceRegistered(spaceInfo);
|
|
1415
1422
|
return (0, import_sdk_services2.ok)(spaceInfo);
|
|
1416
1423
|
} catch (error) {
|
|
1417
1424
|
return (0, import_sdk_services2.err)(
|
|
@@ -1424,6 +1431,11 @@ var SpaceService = class {
|
|
|
1424
1431
|
);
|
|
1425
1432
|
}
|
|
1426
1433
|
}
|
|
1434
|
+
notifySpaceRegistered(space) {
|
|
1435
|
+
if (!this.onSpaceRegisteredFn) return;
|
|
1436
|
+
void Promise.resolve(this.onSpaceRegisteredFn(space)).catch(() => {
|
|
1437
|
+
});
|
|
1438
|
+
}
|
|
1427
1439
|
// ===========================================================================
|
|
1428
1440
|
// Get Space
|
|
1429
1441
|
// ===========================================================================
|
|
@@ -2331,1409 +2343,2257 @@ var TinyCloud = class _TinyCloud {
|
|
|
2331
2343
|
}
|
|
2332
2344
|
};
|
|
2333
2345
|
|
|
2334
|
-
// src/
|
|
2335
|
-
var
|
|
2346
|
+
// src/account/AccountService.ts
|
|
2347
|
+
var import_sdk_services5 = require("@tinycloud/sdk-services");
|
|
2336
2348
|
|
|
2337
|
-
// src/
|
|
2338
|
-
|
|
2339
|
-
|
|
2340
|
-
|
|
2341
|
-
)
|
|
2342
|
-
|
|
2343
|
-
|
|
2344
|
-
throw new Error(`Failed to get peer ID: ${res.status} - ${error}`);
|
|
2349
|
+
// src/manifest.ts
|
|
2350
|
+
var import_ms = __toESM(require("ms"), 1);
|
|
2351
|
+
var import_sdk_services4 = require("@tinycloud/sdk-services");
|
|
2352
|
+
var ManifestValidationError = class extends Error {
|
|
2353
|
+
constructor(message) {
|
|
2354
|
+
super(`Manifest validation failed: ${message}`);
|
|
2355
|
+
this.name = "ManifestValidationError";
|
|
2345
2356
|
}
|
|
2346
|
-
|
|
2357
|
+
};
|
|
2358
|
+
var DEFAULT_EXPIRY = "30d";
|
|
2359
|
+
var DEFAULT_DEFAULTS = true;
|
|
2360
|
+
var DEFAULT_MANIFEST_VERSION = 1;
|
|
2361
|
+
var DEFAULT_MANIFEST_SPACE = "applications";
|
|
2362
|
+
var ACCOUNT_REGISTRY_SPACE = "account";
|
|
2363
|
+
var ACCOUNT_REGISTRY_PATH = "applications/";
|
|
2364
|
+
var SECRETS_SPACE = "secrets";
|
|
2365
|
+
var VAULT_PERMISSION_SERVICE = "tinycloud.vault";
|
|
2366
|
+
var SERVICE_SHORT_TO_LONG = Object.freeze({
|
|
2367
|
+
kv: "tinycloud.kv",
|
|
2368
|
+
sql: "tinycloud.sql",
|
|
2369
|
+
duckdb: "tinycloud.duckdb",
|
|
2370
|
+
capabilities: "tinycloud.capabilities",
|
|
2371
|
+
hooks: "tinycloud.hooks",
|
|
2372
|
+
encryption: "tinycloud.encryption"
|
|
2373
|
+
});
|
|
2374
|
+
var ENCRYPTION_PERMISSION_SERVICE = "tinycloud.encryption";
|
|
2375
|
+
var ENCRYPTION_MANIFEST_SPACE = "encryption";
|
|
2376
|
+
var SERVICE_LONG_TO_SHORT = Object.freeze(
|
|
2377
|
+
Object.fromEntries(
|
|
2378
|
+
Object.entries(SERVICE_SHORT_TO_LONG).map(([s, l]) => [l, s])
|
|
2379
|
+
)
|
|
2380
|
+
);
|
|
2381
|
+
var DEFAULT_STANDARD_ENTRIES = [
|
|
2382
|
+
{
|
|
2383
|
+
service: "tinycloud.kv",
|
|
2384
|
+
space: DEFAULT_MANIFEST_SPACE,
|
|
2385
|
+
path: "/",
|
|
2386
|
+
actions: ["get", "put", "del", "list", "metadata"]
|
|
2387
|
+
},
|
|
2388
|
+
{
|
|
2389
|
+
service: "tinycloud.sql",
|
|
2390
|
+
space: DEFAULT_MANIFEST_SPACE,
|
|
2391
|
+
path: "/",
|
|
2392
|
+
actions: ["read", "write"]
|
|
2393
|
+
}
|
|
2394
|
+
];
|
|
2395
|
+
var DEFAULT_ADMIN_ENTRIES = [
|
|
2396
|
+
{
|
|
2397
|
+
service: "tinycloud.kv",
|
|
2398
|
+
space: DEFAULT_MANIFEST_SPACE,
|
|
2399
|
+
path: "/",
|
|
2400
|
+
actions: ["get", "put", "del", "list", "metadata"]
|
|
2401
|
+
},
|
|
2402
|
+
{
|
|
2403
|
+
service: "tinycloud.sql",
|
|
2404
|
+
space: DEFAULT_MANIFEST_SPACE,
|
|
2405
|
+
path: "/",
|
|
2406
|
+
actions: ["read", "write", "ddl"]
|
|
2407
|
+
}
|
|
2408
|
+
];
|
|
2409
|
+
var DEFAULT_ALL_ENTRIES = [
|
|
2410
|
+
{
|
|
2411
|
+
service: "tinycloud.kv",
|
|
2412
|
+
space: DEFAULT_MANIFEST_SPACE,
|
|
2413
|
+
path: "/",
|
|
2414
|
+
actions: ["get", "put", "del", "list", "metadata"]
|
|
2415
|
+
},
|
|
2416
|
+
{
|
|
2417
|
+
service: "tinycloud.sql",
|
|
2418
|
+
space: DEFAULT_MANIFEST_SPACE,
|
|
2419
|
+
path: "/",
|
|
2420
|
+
actions: ["read", "write", "ddl"]
|
|
2421
|
+
},
|
|
2422
|
+
{
|
|
2423
|
+
service: "tinycloud.duckdb",
|
|
2424
|
+
space: DEFAULT_MANIFEST_SPACE,
|
|
2425
|
+
path: "/",
|
|
2426
|
+
actions: ["read", "write"]
|
|
2427
|
+
}
|
|
2428
|
+
];
|
|
2429
|
+
function parseExpiry(duration) {
|
|
2430
|
+
if (typeof duration !== "string" || duration.length === 0) {
|
|
2431
|
+
throw new ManifestValidationError(
|
|
2432
|
+
`expiry must be a non-empty duration string (got ${JSON.stringify(duration)})`
|
|
2433
|
+
);
|
|
2434
|
+
}
|
|
2435
|
+
const parsed = (0, import_ms.default)(duration);
|
|
2436
|
+
if (typeof parsed !== "number" || !Number.isFinite(parsed) || parsed <= 0) {
|
|
2437
|
+
throw new ManifestValidationError(
|
|
2438
|
+
`invalid expiry duration: ${JSON.stringify(duration)}`
|
|
2439
|
+
);
|
|
2440
|
+
}
|
|
2441
|
+
return parsed;
|
|
2347
2442
|
}
|
|
2348
|
-
|
|
2349
|
-
|
|
2350
|
-
|
|
2351
|
-
|
|
2443
|
+
function expandActionShortNames(service, actions) {
|
|
2444
|
+
return actions.map((a) => {
|
|
2445
|
+
if (a.includes("/")) {
|
|
2446
|
+
return a;
|
|
2447
|
+
}
|
|
2448
|
+
return `${service}/${a}`;
|
|
2352
2449
|
});
|
|
2353
|
-
return {
|
|
2354
|
-
success: res.ok,
|
|
2355
|
-
status: res.status,
|
|
2356
|
-
error: res.ok ? void 0 : await res.text().catch(() => res.statusText)
|
|
2357
|
-
};
|
|
2358
2450
|
}
|
|
2359
|
-
|
|
2360
|
-
|
|
2361
|
-
|
|
2362
|
-
|
|
2363
|
-
|
|
2364
|
-
|
|
2365
|
-
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2451
|
+
function expandPermissionEntry(entry) {
|
|
2452
|
+
if (entry.service === ENCRYPTION_PERMISSION_SERVICE) {
|
|
2453
|
+
return expandEncryptionPermissionEntry(entry);
|
|
2454
|
+
}
|
|
2455
|
+
if (entry.service !== VAULT_PERMISSION_SERVICE) {
|
|
2456
|
+
return [
|
|
2457
|
+
{
|
|
2458
|
+
...entry,
|
|
2459
|
+
actions: expandActionShortNames(entry.service, entry.actions)
|
|
2460
|
+
}
|
|
2461
|
+
];
|
|
2462
|
+
}
|
|
2463
|
+
return expandVaultPermissionEntry(entry);
|
|
2464
|
+
}
|
|
2465
|
+
function expandEncryptionPermissionEntry(entry) {
|
|
2466
|
+
if (typeof entry.path !== "string" || !entry.path.startsWith("urn:tinycloud:encryption:")) {
|
|
2467
|
+
throw new ManifestValidationError(
|
|
2468
|
+
`tinycloud.encryption entries require path to be a networkId URN (got ${JSON.stringify(entry.path)})`
|
|
2469
|
+
);
|
|
2470
|
+
}
|
|
2471
|
+
const normalizedActions = [];
|
|
2472
|
+
for (const action of entry.actions) {
|
|
2473
|
+
if (action === "decrypt" || action === "tinycloud.encryption/decrypt") {
|
|
2474
|
+
normalizedActions.push("tinycloud.encryption/decrypt");
|
|
2475
|
+
continue;
|
|
2476
|
+
}
|
|
2477
|
+
if (action === "network.create" || action === "tinycloud.encryption/network.create") {
|
|
2478
|
+
normalizedActions.push("tinycloud.encryption/network.create");
|
|
2479
|
+
continue;
|
|
2480
|
+
}
|
|
2481
|
+
if (action === "network.revoke" || action === "tinycloud.encryption/network.revoke") {
|
|
2482
|
+
normalizedActions.push("tinycloud.encryption/network.revoke");
|
|
2483
|
+
continue;
|
|
2380
2484
|
}
|
|
2485
|
+
if (action.includes("/")) {
|
|
2486
|
+
throw new ManifestValidationError(
|
|
2487
|
+
`unknown encryption action ${JSON.stringify(action)}; expected decrypt, network.create, or network.revoke`
|
|
2488
|
+
);
|
|
2489
|
+
}
|
|
2490
|
+
throw new ManifestValidationError(
|
|
2491
|
+
`unknown encryption action ${JSON.stringify(action)}; expected decrypt, network.create, or network.revoke`
|
|
2492
|
+
);
|
|
2381
2493
|
}
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2494
|
+
const dedupedActions = [];
|
|
2495
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2496
|
+
for (const a of normalizedActions) {
|
|
2497
|
+
if (!seen.has(a)) {
|
|
2498
|
+
dedupedActions.push(a);
|
|
2499
|
+
seen.add(a);
|
|
2500
|
+
}
|
|
2501
|
+
}
|
|
2502
|
+
return [
|
|
2503
|
+
{
|
|
2504
|
+
service: ENCRYPTION_PERMISSION_SERVICE,
|
|
2505
|
+
space: ENCRYPTION_MANIFEST_SPACE,
|
|
2506
|
+
path: entry.path,
|
|
2507
|
+
actions: dedupedActions,
|
|
2508
|
+
skipPrefix: true,
|
|
2509
|
+
...entry.expiry !== void 0 ? { expiry: entry.expiry } : {},
|
|
2510
|
+
...entry.description !== void 0 ? { description: entry.description } : {}
|
|
2511
|
+
}
|
|
2512
|
+
];
|
|
2387
2513
|
}
|
|
2388
|
-
|
|
2389
|
-
|
|
2390
|
-
var DelegationAction = {
|
|
2391
|
-
CREATE: "tinycloud.delegation/create",
|
|
2392
|
-
REVOKE: "tinycloud.delegation/revoke",
|
|
2393
|
-
LIST: "tinycloud.delegation/list",
|
|
2394
|
-
GET: "tinycloud.delegation/get",
|
|
2395
|
-
CHECK: "tinycloud.delegation/check"
|
|
2396
|
-
};
|
|
2397
|
-
function createError(code, message, cause, meta) {
|
|
2398
|
-
return {
|
|
2399
|
-
code,
|
|
2400
|
-
message,
|
|
2401
|
-
service: "delegation",
|
|
2402
|
-
cause,
|
|
2403
|
-
meta
|
|
2404
|
-
};
|
|
2514
|
+
function expandPermissionEntries(entries) {
|
|
2515
|
+
return entries.flatMap(expandPermissionEntry);
|
|
2405
2516
|
}
|
|
2406
|
-
|
|
2407
|
-
|
|
2408
|
-
|
|
2409
|
-
*
|
|
2410
|
-
* @param config - Configuration including hosts, session, and invoke function
|
|
2411
|
-
*/
|
|
2412
|
-
constructor(config) {
|
|
2413
|
-
this.hosts = config.hosts;
|
|
2414
|
-
this.session = config.session;
|
|
2415
|
-
this.invoke = config.invoke;
|
|
2416
|
-
this.fetchFn = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
2417
|
-
}
|
|
2418
|
-
/**
|
|
2419
|
-
* Updates the session (e.g., after re-authentication).
|
|
2420
|
-
*
|
|
2421
|
-
* @param session - New session to use for operations
|
|
2422
|
-
*/
|
|
2423
|
-
updateSession(session) {
|
|
2424
|
-
this.session = session;
|
|
2517
|
+
function applyPrefix(prefix, path, skipPrefix) {
|
|
2518
|
+
if (skipPrefix) {
|
|
2519
|
+
return path;
|
|
2425
2520
|
}
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
*/
|
|
2429
|
-
get host() {
|
|
2430
|
-
return this.hosts[0];
|
|
2521
|
+
if (prefix === "") {
|
|
2522
|
+
return path;
|
|
2431
2523
|
}
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
*/
|
|
2435
|
-
async invokeOperation(path, action, body) {
|
|
2436
|
-
const headers = this.invoke(this.session, "delegation", path, action);
|
|
2437
|
-
return this.fetchFn(`${this.host}/invoke`, {
|
|
2438
|
-
method: "POST",
|
|
2439
|
-
headers,
|
|
2440
|
-
body
|
|
2441
|
-
});
|
|
2524
|
+
if (path.startsWith("/")) {
|
|
2525
|
+
return `${prefix}${path}`;
|
|
2442
2526
|
}
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
|
|
2452
|
-
|
|
2453
|
-
|
|
2454
|
-
|
|
2455
|
-
|
|
2456
|
-
|
|
2457
|
-
|
|
2458
|
-
|
|
2459
|
-
|
|
2460
|
-
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
|
|
2466
|
-
|
|
2467
|
-
|
|
2468
|
-
|
|
2469
|
-
|
|
2470
|
-
|
|
2471
|
-
|
|
2472
|
-
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2527
|
+
return `${prefix}/${path}`;
|
|
2528
|
+
}
|
|
2529
|
+
async function loadManifest(url) {
|
|
2530
|
+
const fetchFn = globalThis.fetch;
|
|
2531
|
+
if (typeof fetchFn !== "function") {
|
|
2532
|
+
throw new ManifestValidationError(
|
|
2533
|
+
"loadManifest requires a global fetch; pass the manifest object directly on runtimes without fetch"
|
|
2534
|
+
);
|
|
2535
|
+
}
|
|
2536
|
+
const res = await fetchFn(url);
|
|
2537
|
+
if (!res.ok) {
|
|
2538
|
+
throw new ManifestValidationError(
|
|
2539
|
+
`failed to fetch manifest from ${url}: HTTP ${res.status}`
|
|
2540
|
+
);
|
|
2541
|
+
}
|
|
2542
|
+
const json = await res.json();
|
|
2543
|
+
return validateManifest(json);
|
|
2544
|
+
}
|
|
2545
|
+
function validateManifest(input) {
|
|
2546
|
+
if (input === null || typeof input !== "object") {
|
|
2547
|
+
throw new ManifestValidationError("manifest must be an object");
|
|
2548
|
+
}
|
|
2549
|
+
const m = input;
|
|
2550
|
+
if (m.manifest_version !== void 0 && m.manifest_version !== DEFAULT_MANIFEST_VERSION) {
|
|
2551
|
+
throw new ManifestValidationError(
|
|
2552
|
+
`manifest.manifest_version must be ${DEFAULT_MANIFEST_VERSION}`
|
|
2553
|
+
);
|
|
2554
|
+
}
|
|
2555
|
+
if (typeof m.app_id !== "string" || m.app_id.length === 0) {
|
|
2556
|
+
throw new ManifestValidationError(
|
|
2557
|
+
"manifest.app_id is required and must be a non-empty string"
|
|
2558
|
+
);
|
|
2559
|
+
}
|
|
2560
|
+
if (typeof m.name !== "string" || m.name.length === 0) {
|
|
2561
|
+
throw new ManifestValidationError(
|
|
2562
|
+
"manifest.name is required and must be a non-empty string"
|
|
2563
|
+
);
|
|
2564
|
+
}
|
|
2565
|
+
if (m.did !== void 0 && (typeof m.did !== "string" || m.did.length === 0)) {
|
|
2566
|
+
throw new ManifestValidationError(
|
|
2567
|
+
"manifest.did must be a non-empty DID string"
|
|
2568
|
+
);
|
|
2569
|
+
}
|
|
2570
|
+
if (m.space !== void 0 && (typeof m.space !== "string" || m.space.length === 0)) {
|
|
2571
|
+
throw new ManifestValidationError(
|
|
2572
|
+
"manifest.space must be a non-empty string"
|
|
2573
|
+
);
|
|
2574
|
+
}
|
|
2575
|
+
if (m.expiry !== void 0) {
|
|
2576
|
+
parseExpiry(m.expiry);
|
|
2577
|
+
}
|
|
2578
|
+
if (m.permissions !== void 0) {
|
|
2579
|
+
if (!Array.isArray(m.permissions)) {
|
|
2580
|
+
throw new ManifestValidationError(
|
|
2581
|
+
"manifest.permissions must be an array"
|
|
2582
|
+
);
|
|
2481
2583
|
}
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2485
|
-
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2584
|
+
m.permissions.forEach(
|
|
2585
|
+
(p, i) => validatePermissionEntry(p, `permissions[${i}]`)
|
|
2586
|
+
);
|
|
2587
|
+
}
|
|
2588
|
+
if (m.secrets !== void 0) {
|
|
2589
|
+
validateManifestSecrets(m.secrets);
|
|
2590
|
+
}
|
|
2591
|
+
return m;
|
|
2592
|
+
}
|
|
2593
|
+
function validateManifestSecrets(secrets) {
|
|
2594
|
+
if (secrets === null || typeof secrets !== "object" || Array.isArray(secrets)) {
|
|
2595
|
+
throw new ManifestValidationError("manifest.secrets must be an object");
|
|
2596
|
+
}
|
|
2597
|
+
for (const [name, spec] of Object.entries(secrets)) {
|
|
2598
|
+
if (!import_sdk_services4.SECRET_NAME_RE.test(name)) {
|
|
2599
|
+
throw new ManifestValidationError(
|
|
2600
|
+
`manifest.secrets.${name} must match ${import_sdk_services4.SECRET_NAME_RE.source}`
|
|
2601
|
+
);
|
|
2490
2602
|
}
|
|
2491
2603
|
try {
|
|
2492
|
-
|
|
2493
|
-
|
|
2494
|
-
|
|
2495
|
-
actions: params.actions,
|
|
2496
|
-
expiry: params.expiry?.toISOString(),
|
|
2497
|
-
disableSubDelegation: params.disableSubDelegation ?? false,
|
|
2498
|
-
statement: params.statement
|
|
2499
|
-
});
|
|
2500
|
-
const response = await this.invokeOperation(
|
|
2501
|
-
params.path,
|
|
2502
|
-
DelegationAction.CREATE,
|
|
2503
|
-
body
|
|
2604
|
+
(0, import_sdk_services4.resolveSecretPath)(
|
|
2605
|
+
secretNameFromSpec(name, spec),
|
|
2606
|
+
{ scope: secretScopeFromSpec(spec) }
|
|
2504
2607
|
);
|
|
2505
|
-
if (!response.ok) {
|
|
2506
|
-
const errorText = await response.text();
|
|
2507
|
-
return {
|
|
2508
|
-
ok: false,
|
|
2509
|
-
error: createError(
|
|
2510
|
-
DelegationErrorCodes.CREATION_FAILED,
|
|
2511
|
-
`Failed to create delegation: ${response.status} - ${errorText}`,
|
|
2512
|
-
void 0,
|
|
2513
|
-
{ status: response.status, path: params.path }
|
|
2514
|
-
)
|
|
2515
|
-
};
|
|
2516
|
-
}
|
|
2517
|
-
const apiResponse = await response.json();
|
|
2518
|
-
const delegation = {
|
|
2519
|
-
cid: apiResponse.cid ?? "",
|
|
2520
|
-
delegateDID: params.delegateDID,
|
|
2521
|
-
spaceId: this.session.spaceId,
|
|
2522
|
-
path: params.path,
|
|
2523
|
-
actions: params.actions,
|
|
2524
|
-
expiry: params.expiry ?? new Date(Date.now() + EXPIRY.SHARE_MS),
|
|
2525
|
-
isRevoked: false,
|
|
2526
|
-
allowSubDelegation: !(params.disableSubDelegation ?? false),
|
|
2527
|
-
createdAt: /* @__PURE__ */ new Date()
|
|
2528
|
-
};
|
|
2529
|
-
return { ok: true, data: delegation };
|
|
2530
2608
|
} catch (error) {
|
|
2531
|
-
|
|
2532
|
-
|
|
2533
|
-
|
|
2534
|
-
|
|
2535
|
-
|
|
2536
|
-
|
|
2537
|
-
|
|
2538
|
-
|
|
2539
|
-
|
|
2609
|
+
throw new ManifestValidationError(
|
|
2610
|
+
`manifest.secrets.${name}: ${error instanceof Error ? error.message : String(error)}`
|
|
2611
|
+
);
|
|
2612
|
+
}
|
|
2613
|
+
const actions = secretActionsFromSpec(name, spec);
|
|
2614
|
+
if (actions.length === 0) {
|
|
2615
|
+
throw new ManifestValidationError(
|
|
2616
|
+
`manifest.secrets.${name} actions must be non-empty`
|
|
2617
|
+
);
|
|
2618
|
+
}
|
|
2619
|
+
for (const action of actions) {
|
|
2620
|
+
if (typeof action !== "string" || action.length === 0) {
|
|
2621
|
+
throw new ManifestValidationError(
|
|
2622
|
+
`manifest.secrets.${name} actions must be non-empty strings`
|
|
2623
|
+
);
|
|
2540
2624
|
}
|
|
2541
|
-
return {
|
|
2542
|
-
ok: false,
|
|
2543
|
-
error: createError(
|
|
2544
|
-
DelegationErrorCodes.NETWORK_ERROR,
|
|
2545
|
-
`Network error during delegation creation: ${String(error)}`,
|
|
2546
|
-
error instanceof Error ? error : void 0
|
|
2547
|
-
)
|
|
2548
|
-
};
|
|
2549
2625
|
}
|
|
2550
|
-
|
|
2551
|
-
|
|
2552
|
-
* Revokes an existing delegation.
|
|
2553
|
-
*
|
|
2554
|
-
* Once revoked, the delegation can no longer be used to access resources.
|
|
2555
|
-
* This also invalidates any sub-delegations derived from this delegation.
|
|
2556
|
-
*
|
|
2557
|
-
* @param cid - The CID of the delegation to revoke
|
|
2558
|
-
* @returns Result indicating success or an error
|
|
2559
|
-
*
|
|
2560
|
-
* @example
|
|
2561
|
-
* ```typescript
|
|
2562
|
-
* const result = await manager.revoke("bafy...");
|
|
2563
|
-
* if (result.ok) {
|
|
2564
|
-
* console.log("Delegation revoked successfully");
|
|
2565
|
-
* }
|
|
2566
|
-
* ```
|
|
2567
|
-
*/
|
|
2568
|
-
async revoke(cid) {
|
|
2569
|
-
if (!cid) {
|
|
2570
|
-
return {
|
|
2571
|
-
ok: false,
|
|
2572
|
-
error: createError(
|
|
2573
|
-
DelegationErrorCodes.INVALID_INPUT,
|
|
2574
|
-
"cid is required"
|
|
2575
|
-
)
|
|
2576
|
-
};
|
|
2626
|
+
if (spec !== null && typeof spec === "object" && !Array.isArray(spec) && spec.expiry !== void 0) {
|
|
2627
|
+
parseExpiry(spec.expiry);
|
|
2577
2628
|
}
|
|
2578
|
-
try {
|
|
2579
|
-
const body = JSON.stringify({ cid });
|
|
2580
|
-
const response = await this.invokeOperation(
|
|
2581
|
-
cid,
|
|
2582
|
-
DelegationAction.REVOKE,
|
|
2583
|
-
body
|
|
2584
|
-
);
|
|
2585
|
-
if (!response.ok) {
|
|
2586
|
-
const errorText = await response.text();
|
|
2587
|
-
if (response.status === 404) {
|
|
2588
|
-
return {
|
|
2589
|
-
ok: false,
|
|
2590
|
-
error: createError(
|
|
2591
|
-
DelegationErrorCodes.NOT_FOUND,
|
|
2592
|
-
`Delegation not found: ${cid}`
|
|
2593
|
-
)
|
|
2594
|
-
};
|
|
2595
|
-
}
|
|
2596
|
-
return {
|
|
2597
|
-
ok: false,
|
|
2598
|
-
error: createError(
|
|
2599
|
-
DelegationErrorCodes.REVOCATION_FAILED,
|
|
2600
|
-
`Failed to revoke delegation: ${response.status} - ${errorText}`,
|
|
2601
|
-
void 0,
|
|
2602
|
-
{ status: response.status, cid }
|
|
2603
|
-
)
|
|
2604
|
-
};
|
|
2605
|
-
}
|
|
2606
|
-
return { ok: true, data: void 0 };
|
|
2607
|
-
} catch (error) {
|
|
2608
|
-
if (error instanceof Error && error.name === "AbortError") {
|
|
2609
|
-
return {
|
|
2610
|
-
ok: false,
|
|
2611
|
-
error: createError(
|
|
2612
|
-
DelegationErrorCodes.ABORTED,
|
|
2613
|
-
"Request aborted",
|
|
2614
|
-
error
|
|
2615
|
-
)
|
|
2616
|
-
};
|
|
2617
|
-
}
|
|
2618
|
-
return {
|
|
2619
|
-
ok: false,
|
|
2620
|
-
error: createError(
|
|
2621
|
-
DelegationErrorCodes.NETWORK_ERROR,
|
|
2622
|
-
`Network error during delegation revocation: ${String(error)}`,
|
|
2623
|
-
error instanceof Error ? error : void 0
|
|
2624
|
-
)
|
|
2625
|
-
};
|
|
2626
|
-
}
|
|
2627
|
-
}
|
|
2628
|
-
/**
|
|
2629
|
-
* Lists all delegations for the current session's space.
|
|
2630
|
-
*
|
|
2631
|
-
* Returns both delegations created by the current user (as delegator)
|
|
2632
|
-
* and delegations granted to the current user (as delegatee).
|
|
2633
|
-
*
|
|
2634
|
-
* @returns Result containing an array of Delegations or an error
|
|
2635
|
-
*
|
|
2636
|
-
* @example
|
|
2637
|
-
* ```typescript
|
|
2638
|
-
* const result = await manager.list();
|
|
2639
|
-
* if (result.ok) {
|
|
2640
|
-
* for (const delegation of result.data) {
|
|
2641
|
-
* console.log(`${delegation.cid}: ${delegation.path} -> ${delegation.delegateDID}`);
|
|
2642
|
-
* }
|
|
2643
|
-
* }
|
|
2644
|
-
* ```
|
|
2645
|
-
*/
|
|
2646
|
-
async list() {
|
|
2647
|
-
try {
|
|
2648
|
-
const response = await this.invokeOperation("", DelegationAction.LIST);
|
|
2649
|
-
if (!response.ok) {
|
|
2650
|
-
const errorText = await response.text();
|
|
2651
|
-
return {
|
|
2652
|
-
ok: false,
|
|
2653
|
-
error: createError(
|
|
2654
|
-
DelegationErrorCodes.NETWORK_ERROR,
|
|
2655
|
-
`Failed to list delegations: ${response.status} - ${errorText}`,
|
|
2656
|
-
void 0,
|
|
2657
|
-
{ status: response.status }
|
|
2658
|
-
)
|
|
2659
|
-
};
|
|
2660
|
-
}
|
|
2661
|
-
const data = await response.json();
|
|
2662
|
-
const delegations = data.map((item) => ({
|
|
2663
|
-
cid: item.cid,
|
|
2664
|
-
delegateDID: item.delegateDID,
|
|
2665
|
-
delegatorDID: item.delegatorDID,
|
|
2666
|
-
spaceId: item.spaceId,
|
|
2667
|
-
path: item.path,
|
|
2668
|
-
actions: item.actions,
|
|
2669
|
-
expiry: new Date(item.expiry),
|
|
2670
|
-
isRevoked: item.isRevoked,
|
|
2671
|
-
createdAt: item.createdAt ? new Date(item.createdAt) : void 0,
|
|
2672
|
-
parentCid: item.parentCid,
|
|
2673
|
-
allowSubDelegation: item.allowSubDelegation
|
|
2674
|
-
}));
|
|
2675
|
-
return { ok: true, data: delegations };
|
|
2676
|
-
} catch (error) {
|
|
2677
|
-
if (error instanceof Error && error.name === "AbortError") {
|
|
2678
|
-
return {
|
|
2679
|
-
ok: false,
|
|
2680
|
-
error: createError(
|
|
2681
|
-
DelegationErrorCodes.ABORTED,
|
|
2682
|
-
"Request aborted",
|
|
2683
|
-
error
|
|
2684
|
-
)
|
|
2685
|
-
};
|
|
2686
|
-
}
|
|
2687
|
-
return {
|
|
2688
|
-
ok: false,
|
|
2689
|
-
error: createError(
|
|
2690
|
-
DelegationErrorCodes.NETWORK_ERROR,
|
|
2691
|
-
`Network error during delegation list: ${String(error)}`,
|
|
2692
|
-
error instanceof Error ? error : void 0
|
|
2693
|
-
)
|
|
2694
|
-
};
|
|
2695
|
-
}
|
|
2696
|
-
}
|
|
2697
|
-
/**
|
|
2698
|
-
* Gets the full delegation chain for a given delegation.
|
|
2699
|
-
*
|
|
2700
|
-
* Returns the chain of delegations from the root (original delegator)
|
|
2701
|
-
* to the specified delegation, including all intermediate sub-delegations.
|
|
2702
|
-
*
|
|
2703
|
-
* @param cid - The CID of the delegation to get the chain for
|
|
2704
|
-
* @returns Result containing the DelegationChain or an error
|
|
2705
|
-
*
|
|
2706
|
-
* @example
|
|
2707
|
-
* ```typescript
|
|
2708
|
-
* const result = await manager.getChain("bafy...");
|
|
2709
|
-
* if (result.ok) {
|
|
2710
|
-
* console.log("Chain length:", result.data.length);
|
|
2711
|
-
* for (const delegation of result.data) {
|
|
2712
|
-
* console.log(`- ${delegation.delegatorDID} -> ${delegation.delegateDID}`);
|
|
2713
|
-
* }
|
|
2714
|
-
* }
|
|
2715
|
-
* ```
|
|
2716
|
-
*/
|
|
2717
|
-
async getChain(cid) {
|
|
2718
|
-
if (!cid) {
|
|
2719
|
-
return {
|
|
2720
|
-
ok: false,
|
|
2721
|
-
error: createError(
|
|
2722
|
-
DelegationErrorCodes.INVALID_INPUT,
|
|
2723
|
-
"cid is required"
|
|
2724
|
-
)
|
|
2725
|
-
};
|
|
2726
|
-
}
|
|
2727
|
-
try {
|
|
2728
|
-
const body = JSON.stringify({ cid, includeChain: true });
|
|
2729
|
-
const response = await this.invokeOperation(
|
|
2730
|
-
cid,
|
|
2731
|
-
DelegationAction.GET,
|
|
2732
|
-
body
|
|
2733
|
-
);
|
|
2734
|
-
if (!response.ok) {
|
|
2735
|
-
const errorText = await response.text();
|
|
2736
|
-
if (response.status === 404) {
|
|
2737
|
-
return {
|
|
2738
|
-
ok: false,
|
|
2739
|
-
error: createError(
|
|
2740
|
-
DelegationErrorCodes.NOT_FOUND,
|
|
2741
|
-
`Delegation not found: ${cid}`
|
|
2742
|
-
)
|
|
2743
|
-
};
|
|
2744
|
-
}
|
|
2745
|
-
return {
|
|
2746
|
-
ok: false,
|
|
2747
|
-
error: createError(
|
|
2748
|
-
DelegationErrorCodes.NETWORK_ERROR,
|
|
2749
|
-
`Failed to get delegation chain: ${response.status} - ${errorText}`,
|
|
2750
|
-
void 0,
|
|
2751
|
-
{ status: response.status, cid }
|
|
2752
|
-
)
|
|
2753
|
-
};
|
|
2754
|
-
}
|
|
2755
|
-
const data = await response.json();
|
|
2756
|
-
const chain = data.chain.map((item) => ({
|
|
2757
|
-
cid: item.cid,
|
|
2758
|
-
delegateDID: item.delegateDID,
|
|
2759
|
-
delegatorDID: item.delegatorDID,
|
|
2760
|
-
spaceId: item.spaceId,
|
|
2761
|
-
path: item.path,
|
|
2762
|
-
actions: item.actions,
|
|
2763
|
-
expiry: new Date(item.expiry),
|
|
2764
|
-
isRevoked: item.isRevoked,
|
|
2765
|
-
createdAt: item.createdAt ? new Date(item.createdAt) : void 0,
|
|
2766
|
-
parentCid: item.parentCid,
|
|
2767
|
-
allowSubDelegation: item.allowSubDelegation
|
|
2768
|
-
}));
|
|
2769
|
-
return { ok: true, data: chain };
|
|
2770
|
-
} catch (error) {
|
|
2771
|
-
if (error instanceof Error && error.name === "AbortError") {
|
|
2772
|
-
return {
|
|
2773
|
-
ok: false,
|
|
2774
|
-
error: createError(
|
|
2775
|
-
DelegationErrorCodes.ABORTED,
|
|
2776
|
-
"Request aborted",
|
|
2777
|
-
error
|
|
2778
|
-
)
|
|
2779
|
-
};
|
|
2780
|
-
}
|
|
2781
|
-
return {
|
|
2782
|
-
ok: false,
|
|
2783
|
-
error: createError(
|
|
2784
|
-
DelegationErrorCodes.NETWORK_ERROR,
|
|
2785
|
-
`Network error during chain retrieval: ${String(error)}`,
|
|
2786
|
-
error instanceof Error ? error : void 0
|
|
2787
|
-
)
|
|
2788
|
-
};
|
|
2789
|
-
}
|
|
2790
|
-
}
|
|
2791
|
-
/**
|
|
2792
|
-
* Checks if the current session has permission for a given path and action.
|
|
2793
|
-
*
|
|
2794
|
-
* This can be used to verify permissions before attempting an operation,
|
|
2795
|
-
* or to implement custom access control logic.
|
|
2796
|
-
*
|
|
2797
|
-
* @param path - The resource path to check
|
|
2798
|
-
* @param action - The action to check (e.g., "tinycloud.kv/get")
|
|
2799
|
-
* @returns Result containing a boolean indicating permission or an error
|
|
2800
|
-
*
|
|
2801
|
-
* @example
|
|
2802
|
-
* ```typescript
|
|
2803
|
-
* const result = await manager.checkPermission("documents/private/", "tinycloud.kv/put");
|
|
2804
|
-
* if (result.ok && result.data) {
|
|
2805
|
-
* console.log("Permission granted");
|
|
2806
|
-
* } else {
|
|
2807
|
-
* console.log("Permission denied");
|
|
2808
|
-
* }
|
|
2809
|
-
* ```
|
|
2810
|
-
*/
|
|
2811
|
-
async checkPermission(path, action) {
|
|
2812
|
-
if (!path) {
|
|
2813
|
-
return {
|
|
2814
|
-
ok: false,
|
|
2815
|
-
error: createError(
|
|
2816
|
-
DelegationErrorCodes.INVALID_INPUT,
|
|
2817
|
-
"path is required"
|
|
2818
|
-
)
|
|
2819
|
-
};
|
|
2820
|
-
}
|
|
2821
|
-
if (!action) {
|
|
2822
|
-
return {
|
|
2823
|
-
ok: false,
|
|
2824
|
-
error: createError(
|
|
2825
|
-
DelegationErrorCodes.INVALID_INPUT,
|
|
2826
|
-
"action is required"
|
|
2827
|
-
)
|
|
2828
|
-
};
|
|
2829
|
-
}
|
|
2830
|
-
try {
|
|
2831
|
-
const body = JSON.stringify({ path, action });
|
|
2832
|
-
const response = await this.invokeOperation(
|
|
2833
|
-
path,
|
|
2834
|
-
DelegationAction.CHECK,
|
|
2835
|
-
body
|
|
2836
|
-
);
|
|
2837
|
-
if (!response.ok) {
|
|
2838
|
-
if (response.status === 403) {
|
|
2839
|
-
return { ok: true, data: false };
|
|
2840
|
-
}
|
|
2841
|
-
const errorText = await response.text();
|
|
2842
|
-
return {
|
|
2843
|
-
ok: false,
|
|
2844
|
-
error: createError(
|
|
2845
|
-
DelegationErrorCodes.NETWORK_ERROR,
|
|
2846
|
-
`Failed to check permission: ${response.status} - ${errorText}`,
|
|
2847
|
-
void 0,
|
|
2848
|
-
{ status: response.status, path, action }
|
|
2849
|
-
)
|
|
2850
|
-
};
|
|
2851
|
-
}
|
|
2852
|
-
const data = await response.json();
|
|
2853
|
-
return { ok: true, data: data.allowed };
|
|
2854
|
-
} catch (error) {
|
|
2855
|
-
if (error instanceof Error && error.name === "AbortError") {
|
|
2856
|
-
return {
|
|
2857
|
-
ok: false,
|
|
2858
|
-
error: createError(
|
|
2859
|
-
DelegationErrorCodes.ABORTED,
|
|
2860
|
-
"Request aborted",
|
|
2861
|
-
error
|
|
2862
|
-
)
|
|
2863
|
-
};
|
|
2864
|
-
}
|
|
2865
|
-
return {
|
|
2866
|
-
ok: false,
|
|
2867
|
-
error: createError(
|
|
2868
|
-
DelegationErrorCodes.NETWORK_ERROR,
|
|
2869
|
-
`Network error during permission check: ${String(error)}`,
|
|
2870
|
-
error instanceof Error ? error : void 0
|
|
2871
|
-
)
|
|
2872
|
-
};
|
|
2873
|
-
}
|
|
2874
|
-
}
|
|
2875
|
-
};
|
|
2876
|
-
|
|
2877
|
-
// src/delegations/SharingService.schema.ts
|
|
2878
|
-
var import_zod5 = require("zod");
|
|
2879
|
-
var EncodedShareDataSchema = import_zod5.z.object({
|
|
2880
|
-
/** Private key in JWK format (must include d parameter) */
|
|
2881
|
-
key: JWKSchema.refine(
|
|
2882
|
-
(jwk) => typeof jwk.d === "string" && jwk.d.length > 0,
|
|
2883
|
-
{ message: "JWK must include private key (d parameter)" }
|
|
2884
|
-
),
|
|
2885
|
-
/** DID of the key */
|
|
2886
|
-
keyDid: import_zod5.z.string().min(1, "keyDid is required"),
|
|
2887
|
-
/** The delegation granting access */
|
|
2888
|
-
delegation: DelegationSchema,
|
|
2889
|
-
/** Resource path this link grants access to */
|
|
2890
|
-
path: import_zod5.z.string().min(1, "path is required"),
|
|
2891
|
-
/** TinyCloud host URL */
|
|
2892
|
-
host: import_zod5.z.string().url("host must be a valid URL"),
|
|
2893
|
-
/** Space ID */
|
|
2894
|
-
spaceId: import_zod5.z.string().min(1, "spaceId is required"),
|
|
2895
|
-
/** Schema version (must be 1) */
|
|
2896
|
-
version: import_zod5.z.literal(1)
|
|
2897
|
-
});
|
|
2898
|
-
var ReceiveOptionsSchema = import_zod5.z.object({
|
|
2899
|
-
/**
|
|
2900
|
-
* Whether to automatically create a sub-delegation to the current session key.
|
|
2901
|
-
* Default: true
|
|
2902
|
-
*/
|
|
2903
|
-
autoSubdelegate: import_zod5.z.boolean().optional(),
|
|
2904
|
-
/**
|
|
2905
|
-
* Whether to use the current session key for operations (requires autoSubdelegate).
|
|
2906
|
-
* Default: true
|
|
2907
|
-
*/
|
|
2908
|
-
useSessionKey: import_zod5.z.boolean().optional(),
|
|
2909
|
-
/**
|
|
2910
|
-
* Ingestion options passed to CapabilityKeyRegistry.
|
|
2911
|
-
*/
|
|
2912
|
-
ingestOptions: IngestOptionsSchema.optional()
|
|
2913
|
-
});
|
|
2914
|
-
var SharingServiceConfigSchema = import_zod5.z.object({
|
|
2915
|
-
/** TinyCloud host URLs */
|
|
2916
|
-
hosts: import_zod5.z.array(import_zod5.z.string().url()).min(1, "At least one host URL is required"),
|
|
2917
|
-
/**
|
|
2918
|
-
* Active session for authentication.
|
|
2919
|
-
* Required for generate(), optional for receive().
|
|
2920
|
-
*/
|
|
2921
|
-
session: import_zod5.z.unknown().refine(
|
|
2922
|
-
(val) => val === void 0 || val !== null && typeof val === "object",
|
|
2923
|
-
{ message: "Expected a ServiceSession object or undefined" }
|
|
2924
|
-
).optional(),
|
|
2925
|
-
/** Platform-specific invoke function */
|
|
2926
|
-
invoke: import_zod5.z.unknown().refine((val) => typeof val === "function", {
|
|
2927
|
-
message: "Expected an invoke function"
|
|
2928
|
-
}),
|
|
2929
|
-
/** Optional custom fetch implementation */
|
|
2930
|
-
fetch: import_zod5.z.unknown().refine(
|
|
2931
|
-
(val) => val === void 0 || typeof val === "function",
|
|
2932
|
-
{ message: "Expected a fetch function or undefined" }
|
|
2933
|
-
).optional(),
|
|
2934
|
-
/** Key provider for cryptographic operations */
|
|
2935
|
-
keyProvider: KeyProviderSchema,
|
|
2936
|
-
/** Capability key registry for key/delegation management */
|
|
2937
|
-
registry: import_zod5.z.unknown().refine(
|
|
2938
|
-
(val) => val !== null && typeof val === "object",
|
|
2939
|
-
{ message: "Expected an ICapabilityKeyRegistry object" }
|
|
2940
|
-
),
|
|
2941
|
-
/**
|
|
2942
|
-
* Delegation manager for creating delegations.
|
|
2943
|
-
* Required for generate(), optional for receive().
|
|
2944
|
-
*/
|
|
2945
|
-
delegationManager: import_zod5.z.unknown().refine(
|
|
2946
|
-
(val) => val === void 0 || val !== null && typeof val === "object",
|
|
2947
|
-
{ message: "Expected a DelegationManager object or undefined" }
|
|
2948
|
-
).optional(),
|
|
2949
|
-
/** Factory for creating KV service instances */
|
|
2950
|
-
createKVService: import_zod5.z.unknown().refine(
|
|
2951
|
-
(val) => typeof val === "function",
|
|
2952
|
-
{ message: "Expected a createKVService factory function" }
|
|
2953
|
-
),
|
|
2954
|
-
/** Base URL for sharing links (e.g., "https://share.myapp.com") */
|
|
2955
|
-
baseUrl: import_zod5.z.string().optional(),
|
|
2956
|
-
/**
|
|
2957
|
-
* Custom delegation creation function.
|
|
2958
|
-
*/
|
|
2959
|
-
createDelegation: import_zod5.z.unknown().refine((val) => val === void 0 || typeof val === "function", {
|
|
2960
|
-
message: "Expected a createDelegation function or undefined"
|
|
2961
|
-
}).optional(),
|
|
2962
|
-
/**
|
|
2963
|
-
* WASM function for client-side delegation creation.
|
|
2964
|
-
*/
|
|
2965
|
-
createDelegationWasm: import_zod5.z.unknown().refine((val) => val === void 0 || typeof val === "function", {
|
|
2966
|
-
message: "Expected a createDelegationWasm function or undefined"
|
|
2967
|
-
}).optional(),
|
|
2968
|
-
/**
|
|
2969
|
-
* Path prefix for KV operations.
|
|
2970
|
-
*/
|
|
2971
|
-
pathPrefix: import_zod5.z.string().optional(),
|
|
2972
|
-
/**
|
|
2973
|
-
* Session expiry time.
|
|
2974
|
-
*/
|
|
2975
|
-
sessionExpiry: import_zod5.z.date().optional(),
|
|
2976
|
-
/**
|
|
2977
|
-
* Callback to create a DIRECT delegation from wallet to share key.
|
|
2978
|
-
* This is the preferred method for long-lived share links because it
|
|
2979
|
-
* bypasses the session delegation chain entirely.
|
|
2980
|
-
*/
|
|
2981
|
-
onRootDelegationNeeded: import_zod5.z.unknown().refine((val) => val === void 0 || typeof val === "function", {
|
|
2982
|
-
message: "Expected an onRootDelegationNeeded function or undefined"
|
|
2983
|
-
}).optional()
|
|
2984
|
-
});
|
|
2985
|
-
function validateEncodedShareData(data) {
|
|
2986
|
-
const result = EncodedShareDataSchema.safeParse(data);
|
|
2987
|
-
if (!result.success) {
|
|
2988
|
-
return {
|
|
2989
|
-
ok: false,
|
|
2990
|
-
error: {
|
|
2991
|
-
code: DelegationErrorCodes.VALIDATION_ERROR,
|
|
2992
|
-
message: `Invalid share data: ${result.error.message}`,
|
|
2993
|
-
service: "delegation",
|
|
2994
|
-
meta: { issues: result.error.issues }
|
|
2995
|
-
}
|
|
2996
|
-
};
|
|
2997
2629
|
}
|
|
2998
|
-
return { ok: true, data: result.data };
|
|
2999
2630
|
}
|
|
3000
|
-
|
|
3001
|
-
|
|
3002
|
-
|
|
3003
|
-
var import_sdk_services4 = require("@tinycloud/sdk-services");
|
|
3004
|
-
var ManifestValidationError = class extends Error {
|
|
3005
|
-
constructor(message) {
|
|
3006
|
-
super(`Manifest validation failed: ${message}`);
|
|
3007
|
-
this.name = "ManifestValidationError";
|
|
3008
|
-
}
|
|
3009
|
-
};
|
|
3010
|
-
var DEFAULT_EXPIRY = "30d";
|
|
3011
|
-
var DEFAULT_DEFAULTS = true;
|
|
3012
|
-
var DEFAULT_MANIFEST_VERSION = 1;
|
|
3013
|
-
var DEFAULT_MANIFEST_SPACE = "applications";
|
|
3014
|
-
var ACCOUNT_REGISTRY_SPACE = "account";
|
|
3015
|
-
var ACCOUNT_REGISTRY_PATH = "applications/";
|
|
3016
|
-
var SECRETS_SPACE = "secrets";
|
|
3017
|
-
var VAULT_PERMISSION_SERVICE = "tinycloud.vault";
|
|
3018
|
-
var SERVICE_SHORT_TO_LONG = Object.freeze({
|
|
3019
|
-
kv: "tinycloud.kv",
|
|
3020
|
-
sql: "tinycloud.sql",
|
|
3021
|
-
duckdb: "tinycloud.duckdb",
|
|
3022
|
-
capabilities: "tinycloud.capabilities",
|
|
3023
|
-
hooks: "tinycloud.hooks",
|
|
3024
|
-
encryption: "tinycloud.encryption"
|
|
3025
|
-
});
|
|
3026
|
-
var ENCRYPTION_PERMISSION_SERVICE = "tinycloud.encryption";
|
|
3027
|
-
var ENCRYPTION_MANIFEST_SPACE = "encryption";
|
|
3028
|
-
var SERVICE_LONG_TO_SHORT = Object.freeze(
|
|
3029
|
-
Object.fromEntries(
|
|
3030
|
-
Object.entries(SERVICE_SHORT_TO_LONG).map(([s, l]) => [l, s])
|
|
3031
|
-
)
|
|
3032
|
-
);
|
|
3033
|
-
var DEFAULT_STANDARD_ENTRIES = [
|
|
3034
|
-
{
|
|
3035
|
-
service: "tinycloud.kv",
|
|
3036
|
-
space: DEFAULT_MANIFEST_SPACE,
|
|
3037
|
-
path: "/",
|
|
3038
|
-
actions: ["get", "put", "del", "list", "metadata"]
|
|
3039
|
-
},
|
|
3040
|
-
{
|
|
3041
|
-
service: "tinycloud.sql",
|
|
3042
|
-
space: DEFAULT_MANIFEST_SPACE,
|
|
3043
|
-
path: "/",
|
|
3044
|
-
actions: ["read", "write"]
|
|
2631
|
+
function validatePermissionEntry(p, path) {
|
|
2632
|
+
if (p === null || typeof p !== "object") {
|
|
2633
|
+
throw new ManifestValidationError(`${path} must be an object`);
|
|
3045
2634
|
}
|
|
3046
|
-
|
|
3047
|
-
|
|
3048
|
-
|
|
3049
|
-
service: "tinycloud.kv",
|
|
3050
|
-
space: DEFAULT_MANIFEST_SPACE,
|
|
3051
|
-
path: "/",
|
|
3052
|
-
actions: ["get", "put", "del", "list", "metadata"]
|
|
3053
|
-
},
|
|
3054
|
-
{
|
|
3055
|
-
service: "tinycloud.sql",
|
|
3056
|
-
space: DEFAULT_MANIFEST_SPACE,
|
|
3057
|
-
path: "/",
|
|
3058
|
-
actions: ["read", "write", "ddl"]
|
|
2635
|
+
const entry = p;
|
|
2636
|
+
if (typeof entry.service !== "string" || entry.service.length === 0) {
|
|
2637
|
+
throw new ManifestValidationError(`${path}.service is required`);
|
|
3059
2638
|
}
|
|
3060
|
-
|
|
3061
|
-
|
|
3062
|
-
|
|
3063
|
-
|
|
3064
|
-
space: DEFAULT_MANIFEST_SPACE,
|
|
3065
|
-
path: "/",
|
|
3066
|
-
actions: ["get", "put", "del", "list", "metadata"]
|
|
3067
|
-
},
|
|
3068
|
-
{
|
|
3069
|
-
service: "tinycloud.sql",
|
|
3070
|
-
space: DEFAULT_MANIFEST_SPACE,
|
|
3071
|
-
path: "/",
|
|
3072
|
-
actions: ["read", "write", "ddl"]
|
|
3073
|
-
},
|
|
3074
|
-
{
|
|
3075
|
-
service: "tinycloud.duckdb",
|
|
3076
|
-
space: DEFAULT_MANIFEST_SPACE,
|
|
3077
|
-
path: "/",
|
|
3078
|
-
actions: ["read", "write"]
|
|
2639
|
+
if (entry.space !== void 0 && (typeof entry.space !== "string" || entry.space.length === 0)) {
|
|
2640
|
+
throw new ManifestValidationError(
|
|
2641
|
+
`${path}.space must be a non-empty string`
|
|
2642
|
+
);
|
|
3079
2643
|
}
|
|
3080
|
-
|
|
3081
|
-
function parseExpiry(duration) {
|
|
3082
|
-
if (typeof duration !== "string" || duration.length === 0) {
|
|
2644
|
+
if (typeof entry.path !== "string") {
|
|
3083
2645
|
throw new ManifestValidationError(
|
|
3084
|
-
|
|
2646
|
+
`${path}.path is required (use "" or "/" for root)`
|
|
3085
2647
|
);
|
|
3086
2648
|
}
|
|
3087
|
-
|
|
3088
|
-
if (typeof parsed !== "number" || !Number.isFinite(parsed) || parsed <= 0) {
|
|
2649
|
+
if (!Array.isArray(entry.actions) || entry.actions.length === 0) {
|
|
3089
2650
|
throw new ManifestValidationError(
|
|
3090
|
-
|
|
2651
|
+
`${path}.actions must be a non-empty array`
|
|
3091
2652
|
);
|
|
3092
2653
|
}
|
|
3093
|
-
|
|
3094
|
-
|
|
3095
|
-
|
|
3096
|
-
|
|
3097
|
-
|
|
3098
|
-
return a;
|
|
2654
|
+
for (const action of entry.actions) {
|
|
2655
|
+
if (typeof action !== "string" || action.length === 0) {
|
|
2656
|
+
throw new ManifestValidationError(
|
|
2657
|
+
`${path}.actions must contain non-empty strings`
|
|
2658
|
+
);
|
|
3099
2659
|
}
|
|
3100
|
-
|
|
3101
|
-
|
|
2660
|
+
if (entry.service === VAULT_PERMISSION_SERVICE) {
|
|
2661
|
+
vaultActionExpansion(action);
|
|
2662
|
+
}
|
|
2663
|
+
}
|
|
2664
|
+
if (entry.expiry !== void 0) {
|
|
2665
|
+
parseExpiry(entry.expiry);
|
|
2666
|
+
}
|
|
3102
2667
|
}
|
|
3103
|
-
function
|
|
3104
|
-
if (
|
|
3105
|
-
return
|
|
2668
|
+
function normalizeDefaults(value) {
|
|
2669
|
+
if (value === void 0) {
|
|
2670
|
+
return DEFAULT_DEFAULTS;
|
|
3106
2671
|
}
|
|
3107
|
-
if (
|
|
3108
|
-
return
|
|
3109
|
-
{
|
|
3110
|
-
...entry,
|
|
3111
|
-
actions: expandActionShortNames(entry.service, entry.actions)
|
|
3112
|
-
}
|
|
3113
|
-
];
|
|
2672
|
+
if (typeof value === "boolean") {
|
|
2673
|
+
return value;
|
|
3114
2674
|
}
|
|
3115
|
-
|
|
2675
|
+
if (typeof value !== "string") {
|
|
2676
|
+
return true;
|
|
2677
|
+
}
|
|
2678
|
+
const normalized = value.trim().toLowerCase();
|
|
2679
|
+
if (normalized === "admin" || normalized === "all") {
|
|
2680
|
+
return normalized;
|
|
2681
|
+
}
|
|
2682
|
+
return true;
|
|
3116
2683
|
}
|
|
3117
|
-
function
|
|
3118
|
-
if (
|
|
3119
|
-
|
|
3120
|
-
`tinycloud.encryption entries require path to be a networkId URN (got ${JSON.stringify(entry.path)})`
|
|
3121
|
-
);
|
|
2684
|
+
function defaultEntriesForTier(tier) {
|
|
2685
|
+
if (tier === false) {
|
|
2686
|
+
return [];
|
|
3122
2687
|
}
|
|
3123
|
-
const
|
|
3124
|
-
|
|
3125
|
-
|
|
3126
|
-
|
|
3127
|
-
|
|
2688
|
+
const source = tier === "admin" ? DEFAULT_ADMIN_ENTRIES : tier === "all" ? DEFAULT_ALL_ENTRIES : DEFAULT_STANDARD_ENTRIES;
|
|
2689
|
+
return source.map((e) => ({
|
|
2690
|
+
service: e.service,
|
|
2691
|
+
space: e.space,
|
|
2692
|
+
path: e.path,
|
|
2693
|
+
actions: [...e.actions],
|
|
2694
|
+
...e.skipPrefix !== void 0 ? { skipPrefix: e.skipPrefix } : {}
|
|
2695
|
+
}));
|
|
2696
|
+
}
|
|
2697
|
+
function resolveManifest(input) {
|
|
2698
|
+
const manifest = validateManifest(input);
|
|
2699
|
+
const prefix = manifest.prefix !== void 0 ? manifest.prefix : manifest.app_id;
|
|
2700
|
+
const space = manifest.space ?? DEFAULT_MANIFEST_SPACE;
|
|
2701
|
+
const expiryMs = parseExpiry(manifest.expiry ?? DEFAULT_EXPIRY);
|
|
2702
|
+
const includePublicSpace = manifest.includePublicSpace ?? true;
|
|
2703
|
+
const tier = normalizeDefaults(manifest.defaults);
|
|
2704
|
+
const defaultEntries = defaultEntriesForTier(tier);
|
|
2705
|
+
const explicitEntries = manifest.permissions ?? [];
|
|
2706
|
+
const secretEntries = secretEntriesForManifest(manifest.secrets);
|
|
2707
|
+
const allEntries = [
|
|
2708
|
+
...defaultEntries,
|
|
2709
|
+
...explicitEntries,
|
|
2710
|
+
...secretEntries
|
|
2711
|
+
];
|
|
2712
|
+
const resources = withCapabilitiesReadForSpaces(
|
|
2713
|
+
allEntries.flatMap((entry) => resolveEntry(entry, prefix, expiryMs, space))
|
|
2714
|
+
);
|
|
2715
|
+
const additionalDelegates = manifest.did === void 0 ? [] : [
|
|
2716
|
+
{
|
|
2717
|
+
did: manifest.did,
|
|
2718
|
+
name: manifest.name,
|
|
2719
|
+
expiryMs,
|
|
2720
|
+
permissions: resources.map(cloneResourceCapability)
|
|
3128
2721
|
}
|
|
3129
|
-
|
|
3130
|
-
|
|
3131
|
-
|
|
2722
|
+
];
|
|
2723
|
+
return {
|
|
2724
|
+
app_id: manifest.app_id,
|
|
2725
|
+
...manifest.did !== void 0 ? { did: manifest.did } : {},
|
|
2726
|
+
space,
|
|
2727
|
+
resources,
|
|
2728
|
+
expiryMs,
|
|
2729
|
+
includePublicSpace,
|
|
2730
|
+
additionalDelegates
|
|
2731
|
+
};
|
|
2732
|
+
}
|
|
2733
|
+
function normalizeSecretActions(actions) {
|
|
2734
|
+
const out = [];
|
|
2735
|
+
const seen = /* @__PURE__ */ new Set();
|
|
2736
|
+
const add = (action) => {
|
|
2737
|
+
if (!seen.has(action)) {
|
|
2738
|
+
out.push(action);
|
|
2739
|
+
seen.add(action);
|
|
3132
2740
|
}
|
|
3133
|
-
|
|
3134
|
-
|
|
2741
|
+
};
|
|
2742
|
+
for (const action of actions) {
|
|
2743
|
+
if (action === "read") {
|
|
2744
|
+
add("get");
|
|
3135
2745
|
continue;
|
|
3136
2746
|
}
|
|
3137
|
-
if (action
|
|
3138
|
-
|
|
3139
|
-
|
|
3140
|
-
);
|
|
2747
|
+
if (action === "write") {
|
|
2748
|
+
add("put");
|
|
2749
|
+
continue;
|
|
3141
2750
|
}
|
|
3142
|
-
|
|
3143
|
-
|
|
3144
|
-
|
|
3145
|
-
}
|
|
3146
|
-
const dedupedActions = [];
|
|
3147
|
-
const seen = /* @__PURE__ */ new Set();
|
|
3148
|
-
for (const a of normalizedActions) {
|
|
3149
|
-
if (!seen.has(a)) {
|
|
3150
|
-
dedupedActions.push(a);
|
|
3151
|
-
seen.add(a);
|
|
2751
|
+
if (action === "delete") {
|
|
2752
|
+
add("del");
|
|
2753
|
+
continue;
|
|
3152
2754
|
}
|
|
3153
|
-
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
|
|
3157
|
-
|
|
3158
|
-
|
|
3159
|
-
|
|
3160
|
-
skipPrefix: true,
|
|
3161
|
-
...entry.expiry !== void 0 ? { expiry: entry.expiry } : {},
|
|
3162
|
-
...entry.description !== void 0 ? { description: entry.description } : {}
|
|
2755
|
+
if (action === "get" || action === "put" || action === "del" || action === "list" || action === "metadata") {
|
|
2756
|
+
add(action);
|
|
2757
|
+
continue;
|
|
2758
|
+
}
|
|
2759
|
+
if (action === "tinycloud.kv/get" || action === "tinycloud.kv/put" || action === "tinycloud.kv/del" || action === "tinycloud.kv/list" || action === "tinycloud.kv/metadata") {
|
|
2760
|
+
add(action);
|
|
2761
|
+
continue;
|
|
3163
2762
|
}
|
|
3164
|
-
];
|
|
3165
|
-
}
|
|
3166
|
-
function expandPermissionEntries(entries) {
|
|
3167
|
-
return entries.flatMap(expandPermissionEntry);
|
|
3168
|
-
}
|
|
3169
|
-
function applyPrefix(prefix, path, skipPrefix) {
|
|
3170
|
-
if (skipPrefix) {
|
|
3171
|
-
return path;
|
|
3172
|
-
}
|
|
3173
|
-
if (prefix === "") {
|
|
3174
|
-
return path;
|
|
3175
|
-
}
|
|
3176
|
-
if (path.startsWith("/")) {
|
|
3177
|
-
return `${prefix}${path}`;
|
|
3178
|
-
}
|
|
3179
|
-
return `${prefix}/${path}`;
|
|
3180
|
-
}
|
|
3181
|
-
async function loadManifest(url) {
|
|
3182
|
-
const fetchFn = globalThis.fetch;
|
|
3183
|
-
if (typeof fetchFn !== "function") {
|
|
3184
|
-
throw new ManifestValidationError(
|
|
3185
|
-
"loadManifest requires a global fetch; pass the manifest object directly on runtimes without fetch"
|
|
3186
|
-
);
|
|
3187
|
-
}
|
|
3188
|
-
const res = await fetchFn(url);
|
|
3189
|
-
if (!res.ok) {
|
|
3190
2763
|
throw new ManifestValidationError(
|
|
3191
|
-
`
|
|
2764
|
+
`unknown secret action ${JSON.stringify(action)}; expected read, write, delete, list, or metadata`
|
|
3192
2765
|
);
|
|
3193
2766
|
}
|
|
3194
|
-
|
|
3195
|
-
return validateManifest(json);
|
|
2767
|
+
return out;
|
|
3196
2768
|
}
|
|
3197
|
-
function
|
|
3198
|
-
if (
|
|
3199
|
-
|
|
2769
|
+
function secretNameFromSpec(fallbackName, spec) {
|
|
2770
|
+
if (spec !== null && typeof spec === "object" && !Array.isArray(spec)) {
|
|
2771
|
+
return spec.name ?? fallbackName;
|
|
3200
2772
|
}
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
2773
|
+
return fallbackName;
|
|
2774
|
+
}
|
|
2775
|
+
function secretScopeFromSpec(spec) {
|
|
2776
|
+
if (spec !== null && typeof spec === "object" && !Array.isArray(spec)) {
|
|
2777
|
+
return spec.scope;
|
|
3206
2778
|
}
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3210
|
-
|
|
2779
|
+
return void 0;
|
|
2780
|
+
}
|
|
2781
|
+
function secretActionsFromSpec(name, spec) {
|
|
2782
|
+
if (spec === true) {
|
|
2783
|
+
return ["read"];
|
|
3211
2784
|
}
|
|
3212
|
-
if (typeof
|
|
3213
|
-
|
|
3214
|
-
"manifest.name is required and must be a non-empty string"
|
|
3215
|
-
);
|
|
2785
|
+
if (typeof spec === "string") {
|
|
2786
|
+
return [spec];
|
|
3216
2787
|
}
|
|
3217
|
-
if (
|
|
3218
|
-
|
|
3219
|
-
"manifest.did must be a non-empty DID string"
|
|
3220
|
-
);
|
|
2788
|
+
if (Array.isArray(spec)) {
|
|
2789
|
+
return spec;
|
|
3221
2790
|
}
|
|
3222
|
-
if (
|
|
2791
|
+
if (spec === null || typeof spec !== "object") {
|
|
3223
2792
|
throw new ManifestValidationError(
|
|
3224
|
-
|
|
2793
|
+
`manifest.secrets.${name} must be true, a string action, an actions array, or an object`
|
|
3225
2794
|
);
|
|
3226
2795
|
}
|
|
3227
|
-
if (
|
|
3228
|
-
|
|
2796
|
+
if (spec.actions === void 0) {
|
|
2797
|
+
return ["read"];
|
|
3229
2798
|
}
|
|
3230
|
-
if (
|
|
3231
|
-
|
|
3232
|
-
throw new ManifestValidationError(
|
|
3233
|
-
"manifest.permissions must be an array"
|
|
3234
|
-
);
|
|
3235
|
-
}
|
|
3236
|
-
m.permissions.forEach(
|
|
3237
|
-
(p, i) => validatePermissionEntry(p, `permissions[${i}]`)
|
|
3238
|
-
);
|
|
2799
|
+
if (typeof spec.actions === "string") {
|
|
2800
|
+
return [spec.actions];
|
|
3239
2801
|
}
|
|
3240
|
-
if (
|
|
3241
|
-
|
|
2802
|
+
if (Array.isArray(spec.actions)) {
|
|
2803
|
+
return spec.actions;
|
|
3242
2804
|
}
|
|
3243
|
-
|
|
2805
|
+
throw new ManifestValidationError(
|
|
2806
|
+
`manifest.secrets.${name}.actions must be a string or array`
|
|
2807
|
+
);
|
|
3244
2808
|
}
|
|
3245
|
-
function
|
|
3246
|
-
if (secrets ===
|
|
3247
|
-
|
|
2809
|
+
function secretEntriesForManifest(secrets) {
|
|
2810
|
+
if (secrets === void 0) {
|
|
2811
|
+
return [];
|
|
3248
2812
|
}
|
|
2813
|
+
const entries = [];
|
|
3249
2814
|
for (const [name, spec] of Object.entries(secrets)) {
|
|
3250
|
-
if (!import_sdk_services4.SECRET_NAME_RE.test(name)) {
|
|
3251
|
-
throw new ManifestValidationError(
|
|
3252
|
-
`manifest.secrets.${name} must match ${import_sdk_services4.SECRET_NAME_RE.source}`
|
|
3253
|
-
);
|
|
3254
|
-
}
|
|
3255
|
-
try {
|
|
3256
|
-
(0, import_sdk_services4.resolveSecretPath)(
|
|
3257
|
-
secretNameFromSpec(name, spec),
|
|
3258
|
-
{ scope: secretScopeFromSpec(spec) }
|
|
3259
|
-
);
|
|
3260
|
-
} catch (error) {
|
|
3261
|
-
throw new ManifestValidationError(
|
|
3262
|
-
`manifest.secrets.${name}: ${error instanceof Error ? error.message : String(error)}`
|
|
3263
|
-
);
|
|
3264
|
-
}
|
|
3265
2815
|
const actions = secretActionsFromSpec(name, spec);
|
|
3266
|
-
|
|
3267
|
-
|
|
3268
|
-
|
|
3269
|
-
|
|
3270
|
-
}
|
|
3271
|
-
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
|
|
3275
|
-
|
|
2816
|
+
const secretPath = (0, import_sdk_services4.resolveSecretPath)(
|
|
2817
|
+
secretNameFromSpec(name, spec),
|
|
2818
|
+
{ scope: secretScopeFromSpec(spec) }
|
|
2819
|
+
);
|
|
2820
|
+
const extra = spec !== true && typeof spec === "object" && !Array.isArray(spec) ? spec : {};
|
|
2821
|
+
entries.push({
|
|
2822
|
+
service: VAULT_PERMISSION_SERVICE,
|
|
2823
|
+
space: SECRETS_SPACE,
|
|
2824
|
+
path: secretPath.vaultKey,
|
|
2825
|
+
actions: normalizeSecretActions(actions),
|
|
2826
|
+
skipPrefix: true,
|
|
2827
|
+
...extra.expiry !== void 0 ? { expiry: extra.expiry } : {},
|
|
2828
|
+
...extra.description !== void 0 ? { description: extra.description } : {}
|
|
2829
|
+
});
|
|
2830
|
+
}
|
|
2831
|
+
return entries;
|
|
2832
|
+
}
|
|
2833
|
+
function resolveEntry(entry, prefix, _inheritedExpiryMs, inheritedSpace) {
|
|
2834
|
+
const skipPrefixForEntry = entry.skipPrefix === true || entry.service === ENCRYPTION_PERMISSION_SERVICE;
|
|
2835
|
+
const resolvedPath = applyPrefix(prefix, entry.path, skipPrefixForEntry);
|
|
2836
|
+
const entryExpiryMs = entry.expiry !== void 0 ? parseExpiry(entry.expiry) : void 0;
|
|
2837
|
+
return expandPermissionEntry({
|
|
2838
|
+
...entry,
|
|
2839
|
+
space: entry.space ?? inheritedSpace,
|
|
2840
|
+
path: resolvedPath,
|
|
2841
|
+
skipPrefix: true
|
|
2842
|
+
}).map((expanded) => ({
|
|
2843
|
+
service: expanded.service,
|
|
2844
|
+
space: expanded.space ?? inheritedSpace,
|
|
2845
|
+
path: expanded.path,
|
|
2846
|
+
actions: expanded.actions,
|
|
2847
|
+
// Only populate `expiryMs` when the entry had its own expiry override.
|
|
2848
|
+
// When absent, callers use the parent (delegation or manifest) expiry
|
|
2849
|
+
// which is carried on ResolvedDelegate.expiryMs / ResolvedCapabilities.expiryMs.
|
|
2850
|
+
...entryExpiryMs !== void 0 ? { expiryMs: entryExpiryMs } : {},
|
|
2851
|
+
...entry.description !== void 0 ? { description: entry.description } : {}
|
|
2852
|
+
}));
|
|
2853
|
+
}
|
|
2854
|
+
function expandVaultPermissionEntry(entry) {
|
|
2855
|
+
const byBase = /* @__PURE__ */ new Map();
|
|
2856
|
+
for (const action of entry.actions) {
|
|
2857
|
+
const expansion = vaultActionExpansion(action);
|
|
2858
|
+
for (const base of expansion.bases) {
|
|
2859
|
+
const actions = byBase.get(base) ?? [];
|
|
2860
|
+
if (!actions.includes(expansion.action)) {
|
|
2861
|
+
actions.push(expansion.action);
|
|
3276
2862
|
}
|
|
3277
|
-
|
|
3278
|
-
if (spec !== null && typeof spec === "object" && !Array.isArray(spec) && spec.expiry !== void 0) {
|
|
3279
|
-
parseExpiry(spec.expiry);
|
|
2863
|
+
byBase.set(base, actions);
|
|
3280
2864
|
}
|
|
3281
2865
|
}
|
|
2866
|
+
return [...byBase.entries()].map(([base, actions]) => ({
|
|
2867
|
+
...entry,
|
|
2868
|
+
service: "tinycloud.kv",
|
|
2869
|
+
path: vaultKVPath(base, entry.path),
|
|
2870
|
+
actions,
|
|
2871
|
+
skipPrefix: true
|
|
2872
|
+
}));
|
|
3282
2873
|
}
|
|
3283
|
-
function
|
|
3284
|
-
|
|
3285
|
-
|
|
2874
|
+
function vaultActionExpansion(action) {
|
|
2875
|
+
const normalized = normalizeVaultAction(action);
|
|
2876
|
+
if (normalized === "read" || normalized === "get") {
|
|
2877
|
+
return { bases: ["vault"], action: "tinycloud.kv/get" };
|
|
3286
2878
|
}
|
|
3287
|
-
|
|
3288
|
-
|
|
3289
|
-
throw new ManifestValidationError(`${path}.service is required`);
|
|
2879
|
+
if (normalized === "write" || normalized === "put") {
|
|
2880
|
+
return { bases: ["vault"], action: "tinycloud.kv/put" };
|
|
3290
2881
|
}
|
|
3291
|
-
if (
|
|
3292
|
-
|
|
3293
|
-
`${path}.space must be a non-empty string`
|
|
3294
|
-
);
|
|
2882
|
+
if (normalized === "delete" || normalized === "del") {
|
|
2883
|
+
return { bases: ["vault"], action: "tinycloud.kv/del" };
|
|
3295
2884
|
}
|
|
3296
|
-
if (
|
|
3297
|
-
|
|
3298
|
-
`${path}.path is required (use "" or "/" for root)`
|
|
3299
|
-
);
|
|
2885
|
+
if (normalized === "list") {
|
|
2886
|
+
return { bases: ["vault"], action: "tinycloud.kv/list" };
|
|
3300
2887
|
}
|
|
3301
|
-
if (
|
|
2888
|
+
if (normalized === "head") {
|
|
2889
|
+
return { bases: ["vault"], action: "tinycloud.kv/get" };
|
|
2890
|
+
}
|
|
2891
|
+
if (normalized === "metadata") {
|
|
2892
|
+
return { bases: ["vault"], action: "tinycloud.kv/metadata" };
|
|
2893
|
+
}
|
|
2894
|
+
throw new ManifestValidationError(
|
|
2895
|
+
`unknown vault action ${JSON.stringify(action)}; expected read, write, delete, get, put, del, list, head, or metadata`
|
|
2896
|
+
);
|
|
2897
|
+
}
|
|
2898
|
+
function normalizeVaultAction(action) {
|
|
2899
|
+
if (action.startsWith(`${VAULT_PERMISSION_SERVICE}/`)) {
|
|
2900
|
+
return action.slice(`${VAULT_PERMISSION_SERVICE}/`.length);
|
|
2901
|
+
}
|
|
2902
|
+
if (action.startsWith("tinycloud.kv/")) {
|
|
2903
|
+
return action.slice("tinycloud.kv/".length);
|
|
2904
|
+
}
|
|
2905
|
+
if (action.includes("/")) {
|
|
3302
2906
|
throw new ManifestValidationError(
|
|
3303
|
-
|
|
2907
|
+
`unknown vault action ${JSON.stringify(action)}; expected a tinycloud.vault or tinycloud.kv action`
|
|
3304
2908
|
);
|
|
3305
2909
|
}
|
|
3306
|
-
|
|
3307
|
-
|
|
3308
|
-
|
|
3309
|
-
|
|
3310
|
-
|
|
2910
|
+
return action;
|
|
2911
|
+
}
|
|
2912
|
+
function vaultKVPath(base, path) {
|
|
2913
|
+
const normalized = path.startsWith("/") ? path.slice(1) : path;
|
|
2914
|
+
return `${base}/${normalized}`;
|
|
2915
|
+
}
|
|
2916
|
+
function cloneResourceCapability(entry) {
|
|
2917
|
+
return {
|
|
2918
|
+
service: entry.service,
|
|
2919
|
+
space: entry.space,
|
|
2920
|
+
path: entry.path,
|
|
2921
|
+
actions: [...entry.actions],
|
|
2922
|
+
...entry.expiryMs !== void 0 ? { expiryMs: entry.expiryMs } : {},
|
|
2923
|
+
...entry.description !== void 0 ? { description: entry.description } : {}
|
|
2924
|
+
};
|
|
2925
|
+
}
|
|
2926
|
+
function clonePermissionEntry(entry) {
|
|
2927
|
+
return {
|
|
2928
|
+
service: entry.service,
|
|
2929
|
+
...entry.space !== void 0 ? { space: entry.space } : {},
|
|
2930
|
+
path: entry.path,
|
|
2931
|
+
actions: [...entry.actions],
|
|
2932
|
+
...entry.skipPrefix !== void 0 ? { skipPrefix: entry.skipPrefix } : {},
|
|
2933
|
+
...entry.expiry !== void 0 ? { expiry: entry.expiry } : {},
|
|
2934
|
+
...entry.description !== void 0 ? { description: entry.description } : {}
|
|
2935
|
+
};
|
|
2936
|
+
}
|
|
2937
|
+
function dedupeResources(resources) {
|
|
2938
|
+
const byKey = /* @__PURE__ */ new Map();
|
|
2939
|
+
for (const resource of resources) {
|
|
2940
|
+
const key = `${resource.service}\0${resource.space}\0${resource.path}\0${resource.expiryMs ?? ""}`;
|
|
2941
|
+
const existing = byKey.get(key);
|
|
2942
|
+
if (existing === void 0) {
|
|
2943
|
+
byKey.set(key, cloneResourceCapability(resource));
|
|
2944
|
+
continue;
|
|
3311
2945
|
}
|
|
3312
|
-
|
|
3313
|
-
|
|
2946
|
+
const seen = new Set(existing.actions);
|
|
2947
|
+
for (const action of resource.actions) {
|
|
2948
|
+
if (!seen.has(action)) {
|
|
2949
|
+
existing.actions.push(action);
|
|
2950
|
+
seen.add(action);
|
|
2951
|
+
}
|
|
2952
|
+
}
|
|
2953
|
+
if (existing.description === void 0 && resource.description !== void 0) {
|
|
2954
|
+
existing.description = resource.description;
|
|
3314
2955
|
}
|
|
3315
2956
|
}
|
|
3316
|
-
|
|
3317
|
-
parseExpiry(entry.expiry);
|
|
3318
|
-
}
|
|
2957
|
+
return [...byKey.values()];
|
|
3319
2958
|
}
|
|
3320
|
-
function
|
|
3321
|
-
|
|
3322
|
-
|
|
3323
|
-
|
|
3324
|
-
|
|
3325
|
-
|
|
3326
|
-
}
|
|
3327
|
-
if (typeof value !== "string") {
|
|
3328
|
-
return true;
|
|
3329
|
-
}
|
|
3330
|
-
const normalized = value.trim().toLowerCase();
|
|
3331
|
-
if (normalized === "admin" || normalized === "all") {
|
|
3332
|
-
return normalized;
|
|
3333
|
-
}
|
|
3334
|
-
return true;
|
|
2959
|
+
function capabilitiesReadPermission(space) {
|
|
2960
|
+
return {
|
|
2961
|
+
service: "tinycloud.capabilities",
|
|
2962
|
+
space,
|
|
2963
|
+
path: "",
|
|
2964
|
+
actions: ["tinycloud.capabilities/read"]
|
|
2965
|
+
};
|
|
3335
2966
|
}
|
|
3336
|
-
function
|
|
3337
|
-
if (
|
|
2967
|
+
function withCapabilitiesReadForSpaces(resources) {
|
|
2968
|
+
if (resources.length === 0) {
|
|
3338
2969
|
return [];
|
|
3339
2970
|
}
|
|
3340
|
-
const
|
|
3341
|
-
|
|
3342
|
-
|
|
3343
|
-
|
|
3344
|
-
|
|
3345
|
-
|
|
3346
|
-
|
|
2971
|
+
const spaces = new Set(
|
|
2972
|
+
resources.filter((resource) => resource.service !== ENCRYPTION_PERMISSION_SERVICE).map((resource) => resource.space)
|
|
2973
|
+
);
|
|
2974
|
+
return dedupeResources([
|
|
2975
|
+
...resources,
|
|
2976
|
+
...[...spaces].map(capabilitiesReadPermission)
|
|
2977
|
+
]);
|
|
2978
|
+
}
|
|
2979
|
+
function accountRegistryPermissions() {
|
|
2980
|
+
return [ACCOUNT_REGISTRY_PATH, "spaces/"].map((path) => ({
|
|
2981
|
+
service: "tinycloud.kv",
|
|
2982
|
+
space: ACCOUNT_REGISTRY_SPACE,
|
|
2983
|
+
path,
|
|
2984
|
+
actions: ["tinycloud.kv/get", "tinycloud.kv/put", "tinycloud.kv/list"]
|
|
3347
2985
|
}));
|
|
3348
2986
|
}
|
|
3349
|
-
function
|
|
3350
|
-
const manifest = validateManifest(input);
|
|
3351
|
-
const prefix = manifest.prefix !== void 0 ? manifest.prefix : manifest.app_id;
|
|
3352
|
-
const space = manifest.space ?? DEFAULT_MANIFEST_SPACE;
|
|
3353
|
-
const expiryMs = parseExpiry(manifest.expiry ?? DEFAULT_EXPIRY);
|
|
3354
|
-
const includePublicSpace = manifest.includePublicSpace ?? true;
|
|
3355
|
-
const tier = normalizeDefaults(manifest.defaults);
|
|
3356
|
-
const defaultEntries = defaultEntriesForTier(tier);
|
|
3357
|
-
const explicitEntries = manifest.permissions ?? [];
|
|
3358
|
-
const secretEntries = secretEntriesForManifest(manifest.secrets);
|
|
3359
|
-
const allEntries = [
|
|
3360
|
-
...defaultEntries,
|
|
3361
|
-
...explicitEntries,
|
|
3362
|
-
...secretEntries
|
|
3363
|
-
];
|
|
3364
|
-
const resources = withCapabilitiesReadForSpaces(
|
|
3365
|
-
allEntries.flatMap((entry) => resolveEntry(entry, prefix, expiryMs, space))
|
|
3366
|
-
);
|
|
3367
|
-
const additionalDelegates = manifest.did === void 0 ? [] : [
|
|
3368
|
-
{
|
|
3369
|
-
did: manifest.did,
|
|
3370
|
-
name: manifest.name,
|
|
3371
|
-
expiryMs,
|
|
3372
|
-
permissions: resources.map(cloneResourceCapability)
|
|
3373
|
-
}
|
|
3374
|
-
];
|
|
2987
|
+
function accountRegistryIndexPermission() {
|
|
3375
2988
|
return {
|
|
3376
|
-
|
|
3377
|
-
|
|
3378
|
-
|
|
3379
|
-
|
|
3380
|
-
expiryMs,
|
|
3381
|
-
includePublicSpace,
|
|
3382
|
-
additionalDelegates
|
|
2989
|
+
service: "tinycloud.sql",
|
|
2990
|
+
space: ACCOUNT_REGISTRY_SPACE,
|
|
2991
|
+
path: "account",
|
|
2992
|
+
actions: ["tinycloud.sql/read", "tinycloud.sql/write", "tinycloud.sql/ddl"]
|
|
3383
2993
|
};
|
|
3384
2994
|
}
|
|
3385
|
-
function
|
|
3386
|
-
|
|
3387
|
-
|
|
3388
|
-
|
|
3389
|
-
|
|
3390
|
-
|
|
3391
|
-
|
|
2995
|
+
function composeManifestRequest(inputs, options = {}) {
|
|
2996
|
+
if (!Array.isArray(inputs) || inputs.length === 0) {
|
|
2997
|
+
throw new ManifestValidationError(
|
|
2998
|
+
"composeManifestRequest requires at least one manifest"
|
|
2999
|
+
);
|
|
3000
|
+
}
|
|
3001
|
+
const includeAccountRegistryPermissions = options.includeAccountRegistryPermissions ?? true;
|
|
3002
|
+
const manifests = inputs.map(validateManifest);
|
|
3003
|
+
const resolved = manifests.map(resolveManifest);
|
|
3004
|
+
const resources = resolved.flatMap((entry) => entry.resources);
|
|
3005
|
+
const delegationTargets = resolved.flatMap(
|
|
3006
|
+
(entry) => entry.additionalDelegates.map((delegate) => ({
|
|
3007
|
+
...delegate,
|
|
3008
|
+
permissions: dedupeResources(delegate.permissions)
|
|
3009
|
+
}))
|
|
3010
|
+
);
|
|
3011
|
+
if (includeAccountRegistryPermissions) {
|
|
3012
|
+
resources.push(...accountRegistryPermissions());
|
|
3013
|
+
resources.push(accountRegistryIndexPermission());
|
|
3014
|
+
}
|
|
3015
|
+
const resourcesWithImplicitCapabilities = withCapabilitiesReadForSpaces(resources);
|
|
3016
|
+
const manifestsByAppId = /* @__PURE__ */ new Map();
|
|
3017
|
+
for (const manifest of manifests) {
|
|
3018
|
+
const current = manifestsByAppId.get(manifest.app_id);
|
|
3019
|
+
if (current === void 0) {
|
|
3020
|
+
manifestsByAppId.set(manifest.app_id, [manifest]);
|
|
3021
|
+
} else {
|
|
3022
|
+
current.push(manifest);
|
|
3392
3023
|
}
|
|
3024
|
+
}
|
|
3025
|
+
const registryRecords = includeAccountRegistryPermissions ? [...manifestsByAppId.entries()].map(([app_id, appManifests]) => ({
|
|
3026
|
+
key: `${ACCOUNT_REGISTRY_PATH}${app_id}`,
|
|
3027
|
+
app_id,
|
|
3028
|
+
manifests: appManifests.map((manifest) => ({
|
|
3029
|
+
...manifest,
|
|
3030
|
+
permissions: manifest.permissions?.map(clonePermissionEntry)
|
|
3031
|
+
}))
|
|
3032
|
+
})) : [];
|
|
3033
|
+
return {
|
|
3034
|
+
manifests,
|
|
3035
|
+
resources: resourcesWithImplicitCapabilities,
|
|
3036
|
+
delegationTargets,
|
|
3037
|
+
registryRecords,
|
|
3038
|
+
expiryMs: Math.max(...resolved.map((entry) => entry.expiryMs)),
|
|
3039
|
+
includePublicSpace: resolved.some((entry) => entry.includePublicSpace)
|
|
3393
3040
|
};
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3041
|
+
}
|
|
3042
|
+
function resourceCapabilitiesToAbilitiesMap(resources) {
|
|
3043
|
+
const out = {};
|
|
3044
|
+
for (const r of resources) {
|
|
3045
|
+
const shortService = SERVICE_LONG_TO_SHORT[r.service];
|
|
3046
|
+
if (shortService === void 0) {
|
|
3047
|
+
throw new ManifestValidationError(
|
|
3048
|
+
`unknown service '${r.service}' \u2014 no short-form mapping. Known services: ${Object.keys(SERVICE_LONG_TO_SHORT).join(", ")}`
|
|
3049
|
+
);
|
|
3398
3050
|
}
|
|
3399
|
-
if (
|
|
3400
|
-
|
|
3401
|
-
continue;
|
|
3051
|
+
if (out[shortService] === void 0) {
|
|
3052
|
+
out[shortService] = {};
|
|
3402
3053
|
}
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3054
|
+
const pathsMap = out[shortService];
|
|
3055
|
+
const existing = pathsMap[r.path];
|
|
3056
|
+
if (existing === void 0) {
|
|
3057
|
+
pathsMap[r.path] = [...r.actions];
|
|
3058
|
+
} else {
|
|
3059
|
+
const seen = new Set(existing);
|
|
3060
|
+
for (const action of r.actions) {
|
|
3061
|
+
if (!seen.has(action)) {
|
|
3062
|
+
existing.push(action);
|
|
3063
|
+
seen.add(action);
|
|
3064
|
+
}
|
|
3065
|
+
}
|
|
3406
3066
|
}
|
|
3407
|
-
|
|
3408
|
-
|
|
3409
|
-
|
|
3067
|
+
}
|
|
3068
|
+
return out;
|
|
3069
|
+
}
|
|
3070
|
+
function resourceCapabilitiesToSpaceAbilitiesMap(resources) {
|
|
3071
|
+
const grouped = /* @__PURE__ */ new Map();
|
|
3072
|
+
for (const resource of resources) {
|
|
3073
|
+
const entries = grouped.get(resource.space);
|
|
3074
|
+
if (entries === void 0) {
|
|
3075
|
+
grouped.set(resource.space, [resource]);
|
|
3076
|
+
} else {
|
|
3077
|
+
entries.push(resource);
|
|
3410
3078
|
}
|
|
3411
|
-
|
|
3412
|
-
|
|
3413
|
-
|
|
3079
|
+
}
|
|
3080
|
+
const out = {};
|
|
3081
|
+
for (const [space, entries] of grouped.entries()) {
|
|
3082
|
+
out[space] = resourceCapabilitiesToAbilitiesMap(entries);
|
|
3083
|
+
}
|
|
3084
|
+
return out;
|
|
3085
|
+
}
|
|
3086
|
+
function manifestAbilitiesUnion(resolved) {
|
|
3087
|
+
const all = [...resolved.resources];
|
|
3088
|
+
for (const delegate of resolved.additionalDelegates) {
|
|
3089
|
+
for (const perm of delegate.permissions) {
|
|
3090
|
+
all.push(perm);
|
|
3414
3091
|
}
|
|
3415
|
-
throw new ManifestValidationError(
|
|
3416
|
-
`unknown secret action ${JSON.stringify(action)}; expected read, write, delete, list, or metadata`
|
|
3417
|
-
);
|
|
3418
3092
|
}
|
|
3419
|
-
return
|
|
3420
|
-
}
|
|
3421
|
-
|
|
3422
|
-
|
|
3423
|
-
|
|
3093
|
+
return resourceCapabilitiesToAbilitiesMap(all);
|
|
3094
|
+
}
|
|
3095
|
+
|
|
3096
|
+
// src/account/AccountService.ts
|
|
3097
|
+
var SERVICE_NAME2 = "account";
|
|
3098
|
+
var ACCOUNT_INDEX_DB = "account";
|
|
3099
|
+
var ACCOUNT_INDEX_NAMESPACE = "tinycloud.account.index";
|
|
3100
|
+
var ACCOUNT_SPACES_PATH = "spaces/";
|
|
3101
|
+
var AccountService = class {
|
|
3102
|
+
constructor(config) {
|
|
3103
|
+
this.config = config;
|
|
3104
|
+
this.applications = {
|
|
3105
|
+
list: async (options = {}) => {
|
|
3106
|
+
if (options.preferIndex) {
|
|
3107
|
+
const indexed = await this.index.applications.list();
|
|
3108
|
+
if (indexed.ok && indexed.data.length > 0) return indexed;
|
|
3109
|
+
if (!indexed.ok && !isMissingIndexError(indexed.error)) return indexed;
|
|
3110
|
+
const canonical = await this.applications.list();
|
|
3111
|
+
if (canonical.ok && options.refreshIndex !== false) {
|
|
3112
|
+
await this.replaceApplicationsIndexQuietly(canonical.data);
|
|
3113
|
+
}
|
|
3114
|
+
return canonical;
|
|
3115
|
+
}
|
|
3116
|
+
const kvResult = this.accountKV();
|
|
3117
|
+
if (!kvResult.ok) return kvResult;
|
|
3118
|
+
const listed = await kvResult.data.list({ prefix: ACCOUNT_REGISTRY_PATH });
|
|
3119
|
+
if (!listed.ok) return accountErr(listed.error);
|
|
3120
|
+
const applications = [];
|
|
3121
|
+
for (const key of listed.data.keys) {
|
|
3122
|
+
const loaded = await kvResult.data.get(key);
|
|
3123
|
+
if (!loaded.ok) return accountErr(loaded.error);
|
|
3124
|
+
applications.push(applicationFromRecord(key, loaded.data.data));
|
|
3125
|
+
}
|
|
3126
|
+
applications.sort((a, b) => a.appId.localeCompare(b.appId));
|
|
3127
|
+
return (0, import_sdk_services5.ok)(applications);
|
|
3128
|
+
},
|
|
3129
|
+
get: async (appId) => {
|
|
3130
|
+
const kvResult = this.accountKV();
|
|
3131
|
+
if (!kvResult.ok) return kvResult;
|
|
3132
|
+
const key = applicationKey(appId);
|
|
3133
|
+
const loaded = await kvResult.data.get(key);
|
|
3134
|
+
if (!loaded.ok) return accountErr(loaded.error);
|
|
3135
|
+
return (0, import_sdk_services5.ok)(applicationFromRecord(key, loaded.data.data));
|
|
3136
|
+
},
|
|
3137
|
+
register: async (manifest) => {
|
|
3138
|
+
const manifests = Array.isArray(manifest) ? manifest : [manifest];
|
|
3139
|
+
const request = composeManifestRequest(manifests);
|
|
3140
|
+
if (request.registryRecords.length === 0) {
|
|
3141
|
+
return (0, import_sdk_services5.err)(
|
|
3142
|
+
(0, import_sdk_services5.serviceError)(
|
|
3143
|
+
"INVALID_MANIFEST",
|
|
3144
|
+
"Manifest did not produce an account application registry record",
|
|
3145
|
+
SERVICE_NAME2
|
|
3146
|
+
)
|
|
3147
|
+
);
|
|
3148
|
+
}
|
|
3149
|
+
await this.config.ensureAccountSpaceHosted?.();
|
|
3150
|
+
const kvResult = this.accountKV();
|
|
3151
|
+
if (!kvResult.ok) return kvResult;
|
|
3152
|
+
let registered;
|
|
3153
|
+
for (const record of request.registryRecords) {
|
|
3154
|
+
const manifestHash = hashJson(record.manifests);
|
|
3155
|
+
if (await this.indexHasApplicationHash(record.app_id, manifestHash)) {
|
|
3156
|
+
registered = {
|
|
3157
|
+
appId: record.app_id,
|
|
3158
|
+
manifests: record.manifests,
|
|
3159
|
+
manifestHash,
|
|
3160
|
+
name: record.manifests[0]?.name,
|
|
3161
|
+
description: record.manifests[0]?.description
|
|
3162
|
+
};
|
|
3163
|
+
continue;
|
|
3164
|
+
}
|
|
3165
|
+
const stored = {
|
|
3166
|
+
app_id: record.app_id,
|
|
3167
|
+
manifests: record.manifests,
|
|
3168
|
+
manifest_hash: manifestHash,
|
|
3169
|
+
updated_at: (/* @__PURE__ */ new Date()).toISOString()
|
|
3170
|
+
};
|
|
3171
|
+
const written = await kvResult.data.put(record.key, stored);
|
|
3172
|
+
if (!written.ok) return accountErr(written.error);
|
|
3173
|
+
registered = applicationFromRecord(record.key, stored);
|
|
3174
|
+
await this.upsertApplicationIndexQuietly(registered);
|
|
3175
|
+
}
|
|
3176
|
+
return (0, import_sdk_services5.ok)(registered);
|
|
3177
|
+
},
|
|
3178
|
+
remove: async (appId) => {
|
|
3179
|
+
const kvResult = this.accountKV();
|
|
3180
|
+
if (!kvResult.ok) return kvResult;
|
|
3181
|
+
const removed = await kvResult.data.delete(applicationKey(appId));
|
|
3182
|
+
if (!removed.ok) return accountErr(removed.error);
|
|
3183
|
+
await this.deleteApplicationIndexQuietly(appId);
|
|
3184
|
+
return (0, import_sdk_services5.ok)(void 0);
|
|
3185
|
+
}
|
|
3186
|
+
};
|
|
3187
|
+
this.spaces = {
|
|
3188
|
+
list: async (options = {}) => {
|
|
3189
|
+
if (options.preferIndex) {
|
|
3190
|
+
const indexed = await this.index.spaces.list();
|
|
3191
|
+
if (indexed.ok && indexed.data.length > 0) return indexed;
|
|
3192
|
+
if (!indexed.ok && !isMissingIndexError(indexed.error)) return indexed;
|
|
3193
|
+
const canonical = await this.spaces.syncAccessible();
|
|
3194
|
+
if (canonical.ok && options.refreshIndex !== false) {
|
|
3195
|
+
await this.replaceSpacesIndexQuietly(canonical.data);
|
|
3196
|
+
}
|
|
3197
|
+
return canonical;
|
|
3198
|
+
}
|
|
3199
|
+
const kvResult = this.accountKV();
|
|
3200
|
+
if (!kvResult.ok) return kvResult;
|
|
3201
|
+
const listed = await kvResult.data.list({ prefix: ACCOUNT_SPACES_PATH });
|
|
3202
|
+
if (!listed.ok) return accountErr(listed.error);
|
|
3203
|
+
const spaces = [];
|
|
3204
|
+
for (const key of listed.data.keys) {
|
|
3205
|
+
const loaded = await kvResult.data.get(key);
|
|
3206
|
+
if (!loaded.ok) return accountErr(loaded.error);
|
|
3207
|
+
spaces.push(spaceFromRecord(key, loaded.data.data));
|
|
3208
|
+
}
|
|
3209
|
+
spaces.sort((a, b) => a.name.localeCompare(b.name) || a.spaceId.localeCompare(b.spaceId));
|
|
3210
|
+
return (0, import_sdk_services5.ok)(spaces);
|
|
3211
|
+
},
|
|
3212
|
+
get: async (spaceId) => {
|
|
3213
|
+
const kvResult = this.accountKV();
|
|
3214
|
+
if (!kvResult.ok) return kvResult;
|
|
3215
|
+
const loaded = await kvResult.data.get(spaceKey(spaceId));
|
|
3216
|
+
if (!loaded.ok) return accountErr(loaded.error);
|
|
3217
|
+
return (0, import_sdk_services5.ok)(spaceFromRecord(spaceKey(spaceId), loaded.data.data));
|
|
3218
|
+
},
|
|
3219
|
+
register: async (space) => {
|
|
3220
|
+
await this.config.ensureAccountSpaceHosted?.();
|
|
3221
|
+
const kvResult = this.accountKV();
|
|
3222
|
+
if (!kvResult.ok) return kvResult;
|
|
3223
|
+
const stored = spaceRecordFromInput(space);
|
|
3224
|
+
const written = await kvResult.data.put(spaceKey(stored.space_id), stored);
|
|
3225
|
+
if (!written.ok) return accountErr(written.error);
|
|
3226
|
+
const registered = spaceFromRecord(spaceKey(stored.space_id), stored);
|
|
3227
|
+
await this.upsertSpaceIndexQuietly(registered);
|
|
3228
|
+
return (0, import_sdk_services5.ok)(registered);
|
|
3229
|
+
},
|
|
3230
|
+
syncAccessible: async () => {
|
|
3231
|
+
const listed = await this.config.getSpaces().list();
|
|
3232
|
+
if (!listed.ok) return accountErr(listed.error);
|
|
3233
|
+
const registered = [];
|
|
3234
|
+
for (const space of listed.data) {
|
|
3235
|
+
const result = await this.spaces.register(space);
|
|
3236
|
+
if (!result.ok) return result;
|
|
3237
|
+
registered.push(result.data);
|
|
3238
|
+
}
|
|
3239
|
+
return (0, import_sdk_services5.ok)(registered);
|
|
3240
|
+
},
|
|
3241
|
+
remove: async (spaceId) => {
|
|
3242
|
+
const kvResult = this.accountKV();
|
|
3243
|
+
if (!kvResult.ok) return kvResult;
|
|
3244
|
+
const removed = await kvResult.data.delete(spaceKey(spaceId));
|
|
3245
|
+
if (!removed.ok) return accountErr(removed.error);
|
|
3246
|
+
await this.deleteSpaceIndexQuietly(spaceId);
|
|
3247
|
+
return (0, import_sdk_services5.ok)(void 0);
|
|
3248
|
+
}
|
|
3249
|
+
};
|
|
3250
|
+
this.delegations = {
|
|
3251
|
+
list: async (options = {}) => {
|
|
3252
|
+
if (options.preferIndex) {
|
|
3253
|
+
const indexed = await this.index.delegations.list(options);
|
|
3254
|
+
if (indexed.ok && indexed.data.length > 0) return indexed;
|
|
3255
|
+
if (!indexed.ok && !isMissingIndexError(indexed.error)) return indexed;
|
|
3256
|
+
const live = await this.delegations.list({
|
|
3257
|
+
direction: options.direction,
|
|
3258
|
+
space: options.space
|
|
3259
|
+
});
|
|
3260
|
+
if (live.ok && options.refreshIndex !== false) {
|
|
3261
|
+
await this.replaceDelegationsIndexQuietly(live.data);
|
|
3262
|
+
}
|
|
3263
|
+
return live;
|
|
3264
|
+
}
|
|
3265
|
+
const spaces = await this.config.getSpaces().list();
|
|
3266
|
+
if (!spaces.ok) return accountErr(spaces.error);
|
|
3267
|
+
const targetSpaces = options.space ? spaces.data.filter((space) => space.id === options.space || space.name === options.space) : spaces.data;
|
|
3268
|
+
const delegations = [];
|
|
3269
|
+
for (const space of targetSpaces) {
|
|
3270
|
+
const scoped = this.config.getSpaces().get(space.id).delegations;
|
|
3271
|
+
if (options.direction !== "received") {
|
|
3272
|
+
const granted = await scoped.list();
|
|
3273
|
+
if (!granted.ok) return accountErr(granted.error);
|
|
3274
|
+
delegations.push(...granted.data.map((d) => mapDelegation(d, space, "granted")));
|
|
3275
|
+
}
|
|
3276
|
+
if (options.direction !== "granted") {
|
|
3277
|
+
const received = await scoped.listReceived();
|
|
3278
|
+
if (!received.ok) return accountErr(received.error);
|
|
3279
|
+
delegations.push(...received.data.map((d) => mapDelegation(d, space, "received")));
|
|
3280
|
+
}
|
|
3281
|
+
}
|
|
3282
|
+
delegations.sort((a, b) => a.spaceId.localeCompare(b.spaceId) || a.cid.localeCompare(b.cid));
|
|
3283
|
+
return (0, import_sdk_services5.ok)(delegations);
|
|
3284
|
+
},
|
|
3285
|
+
revoke: async (options) => {
|
|
3286
|
+
const space = await this.resolveSpace(options.space);
|
|
3287
|
+
if (!space.ok) return space;
|
|
3288
|
+
const revoked = await this.config.getSpaces().get(space.data.id).delegations.revoke(options.cid);
|
|
3289
|
+
if (!revoked.ok) return accountErr(revoked.error);
|
|
3290
|
+
return (0, import_sdk_services5.ok)(void 0);
|
|
3291
|
+
}
|
|
3292
|
+
};
|
|
3293
|
+
this.index = {
|
|
3294
|
+
rebuild: async () => {
|
|
3295
|
+
const dbResult = this.accountDb();
|
|
3296
|
+
if (!dbResult.ok) return dbResult;
|
|
3297
|
+
const applications = await this.applications.list();
|
|
3298
|
+
if (!applications.ok) return applications;
|
|
3299
|
+
const spaces = await this.spaces.list();
|
|
3300
|
+
if (!spaces.ok) return spaces;
|
|
3301
|
+
const delegations = await this.delegations.list();
|
|
3302
|
+
if (!delegations.ok) return delegations;
|
|
3303
|
+
const syncedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
3304
|
+
const schema = await this.ensureAccountIndex(dbResult.data);
|
|
3305
|
+
if (!schema.ok) return schema;
|
|
3306
|
+
const statements = [
|
|
3307
|
+
{ sql: "DELETE FROM applications" },
|
|
3308
|
+
{ sql: "DELETE FROM application_state" },
|
|
3309
|
+
{ sql: "DELETE FROM spaces" },
|
|
3310
|
+
{ sql: "DELETE FROM delegations" },
|
|
3311
|
+
{ sql: "DELETE FROM sync_state" },
|
|
3312
|
+
...applications.data.map((app) => ({
|
|
3313
|
+
sql: "INSERT INTO applications (app_id, name, description, updated_at, manifest_json) VALUES (?, ?, ?, ?, ?)",
|
|
3314
|
+
params: [
|
|
3315
|
+
app.appId,
|
|
3316
|
+
app.name ?? null,
|
|
3317
|
+
app.description ?? null,
|
|
3318
|
+
app.updatedAt ?? syncedAt,
|
|
3319
|
+
JSON.stringify(app.manifests)
|
|
3320
|
+
]
|
|
3321
|
+
})),
|
|
3322
|
+
...applications.data.map((app) => ({
|
|
3323
|
+
sql: "INSERT OR REPLACE INTO application_state (app_id, manifest_hash, indexed_at) VALUES (?, ?, ?)",
|
|
3324
|
+
params: [app.appId, app.manifestHash ?? hashJson(app.manifests), syncedAt]
|
|
3325
|
+
})),
|
|
3326
|
+
...spaces.data.map((space) => ({
|
|
3327
|
+
sql: "INSERT OR REPLACE INTO spaces (space_id, name, owner_did, type, permissions_json, status, registered_at, updated_at, expires_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
3328
|
+
params: [
|
|
3329
|
+
space.spaceId,
|
|
3330
|
+
space.name,
|
|
3331
|
+
space.ownerDid,
|
|
3332
|
+
space.type,
|
|
3333
|
+
JSON.stringify(space.permissions),
|
|
3334
|
+
space.status,
|
|
3335
|
+
space.registeredAt ?? syncedAt,
|
|
3336
|
+
space.updatedAt ?? syncedAt,
|
|
3337
|
+
space.expiresAt?.toISOString() ?? null
|
|
3338
|
+
]
|
|
3339
|
+
})),
|
|
3340
|
+
...delegations.data.map((delegation) => ({
|
|
3341
|
+
sql: "INSERT INTO delegations (cid, direction, space_id, space_name, counterparty_did, delegate_did, delegator_did, path, actions_json, expiry, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
3342
|
+
params: [
|
|
3343
|
+
delegation.cid,
|
|
3344
|
+
delegation.direction,
|
|
3345
|
+
delegation.spaceId,
|
|
3346
|
+
delegation.spaceName ?? null,
|
|
3347
|
+
delegation.counterpartyDid,
|
|
3348
|
+
delegation.delegateDid,
|
|
3349
|
+
delegation.delegatorDid ?? null,
|
|
3350
|
+
delegation.path,
|
|
3351
|
+
JSON.stringify(delegation.actions),
|
|
3352
|
+
delegation.expiry.toISOString(),
|
|
3353
|
+
delegation.status,
|
|
3354
|
+
delegation.createdAt?.toISOString() ?? null,
|
|
3355
|
+
syncedAt
|
|
3356
|
+
]
|
|
3357
|
+
})),
|
|
3358
|
+
{
|
|
3359
|
+
sql: "INSERT INTO sync_state (source, synced_at, count) VALUES (?, ?, ?)",
|
|
3360
|
+
params: ["applications", syncedAt, applications.data.length]
|
|
3361
|
+
},
|
|
3362
|
+
{
|
|
3363
|
+
sql: "INSERT INTO sync_state (source, synced_at, count) VALUES (?, ?, ?)",
|
|
3364
|
+
params: ["spaces", syncedAt, spaces.data.length]
|
|
3365
|
+
},
|
|
3366
|
+
{
|
|
3367
|
+
sql: "INSERT INTO sync_state (source, synced_at, count) VALUES (?, ?, ?)",
|
|
3368
|
+
params: ["delegations", syncedAt, delegations.data.length]
|
|
3369
|
+
}
|
|
3370
|
+
];
|
|
3371
|
+
const rebuilt = await dbResult.data.batch(statements);
|
|
3372
|
+
if (!rebuilt.ok) return accountErr(rebuilt.error);
|
|
3373
|
+
return (0, import_sdk_services5.ok)({
|
|
3374
|
+
database: ACCOUNT_INDEX_DB,
|
|
3375
|
+
applications: applications.data.length,
|
|
3376
|
+
spaces: spaces.data.length,
|
|
3377
|
+
delegations: delegations.data.length,
|
|
3378
|
+
syncedAt
|
|
3379
|
+
});
|
|
3380
|
+
},
|
|
3381
|
+
applications: {
|
|
3382
|
+
list: async () => {
|
|
3383
|
+
const dbResult = this.accountDb();
|
|
3384
|
+
if (!dbResult.ok) return dbResult;
|
|
3385
|
+
const queried = await dbResult.data.query(
|
|
3386
|
+
"SELECT applications.app_id, name, description, updated_at, manifest_json, application_state.manifest_hash FROM applications LEFT JOIN application_state ON applications.app_id = application_state.app_id ORDER BY applications.app_id"
|
|
3387
|
+
);
|
|
3388
|
+
if (!queried.ok) return accountErr(queried.error);
|
|
3389
|
+
return (0, import_sdk_services5.ok)(queried.data.rows.map(indexedApplicationFromRow));
|
|
3390
|
+
}
|
|
3391
|
+
},
|
|
3392
|
+
spaces: {
|
|
3393
|
+
list: async () => {
|
|
3394
|
+
const dbResult = this.accountDb();
|
|
3395
|
+
if (!dbResult.ok) return dbResult;
|
|
3396
|
+
const queried = await dbResult.data.query(
|
|
3397
|
+
"SELECT space_id, name, owner_did, type, permissions_json, status, registered_at, updated_at, expires_at FROM spaces ORDER BY name, space_id"
|
|
3398
|
+
);
|
|
3399
|
+
if (!queried.ok) return accountErr(queried.error);
|
|
3400
|
+
return (0, import_sdk_services5.ok)(queried.data.rows.map(indexedSpaceFromRow));
|
|
3401
|
+
}
|
|
3402
|
+
},
|
|
3403
|
+
delegations: {
|
|
3404
|
+
list: async (options = {}) => {
|
|
3405
|
+
const dbResult = this.accountDb();
|
|
3406
|
+
if (!dbResult.ok) return dbResult;
|
|
3407
|
+
const where = [];
|
|
3408
|
+
const params = [];
|
|
3409
|
+
if (options.direction && options.direction !== "all") {
|
|
3410
|
+
where.push("direction = ?");
|
|
3411
|
+
params.push(options.direction);
|
|
3412
|
+
}
|
|
3413
|
+
if (options.space) {
|
|
3414
|
+
where.push("(space_id = ? OR space_name = ?)");
|
|
3415
|
+
params.push(options.space, options.space);
|
|
3416
|
+
}
|
|
3417
|
+
const queried = await dbResult.data.query(
|
|
3418
|
+
`SELECT cid, direction, space_id, space_name, counterparty_did, delegate_did, delegator_did, path, actions_json, expiry, status, created_at FROM delegations${where.length > 0 ? ` WHERE ${where.join(" AND ")}` : ""} ORDER BY space_id, cid`,
|
|
3419
|
+
params
|
|
3420
|
+
);
|
|
3421
|
+
if (!queried.ok) return accountErr(queried.error);
|
|
3422
|
+
return (0, import_sdk_services5.ok)(queried.data.rows.map(indexedDelegationFromRow));
|
|
3423
|
+
}
|
|
3424
|
+
},
|
|
3425
|
+
query: async (sql, params) => {
|
|
3426
|
+
const dbResult = this.accountDb();
|
|
3427
|
+
if (!dbResult.ok) return dbResult;
|
|
3428
|
+
const queried = await dbResult.data.query(sql, params);
|
|
3429
|
+
if (!queried.ok) return accountErr(queried.error);
|
|
3430
|
+
return (0, import_sdk_services5.ok)(queried.data);
|
|
3431
|
+
},
|
|
3432
|
+
status: async () => {
|
|
3433
|
+
const dbResult = this.accountDb();
|
|
3434
|
+
if (!dbResult.ok) return dbResult;
|
|
3435
|
+
const queried = await dbResult.data.query(
|
|
3436
|
+
"SELECT source, synced_at, count FROM sync_state ORDER BY source"
|
|
3437
|
+
);
|
|
3438
|
+
if (!queried.ok) {
|
|
3439
|
+
if (isMissingIndexError(queried.error)) {
|
|
3440
|
+
return (0, import_sdk_services5.ok)({ database: ACCOUNT_INDEX_DB, state: "missing", sources: [] });
|
|
3441
|
+
}
|
|
3442
|
+
return accountErr(queried.error);
|
|
3443
|
+
}
|
|
3444
|
+
return (0, import_sdk_services5.ok)({
|
|
3445
|
+
database: ACCOUNT_INDEX_DB,
|
|
3446
|
+
state: "ready",
|
|
3447
|
+
sources: queried.data.rows.map(([source, syncedAt, count]) => ({
|
|
3448
|
+
source,
|
|
3449
|
+
syncedAt,
|
|
3450
|
+
count
|
|
3451
|
+
}))
|
|
3452
|
+
});
|
|
3453
|
+
}
|
|
3454
|
+
};
|
|
3455
|
+
}
|
|
3456
|
+
async status() {
|
|
3457
|
+
const apps = await this.applications.list();
|
|
3458
|
+
if (!apps.ok) return apps;
|
|
3459
|
+
const delegations = await this.delegations.list();
|
|
3460
|
+
if (!delegations.ok) return delegations;
|
|
3461
|
+
const spaces = await this.spaces.list();
|
|
3462
|
+
if (!spaces.ok) return spaces;
|
|
3463
|
+
return (0, import_sdk_services5.ok)({
|
|
3464
|
+
did: this.config.getDid(),
|
|
3465
|
+
host: this.config.getHost(),
|
|
3466
|
+
primarySpaceId: this.config.getPrimarySpaceId(),
|
|
3467
|
+
accountSpaceId: this.config.getAccountSpaceId(),
|
|
3468
|
+
applications: apps.data.length,
|
|
3469
|
+
spaces: spaces.data.length,
|
|
3470
|
+
grantedDelegations: delegations.data.filter((d) => d.direction === "granted").length,
|
|
3471
|
+
receivedDelegations: delegations.data.filter((d) => d.direction === "received").length
|
|
3472
|
+
});
|
|
3424
3473
|
}
|
|
3425
|
-
|
|
3426
|
-
|
|
3427
|
-
|
|
3428
|
-
|
|
3429
|
-
|
|
3474
|
+
accountKV() {
|
|
3475
|
+
const accountSpaceId = this.config.getAccountSpaceId();
|
|
3476
|
+
if (!accountSpaceId) {
|
|
3477
|
+
return (0, import_sdk_services5.err)(
|
|
3478
|
+
(0, import_sdk_services5.serviceError)(
|
|
3479
|
+
"ACCOUNT_SPACE_UNAVAILABLE",
|
|
3480
|
+
"Account space is unavailable. Sign in with a wallet-backed profile first.",
|
|
3481
|
+
SERVICE_NAME2
|
|
3482
|
+
)
|
|
3483
|
+
);
|
|
3484
|
+
}
|
|
3485
|
+
return (0, import_sdk_services5.ok)(this.config.getSpaces().get(accountSpaceId).kv);
|
|
3430
3486
|
}
|
|
3431
|
-
|
|
3432
|
-
|
|
3433
|
-
|
|
3434
|
-
|
|
3435
|
-
|
|
3487
|
+
accountDb() {
|
|
3488
|
+
const db = this.config.getAccountDb?.();
|
|
3489
|
+
if (!db) {
|
|
3490
|
+
return (0, import_sdk_services5.err)(
|
|
3491
|
+
(0, import_sdk_services5.serviceError)(
|
|
3492
|
+
"ACCOUNT_INDEX_UNAVAILABLE",
|
|
3493
|
+
"Account index database is unavailable. Sign in with a wallet-backed profile first.",
|
|
3494
|
+
SERVICE_NAME2
|
|
3495
|
+
)
|
|
3496
|
+
);
|
|
3497
|
+
}
|
|
3498
|
+
return (0, import_sdk_services5.ok)(db);
|
|
3436
3499
|
}
|
|
3437
|
-
|
|
3438
|
-
|
|
3500
|
+
async indexHasApplicationHash(appId, manifestHash) {
|
|
3501
|
+
const dbResult = this.accountDb();
|
|
3502
|
+
if (!dbResult.ok) return false;
|
|
3503
|
+
const schema = await this.ensureAccountIndex(dbResult.data);
|
|
3504
|
+
if (!schema.ok) return false;
|
|
3505
|
+
const queried = await dbResult.data.query(
|
|
3506
|
+
"SELECT 1 FROM application_state WHERE app_id = ? AND manifest_hash = ? LIMIT 1",
|
|
3507
|
+
[appId, manifestHash]
|
|
3508
|
+
);
|
|
3509
|
+
return queried.ok && queried.data.rows.length > 0;
|
|
3510
|
+
}
|
|
3511
|
+
async upsertApplicationIndexQuietly(app) {
|
|
3512
|
+
await ignoreIndexFailure(() => this.upsertApplicationIndex(app));
|
|
3513
|
+
}
|
|
3514
|
+
async upsertApplicationIndex(app) {
|
|
3515
|
+
const dbResult = this.accountDb();
|
|
3516
|
+
if (!dbResult.ok) return (0, import_sdk_services5.ok)(void 0);
|
|
3517
|
+
const schema = await this.ensureAccountIndex(dbResult.data);
|
|
3518
|
+
if (!schema.ok) return schema;
|
|
3519
|
+
const updatedAt = app.updatedAt ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
3520
|
+
const manifestHash = app.manifestHash ?? hashJson(app.manifests);
|
|
3521
|
+
const written = await dbResult.data.batch([
|
|
3522
|
+
{
|
|
3523
|
+
sql: "INSERT OR REPLACE INTO applications (app_id, name, description, updated_at, manifest_json) VALUES (?, ?, ?, ?, ?)",
|
|
3524
|
+
params: [
|
|
3525
|
+
app.appId,
|
|
3526
|
+
app.name ?? null,
|
|
3527
|
+
app.description ?? null,
|
|
3528
|
+
updatedAt,
|
|
3529
|
+
JSON.stringify(app.manifests)
|
|
3530
|
+
]
|
|
3531
|
+
},
|
|
3532
|
+
{
|
|
3533
|
+
sql: "INSERT OR REPLACE INTO application_state (app_id, manifest_hash, indexed_at) VALUES (?, ?, ?)",
|
|
3534
|
+
params: [app.appId, manifestHash, updatedAt]
|
|
3535
|
+
}
|
|
3536
|
+
]);
|
|
3537
|
+
if (!written.ok) return accountErr(written.error);
|
|
3538
|
+
return (0, import_sdk_services5.ok)(void 0);
|
|
3439
3539
|
}
|
|
3440
|
-
|
|
3441
|
-
|
|
3540
|
+
async deleteApplicationIndexQuietly(appId) {
|
|
3541
|
+
await ignoreIndexFailure(() => this.deleteApplicationIndex(appId));
|
|
3542
|
+
}
|
|
3543
|
+
async deleteApplicationIndex(appId) {
|
|
3544
|
+
const dbResult = this.accountDb();
|
|
3545
|
+
if (!dbResult.ok) return (0, import_sdk_services5.ok)(void 0);
|
|
3546
|
+
const schema = await this.ensureAccountIndex(dbResult.data);
|
|
3547
|
+
if (!schema.ok) return schema;
|
|
3548
|
+
const deleted = await dbResult.data.batch([
|
|
3549
|
+
{ sql: "DELETE FROM applications WHERE app_id = ?", params: [appId] },
|
|
3550
|
+
{ sql: "DELETE FROM application_state WHERE app_id = ?", params: [appId] }
|
|
3551
|
+
]);
|
|
3552
|
+
if (!deleted.ok) return accountErr(deleted.error);
|
|
3553
|
+
return (0, import_sdk_services5.ok)(void 0);
|
|
3442
3554
|
}
|
|
3443
|
-
|
|
3444
|
-
|
|
3445
|
-
`manifest.secrets.${name} must be true, a string action, an actions array, or an object`
|
|
3446
|
-
);
|
|
3555
|
+
async upsertSpaceIndexQuietly(space) {
|
|
3556
|
+
await ignoreIndexFailure(() => this.upsertSpaceIndex(space));
|
|
3447
3557
|
}
|
|
3448
|
-
|
|
3449
|
-
|
|
3558
|
+
async upsertSpaceIndex(space) {
|
|
3559
|
+
const dbResult = this.accountDb();
|
|
3560
|
+
if (!dbResult.ok) return (0, import_sdk_services5.ok)(void 0);
|
|
3561
|
+
const schema = await this.ensureAccountIndex(dbResult.data);
|
|
3562
|
+
if (!schema.ok) return schema;
|
|
3563
|
+
const updatedAt = space.updatedAt ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
3564
|
+
const written = await dbResult.data.batch([
|
|
3565
|
+
{
|
|
3566
|
+
sql: "INSERT OR REPLACE INTO spaces (space_id, name, owner_did, type, permissions_json, status, registered_at, updated_at, expires_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
3567
|
+
params: [
|
|
3568
|
+
space.spaceId,
|
|
3569
|
+
space.name,
|
|
3570
|
+
space.ownerDid,
|
|
3571
|
+
space.type,
|
|
3572
|
+
JSON.stringify(space.permissions),
|
|
3573
|
+
space.status,
|
|
3574
|
+
space.registeredAt ?? updatedAt,
|
|
3575
|
+
updatedAt,
|
|
3576
|
+
space.expiresAt?.toISOString() ?? null
|
|
3577
|
+
]
|
|
3578
|
+
}
|
|
3579
|
+
]);
|
|
3580
|
+
if (!written.ok) return accountErr(written.error);
|
|
3581
|
+
return (0, import_sdk_services5.ok)(void 0);
|
|
3450
3582
|
}
|
|
3451
|
-
|
|
3452
|
-
|
|
3583
|
+
async deleteSpaceIndexQuietly(spaceId) {
|
|
3584
|
+
await ignoreIndexFailure(() => this.deleteSpaceIndex(spaceId));
|
|
3585
|
+
}
|
|
3586
|
+
async deleteSpaceIndex(spaceId) {
|
|
3587
|
+
const dbResult = this.accountDb();
|
|
3588
|
+
if (!dbResult.ok) return (0, import_sdk_services5.ok)(void 0);
|
|
3589
|
+
const schema = await this.ensureAccountIndex(dbResult.data);
|
|
3590
|
+
if (!schema.ok) return schema;
|
|
3591
|
+
const deleted = await dbResult.data.batch([
|
|
3592
|
+
{ sql: "DELETE FROM spaces WHERE space_id = ?", params: [spaceId] }
|
|
3593
|
+
]);
|
|
3594
|
+
if (!deleted.ok) return accountErr(deleted.error);
|
|
3595
|
+
return (0, import_sdk_services5.ok)(void 0);
|
|
3453
3596
|
}
|
|
3454
|
-
|
|
3455
|
-
|
|
3597
|
+
async resolveSpace(space) {
|
|
3598
|
+
const listed = await this.config.getSpaces().list();
|
|
3599
|
+
if (!listed.ok) return accountErr(listed.error);
|
|
3600
|
+
const found = listed.data.find((candidate) => candidate.id === space || candidate.name === space);
|
|
3601
|
+
if (!found) {
|
|
3602
|
+
return (0, import_sdk_services5.err)(
|
|
3603
|
+
(0, import_sdk_services5.serviceError)("SPACE_NOT_FOUND", `No account space found for ${JSON.stringify(space)}`, SERVICE_NAME2)
|
|
3604
|
+
);
|
|
3605
|
+
}
|
|
3606
|
+
return (0, import_sdk_services5.ok)(found);
|
|
3607
|
+
}
|
|
3608
|
+
async replaceApplicationsIndexQuietly(applications) {
|
|
3609
|
+
await ignoreIndexFailure(async () => {
|
|
3610
|
+
const dbResult = this.accountDb();
|
|
3611
|
+
if (!dbResult.ok) return;
|
|
3612
|
+
const syncedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
3613
|
+
const schema = await this.ensureAccountIndex(dbResult.data);
|
|
3614
|
+
if (!schema.ok) return;
|
|
3615
|
+
await dbResult.data.batch([
|
|
3616
|
+
{ sql: "DELETE FROM applications" },
|
|
3617
|
+
{ sql: "DELETE FROM application_state" },
|
|
3618
|
+
...applications.map((app) => ({
|
|
3619
|
+
sql: "INSERT OR REPLACE INTO applications (app_id, name, description, updated_at, manifest_json) VALUES (?, ?, ?, ?, ?)",
|
|
3620
|
+
params: [
|
|
3621
|
+
app.appId,
|
|
3622
|
+
app.name ?? null,
|
|
3623
|
+
app.description ?? null,
|
|
3624
|
+
app.updatedAt ?? syncedAt,
|
|
3625
|
+
JSON.stringify(app.manifests)
|
|
3626
|
+
]
|
|
3627
|
+
})),
|
|
3628
|
+
...applications.map((app) => ({
|
|
3629
|
+
sql: "INSERT OR REPLACE INTO application_state (app_id, manifest_hash, indexed_at) VALUES (?, ?, ?)",
|
|
3630
|
+
params: [app.appId, app.manifestHash ?? hashJson(app.manifests), syncedAt]
|
|
3631
|
+
})),
|
|
3632
|
+
{
|
|
3633
|
+
sql: "INSERT OR REPLACE INTO sync_state (source, synced_at, count) VALUES (?, ?, ?)",
|
|
3634
|
+
params: ["applications", syncedAt, applications.length]
|
|
3635
|
+
}
|
|
3636
|
+
]);
|
|
3637
|
+
});
|
|
3456
3638
|
}
|
|
3457
|
-
|
|
3458
|
-
|
|
3459
|
-
|
|
3460
|
-
|
|
3461
|
-
|
|
3462
|
-
|
|
3463
|
-
|
|
3639
|
+
async replaceSpacesIndexQuietly(spaces) {
|
|
3640
|
+
await ignoreIndexFailure(async () => {
|
|
3641
|
+
const dbResult = this.accountDb();
|
|
3642
|
+
if (!dbResult.ok) return;
|
|
3643
|
+
const syncedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
3644
|
+
const schema = await this.ensureAccountIndex(dbResult.data);
|
|
3645
|
+
if (!schema.ok) return;
|
|
3646
|
+
await dbResult.data.batch([
|
|
3647
|
+
{ sql: "DELETE FROM spaces" },
|
|
3648
|
+
...spaces.map((space) => ({
|
|
3649
|
+
sql: "INSERT OR REPLACE INTO spaces (space_id, name, owner_did, type, permissions_json, status, registered_at, updated_at, expires_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
3650
|
+
params: [
|
|
3651
|
+
space.spaceId,
|
|
3652
|
+
space.name,
|
|
3653
|
+
space.ownerDid,
|
|
3654
|
+
space.type,
|
|
3655
|
+
JSON.stringify(space.permissions),
|
|
3656
|
+
space.status,
|
|
3657
|
+
space.registeredAt ?? syncedAt,
|
|
3658
|
+
space.updatedAt ?? syncedAt,
|
|
3659
|
+
space.expiresAt?.toISOString() ?? null
|
|
3660
|
+
]
|
|
3661
|
+
})),
|
|
3662
|
+
{
|
|
3663
|
+
sql: "INSERT OR REPLACE INTO sync_state (source, synced_at, count) VALUES (?, ?, ?)",
|
|
3664
|
+
params: ["spaces", syncedAt, spaces.length]
|
|
3665
|
+
}
|
|
3666
|
+
]);
|
|
3667
|
+
});
|
|
3464
3668
|
}
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3669
|
+
async replaceDelegationsIndexQuietly(delegations) {
|
|
3670
|
+
await ignoreIndexFailure(async () => {
|
|
3671
|
+
const dbResult = this.accountDb();
|
|
3672
|
+
if (!dbResult.ok) return;
|
|
3673
|
+
const syncedAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
3674
|
+
const schema = await this.ensureAccountIndex(dbResult.data);
|
|
3675
|
+
if (!schema.ok) return;
|
|
3676
|
+
await dbResult.data.batch([
|
|
3677
|
+
{ sql: "DELETE FROM delegations" },
|
|
3678
|
+
...delegations.map((delegation) => ({
|
|
3679
|
+
sql: "INSERT OR REPLACE INTO delegations (cid, direction, space_id, space_name, counterparty_did, delegate_did, delegator_did, path, actions_json, expiry, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
|
3680
|
+
params: [
|
|
3681
|
+
delegation.cid,
|
|
3682
|
+
delegation.direction,
|
|
3683
|
+
delegation.spaceId,
|
|
3684
|
+
delegation.spaceName ?? null,
|
|
3685
|
+
delegation.counterpartyDid,
|
|
3686
|
+
delegation.delegateDid,
|
|
3687
|
+
delegation.delegatorDid ?? null,
|
|
3688
|
+
delegation.path,
|
|
3689
|
+
JSON.stringify(delegation.actions),
|
|
3690
|
+
delegation.expiry.toISOString(),
|
|
3691
|
+
delegation.status,
|
|
3692
|
+
delegation.createdAt?.toISOString() ?? null,
|
|
3693
|
+
syncedAt
|
|
3694
|
+
]
|
|
3695
|
+
})),
|
|
3696
|
+
{
|
|
3697
|
+
sql: "INSERT OR REPLACE INTO sync_state (source, synced_at, count) VALUES (?, ?, ?)",
|
|
3698
|
+
params: ["delegations", syncedAt, delegations.length]
|
|
3699
|
+
}
|
|
3700
|
+
]);
|
|
3481
3701
|
});
|
|
3482
3702
|
}
|
|
3483
|
-
|
|
3703
|
+
async ensureAccountIndex(db) {
|
|
3704
|
+
const migrated = await db.migrations.apply({
|
|
3705
|
+
namespace: ACCOUNT_INDEX_NAMESPACE,
|
|
3706
|
+
migrations: [
|
|
3707
|
+
{
|
|
3708
|
+
id: "001_initial",
|
|
3709
|
+
sql: ACCOUNT_INDEX_SCHEMA
|
|
3710
|
+
}
|
|
3711
|
+
]
|
|
3712
|
+
});
|
|
3713
|
+
if (!migrated.ok) return accountErr(migrated.error);
|
|
3714
|
+
return (0, import_sdk_services5.ok)(void 0);
|
|
3715
|
+
}
|
|
3716
|
+
};
|
|
3717
|
+
var ACCOUNT_INDEX_SCHEMA = [
|
|
3718
|
+
`CREATE TABLE IF NOT EXISTS applications (
|
|
3719
|
+
app_id TEXT PRIMARY KEY,
|
|
3720
|
+
name TEXT,
|
|
3721
|
+
description TEXT,
|
|
3722
|
+
updated_at TEXT,
|
|
3723
|
+
manifest_json TEXT NOT NULL
|
|
3724
|
+
)`,
|
|
3725
|
+
`CREATE TABLE IF NOT EXISTS application_state (
|
|
3726
|
+
app_id TEXT PRIMARY KEY,
|
|
3727
|
+
manifest_hash TEXT NOT NULL,
|
|
3728
|
+
indexed_at TEXT NOT NULL
|
|
3729
|
+
)`,
|
|
3730
|
+
`CREATE TABLE IF NOT EXISTS spaces (
|
|
3731
|
+
space_id TEXT PRIMARY KEY,
|
|
3732
|
+
name TEXT NOT NULL,
|
|
3733
|
+
owner_did TEXT NOT NULL,
|
|
3734
|
+
type TEXT NOT NULL,
|
|
3735
|
+
permissions_json TEXT NOT NULL,
|
|
3736
|
+
status TEXT NOT NULL,
|
|
3737
|
+
registered_at TEXT,
|
|
3738
|
+
updated_at TEXT NOT NULL,
|
|
3739
|
+
expires_at TEXT
|
|
3740
|
+
)`,
|
|
3741
|
+
`CREATE TABLE IF NOT EXISTS delegations (
|
|
3742
|
+
cid TEXT PRIMARY KEY,
|
|
3743
|
+
direction TEXT NOT NULL,
|
|
3744
|
+
space_id TEXT NOT NULL,
|
|
3745
|
+
space_name TEXT,
|
|
3746
|
+
counterparty_did TEXT NOT NULL,
|
|
3747
|
+
delegate_did TEXT NOT NULL,
|
|
3748
|
+
delegator_did TEXT,
|
|
3749
|
+
path TEXT NOT NULL,
|
|
3750
|
+
actions_json TEXT NOT NULL,
|
|
3751
|
+
expiry TEXT NOT NULL,
|
|
3752
|
+
status TEXT NOT NULL,
|
|
3753
|
+
created_at TEXT,
|
|
3754
|
+
updated_at TEXT NOT NULL
|
|
3755
|
+
)`,
|
|
3756
|
+
`CREATE TABLE IF NOT EXISTS sync_state (
|
|
3757
|
+
source TEXT PRIMARY KEY,
|
|
3758
|
+
synced_at TEXT NOT NULL,
|
|
3759
|
+
count INTEGER NOT NULL
|
|
3760
|
+
)`,
|
|
3761
|
+
"CREATE INDEX IF NOT EXISTS idx_delegations_direction ON delegations(direction)",
|
|
3762
|
+
"CREATE INDEX IF NOT EXISTS idx_delegations_space ON delegations(space_id)",
|
|
3763
|
+
"CREATE INDEX IF NOT EXISTS idx_delegations_counterparty ON delegations(counterparty_did)",
|
|
3764
|
+
"CREATE INDEX IF NOT EXISTS idx_spaces_owner ON spaces(owner_did)",
|
|
3765
|
+
"CREATE INDEX IF NOT EXISTS idx_spaces_type ON spaces(type)"
|
|
3766
|
+
];
|
|
3767
|
+
function applicationKey(appId) {
|
|
3768
|
+
return `${ACCOUNT_REGISTRY_PATH}${appId}`;
|
|
3484
3769
|
}
|
|
3485
|
-
function
|
|
3486
|
-
|
|
3487
|
-
const resolvedPath = applyPrefix(prefix, entry.path, skipPrefixForEntry);
|
|
3488
|
-
const entryExpiryMs = entry.expiry !== void 0 ? parseExpiry(entry.expiry) : void 0;
|
|
3489
|
-
return expandPermissionEntry({
|
|
3490
|
-
...entry,
|
|
3491
|
-
space: entry.space ?? inheritedSpace,
|
|
3492
|
-
path: resolvedPath,
|
|
3493
|
-
skipPrefix: true
|
|
3494
|
-
}).map((expanded) => ({
|
|
3495
|
-
service: expanded.service,
|
|
3496
|
-
space: expanded.space ?? inheritedSpace,
|
|
3497
|
-
path: expanded.path,
|
|
3498
|
-
actions: expanded.actions,
|
|
3499
|
-
// Only populate `expiryMs` when the entry had its own expiry override.
|
|
3500
|
-
// When absent, callers use the parent (delegation or manifest) expiry
|
|
3501
|
-
// which is carried on ResolvedDelegate.expiryMs / ResolvedCapabilities.expiryMs.
|
|
3502
|
-
...entryExpiryMs !== void 0 ? { expiryMs: entryExpiryMs } : {},
|
|
3503
|
-
...entry.description !== void 0 ? { description: entry.description } : {}
|
|
3504
|
-
}));
|
|
3770
|
+
function appIdFromKey(key) {
|
|
3771
|
+
return key.startsWith(ACCOUNT_REGISTRY_PATH) ? key.slice(ACCOUNT_REGISTRY_PATH.length) : key;
|
|
3505
3772
|
}
|
|
3506
|
-
function
|
|
3507
|
-
const
|
|
3508
|
-
|
|
3509
|
-
|
|
3510
|
-
|
|
3511
|
-
|
|
3512
|
-
|
|
3513
|
-
|
|
3514
|
-
|
|
3515
|
-
|
|
3516
|
-
|
|
3517
|
-
}
|
|
3518
|
-
return [...byBase.entries()].map(([base, actions]) => ({
|
|
3519
|
-
...entry,
|
|
3520
|
-
service: "tinycloud.kv",
|
|
3521
|
-
path: vaultKVPath(base, entry.path),
|
|
3522
|
-
actions,
|
|
3523
|
-
skipPrefix: true
|
|
3524
|
-
}));
|
|
3773
|
+
function applicationFromRecord(key, record) {
|
|
3774
|
+
const manifests = Array.isArray(record.manifests) ? record.manifests : [];
|
|
3775
|
+
const first = manifests[0];
|
|
3776
|
+
return {
|
|
3777
|
+
appId: record.app_id ?? record.appId ?? first?.app_id ?? appIdFromKey(key),
|
|
3778
|
+
manifests,
|
|
3779
|
+
updatedAt: record.updated_at ?? record.updatedAt,
|
|
3780
|
+
name: first?.name,
|
|
3781
|
+
description: first?.description,
|
|
3782
|
+
manifestHash: record.manifest_hash ?? record.manifestHash ?? hashJson(manifests)
|
|
3783
|
+
};
|
|
3525
3784
|
}
|
|
3526
|
-
function
|
|
3527
|
-
const
|
|
3528
|
-
|
|
3529
|
-
|
|
3530
|
-
|
|
3531
|
-
|
|
3532
|
-
|
|
3533
|
-
|
|
3534
|
-
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
|
|
3538
|
-
|
|
3539
|
-
|
|
3540
|
-
|
|
3541
|
-
|
|
3785
|
+
function indexedApplicationFromRow(row) {
|
|
3786
|
+
const [appId, name, description, updatedAt, manifestJson, manifestHash] = row;
|
|
3787
|
+
return {
|
|
3788
|
+
appId,
|
|
3789
|
+
name: name ?? void 0,
|
|
3790
|
+
description: description ?? void 0,
|
|
3791
|
+
updatedAt: updatedAt ?? void 0,
|
|
3792
|
+
manifests: JSON.parse(manifestJson),
|
|
3793
|
+
manifestHash: manifestHash ?? void 0
|
|
3794
|
+
};
|
|
3795
|
+
}
|
|
3796
|
+
function spaceKey(spaceId) {
|
|
3797
|
+
return `${ACCOUNT_SPACES_PATH}${spaceId}`;
|
|
3798
|
+
}
|
|
3799
|
+
function spaceIdFromKey(key) {
|
|
3800
|
+
return key.startsWith(ACCOUNT_SPACES_PATH) ? key.slice(ACCOUNT_SPACES_PATH.length) : key;
|
|
3801
|
+
}
|
|
3802
|
+
function spaceRecordFromInput(space) {
|
|
3803
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
3804
|
+
const accountSpace = "spaceId" in space ? space : {
|
|
3805
|
+
spaceId: space.id,
|
|
3806
|
+
name: space.name ?? space.id.split(":").pop() ?? space.id,
|
|
3807
|
+
ownerDid: space.owner ?? "",
|
|
3808
|
+
type: space.type ?? "discovered",
|
|
3809
|
+
permissions: space.permissions ?? [],
|
|
3810
|
+
status: "active",
|
|
3811
|
+
expiresAt: space.expiresAt
|
|
3812
|
+
};
|
|
3813
|
+
return {
|
|
3814
|
+
space_id: accountSpace.spaceId,
|
|
3815
|
+
name: accountSpace.name,
|
|
3816
|
+
owner_did: accountSpace.ownerDid,
|
|
3817
|
+
type: accountSpace.type,
|
|
3818
|
+
permissions: accountSpace.permissions,
|
|
3819
|
+
status: accountSpace.status,
|
|
3820
|
+
registered_at: accountSpace.registeredAt ?? now,
|
|
3821
|
+
updated_at: now,
|
|
3822
|
+
expires_at: accountSpace.expiresAt instanceof Date ? accountSpace.expiresAt.toISOString() : accountSpace.expiresAt
|
|
3823
|
+
};
|
|
3824
|
+
}
|
|
3825
|
+
function spaceFromRecord(key, record) {
|
|
3826
|
+
const expiresAt = record.expires_at ?? record.expiresAt;
|
|
3827
|
+
return {
|
|
3828
|
+
spaceId: record.space_id ?? record.spaceId ?? spaceIdFromKey(key),
|
|
3829
|
+
name: record.name ?? spaceIdFromKey(key).split(":").pop() ?? spaceIdFromKey(key),
|
|
3830
|
+
ownerDid: record.owner_did ?? record.ownerDid ?? record.owner ?? "",
|
|
3831
|
+
type: record.type ?? "discovered",
|
|
3832
|
+
permissions: Array.isArray(record.permissions) ? record.permissions : [],
|
|
3833
|
+
status: record.status ?? "active",
|
|
3834
|
+
registeredAt: record.registered_at ?? record.registeredAt,
|
|
3835
|
+
updatedAt: record.updated_at ?? record.updatedAt,
|
|
3836
|
+
expiresAt: expiresAt ? new Date(expiresAt) : void 0
|
|
3837
|
+
};
|
|
3838
|
+
}
|
|
3839
|
+
function indexedSpaceFromRow(row) {
|
|
3840
|
+
const [spaceId, name, ownerDid, type, permissionsJson, status, registeredAt, updatedAt, expiresAt] = row;
|
|
3841
|
+
return {
|
|
3842
|
+
spaceId,
|
|
3843
|
+
name,
|
|
3844
|
+
ownerDid,
|
|
3845
|
+
type,
|
|
3846
|
+
permissions: JSON.parse(permissionsJson),
|
|
3847
|
+
status,
|
|
3848
|
+
registeredAt: registeredAt ?? void 0,
|
|
3849
|
+
updatedAt,
|
|
3850
|
+
expiresAt: expiresAt ? new Date(expiresAt) : void 0
|
|
3851
|
+
};
|
|
3852
|
+
}
|
|
3853
|
+
function hashJson(value) {
|
|
3854
|
+
const input = stableJson(value);
|
|
3855
|
+
let hash = 0xcbf29ce484222325n;
|
|
3856
|
+
const prime = 0x100000001b3n;
|
|
3857
|
+
for (let index = 0; index < input.length; index += 1) {
|
|
3858
|
+
hash ^= BigInt(input.charCodeAt(index));
|
|
3859
|
+
hash = BigInt.asUintN(64, hash * prime);
|
|
3860
|
+
}
|
|
3861
|
+
return hash.toString(16).padStart(16, "0");
|
|
3862
|
+
}
|
|
3863
|
+
function stableJson(value) {
|
|
3864
|
+
if (value === null || typeof value !== "object") {
|
|
3865
|
+
return JSON.stringify(value);
|
|
3542
3866
|
}
|
|
3543
|
-
if (
|
|
3544
|
-
return {
|
|
3867
|
+
if (Array.isArray(value)) {
|
|
3868
|
+
return `[${value.map(stableJson).join(",")}]`;
|
|
3545
3869
|
}
|
|
3546
|
-
|
|
3547
|
-
|
|
3548
|
-
);
|
|
3870
|
+
const object = value;
|
|
3871
|
+
return `{${Object.keys(object).sort().map((key) => `${JSON.stringify(key)}:${stableJson(object[key])}`).join(",")}}`;
|
|
3549
3872
|
}
|
|
3550
|
-
function
|
|
3551
|
-
|
|
3552
|
-
|
|
3873
|
+
function indexedDelegationFromRow(row) {
|
|
3874
|
+
const [
|
|
3875
|
+
cid,
|
|
3876
|
+
direction,
|
|
3877
|
+
spaceId,
|
|
3878
|
+
spaceName,
|
|
3879
|
+
counterpartyDid,
|
|
3880
|
+
delegateDid,
|
|
3881
|
+
delegatorDid,
|
|
3882
|
+
path,
|
|
3883
|
+
actionsJson,
|
|
3884
|
+
expiry,
|
|
3885
|
+
status,
|
|
3886
|
+
createdAt
|
|
3887
|
+
] = row;
|
|
3888
|
+
return {
|
|
3889
|
+
cid,
|
|
3890
|
+
direction,
|
|
3891
|
+
spaceId,
|
|
3892
|
+
spaceName: spaceName ?? void 0,
|
|
3893
|
+
counterpartyDid,
|
|
3894
|
+
delegateDid,
|
|
3895
|
+
delegatorDid: delegatorDid ?? void 0,
|
|
3896
|
+
path,
|
|
3897
|
+
actions: JSON.parse(actionsJson),
|
|
3898
|
+
expiry: new Date(expiry),
|
|
3899
|
+
status,
|
|
3900
|
+
createdAt: createdAt ? new Date(createdAt) : void 0
|
|
3901
|
+
};
|
|
3902
|
+
}
|
|
3903
|
+
function mapDelegation(delegation, space, direction) {
|
|
3904
|
+
return {
|
|
3905
|
+
cid: delegation.cid,
|
|
3906
|
+
direction,
|
|
3907
|
+
spaceId: delegation.spaceId || space.id,
|
|
3908
|
+
spaceName: space.name,
|
|
3909
|
+
counterpartyDid: direction === "granted" ? delegation.delegateDID : delegation.delegatorDID ?? delegation.delegateDID,
|
|
3910
|
+
delegateDid: delegation.delegateDID,
|
|
3911
|
+
delegatorDid: delegation.delegatorDID,
|
|
3912
|
+
path: delegation.path,
|
|
3913
|
+
actions: delegation.actions,
|
|
3914
|
+
expiry: delegation.expiry,
|
|
3915
|
+
status: delegation.isRevoked ? "revoked" : delegation.expiry.getTime() <= Date.now() ? "expired" : "active",
|
|
3916
|
+
createdAt: delegation.createdAt
|
|
3917
|
+
};
|
|
3918
|
+
}
|
|
3919
|
+
function accountErr(error) {
|
|
3920
|
+
return (0, import_sdk_services5.err)((0, import_sdk_services5.serviceError)(error.code, error.message, SERVICE_NAME2, { cause: error.cause, meta: error.meta }));
|
|
3921
|
+
}
|
|
3922
|
+
function isMissingIndexError(error) {
|
|
3923
|
+
return /no such table:/i.test(error.message);
|
|
3924
|
+
}
|
|
3925
|
+
async function ignoreIndexFailure(task) {
|
|
3926
|
+
try {
|
|
3927
|
+
await task();
|
|
3928
|
+
} catch {
|
|
3553
3929
|
}
|
|
3554
|
-
|
|
3555
|
-
|
|
3930
|
+
}
|
|
3931
|
+
|
|
3932
|
+
// src/index.ts
|
|
3933
|
+
var import_sdk_services7 = require("@tinycloud/sdk-services");
|
|
3934
|
+
|
|
3935
|
+
// src/space.ts
|
|
3936
|
+
async function fetchPeerId(host, spaceId) {
|
|
3937
|
+
const res = await fetch(
|
|
3938
|
+
`${host}/peer/generate/${encodeURIComponent(spaceId)}`
|
|
3939
|
+
);
|
|
3940
|
+
if (!res.ok) {
|
|
3941
|
+
const error = await res.text().catch(() => res.statusText);
|
|
3942
|
+
throw new Error(`Failed to get peer ID: ${res.status} - ${error}`);
|
|
3556
3943
|
}
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3944
|
+
return res.text();
|
|
3945
|
+
}
|
|
3946
|
+
async function submitHostDelegation(host, headers) {
|
|
3947
|
+
const res = await fetch(`${host}/delegate`, {
|
|
3948
|
+
method: "POST",
|
|
3949
|
+
headers
|
|
3950
|
+
});
|
|
3951
|
+
return {
|
|
3952
|
+
success: res.ok,
|
|
3953
|
+
status: res.status,
|
|
3954
|
+
error: res.ok ? void 0 : await res.text().catch(() => res.statusText)
|
|
3955
|
+
};
|
|
3956
|
+
}
|
|
3957
|
+
async function activateSessionWithHost(host, delegationHeader) {
|
|
3958
|
+
const res = await fetch(`${host}/delegate`, {
|
|
3959
|
+
method: "POST",
|
|
3960
|
+
headers: delegationHeader
|
|
3961
|
+
});
|
|
3962
|
+
if (res.ok) {
|
|
3963
|
+
try {
|
|
3964
|
+
const body = await res.json();
|
|
3965
|
+
return {
|
|
3966
|
+
success: true,
|
|
3967
|
+
status: res.status,
|
|
3968
|
+
activated: body.activated ?? [],
|
|
3969
|
+
skipped: body.skipped ?? []
|
|
3970
|
+
};
|
|
3971
|
+
} catch {
|
|
3972
|
+
return {
|
|
3973
|
+
success: true,
|
|
3974
|
+
status: res.status,
|
|
3975
|
+
activated: [],
|
|
3976
|
+
skipped: []
|
|
3977
|
+
};
|
|
3978
|
+
}
|
|
3561
3979
|
}
|
|
3562
|
-
return action;
|
|
3563
|
-
}
|
|
3564
|
-
function vaultKVPath(base, path) {
|
|
3565
|
-
const normalized = path.startsWith("/") ? path.slice(1) : path;
|
|
3566
|
-
return `${base}/${normalized}`;
|
|
3567
|
-
}
|
|
3568
|
-
function cloneResourceCapability(entry) {
|
|
3569
3980
|
return {
|
|
3570
|
-
|
|
3571
|
-
|
|
3572
|
-
|
|
3573
|
-
actions: [...entry.actions],
|
|
3574
|
-
...entry.expiryMs !== void 0 ? { expiryMs: entry.expiryMs } : {},
|
|
3575
|
-
...entry.description !== void 0 ? { description: entry.description } : {}
|
|
3981
|
+
success: false,
|
|
3982
|
+
status: res.status,
|
|
3983
|
+
error: await res.text().catch(() => res.statusText)
|
|
3576
3984
|
};
|
|
3577
3985
|
}
|
|
3578
|
-
|
|
3986
|
+
|
|
3987
|
+
// src/delegations/DelegationManager.ts
|
|
3988
|
+
var DelegationAction = {
|
|
3989
|
+
CREATE: "tinycloud.delegation/create",
|
|
3990
|
+
REVOKE: "tinycloud.delegation/revoke",
|
|
3991
|
+
LIST: "tinycloud.delegation/list",
|
|
3992
|
+
GET: "tinycloud.delegation/get",
|
|
3993
|
+
CHECK: "tinycloud.delegation/check"
|
|
3994
|
+
};
|
|
3995
|
+
function createError(code, message, cause, meta) {
|
|
3579
3996
|
return {
|
|
3580
|
-
|
|
3581
|
-
|
|
3582
|
-
|
|
3583
|
-
|
|
3584
|
-
|
|
3585
|
-
...entry.expiry !== void 0 ? { expiry: entry.expiry } : {},
|
|
3586
|
-
...entry.description !== void 0 ? { description: entry.description } : {}
|
|
3997
|
+
code,
|
|
3998
|
+
message,
|
|
3999
|
+
service: "delegation",
|
|
4000
|
+
cause,
|
|
4001
|
+
meta
|
|
3587
4002
|
};
|
|
3588
4003
|
}
|
|
3589
|
-
|
|
3590
|
-
|
|
3591
|
-
|
|
3592
|
-
|
|
3593
|
-
|
|
3594
|
-
|
|
3595
|
-
|
|
3596
|
-
|
|
4004
|
+
var DelegationManager = class {
|
|
4005
|
+
/**
|
|
4006
|
+
* Creates a new DelegationManager instance.
|
|
4007
|
+
*
|
|
4008
|
+
* @param config - Configuration including hosts, session, and invoke function
|
|
4009
|
+
*/
|
|
4010
|
+
constructor(config) {
|
|
4011
|
+
this.hosts = config.hosts;
|
|
4012
|
+
this.session = config.session;
|
|
4013
|
+
this.invoke = config.invoke;
|
|
4014
|
+
this.fetchFn = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
4015
|
+
}
|
|
4016
|
+
/**
|
|
4017
|
+
* Updates the session (e.g., after re-authentication).
|
|
4018
|
+
*
|
|
4019
|
+
* @param session - New session to use for operations
|
|
4020
|
+
*/
|
|
4021
|
+
updateSession(session) {
|
|
4022
|
+
this.session = session;
|
|
4023
|
+
}
|
|
4024
|
+
/**
|
|
4025
|
+
* Gets the primary host URL.
|
|
4026
|
+
*/
|
|
4027
|
+
get host() {
|
|
4028
|
+
return this.hosts[0];
|
|
4029
|
+
}
|
|
4030
|
+
/**
|
|
4031
|
+
* Executes an invoke operation against the delegation API.
|
|
4032
|
+
*/
|
|
4033
|
+
async invokeOperation(path, action, body) {
|
|
4034
|
+
const headers = this.invoke(this.session, "delegation", path, action);
|
|
4035
|
+
return this.fetchFn(`${this.host}/invoke`, {
|
|
4036
|
+
method: "POST",
|
|
4037
|
+
headers,
|
|
4038
|
+
body
|
|
4039
|
+
});
|
|
4040
|
+
}
|
|
4041
|
+
/**
|
|
4042
|
+
* Creates a new delegation.
|
|
4043
|
+
*
|
|
4044
|
+
* Delegates specific permissions to another DID for a given path.
|
|
4045
|
+
* The delegatee can then use these permissions to access resources
|
|
4046
|
+
* within the specified scope.
|
|
4047
|
+
*
|
|
4048
|
+
* @param params - Parameters for the delegation
|
|
4049
|
+
* @returns Result containing the created Delegation or an error
|
|
4050
|
+
*
|
|
4051
|
+
* @example
|
|
4052
|
+
* ```typescript
|
|
4053
|
+
* const result = await manager.create({
|
|
4054
|
+
* delegateDID: bob.did,
|
|
4055
|
+
* path: "documents/shared/",
|
|
4056
|
+
* actions: ["tinycloud.kv/get", "tinycloud.kv/put"],
|
|
4057
|
+
* expiry: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 days
|
|
4058
|
+
* });
|
|
4059
|
+
* ```
|
|
4060
|
+
*/
|
|
4061
|
+
async create(params) {
|
|
4062
|
+
if (!params.delegateDID) {
|
|
4063
|
+
return {
|
|
4064
|
+
ok: false,
|
|
4065
|
+
error: createError(
|
|
4066
|
+
DelegationErrorCodes.INVALID_INPUT,
|
|
4067
|
+
"delegateDID is required"
|
|
4068
|
+
)
|
|
4069
|
+
};
|
|
3597
4070
|
}
|
|
3598
|
-
|
|
3599
|
-
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
4071
|
+
if (!params.path) {
|
|
4072
|
+
return {
|
|
4073
|
+
ok: false,
|
|
4074
|
+
error: createError(
|
|
4075
|
+
DelegationErrorCodes.INVALID_INPUT,
|
|
4076
|
+
"path is required"
|
|
4077
|
+
)
|
|
4078
|
+
};
|
|
4079
|
+
}
|
|
4080
|
+
if (!params.actions || params.actions.length === 0) {
|
|
4081
|
+
return {
|
|
4082
|
+
ok: false,
|
|
4083
|
+
error: createError(
|
|
4084
|
+
DelegationErrorCodes.INVALID_INPUT,
|
|
4085
|
+
"at least one action is required"
|
|
4086
|
+
)
|
|
4087
|
+
};
|
|
4088
|
+
}
|
|
4089
|
+
try {
|
|
4090
|
+
const body = JSON.stringify({
|
|
4091
|
+
delegateDID: params.delegateDID,
|
|
4092
|
+
path: params.path,
|
|
4093
|
+
actions: params.actions,
|
|
4094
|
+
expiry: params.expiry?.toISOString(),
|
|
4095
|
+
disableSubDelegation: params.disableSubDelegation ?? false,
|
|
4096
|
+
statement: params.statement
|
|
4097
|
+
});
|
|
4098
|
+
const response = await this.invokeOperation(
|
|
4099
|
+
params.path,
|
|
4100
|
+
DelegationAction.CREATE,
|
|
4101
|
+
body
|
|
4102
|
+
);
|
|
4103
|
+
if (!response.ok) {
|
|
4104
|
+
const errorText = await response.text();
|
|
4105
|
+
return {
|
|
4106
|
+
ok: false,
|
|
4107
|
+
error: createError(
|
|
4108
|
+
DelegationErrorCodes.CREATION_FAILED,
|
|
4109
|
+
`Failed to create delegation: ${response.status} - ${errorText}`,
|
|
4110
|
+
void 0,
|
|
4111
|
+
{ status: response.status, path: params.path }
|
|
4112
|
+
)
|
|
4113
|
+
};
|
|
4114
|
+
}
|
|
4115
|
+
const apiResponse = await response.json();
|
|
4116
|
+
const delegation = {
|
|
4117
|
+
cid: apiResponse.cid ?? "",
|
|
4118
|
+
delegateDID: params.delegateDID,
|
|
4119
|
+
spaceId: this.session.spaceId,
|
|
4120
|
+
path: params.path,
|
|
4121
|
+
actions: params.actions,
|
|
4122
|
+
expiry: params.expiry ?? new Date(Date.now() + EXPIRY.SHARE_MS),
|
|
4123
|
+
isRevoked: false,
|
|
4124
|
+
allowSubDelegation: !(params.disableSubDelegation ?? false),
|
|
4125
|
+
createdAt: /* @__PURE__ */ new Date()
|
|
4126
|
+
};
|
|
4127
|
+
return { ok: true, data: delegation };
|
|
4128
|
+
} catch (error) {
|
|
4129
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
4130
|
+
return {
|
|
4131
|
+
ok: false,
|
|
4132
|
+
error: createError(
|
|
4133
|
+
DelegationErrorCodes.ABORTED,
|
|
4134
|
+
"Request aborted",
|
|
4135
|
+
error
|
|
4136
|
+
)
|
|
4137
|
+
};
|
|
4138
|
+
}
|
|
4139
|
+
return {
|
|
4140
|
+
ok: false,
|
|
4141
|
+
error: createError(
|
|
4142
|
+
DelegationErrorCodes.NETWORK_ERROR,
|
|
4143
|
+
`Network error during delegation creation: ${String(error)}`,
|
|
4144
|
+
error instanceof Error ? error : void 0
|
|
4145
|
+
)
|
|
4146
|
+
};
|
|
4147
|
+
}
|
|
4148
|
+
}
|
|
4149
|
+
/**
|
|
4150
|
+
* Revokes an existing delegation.
|
|
4151
|
+
*
|
|
4152
|
+
* Once revoked, the delegation can no longer be used to access resources.
|
|
4153
|
+
* This also invalidates any sub-delegations derived from this delegation.
|
|
4154
|
+
*
|
|
4155
|
+
* @param cid - The CID of the delegation to revoke
|
|
4156
|
+
* @returns Result indicating success or an error
|
|
4157
|
+
*
|
|
4158
|
+
* @example
|
|
4159
|
+
* ```typescript
|
|
4160
|
+
* const result = await manager.revoke("bafy...");
|
|
4161
|
+
* if (result.ok) {
|
|
4162
|
+
* console.log("Delegation revoked successfully");
|
|
4163
|
+
* }
|
|
4164
|
+
* ```
|
|
4165
|
+
*/
|
|
4166
|
+
async revoke(cid) {
|
|
4167
|
+
if (!cid) {
|
|
4168
|
+
return {
|
|
4169
|
+
ok: false,
|
|
4170
|
+
error: createError(
|
|
4171
|
+
DelegationErrorCodes.INVALID_INPUT,
|
|
4172
|
+
"cid is required"
|
|
4173
|
+
)
|
|
4174
|
+
};
|
|
4175
|
+
}
|
|
4176
|
+
try {
|
|
4177
|
+
const body = JSON.stringify({ cid });
|
|
4178
|
+
const response = await this.invokeOperation(
|
|
4179
|
+
cid,
|
|
4180
|
+
DelegationAction.REVOKE,
|
|
4181
|
+
body
|
|
4182
|
+
);
|
|
4183
|
+
if (!response.ok) {
|
|
4184
|
+
const errorText = await response.text();
|
|
4185
|
+
if (response.status === 404) {
|
|
4186
|
+
return {
|
|
4187
|
+
ok: false,
|
|
4188
|
+
error: createError(
|
|
4189
|
+
DelegationErrorCodes.NOT_FOUND,
|
|
4190
|
+
`Delegation not found: ${cid}`
|
|
4191
|
+
)
|
|
4192
|
+
};
|
|
4193
|
+
}
|
|
4194
|
+
return {
|
|
4195
|
+
ok: false,
|
|
4196
|
+
error: createError(
|
|
4197
|
+
DelegationErrorCodes.REVOCATION_FAILED,
|
|
4198
|
+
`Failed to revoke delegation: ${response.status} - ${errorText}`,
|
|
4199
|
+
void 0,
|
|
4200
|
+
{ status: response.status, cid }
|
|
4201
|
+
)
|
|
4202
|
+
};
|
|
3603
4203
|
}
|
|
4204
|
+
return { ok: true, data: void 0 };
|
|
4205
|
+
} catch (error) {
|
|
4206
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
4207
|
+
return {
|
|
4208
|
+
ok: false,
|
|
4209
|
+
error: createError(
|
|
4210
|
+
DelegationErrorCodes.ABORTED,
|
|
4211
|
+
"Request aborted",
|
|
4212
|
+
error
|
|
4213
|
+
)
|
|
4214
|
+
};
|
|
4215
|
+
}
|
|
4216
|
+
return {
|
|
4217
|
+
ok: false,
|
|
4218
|
+
error: createError(
|
|
4219
|
+
DelegationErrorCodes.NETWORK_ERROR,
|
|
4220
|
+
`Network error during delegation revocation: ${String(error)}`,
|
|
4221
|
+
error instanceof Error ? error : void 0
|
|
4222
|
+
)
|
|
4223
|
+
};
|
|
3604
4224
|
}
|
|
3605
|
-
if (existing.description === void 0 && resource.description !== void 0) {
|
|
3606
|
-
existing.description = resource.description;
|
|
3607
|
-
}
|
|
3608
|
-
}
|
|
3609
|
-
return [...byKey.values()];
|
|
3610
|
-
}
|
|
3611
|
-
function capabilitiesReadPermission(space) {
|
|
3612
|
-
return {
|
|
3613
|
-
service: "tinycloud.capabilities",
|
|
3614
|
-
space,
|
|
3615
|
-
path: "",
|
|
3616
|
-
actions: ["tinycloud.capabilities/read"]
|
|
3617
|
-
};
|
|
3618
|
-
}
|
|
3619
|
-
function withCapabilitiesReadForSpaces(resources) {
|
|
3620
|
-
if (resources.length === 0) {
|
|
3621
|
-
return [];
|
|
3622
|
-
}
|
|
3623
|
-
const spaces = new Set(
|
|
3624
|
-
resources.filter((resource) => resource.service !== ENCRYPTION_PERMISSION_SERVICE).map((resource) => resource.space)
|
|
3625
|
-
);
|
|
3626
|
-
return dedupeResources([
|
|
3627
|
-
...resources,
|
|
3628
|
-
...[...spaces].map(capabilitiesReadPermission)
|
|
3629
|
-
]);
|
|
3630
|
-
}
|
|
3631
|
-
function accountRegistryPermission() {
|
|
3632
|
-
return {
|
|
3633
|
-
service: "tinycloud.kv",
|
|
3634
|
-
space: ACCOUNT_REGISTRY_SPACE,
|
|
3635
|
-
path: ACCOUNT_REGISTRY_PATH,
|
|
3636
|
-
actions: ["tinycloud.kv/get", "tinycloud.kv/put", "tinycloud.kv/list"]
|
|
3637
|
-
};
|
|
3638
|
-
}
|
|
3639
|
-
function composeManifestRequest(inputs, options = {}) {
|
|
3640
|
-
if (!Array.isArray(inputs) || inputs.length === 0) {
|
|
3641
|
-
throw new ManifestValidationError(
|
|
3642
|
-
"composeManifestRequest requires at least one manifest"
|
|
3643
|
-
);
|
|
3644
4225
|
}
|
|
3645
|
-
|
|
3646
|
-
|
|
3647
|
-
|
|
3648
|
-
|
|
3649
|
-
|
|
3650
|
-
|
|
3651
|
-
|
|
3652
|
-
|
|
3653
|
-
|
|
3654
|
-
|
|
3655
|
-
|
|
3656
|
-
|
|
3657
|
-
|
|
3658
|
-
|
|
3659
|
-
|
|
3660
|
-
|
|
3661
|
-
|
|
3662
|
-
|
|
3663
|
-
|
|
3664
|
-
|
|
3665
|
-
|
|
4226
|
+
/**
|
|
4227
|
+
* Lists all delegations for the current session's space.
|
|
4228
|
+
*
|
|
4229
|
+
* Returns both delegations created by the current user (as delegator)
|
|
4230
|
+
* and delegations granted to the current user (as delegatee).
|
|
4231
|
+
*
|
|
4232
|
+
* @returns Result containing an array of Delegations or an error
|
|
4233
|
+
*
|
|
4234
|
+
* @example
|
|
4235
|
+
* ```typescript
|
|
4236
|
+
* const result = await manager.list();
|
|
4237
|
+
* if (result.ok) {
|
|
4238
|
+
* for (const delegation of result.data) {
|
|
4239
|
+
* console.log(`${delegation.cid}: ${delegation.path} -> ${delegation.delegateDID}`);
|
|
4240
|
+
* }
|
|
4241
|
+
* }
|
|
4242
|
+
* ```
|
|
4243
|
+
*/
|
|
4244
|
+
async list() {
|
|
4245
|
+
try {
|
|
4246
|
+
const response = await this.invokeOperation("", DelegationAction.LIST);
|
|
4247
|
+
if (!response.ok) {
|
|
4248
|
+
const errorText = await response.text();
|
|
4249
|
+
return {
|
|
4250
|
+
ok: false,
|
|
4251
|
+
error: createError(
|
|
4252
|
+
DelegationErrorCodes.NETWORK_ERROR,
|
|
4253
|
+
`Failed to list delegations: ${response.status} - ${errorText}`,
|
|
4254
|
+
void 0,
|
|
4255
|
+
{ status: response.status }
|
|
4256
|
+
)
|
|
4257
|
+
};
|
|
4258
|
+
}
|
|
4259
|
+
const data = await response.json();
|
|
4260
|
+
const delegations = data.map((item) => ({
|
|
4261
|
+
cid: item.cid,
|
|
4262
|
+
delegateDID: item.delegateDID,
|
|
4263
|
+
delegatorDID: item.delegatorDID,
|
|
4264
|
+
spaceId: item.spaceId,
|
|
4265
|
+
path: item.path,
|
|
4266
|
+
actions: item.actions,
|
|
4267
|
+
expiry: new Date(item.expiry),
|
|
4268
|
+
isRevoked: item.isRevoked,
|
|
4269
|
+
createdAt: item.createdAt ? new Date(item.createdAt) : void 0,
|
|
4270
|
+
parentCid: item.parentCid,
|
|
4271
|
+
allowSubDelegation: item.allowSubDelegation
|
|
4272
|
+
}));
|
|
4273
|
+
return { ok: true, data: delegations };
|
|
4274
|
+
} catch (error) {
|
|
4275
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
4276
|
+
return {
|
|
4277
|
+
ok: false,
|
|
4278
|
+
error: createError(
|
|
4279
|
+
DelegationErrorCodes.ABORTED,
|
|
4280
|
+
"Request aborted",
|
|
4281
|
+
error
|
|
4282
|
+
)
|
|
4283
|
+
};
|
|
4284
|
+
}
|
|
4285
|
+
return {
|
|
4286
|
+
ok: false,
|
|
4287
|
+
error: createError(
|
|
4288
|
+
DelegationErrorCodes.NETWORK_ERROR,
|
|
4289
|
+
`Network error during delegation list: ${String(error)}`,
|
|
4290
|
+
error instanceof Error ? error : void 0
|
|
4291
|
+
)
|
|
4292
|
+
};
|
|
3666
4293
|
}
|
|
3667
4294
|
}
|
|
3668
|
-
|
|
3669
|
-
|
|
3670
|
-
|
|
3671
|
-
|
|
3672
|
-
|
|
3673
|
-
|
|
3674
|
-
|
|
3675
|
-
|
|
3676
|
-
|
|
3677
|
-
|
|
3678
|
-
|
|
3679
|
-
|
|
3680
|
-
|
|
3681
|
-
|
|
3682
|
-
|
|
3683
|
-
|
|
3684
|
-
}
|
|
3685
|
-
|
|
3686
|
-
|
|
3687
|
-
|
|
3688
|
-
|
|
3689
|
-
if (
|
|
3690
|
-
|
|
3691
|
-
|
|
4295
|
+
/**
|
|
4296
|
+
* Gets the full delegation chain for a given delegation.
|
|
4297
|
+
*
|
|
4298
|
+
* Returns the chain of delegations from the root (original delegator)
|
|
4299
|
+
* to the specified delegation, including all intermediate sub-delegations.
|
|
4300
|
+
*
|
|
4301
|
+
* @param cid - The CID of the delegation to get the chain for
|
|
4302
|
+
* @returns Result containing the DelegationChain or an error
|
|
4303
|
+
*
|
|
4304
|
+
* @example
|
|
4305
|
+
* ```typescript
|
|
4306
|
+
* const result = await manager.getChain("bafy...");
|
|
4307
|
+
* if (result.ok) {
|
|
4308
|
+
* console.log("Chain length:", result.data.length);
|
|
4309
|
+
* for (const delegation of result.data) {
|
|
4310
|
+
* console.log(`- ${delegation.delegatorDID} -> ${delegation.delegateDID}`);
|
|
4311
|
+
* }
|
|
4312
|
+
* }
|
|
4313
|
+
* ```
|
|
4314
|
+
*/
|
|
4315
|
+
async getChain(cid) {
|
|
4316
|
+
if (!cid) {
|
|
4317
|
+
return {
|
|
4318
|
+
ok: false,
|
|
4319
|
+
error: createError(
|
|
4320
|
+
DelegationErrorCodes.INVALID_INPUT,
|
|
4321
|
+
"cid is required"
|
|
4322
|
+
)
|
|
4323
|
+
};
|
|
4324
|
+
}
|
|
4325
|
+
try {
|
|
4326
|
+
const body = JSON.stringify({ cid, includeChain: true });
|
|
4327
|
+
const response = await this.invokeOperation(
|
|
4328
|
+
cid,
|
|
4329
|
+
DelegationAction.GET,
|
|
4330
|
+
body
|
|
3692
4331
|
);
|
|
4332
|
+
if (!response.ok) {
|
|
4333
|
+
const errorText = await response.text();
|
|
4334
|
+
if (response.status === 404) {
|
|
4335
|
+
return {
|
|
4336
|
+
ok: false,
|
|
4337
|
+
error: createError(
|
|
4338
|
+
DelegationErrorCodes.NOT_FOUND,
|
|
4339
|
+
`Delegation not found: ${cid}`
|
|
4340
|
+
)
|
|
4341
|
+
};
|
|
4342
|
+
}
|
|
4343
|
+
return {
|
|
4344
|
+
ok: false,
|
|
4345
|
+
error: createError(
|
|
4346
|
+
DelegationErrorCodes.NETWORK_ERROR,
|
|
4347
|
+
`Failed to get delegation chain: ${response.status} - ${errorText}`,
|
|
4348
|
+
void 0,
|
|
4349
|
+
{ status: response.status, cid }
|
|
4350
|
+
)
|
|
4351
|
+
};
|
|
4352
|
+
}
|
|
4353
|
+
const data = await response.json();
|
|
4354
|
+
const chain = data.chain.map((item) => ({
|
|
4355
|
+
cid: item.cid,
|
|
4356
|
+
delegateDID: item.delegateDID,
|
|
4357
|
+
delegatorDID: item.delegatorDID,
|
|
4358
|
+
spaceId: item.spaceId,
|
|
4359
|
+
path: item.path,
|
|
4360
|
+
actions: item.actions,
|
|
4361
|
+
expiry: new Date(item.expiry),
|
|
4362
|
+
isRevoked: item.isRevoked,
|
|
4363
|
+
createdAt: item.createdAt ? new Date(item.createdAt) : void 0,
|
|
4364
|
+
parentCid: item.parentCid,
|
|
4365
|
+
allowSubDelegation: item.allowSubDelegation
|
|
4366
|
+
}));
|
|
4367
|
+
return { ok: true, data: chain };
|
|
4368
|
+
} catch (error) {
|
|
4369
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
4370
|
+
return {
|
|
4371
|
+
ok: false,
|
|
4372
|
+
error: createError(
|
|
4373
|
+
DelegationErrorCodes.ABORTED,
|
|
4374
|
+
"Request aborted",
|
|
4375
|
+
error
|
|
4376
|
+
)
|
|
4377
|
+
};
|
|
4378
|
+
}
|
|
4379
|
+
return {
|
|
4380
|
+
ok: false,
|
|
4381
|
+
error: createError(
|
|
4382
|
+
DelegationErrorCodes.NETWORK_ERROR,
|
|
4383
|
+
`Network error during chain retrieval: ${String(error)}`,
|
|
4384
|
+
error instanceof Error ? error : void 0
|
|
4385
|
+
)
|
|
4386
|
+
};
|
|
3693
4387
|
}
|
|
3694
|
-
|
|
3695
|
-
|
|
4388
|
+
}
|
|
4389
|
+
/**
|
|
4390
|
+
* Checks if the current session has permission for a given path and action.
|
|
4391
|
+
*
|
|
4392
|
+
* This can be used to verify permissions before attempting an operation,
|
|
4393
|
+
* or to implement custom access control logic.
|
|
4394
|
+
*
|
|
4395
|
+
* @param path - The resource path to check
|
|
4396
|
+
* @param action - The action to check (e.g., "tinycloud.kv/get")
|
|
4397
|
+
* @returns Result containing a boolean indicating permission or an error
|
|
4398
|
+
*
|
|
4399
|
+
* @example
|
|
4400
|
+
* ```typescript
|
|
4401
|
+
* const result = await manager.checkPermission("documents/private/", "tinycloud.kv/put");
|
|
4402
|
+
* if (result.ok && result.data) {
|
|
4403
|
+
* console.log("Permission granted");
|
|
4404
|
+
* } else {
|
|
4405
|
+
* console.log("Permission denied");
|
|
4406
|
+
* }
|
|
4407
|
+
* ```
|
|
4408
|
+
*/
|
|
4409
|
+
async checkPermission(path, action) {
|
|
4410
|
+
if (!path) {
|
|
4411
|
+
return {
|
|
4412
|
+
ok: false,
|
|
4413
|
+
error: createError(
|
|
4414
|
+
DelegationErrorCodes.INVALID_INPUT,
|
|
4415
|
+
"path is required"
|
|
4416
|
+
)
|
|
4417
|
+
};
|
|
3696
4418
|
}
|
|
3697
|
-
|
|
3698
|
-
|
|
3699
|
-
|
|
3700
|
-
|
|
3701
|
-
|
|
3702
|
-
|
|
3703
|
-
|
|
3704
|
-
|
|
3705
|
-
|
|
3706
|
-
|
|
4419
|
+
if (!action) {
|
|
4420
|
+
return {
|
|
4421
|
+
ok: false,
|
|
4422
|
+
error: createError(
|
|
4423
|
+
DelegationErrorCodes.INVALID_INPUT,
|
|
4424
|
+
"action is required"
|
|
4425
|
+
)
|
|
4426
|
+
};
|
|
4427
|
+
}
|
|
4428
|
+
try {
|
|
4429
|
+
const body = JSON.stringify({ path, action });
|
|
4430
|
+
const response = await this.invokeOperation(
|
|
4431
|
+
path,
|
|
4432
|
+
DelegationAction.CHECK,
|
|
4433
|
+
body
|
|
4434
|
+
);
|
|
4435
|
+
if (!response.ok) {
|
|
4436
|
+
if (response.status === 403) {
|
|
4437
|
+
return { ok: true, data: false };
|
|
3707
4438
|
}
|
|
4439
|
+
const errorText = await response.text();
|
|
4440
|
+
return {
|
|
4441
|
+
ok: false,
|
|
4442
|
+
error: createError(
|
|
4443
|
+
DelegationErrorCodes.NETWORK_ERROR,
|
|
4444
|
+
`Failed to check permission: ${response.status} - ${errorText}`,
|
|
4445
|
+
void 0,
|
|
4446
|
+
{ status: response.status, path, action }
|
|
4447
|
+
)
|
|
4448
|
+
};
|
|
3708
4449
|
}
|
|
4450
|
+
const data = await response.json();
|
|
4451
|
+
return { ok: true, data: data.allowed };
|
|
4452
|
+
} catch (error) {
|
|
4453
|
+
if (error instanceof Error && error.name === "AbortError") {
|
|
4454
|
+
return {
|
|
4455
|
+
ok: false,
|
|
4456
|
+
error: createError(
|
|
4457
|
+
DelegationErrorCodes.ABORTED,
|
|
4458
|
+
"Request aborted",
|
|
4459
|
+
error
|
|
4460
|
+
)
|
|
4461
|
+
};
|
|
4462
|
+
}
|
|
4463
|
+
return {
|
|
4464
|
+
ok: false,
|
|
4465
|
+
error: createError(
|
|
4466
|
+
DelegationErrorCodes.NETWORK_ERROR,
|
|
4467
|
+
`Network error during permission check: ${String(error)}`,
|
|
4468
|
+
error instanceof Error ? error : void 0
|
|
4469
|
+
)
|
|
4470
|
+
};
|
|
3709
4471
|
}
|
|
3710
4472
|
}
|
|
3711
|
-
|
|
3712
|
-
|
|
3713
|
-
|
|
3714
|
-
|
|
3715
|
-
|
|
3716
|
-
|
|
3717
|
-
|
|
3718
|
-
|
|
3719
|
-
|
|
3720
|
-
|
|
3721
|
-
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
|
|
3731
|
-
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
4473
|
+
};
|
|
4474
|
+
|
|
4475
|
+
// src/delegations/SharingService.schema.ts
|
|
4476
|
+
var import_zod5 = require("zod");
|
|
4477
|
+
var EncodedShareDataSchema = import_zod5.z.object({
|
|
4478
|
+
/** Private key in JWK format (must include d parameter) */
|
|
4479
|
+
key: JWKSchema.refine(
|
|
4480
|
+
(jwk) => typeof jwk.d === "string" && jwk.d.length > 0,
|
|
4481
|
+
{ message: "JWK must include private key (d parameter)" }
|
|
4482
|
+
),
|
|
4483
|
+
/** DID of the key */
|
|
4484
|
+
keyDid: import_zod5.z.string().min(1, "keyDid is required"),
|
|
4485
|
+
/** The delegation granting access */
|
|
4486
|
+
delegation: DelegationSchema,
|
|
4487
|
+
/** Resource path this link grants access to */
|
|
4488
|
+
path: import_zod5.z.string().min(1, "path is required"),
|
|
4489
|
+
/** TinyCloud host URL */
|
|
4490
|
+
host: import_zod5.z.string().url("host must be a valid URL"),
|
|
4491
|
+
/** Space ID */
|
|
4492
|
+
spaceId: import_zod5.z.string().min(1, "spaceId is required"),
|
|
4493
|
+
/** Schema version (must be 1) */
|
|
4494
|
+
version: import_zod5.z.literal(1)
|
|
4495
|
+
});
|
|
4496
|
+
var ReceiveOptionsSchema = import_zod5.z.object({
|
|
4497
|
+
/**
|
|
4498
|
+
* Whether to automatically create a sub-delegation to the current session key.
|
|
4499
|
+
* Default: true
|
|
4500
|
+
*/
|
|
4501
|
+
autoSubdelegate: import_zod5.z.boolean().optional(),
|
|
4502
|
+
/**
|
|
4503
|
+
* Whether to use the current session key for operations (requires autoSubdelegate).
|
|
4504
|
+
* Default: true
|
|
4505
|
+
*/
|
|
4506
|
+
useSessionKey: import_zod5.z.boolean().optional(),
|
|
4507
|
+
/**
|
|
4508
|
+
* Ingestion options passed to CapabilityKeyRegistry.
|
|
4509
|
+
*/
|
|
4510
|
+
ingestOptions: IngestOptionsSchema.optional()
|
|
4511
|
+
});
|
|
4512
|
+
var SharingServiceConfigSchema = import_zod5.z.object({
|
|
4513
|
+
/** TinyCloud host URLs */
|
|
4514
|
+
hosts: import_zod5.z.array(import_zod5.z.string().url()).min(1, "At least one host URL is required"),
|
|
4515
|
+
/**
|
|
4516
|
+
* Active session for authentication.
|
|
4517
|
+
* Required for generate(), optional for receive().
|
|
4518
|
+
*/
|
|
4519
|
+
session: import_zod5.z.unknown().refine(
|
|
4520
|
+
(val) => val === void 0 || val !== null && typeof val === "object",
|
|
4521
|
+
{ message: "Expected a ServiceSession object or undefined" }
|
|
4522
|
+
).optional(),
|
|
4523
|
+
/** Platform-specific invoke function */
|
|
4524
|
+
invoke: import_zod5.z.unknown().refine((val) => typeof val === "function", {
|
|
4525
|
+
message: "Expected an invoke function"
|
|
4526
|
+
}),
|
|
4527
|
+
/** Optional custom fetch implementation */
|
|
4528
|
+
fetch: import_zod5.z.unknown().refine(
|
|
4529
|
+
(val) => val === void 0 || typeof val === "function",
|
|
4530
|
+
{ message: "Expected a fetch function or undefined" }
|
|
4531
|
+
).optional(),
|
|
4532
|
+
/** Key provider for cryptographic operations */
|
|
4533
|
+
keyProvider: KeyProviderSchema,
|
|
4534
|
+
/** Capability key registry for key/delegation management */
|
|
4535
|
+
registry: import_zod5.z.unknown().refine(
|
|
4536
|
+
(val) => val !== null && typeof val === "object",
|
|
4537
|
+
{ message: "Expected an ICapabilityKeyRegistry object" }
|
|
4538
|
+
),
|
|
4539
|
+
/**
|
|
4540
|
+
* Delegation manager for creating delegations.
|
|
4541
|
+
* Required for generate(), optional for receive().
|
|
4542
|
+
*/
|
|
4543
|
+
delegationManager: import_zod5.z.unknown().refine(
|
|
4544
|
+
(val) => val === void 0 || val !== null && typeof val === "object",
|
|
4545
|
+
{ message: "Expected a DelegationManager object or undefined" }
|
|
4546
|
+
).optional(),
|
|
4547
|
+
/** Factory for creating KV service instances */
|
|
4548
|
+
createKVService: import_zod5.z.unknown().refine(
|
|
4549
|
+
(val) => typeof val === "function",
|
|
4550
|
+
{ message: "Expected a createKVService factory function" }
|
|
4551
|
+
),
|
|
4552
|
+
/** Base URL for sharing links (e.g., "https://share.myapp.com") */
|
|
4553
|
+
baseUrl: import_zod5.z.string().optional(),
|
|
4554
|
+
/**
|
|
4555
|
+
* Custom delegation creation function.
|
|
4556
|
+
*/
|
|
4557
|
+
createDelegation: import_zod5.z.unknown().refine((val) => val === void 0 || typeof val === "function", {
|
|
4558
|
+
message: "Expected a createDelegation function or undefined"
|
|
4559
|
+
}).optional(),
|
|
4560
|
+
/**
|
|
4561
|
+
* WASM function for client-side delegation creation.
|
|
4562
|
+
*/
|
|
4563
|
+
createDelegationWasm: import_zod5.z.unknown().refine((val) => val === void 0 || typeof val === "function", {
|
|
4564
|
+
message: "Expected a createDelegationWasm function or undefined"
|
|
4565
|
+
}).optional(),
|
|
4566
|
+
/**
|
|
4567
|
+
* Path prefix for KV operations.
|
|
4568
|
+
*/
|
|
4569
|
+
pathPrefix: import_zod5.z.string().optional(),
|
|
4570
|
+
/**
|
|
4571
|
+
* Session expiry time.
|
|
4572
|
+
*/
|
|
4573
|
+
sessionExpiry: import_zod5.z.date().optional(),
|
|
4574
|
+
/**
|
|
4575
|
+
* Callback to create a DIRECT delegation from wallet to share key.
|
|
4576
|
+
* This is the preferred method for long-lived share links because it
|
|
4577
|
+
* bypasses the session delegation chain entirely.
|
|
4578
|
+
*/
|
|
4579
|
+
onRootDelegationNeeded: import_zod5.z.unknown().refine((val) => val === void 0 || typeof val === "function", {
|
|
4580
|
+
message: "Expected an onRootDelegationNeeded function or undefined"
|
|
4581
|
+
}).optional()
|
|
4582
|
+
});
|
|
4583
|
+
function validateEncodedShareData(data) {
|
|
4584
|
+
const result = EncodedShareDataSchema.safeParse(data);
|
|
4585
|
+
if (!result.success) {
|
|
4586
|
+
return {
|
|
4587
|
+
ok: false,
|
|
4588
|
+
error: {
|
|
4589
|
+
code: DelegationErrorCodes.VALIDATION_ERROR,
|
|
4590
|
+
message: `Invalid share data: ${result.error.message}`,
|
|
4591
|
+
service: "delegation",
|
|
4592
|
+
meta: { issues: result.error.issues }
|
|
4593
|
+
}
|
|
4594
|
+
};
|
|
3735
4595
|
}
|
|
3736
|
-
return
|
|
4596
|
+
return { ok: true, data: result.data };
|
|
3737
4597
|
}
|
|
3738
4598
|
|
|
3739
4599
|
// src/delegations/SharingService.ts
|
|
@@ -3914,13 +4774,13 @@ var SharingService = class {
|
|
|
3914
4774
|
)
|
|
3915
4775
|
};
|
|
3916
4776
|
}
|
|
3917
|
-
} catch (
|
|
4777
|
+
} catch (err6) {
|
|
3918
4778
|
return {
|
|
3919
4779
|
ok: false,
|
|
3920
4780
|
error: createError2(
|
|
3921
4781
|
DelegationErrorCodes.CREATION_FAILED,
|
|
3922
|
-
`Failed to generate session key for share: ${
|
|
3923
|
-
|
|
4782
|
+
`Failed to generate session key for share: ${err6 instanceof Error ? err6.message : String(err6)}`,
|
|
4783
|
+
err6 instanceof Error ? err6 : void 0
|
|
3924
4784
|
)
|
|
3925
4785
|
};
|
|
3926
4786
|
}
|
|
@@ -3966,7 +4826,7 @@ var SharingService = class {
|
|
|
3966
4826
|
}
|
|
3967
4827
|
delegation = parsed;
|
|
3968
4828
|
}
|
|
3969
|
-
} catch (
|
|
4829
|
+
} catch (err6) {
|
|
3970
4830
|
const fallbackResult = await this.handleSessionExtensionFallback(requestedExpiry);
|
|
3971
4831
|
expiry = fallbackResult.expiry;
|
|
3972
4832
|
const delegationResult = await this.createSessionDelegation(plainDID, fullPath, actions, expiry);
|
|
@@ -4164,13 +5024,13 @@ var SharingService = class {
|
|
|
4164
5024
|
allowSubDelegation: true,
|
|
4165
5025
|
createdAt: /* @__PURE__ */ new Date()
|
|
4166
5026
|
};
|
|
4167
|
-
} catch (
|
|
5027
|
+
} catch (err6) {
|
|
4168
5028
|
return {
|
|
4169
5029
|
ok: false,
|
|
4170
5030
|
error: createError2(
|
|
4171
5031
|
DelegationErrorCodes.CREATION_FAILED,
|
|
4172
|
-
`Failed to create delegation via WASM: ${
|
|
4173
|
-
|
|
5032
|
+
`Failed to create delegation via WASM: ${err6 instanceof Error ? err6.message : String(err6)}`,
|
|
5033
|
+
err6 instanceof Error ? err6 : void 0
|
|
4174
5034
|
)
|
|
4175
5035
|
};
|
|
4176
5036
|
}
|
|
@@ -4251,8 +5111,8 @@ var SharingService = class {
|
|
|
4251
5111
|
let activeKey = keyInfo;
|
|
4252
5112
|
if (autoSubdelegate && useSessionKey && this.session) {
|
|
4253
5113
|
try {
|
|
4254
|
-
} catch (
|
|
4255
|
-
console.warn("Auto-subdelegation failed, using ingested key directly:",
|
|
5114
|
+
} catch (err6) {
|
|
5115
|
+
console.warn("Auto-subdelegation failed, using ingested key directly:", err6);
|
|
4256
5116
|
}
|
|
4257
5117
|
}
|
|
4258
5118
|
const authHeader = shareData.delegation.authHeader ?? `Bearer ${shareData.delegation.cid}`;
|
|
@@ -4350,26 +5210,26 @@ var SharingService = class {
|
|
|
4350
5210
|
let jsonString;
|
|
4351
5211
|
try {
|
|
4352
5212
|
jsonString = base64UrlDecode(base64Data);
|
|
4353
|
-
} catch (
|
|
5213
|
+
} catch (err6) {
|
|
4354
5214
|
return {
|
|
4355
5215
|
ok: false,
|
|
4356
5216
|
error: createError2(
|
|
4357
5217
|
DelegationErrorCodes.INVALID_TOKEN,
|
|
4358
|
-
`Failed to decode base64 data: ${
|
|
4359
|
-
|
|
5218
|
+
`Failed to decode base64 data: ${err6 instanceof Error ? err6.message : String(err6)}`,
|
|
5219
|
+
err6 instanceof Error ? err6 : void 0
|
|
4360
5220
|
)
|
|
4361
5221
|
};
|
|
4362
5222
|
}
|
|
4363
5223
|
let parsed;
|
|
4364
5224
|
try {
|
|
4365
5225
|
parsed = JSON.parse(jsonString);
|
|
4366
|
-
} catch (
|
|
5226
|
+
} catch (err6) {
|
|
4367
5227
|
return {
|
|
4368
5228
|
ok: false,
|
|
4369
5229
|
error: createError2(
|
|
4370
5230
|
DelegationErrorCodes.INVALID_TOKEN,
|
|
4371
|
-
`Failed to parse share data JSON: ${
|
|
4372
|
-
|
|
5231
|
+
`Failed to parse share data JSON: ${err6 instanceof Error ? err6.message : String(err6)}`,
|
|
5232
|
+
err6 instanceof Error ? err6 : void 0
|
|
4373
5233
|
)
|
|
4374
5234
|
};
|
|
4375
5235
|
}
|
|
@@ -4396,8 +5256,8 @@ function createSharingService(config) {
|
|
|
4396
5256
|
}
|
|
4397
5257
|
|
|
4398
5258
|
// src/authorization/CapabilityKeyRegistry.ts
|
|
4399
|
-
var
|
|
4400
|
-
var
|
|
5259
|
+
var import_sdk_services6 = require("@tinycloud/sdk-services");
|
|
5260
|
+
var SERVICE_NAME3 = "capability-key-registry";
|
|
4401
5261
|
var CapabilityKeyRegistryErrorCodes = {
|
|
4402
5262
|
/** Key not found in registry */
|
|
4403
5263
|
KEY_NOT_FOUND: "KEY_NOT_FOUND",
|
|
@@ -4614,11 +5474,11 @@ var CapabilityKeyRegistry = class {
|
|
|
4614
5474
|
revokeDelegation(cid) {
|
|
4615
5475
|
const stored = this.store.byCid.get(cid);
|
|
4616
5476
|
if (!stored) {
|
|
4617
|
-
return (0,
|
|
4618
|
-
(0,
|
|
5477
|
+
return (0, import_sdk_services6.err)(
|
|
5478
|
+
(0, import_sdk_services6.serviceError)(
|
|
4619
5479
|
CapabilityKeyRegistryErrorCodes.KEY_NOT_FOUND,
|
|
4620
5480
|
`Delegation not found: ${cid}`,
|
|
4621
|
-
|
|
5481
|
+
SERVICE_NAME3
|
|
4622
5482
|
)
|
|
4623
5483
|
);
|
|
4624
5484
|
}
|
|
@@ -4637,7 +5497,7 @@ var CapabilityKeyRegistry = class {
|
|
|
4637
5497
|
}
|
|
4638
5498
|
}
|
|
4639
5499
|
}
|
|
4640
|
-
return (0,
|
|
5500
|
+
return (0, import_sdk_services6.ok)(void 0);
|
|
4641
5501
|
}
|
|
4642
5502
|
// ===========================================================================
|
|
4643
5503
|
// Search
|
|
@@ -4866,8 +5726,8 @@ async function checkNodeInfo(host, sdkProtocol, fetchFn = globalThis.fetch.bind(
|
|
|
4866
5726
|
response = await fetchFn(`${host}/info`, {
|
|
4867
5727
|
signal: AbortSignal.timeout(5e3)
|
|
4868
5728
|
});
|
|
4869
|
-
} catch (
|
|
4870
|
-
throw new VersionCheckError(host,
|
|
5729
|
+
} catch (err6) {
|
|
5730
|
+
throw new VersionCheckError(host, err6);
|
|
4871
5731
|
}
|
|
4872
5732
|
if (!response.ok) {
|
|
4873
5733
|
throw new VersionCheckError(host);
|
|
@@ -5400,6 +6260,7 @@ function parseRecapCapabilities(parseWasm, siwe) {
|
|
|
5400
6260
|
0 && (module.exports = {
|
|
5401
6261
|
ACCOUNT_REGISTRY_PATH,
|
|
5402
6262
|
ACCOUNT_REGISTRY_SPACE,
|
|
6263
|
+
AccountService,
|
|
5403
6264
|
AutoApproveSpaceCreationHandler,
|
|
5404
6265
|
CapabilityKeyRegistry,
|
|
5405
6266
|
CapabilityKeyRegistryErrorCodes,
|