@tinycloud/sdk-services 3.1.0-beta.2 → 3.1.0-beta.7

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.
@@ -1,4 +1,4 @@
1
- import { B as BaseService } from '../BaseService-BhpVpA9b.cjs';
1
+ import { B as BaseService } from '../BaseService-C8zRy0ww.cjs';
2
2
  import { P as PermissionHint, I as IService, R as Result } from '../types-qwMmB7qB.cjs';
3
3
  export { DecryptTransportResponseError } from '@tinycloud/sdk-services/internal/decrypt-transport-response-error';
4
4
 
@@ -1,4 +1,4 @@
1
- import { B as BaseService } from '../BaseService-CuV-ZVPs.js';
1
+ import { B as BaseService } from '../BaseService-DU-lE-Qx.js';
2
2
  import { P as PermissionHint, I as IService, R as Result } from '../types-qwMmB7qB.js';
3
3
  export { DecryptTransportResponseError } from '@tinycloud/sdk-services/internal/decrypt-transport-response-error';
4
4
 
@@ -59,11 +59,131 @@ function err(error) {
59
59
  }
60
60
 
61
61
  // src/errors.ts
62
- function timeoutError(service) {
62
+ import { CAPABILITY_REGISTRY } from "@tinycloud/bootstrap";
63
+
64
+ // src/encryption/networkId.ts
65
+ var URN_PREFIX = "urn:tinycloud:encryption:";
66
+ var NETWORK_NAME_RE = /^[a-z0-9][a-z0-9-]*$/;
67
+ var PKH_EIP155_DID_RE = /^did:pkh:eip155:(\d+):(0x[a-fA-F0-9]{40})$/;
68
+ var NetworkIdError = class extends Error {
69
+ constructor(message) {
70
+ super(message);
71
+ this.name = "NetworkIdError";
72
+ }
73
+ };
74
+ function parseNetworkId(networkId) {
75
+ if (typeof networkId !== "string" || networkId.length === 0) {
76
+ throw new NetworkIdError("networkId must be a non-empty string");
77
+ }
78
+ if (!networkId.startsWith(URN_PREFIX)) {
79
+ throw new NetworkIdError(
80
+ `networkId must start with ${URN_PREFIX} (got ${JSON.stringify(networkId)})`
81
+ );
82
+ }
83
+ const body = networkId.slice(URN_PREFIX.length);
84
+ const lastColon = body.lastIndexOf(":");
85
+ if (lastColon <= 0 || lastColon === body.length - 1) {
86
+ throw new NetworkIdError(
87
+ `networkId missing ownerDid or name segment (got ${JSON.stringify(networkId)})`
88
+ );
89
+ }
90
+ const ownerDid = body.slice(0, lastColon);
91
+ const name = body.slice(lastColon + 1);
92
+ if (!ownerDid.startsWith("did:")) {
93
+ throw new NetworkIdError(
94
+ `networkId ownerDid must be a DID (got ${JSON.stringify(ownerDid)})`
95
+ );
96
+ }
97
+ const didParts = ownerDid.split(":");
98
+ if (didParts.length < 3 || didParts.some((p) => p.length === 0)) {
99
+ throw new NetworkIdError(
100
+ `networkId ownerDid is not a well-formed DID (got ${JSON.stringify(ownerDid)})`
101
+ );
102
+ }
103
+ if (!NETWORK_NAME_RE.test(name)) {
104
+ throw new NetworkIdError(
105
+ `networkId name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
106
+ );
107
+ }
108
+ return { networkId, ownerDid, name };
109
+ }
110
+ function buildNetworkId(ownerDid, name) {
111
+ if (typeof ownerDid !== "string" || !ownerDid.startsWith("did:")) {
112
+ throw new NetworkIdError("ownerDid must be a DID");
113
+ }
114
+ if (typeof name !== "string" || !NETWORK_NAME_RE.test(name)) {
115
+ throw new NetworkIdError(
116
+ `network name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
117
+ );
118
+ }
119
+ const networkId = `${URN_PREFIX}${ownerDid}:${name}`;
120
+ parseNetworkId(networkId);
121
+ return networkId;
122
+ }
123
+ function isNetworkId(networkId) {
124
+ if (typeof networkId !== "string") {
125
+ return false;
126
+ }
127
+ try {
128
+ parseNetworkId(networkId);
129
+ return true;
130
+ } catch {
131
+ return false;
132
+ }
133
+ }
134
+ function parsePkhOwnerDid(ownerDid) {
135
+ const match = ownerDid.match(PKH_EIP155_DID_RE);
136
+ if (!match) return null;
137
+ return {
138
+ chainId: match[1],
139
+ address: match[2].toLowerCase()
140
+ };
141
+ }
142
+ function ownerDidMatches(a, b) {
143
+ const aPkh = parsePkhOwnerDid(a);
144
+ const bPkh = parsePkhOwnerDid(b);
145
+ if (aPkh && bPkh) {
146
+ return aPkh.chainId === bPkh.chainId && aPkh.address === bPkh.address;
147
+ }
148
+ return a === b;
149
+ }
150
+ function networkDiscoveryKey(name) {
151
+ if (!NETWORK_NAME_RE.test(name)) {
152
+ throw new NetworkIdError(
153
+ `network name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
154
+ );
155
+ }
156
+ return `.well-known/encryption/network/${name}`;
157
+ }
158
+ var ENCRYPTION_NETWORK_URN_PREFIX = URN_PREFIX;
159
+ var NETWORK_NAME_PATTERN = NETWORK_NAME_RE;
160
+
161
+ // src/errors.ts
162
+ var RequestTimeoutError = class extends Error {
163
+ constructor(timeoutMs) {
164
+ super(`Request timed out after ${timeoutMs}ms.`);
165
+ this.name = "TimeoutError";
166
+ this.timeoutMs = timeoutMs;
167
+ }
168
+ };
169
+ function isRequestTimeoutError(error) {
170
+ return error instanceof RequestTimeoutError || error instanceof Error && error.name === "TimeoutError" && typeof error.timeoutMs === "number";
171
+ }
172
+ function timeoutError(service, timeoutMs, cause) {
173
+ if (timeoutMs === void 0) {
174
+ return {
175
+ code: ErrorCodes.TIMEOUT,
176
+ message: "Request timed out.",
177
+ service,
178
+ ...cause === void 0 ? {} : { cause }
179
+ };
180
+ }
63
181
  return {
64
182
  code: ErrorCodes.TIMEOUT,
65
- message: "Request timed out.",
66
- service
183
+ message: `Request timed out after ${timeoutMs}ms.`,
184
+ service,
185
+ ...cause === void 0 ? {} : { cause },
186
+ meta: { timeoutMs }
67
187
  };
68
188
  }
69
189
  function abortedError(service) {
@@ -73,8 +193,17 @@ function abortedError(service) {
73
193
  service
74
194
  };
75
195
  }
196
+ var grantableActions = new Set(
197
+ CAPABILITY_REGISTRY.map(({ urn }) => urn).filter((urn) => !urn.endsWith("/*"))
198
+ );
199
+ var capabilityServices = new Set(
200
+ CAPABILITY_REGISTRY.map(({ service }) => service.slice("tinycloud.".length))
201
+ );
76
202
  function wrapError(service, error, defaultCode = ErrorCodes.NETWORK_ERROR) {
77
203
  if (error instanceof Error) {
204
+ if (isRequestTimeoutError(error)) {
205
+ return timeoutError(service, error.timeoutMs, error);
206
+ }
78
207
  if (error.name === "AbortError") {
79
208
  return abortedError(service);
80
209
  }
@@ -96,6 +225,7 @@ function wrapError(service, error, defaultCode = ErrorCodes.NETWORK_ERROR) {
96
225
  }
97
226
 
98
227
  // src/base/BaseService.ts
228
+ var MAX_TIMER_DELAY_MS = 2147483647;
99
229
  var BaseService = class {
100
230
  constructor() {
101
231
  /**
@@ -276,6 +406,64 @@ var BaseService = class {
276
406
  }
277
407
  return controller.signal;
278
408
  }
409
+ /**
410
+ * Create the abort signal for one request, bounded by the request timeout.
411
+ *
412
+ * The signal aborts when the service signs out, the context aborts, the
413
+ * caller's `signal` aborts, or the timeout elapses, whichever comes first.
414
+ * A timeout aborts with a {@link RequestTimeoutError} (`TimeoutError`), which
415
+ * `wrapError` maps to `ErrorCodes.TIMEOUT`; the other sources keep their own
416
+ * reason (normally `AbortError`, mapped to `ErrorCodes.ABORTED`).
417
+ *
418
+ * The timeout is `timeoutMs` when given, otherwise `config.timeout`. Only a
419
+ * positive, finite value (at most 2^31-1 ms) applies; `undefined`, `0`, and
420
+ * `Infinity` mean no timeout, so a per-call `0` opts out of a configured one.
421
+ *
422
+ * Callers must call `dispose()` once the response body has been consumed,
423
+ * which clears the timer and removes the listeners this adds to the
424
+ * long-lived service and context signals.
425
+ *
426
+ * @param signal - The caller's abort signal for this request
427
+ * @param timeoutMs - Per-request timeout override in milliseconds
428
+ */
429
+ createRequestSignal(signal, timeoutMs) {
430
+ const controller = new AbortController();
431
+ const detachers = [];
432
+ let timer;
433
+ const dispose = () => {
434
+ if (timer !== void 0) {
435
+ clearTimeout(timer);
436
+ timer = void 0;
437
+ }
438
+ for (const detach of detachers.splice(0)) {
439
+ detach();
440
+ }
441
+ };
442
+ const abort = (reason) => {
443
+ dispose();
444
+ controller.abort(reason);
445
+ };
446
+ const requestSignal = { signal: controller.signal, dispose };
447
+ const parents = [
448
+ this.abortController.signal,
449
+ this.context?.abortSignal,
450
+ signal
451
+ ].filter((parent) => parent !== void 0);
452
+ for (const parent of parents) {
453
+ if (parent.aborted) {
454
+ abort(parent.reason);
455
+ return requestSignal;
456
+ }
457
+ const onAbort = () => abort(parent.reason);
458
+ parent.addEventListener("abort", onAbort, { once: true });
459
+ detachers.push(() => parent.removeEventListener("abort", onAbort));
460
+ }
461
+ const timeout = timeoutMs ?? this._config.timeout;
462
+ if (typeof timeout === "number" && timeout > 0 && timeout <= MAX_TIMER_DELAY_MS) {
463
+ timer = setTimeout(() => abort(new RequestTimeoutError(timeout)), timeout);
464
+ }
465
+ return requestSignal;
466
+ }
279
467
  /**
280
468
  * Wrap an operation with error handling and telemetry.
281
469
  *
@@ -415,103 +603,6 @@ function canonicalHashHex(sha256, value) {
415
603
  return hexEncode(sha256(utf8Encode(canonical)));
416
604
  }
417
605
 
418
- // src/encryption/networkId.ts
419
- var URN_PREFIX = "urn:tinycloud:encryption:";
420
- var NETWORK_NAME_RE = /^[a-z0-9][a-z0-9-]*$/;
421
- var PKH_EIP155_DID_RE = /^did:pkh:eip155:(\d+):(0x[a-fA-F0-9]{40})$/;
422
- var NetworkIdError = class extends Error {
423
- constructor(message) {
424
- super(message);
425
- this.name = "NetworkIdError";
426
- }
427
- };
428
- function parseNetworkId(networkId) {
429
- if (typeof networkId !== "string" || networkId.length === 0) {
430
- throw new NetworkIdError("networkId must be a non-empty string");
431
- }
432
- if (!networkId.startsWith(URN_PREFIX)) {
433
- throw new NetworkIdError(
434
- `networkId must start with ${URN_PREFIX} (got ${JSON.stringify(networkId)})`
435
- );
436
- }
437
- const body = networkId.slice(URN_PREFIX.length);
438
- const lastColon = body.lastIndexOf(":");
439
- if (lastColon <= 0 || lastColon === body.length - 1) {
440
- throw new NetworkIdError(
441
- `networkId missing ownerDid or name segment (got ${JSON.stringify(networkId)})`
442
- );
443
- }
444
- const ownerDid = body.slice(0, lastColon);
445
- const name = body.slice(lastColon + 1);
446
- if (!ownerDid.startsWith("did:")) {
447
- throw new NetworkIdError(
448
- `networkId ownerDid must be a DID (got ${JSON.stringify(ownerDid)})`
449
- );
450
- }
451
- const didParts = ownerDid.split(":");
452
- if (didParts.length < 3 || didParts.some((p) => p.length === 0)) {
453
- throw new NetworkIdError(
454
- `networkId ownerDid is not a well-formed DID (got ${JSON.stringify(ownerDid)})`
455
- );
456
- }
457
- if (!NETWORK_NAME_RE.test(name)) {
458
- throw new NetworkIdError(
459
- `networkId name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
460
- );
461
- }
462
- return { networkId, ownerDid, name };
463
- }
464
- function buildNetworkId(ownerDid, name) {
465
- if (typeof ownerDid !== "string" || !ownerDid.startsWith("did:")) {
466
- throw new NetworkIdError("ownerDid must be a DID");
467
- }
468
- if (typeof name !== "string" || !NETWORK_NAME_RE.test(name)) {
469
- throw new NetworkIdError(
470
- `network name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
471
- );
472
- }
473
- const networkId = `${URN_PREFIX}${ownerDid}:${name}`;
474
- parseNetworkId(networkId);
475
- return networkId;
476
- }
477
- function isNetworkId(networkId) {
478
- if (typeof networkId !== "string") {
479
- return false;
480
- }
481
- try {
482
- parseNetworkId(networkId);
483
- return true;
484
- } catch {
485
- return false;
486
- }
487
- }
488
- function parsePkhOwnerDid(ownerDid) {
489
- const match = ownerDid.match(PKH_EIP155_DID_RE);
490
- if (!match) return null;
491
- return {
492
- chainId: match[1],
493
- address: match[2].toLowerCase()
494
- };
495
- }
496
- function ownerDidMatches(a, b) {
497
- const aPkh = parsePkhOwnerDid(a);
498
- const bPkh = parsePkhOwnerDid(b);
499
- if (aPkh && bPkh) {
500
- return aPkh.chainId === bPkh.chainId && aPkh.address === bPkh.address;
501
- }
502
- return a === b;
503
- }
504
- function networkDiscoveryKey(name) {
505
- if (!NETWORK_NAME_RE.test(name)) {
506
- throw new NetworkIdError(
507
- `network name ${JSON.stringify(name)} must match ${NETWORK_NAME_RE.source}`
508
- );
509
- }
510
- return `.well-known/encryption/network/${name}`;
511
- }
512
- var ENCRYPTION_NETWORK_URN_PREFIX = URN_PREFIX;
513
- var NETWORK_NAME_PATTERN = NETWORK_NAME_RE;
514
-
515
606
  // src/encryption/types.ts
516
607
  var DEFAULT_ENCRYPTION_ALG = "x25519-aes256gcm/v1";
517
608
  var ENVELOPE_VERSION = 1;