@tinycloud/sdk-services 3.1.0-beta.6 → 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
 
@@ -159,11 +159,31 @@ var ENCRYPTION_NETWORK_URN_PREFIX = URN_PREFIX;
159
159
  var NETWORK_NAME_PATTERN = NETWORK_NAME_RE;
160
160
 
161
161
  // src/errors.ts
162
- function timeoutError(service) {
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
+ }
163
181
  return {
164
182
  code: ErrorCodes.TIMEOUT,
165
- message: "Request timed out.",
166
- service
183
+ message: `Request timed out after ${timeoutMs}ms.`,
184
+ service,
185
+ ...cause === void 0 ? {} : { cause },
186
+ meta: { timeoutMs }
167
187
  };
168
188
  }
169
189
  function abortedError(service) {
@@ -181,6 +201,9 @@ var capabilityServices = new Set(
181
201
  );
182
202
  function wrapError(service, error, defaultCode = ErrorCodes.NETWORK_ERROR) {
183
203
  if (error instanceof Error) {
204
+ if (isRequestTimeoutError(error)) {
205
+ return timeoutError(service, error.timeoutMs, error);
206
+ }
184
207
  if (error.name === "AbortError") {
185
208
  return abortedError(service);
186
209
  }
@@ -202,6 +225,7 @@ function wrapError(service, error, defaultCode = ErrorCodes.NETWORK_ERROR) {
202
225
  }
203
226
 
204
227
  // src/base/BaseService.ts
228
+ var MAX_TIMER_DELAY_MS = 2147483647;
205
229
  var BaseService = class {
206
230
  constructor() {
207
231
  /**
@@ -382,6 +406,64 @@ var BaseService = class {
382
406
  }
383
407
  return controller.signal;
384
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
+ }
385
467
  /**
386
468
  * Wrap an operation with error handling and telemetry.
387
469
  *