@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,5 +1,56 @@
1
1
  import { I as IService, a as IServiceContext, S as ServiceSession, b as ServiceError, R as Result } from './types-qwMmB7qB.cjs';
2
2
 
3
+ /**
4
+ * Base Service Types
5
+ *
6
+ * Types specific to the base service infrastructure.
7
+ */
8
+
9
+ /**
10
+ * Service constructor type for registration.
11
+ * Used by the SDK to instantiate services.
12
+ */
13
+ interface ServiceConstructor<TConfig = Record<string, unknown>, TService extends IService = IService> {
14
+ /** Service identifier used for registration */
15
+ readonly serviceName: string;
16
+ /** Create a new instance of the service */
17
+ new (config?: TConfig): TService;
18
+ }
19
+ /**
20
+ * Service registration entry.
21
+ */
22
+ interface ServiceRegistration {
23
+ /** The service class constructor */
24
+ constructor: ServiceConstructor;
25
+ /** Configuration for this service instance */
26
+ config?: Record<string, unknown>;
27
+ }
28
+ /**
29
+ * Options for base service operations.
30
+ */
31
+ interface BaseServiceOptions {
32
+ /** Override the default timeout for this operation */
33
+ timeout?: number;
34
+ /** Custom abort signal for this operation */
35
+ signal?: AbortSignal;
36
+ }
37
+ /**
38
+ * A per-request abort signal and the cleanup that releases it.
39
+ * Created by `BaseService.createRequestSignal()`.
40
+ */
41
+ interface RequestSignal {
42
+ /**
43
+ * Aborts on service sign-out, context abort, the caller's signal, or when
44
+ * the request timeout elapses (reason: a `TimeoutError`).
45
+ */
46
+ readonly signal: AbortSignal;
47
+ /**
48
+ * Clear the timeout and detach from the parent signals. Call once the
49
+ * response has been consumed. Idempotent.
50
+ */
51
+ dispose(): void;
52
+ }
53
+
3
54
  /**
4
55
  * BaseService - Abstract base class for all TinyCloud services.
5
56
  *
@@ -144,6 +195,27 @@ declare abstract class BaseService implements IService {
144
195
  * @returns A combined abort signal
145
196
  */
146
197
  protected combineSignals(...signals: (AbortSignal | undefined)[]): AbortSignal;
198
+ /**
199
+ * Create the abort signal for one request, bounded by the request timeout.
200
+ *
201
+ * The signal aborts when the service signs out, the context aborts, the
202
+ * caller's `signal` aborts, or the timeout elapses, whichever comes first.
203
+ * A timeout aborts with a {@link RequestTimeoutError} (`TimeoutError`), which
204
+ * `wrapError` maps to `ErrorCodes.TIMEOUT`; the other sources keep their own
205
+ * reason (normally `AbortError`, mapped to `ErrorCodes.ABORTED`).
206
+ *
207
+ * The timeout is `timeoutMs` when given, otherwise `config.timeout`. Only a
208
+ * positive, finite value (at most 2^31-1 ms) applies; `undefined`, `0`, and
209
+ * `Infinity` mean no timeout, so a per-call `0` opts out of a configured one.
210
+ *
211
+ * Callers must call `dispose()` once the response body has been consumed,
212
+ * which clears the timer and removes the listeners this adds to the
213
+ * long-lived service and context signals.
214
+ *
215
+ * @param signal - The caller's abort signal for this request
216
+ * @param timeoutMs - Per-request timeout override in milliseconds
217
+ */
218
+ protected createRequestSignal(signal?: AbortSignal, timeoutMs?: number): RequestSignal;
147
219
  /**
148
220
  * Wrap an operation with error handling and telemetry.
149
221
  *
@@ -155,4 +227,4 @@ declare abstract class BaseService implements IService {
155
227
  protected withTelemetry<T>(action: string, key: string | undefined, operation: () => Promise<Result<T>>): Promise<Result<T>>;
156
228
  }
157
229
 
158
- export { BaseService as B };
230
+ export { BaseService as B, type RequestSignal as R, type ServiceConstructor as S, type BaseServiceOptions as a, type ServiceRegistration as b };
@@ -1,5 +1,56 @@
1
1
  import { I as IService, a as IServiceContext, S as ServiceSession, b as ServiceError, R as Result } from './types-qwMmB7qB.js';
2
2
 
3
+ /**
4
+ * Base Service Types
5
+ *
6
+ * Types specific to the base service infrastructure.
7
+ */
8
+
9
+ /**
10
+ * Service constructor type for registration.
11
+ * Used by the SDK to instantiate services.
12
+ */
13
+ interface ServiceConstructor<TConfig = Record<string, unknown>, TService extends IService = IService> {
14
+ /** Service identifier used for registration */
15
+ readonly serviceName: string;
16
+ /** Create a new instance of the service */
17
+ new (config?: TConfig): TService;
18
+ }
19
+ /**
20
+ * Service registration entry.
21
+ */
22
+ interface ServiceRegistration {
23
+ /** The service class constructor */
24
+ constructor: ServiceConstructor;
25
+ /** Configuration for this service instance */
26
+ config?: Record<string, unknown>;
27
+ }
28
+ /**
29
+ * Options for base service operations.
30
+ */
31
+ interface BaseServiceOptions {
32
+ /** Override the default timeout for this operation */
33
+ timeout?: number;
34
+ /** Custom abort signal for this operation */
35
+ signal?: AbortSignal;
36
+ }
37
+ /**
38
+ * A per-request abort signal and the cleanup that releases it.
39
+ * Created by `BaseService.createRequestSignal()`.
40
+ */
41
+ interface RequestSignal {
42
+ /**
43
+ * Aborts on service sign-out, context abort, the caller's signal, or when
44
+ * the request timeout elapses (reason: a `TimeoutError`).
45
+ */
46
+ readonly signal: AbortSignal;
47
+ /**
48
+ * Clear the timeout and detach from the parent signals. Call once the
49
+ * response has been consumed. Idempotent.
50
+ */
51
+ dispose(): void;
52
+ }
53
+
3
54
  /**
4
55
  * BaseService - Abstract base class for all TinyCloud services.
5
56
  *
@@ -144,6 +195,27 @@ declare abstract class BaseService implements IService {
144
195
  * @returns A combined abort signal
145
196
  */
146
197
  protected combineSignals(...signals: (AbortSignal | undefined)[]): AbortSignal;
198
+ /**
199
+ * Create the abort signal for one request, bounded by the request timeout.
200
+ *
201
+ * The signal aborts when the service signs out, the context aborts, the
202
+ * caller's `signal` aborts, or the timeout elapses, whichever comes first.
203
+ * A timeout aborts with a {@link RequestTimeoutError} (`TimeoutError`), which
204
+ * `wrapError` maps to `ErrorCodes.TIMEOUT`; the other sources keep their own
205
+ * reason (normally `AbortError`, mapped to `ErrorCodes.ABORTED`).
206
+ *
207
+ * The timeout is `timeoutMs` when given, otherwise `config.timeout`. Only a
208
+ * positive, finite value (at most 2^31-1 ms) applies; `undefined`, `0`, and
209
+ * `Infinity` mean no timeout, so a per-call `0` opts out of a configured one.
210
+ *
211
+ * Callers must call `dispose()` once the response body has been consumed,
212
+ * which clears the timer and removes the listeners this adds to the
213
+ * long-lived service and context signals.
214
+ *
215
+ * @param signal - The caller's abort signal for this request
216
+ * @param timeoutMs - Per-request timeout override in milliseconds
217
+ */
218
+ protected createRequestSignal(signal?: AbortSignal, timeoutMs?: number): RequestSignal;
147
219
  /**
148
220
  * Wrap an operation with error handling and telemetry.
149
221
  *
@@ -155,4 +227,4 @@ declare abstract class BaseService implements IService {
155
227
  protected withTelemetry<T>(action: string, key: string | undefined, operation: () => Promise<Result<T>>): Promise<Result<T>>;
156
228
  }
157
229
 
158
- export { BaseService as B };
230
+ export { BaseService as B, type RequestSignal as R, type ServiceConstructor as S, type BaseServiceOptions as a, type ServiceRegistration as b };
@@ -225,11 +225,31 @@ var ENCRYPTION_NETWORK_URN_PREFIX = URN_PREFIX;
225
225
  var NETWORK_NAME_PATTERN = NETWORK_NAME_RE;
226
226
 
227
227
  // src/errors.ts
228
- function timeoutError(service) {
228
+ var RequestTimeoutError = class extends Error {
229
+ constructor(timeoutMs) {
230
+ super(`Request timed out after ${timeoutMs}ms.`);
231
+ this.name = "TimeoutError";
232
+ this.timeoutMs = timeoutMs;
233
+ }
234
+ };
235
+ function isRequestTimeoutError(error) {
236
+ return error instanceof RequestTimeoutError || error instanceof Error && error.name === "TimeoutError" && typeof error.timeoutMs === "number";
237
+ }
238
+ function timeoutError(service, timeoutMs, cause) {
239
+ if (timeoutMs === void 0) {
240
+ return {
241
+ code: ErrorCodes.TIMEOUT,
242
+ message: "Request timed out.",
243
+ service,
244
+ ...cause === void 0 ? {} : { cause }
245
+ };
246
+ }
229
247
  return {
230
248
  code: ErrorCodes.TIMEOUT,
231
- message: "Request timed out.",
232
- service
249
+ message: `Request timed out after ${timeoutMs}ms.`,
250
+ service,
251
+ ...cause === void 0 ? {} : { cause },
252
+ meta: { timeoutMs }
233
253
  };
234
254
  }
235
255
  function abortedError(service) {
@@ -247,6 +267,9 @@ var capabilityServices = new Set(
247
267
  );
248
268
  function wrapError(service, error, defaultCode = ErrorCodes.NETWORK_ERROR) {
249
269
  if (error instanceof Error) {
270
+ if (isRequestTimeoutError(error)) {
271
+ return timeoutError(service, error.timeoutMs, error);
272
+ }
250
273
  if (error.name === "AbortError") {
251
274
  return abortedError(service);
252
275
  }
@@ -268,6 +291,7 @@ function wrapError(service, error, defaultCode = ErrorCodes.NETWORK_ERROR) {
268
291
  }
269
292
 
270
293
  // src/base/BaseService.ts
294
+ var MAX_TIMER_DELAY_MS = 2147483647;
271
295
  var BaseService = class {
272
296
  constructor() {
273
297
  /**
@@ -448,6 +472,64 @@ var BaseService = class {
448
472
  }
449
473
  return controller.signal;
450
474
  }
475
+ /**
476
+ * Create the abort signal for one request, bounded by the request timeout.
477
+ *
478
+ * The signal aborts when the service signs out, the context aborts, the
479
+ * caller's `signal` aborts, or the timeout elapses, whichever comes first.
480
+ * A timeout aborts with a {@link RequestTimeoutError} (`TimeoutError`), which
481
+ * `wrapError` maps to `ErrorCodes.TIMEOUT`; the other sources keep their own
482
+ * reason (normally `AbortError`, mapped to `ErrorCodes.ABORTED`).
483
+ *
484
+ * The timeout is `timeoutMs` when given, otherwise `config.timeout`. Only a
485
+ * positive, finite value (at most 2^31-1 ms) applies; `undefined`, `0`, and
486
+ * `Infinity` mean no timeout, so a per-call `0` opts out of a configured one.
487
+ *
488
+ * Callers must call `dispose()` once the response body has been consumed,
489
+ * which clears the timer and removes the listeners this adds to the
490
+ * long-lived service and context signals.
491
+ *
492
+ * @param signal - The caller's abort signal for this request
493
+ * @param timeoutMs - Per-request timeout override in milliseconds
494
+ */
495
+ createRequestSignal(signal, timeoutMs) {
496
+ const controller = new AbortController();
497
+ const detachers = [];
498
+ let timer;
499
+ const dispose = () => {
500
+ if (timer !== void 0) {
501
+ clearTimeout(timer);
502
+ timer = void 0;
503
+ }
504
+ for (const detach of detachers.splice(0)) {
505
+ detach();
506
+ }
507
+ };
508
+ const abort = (reason) => {
509
+ dispose();
510
+ controller.abort(reason);
511
+ };
512
+ const requestSignal = { signal: controller.signal, dispose };
513
+ const parents = [
514
+ this.abortController.signal,
515
+ this.context?.abortSignal,
516
+ signal
517
+ ].filter((parent) => parent !== void 0);
518
+ for (const parent of parents) {
519
+ if (parent.aborted) {
520
+ abort(parent.reason);
521
+ return requestSignal;
522
+ }
523
+ const onAbort = () => abort(parent.reason);
524
+ parent.addEventListener("abort", onAbort, { once: true });
525
+ detachers.push(() => parent.removeEventListener("abort", onAbort));
526
+ }
527
+ const timeout = timeoutMs ?? this._config.timeout;
528
+ if (typeof timeout === "number" && timeout > 0 && timeout <= MAX_TIMER_DELAY_MS) {
529
+ timer = setTimeout(() => abort(new RequestTimeoutError(timeout)), timeout);
530
+ }
531
+ return requestSignal;
532
+ }
451
533
  /**
452
534
  * Wrap an operation with error handling and telemetry.
453
535
  *