@private.me/xbind 3.0.2 → 3.0.4

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.
Files changed (222) hide show
  1. package/README.md +2366 -204
  2. package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1 -1920
  3. package/dist-standalone/_deps/shared/cjs/errors.js +1 -729
  4. package/dist-standalone/_deps/shared/cjs/index.js +1 -463
  5. package/dist-standalone/_deps/shared/cjs/types.js +1 -315
  6. package/dist-standalone/_deps/shared/errors.js +1 -244
  7. package/dist-standalone/_deps/shared/index.js +1 -72
  8. package/dist-standalone/_deps/shared/types.js +1 -86
  9. package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -1
  10. package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -1
  11. package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +1 -1
  12. package/dist-standalone/_deps/ux-helpers/cjs/progress.js +1 -1
  13. package/dist-standalone/_deps/ux-helpers/cjs/search.js +1 -1
  14. package/dist-standalone/_deps/ux-helpers/cjs/types.js +1 -1
  15. package/dist-standalone/_deps/ux-helpers/errors.js +1 -1
  16. package/dist-standalone/_deps/ux-helpers/index.js +1 -1
  17. package/dist-standalone/_deps/ux-helpers/pagination.js +1 -1
  18. package/dist-standalone/_deps/ux-helpers/progress.js +1 -1
  19. package/dist-standalone/_deps/ux-helpers/search.js +1 -1
  20. package/dist-standalone/_deps/xchange/auto-accept.js +1 -1
  21. package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -1
  22. package/dist-standalone/_deps/xchange/cjs/errors.js +1 -1
  23. package/dist-standalone/_deps/xchange/cjs/index.js +1 -1
  24. package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -1
  25. package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -1
  26. package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -1
  27. package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -1
  28. package/dist-standalone/_deps/xchange/errors.js +1 -1
  29. package/dist-standalone/_deps/xchange/index.js +1 -1
  30. package/dist-standalone/_deps/xchange/invite-client.js +1 -1
  31. package/dist-standalone/_deps/xchange/lazy-init.js +1 -1
  32. package/dist-standalone/_deps/xchange/trust-integration.js +1 -1
  33. package/dist-standalone/_deps/xchange/xchange.js +1 -1
  34. package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -1
  35. package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -1
  36. package/dist-standalone/_deps/xregistry/cjs/index.js +1 -1
  37. package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -1
  38. package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -1
  39. package/dist-standalone/_deps/xregistry/cjs/types.js +1 -1
  40. package/dist-standalone/_deps/xregistry/discovery.js +1 -1
  41. package/dist-standalone/_deps/xregistry/errors.js +1 -1
  42. package/dist-standalone/_deps/xregistry/index.js +1 -1
  43. package/dist-standalone/_deps/xregistry/registry.js +1 -1
  44. package/dist-standalone/_deps/xregistry/schema.js +1 -1
  45. package/dist-standalone/_deps/xregistry/types.js +1 -1
  46. package/dist-standalone/agent-call.d.ts +2 -2
  47. package/dist-standalone/agent-call.js +1 -659
  48. package/dist-standalone/agent-sdk.js +1 -328
  49. package/dist-standalone/agent.d.ts +2 -0
  50. package/dist-standalone/agent.js +1 -1800
  51. package/dist-standalone/approval.js +1 -193
  52. package/dist-standalone/async-iterators.d.ts +3 -3
  53. package/dist-standalone/async-iterators.js +1 -382
  54. package/dist-standalone/auth.js +1 -219
  55. package/dist-standalone/auto-accept.js +1 -229
  56. package/dist-standalone/backup-config.js +1 -201
  57. package/dist-standalone/backup.js +1 -326
  58. package/dist-standalone/batch-operations.js +1 -388
  59. package/dist-standalone/cancellation.js +1 -477
  60. package/dist-standalone/checkpoint.js +1 -186
  61. package/dist-standalone/circuit-breaker.js +1 -468
  62. package/dist-standalone/cjs/agent-call.js +1 -701
  63. package/dist-standalone/cjs/agent-sdk.js +1 -332
  64. package/dist-standalone/cjs/agent.js +1 -1837
  65. package/dist-standalone/cjs/approval.js +1 -199
  66. package/dist-standalone/cjs/async-iterators.js +1 -392
  67. package/dist-standalone/cjs/auth.js +1 -225
  68. package/dist-standalone/cjs/auto-accept.js +1 -233
  69. package/dist-standalone/cjs/backup-config.js +1 -207
  70. package/dist-standalone/cjs/backup.js +1 -330
  71. package/dist-standalone/cjs/batch-operations.js +1 -397
  72. package/dist-standalone/cjs/cancellation.js +1 -490
  73. package/dist-standalone/cjs/checkpoint.js +1 -193
  74. package/dist-standalone/cjs/circuit-breaker.js +1 -476
  75. package/dist-standalone/cjs/cli/init.js +1 -492
  76. package/dist-standalone/cjs/config-validation.js +1 -522
  77. package/dist-standalone/cjs/connect.js +1 -312
  78. package/dist-standalone/cjs/connection-pool.js +1 -506
  79. package/dist-standalone/cjs/correlation-id.js +1 -339
  80. package/dist-standalone/cjs/crypto-utils.js +1 -176
  81. package/dist-standalone/cjs/debug-mode.js +1 -534
  82. package/dist-standalone/cjs/did-document.js +1 -101
  83. package/dist-standalone/cjs/did-privateme.js +1 -130
  84. package/dist-standalone/cjs/did-web.js +1 -201
  85. package/dist-standalone/cjs/discovery.js +1 -462
  86. package/dist-standalone/cjs/dual-mode.js +1 -251
  87. package/dist-standalone/cjs/email-templates.js +1 -313
  88. package/dist-standalone/cjs/email-transport.js +1 -239
  89. package/dist-standalone/cjs/envelope.js +1 -538
  90. package/dist-standalone/cjs/errors.js +1 -913
  91. package/dist-standalone/cjs/event-emitter.js +1 -461
  92. package/dist-standalone/cjs/gateway-state.js +1 -55
  93. package/dist-standalone/cjs/gateway-transport.js +1 -120
  94. package/dist-standalone/cjs/graceful-degradation.js +1 -403
  95. package/dist-standalone/cjs/guardrails.js +1 -223
  96. package/dist-standalone/cjs/health-check.js +1 -336
  97. package/dist-standalone/cjs/http-compat.js +1 -272
  98. package/dist-standalone/cjs/http-status-map.js +1 -571
  99. package/dist-standalone/cjs/identity.js +1 -645
  100. package/dist-standalone/cjs/index.js +1 -406
  101. package/dist-standalone/cjs/invitation.js +1 -421
  102. package/dist-standalone/cjs/invite.js +1 -328
  103. package/dist-standalone/cjs/key-agreement.js +1 -335
  104. package/dist-standalone/cjs/lazy-init.js +1 -300
  105. package/dist-standalone/cjs/logger.js +1 -291
  106. package/dist-standalone/cjs/loopback-transport.js +1 -0
  107. package/dist-standalone/cjs/mdns-discovery.js +1 -202
  108. package/dist-standalone/cjs/nonce-store.js +1 -80
  109. package/dist-standalone/cjs/pairing-manager.js +1 -223
  110. package/dist-standalone/cjs/plugin-system.js +1 -264
  111. package/dist-standalone/cjs/plugins/logging.js +1 -168
  112. package/dist-standalone/cjs/plugins/metrics.js +1 -181
  113. package/dist-standalone/cjs/plugins/validation.js +1 -302
  114. package/dist-standalone/cjs/policy.js +1 -320
  115. package/dist-standalone/cjs/progress-callbacks.js +1 -583
  116. package/dist-standalone/cjs/redis-nonce-store.js +1 -76
  117. package/dist-standalone/cjs/registry-middleware.js +1 -50
  118. package/dist-standalone/cjs/retry-strategies.js +1 -544
  119. package/dist-standalone/cjs/retry-transport.js +1 -102
  120. package/dist-standalone/cjs/runtime/browser.js +1 -533
  121. package/dist-standalone/cjs/runtime/edge.js +1 -526
  122. package/dist-standalone/cjs/runtime/react-native.js +1 -394
  123. package/dist-standalone/cjs/security-policy.js +1 -245
  124. package/dist-standalone/cjs/serialization.js +1 -1040
  125. package/dist-standalone/cjs/split-channel.js +1 -225
  126. package/dist-standalone/cjs/subscription-proof.js +1 -230
  127. package/dist-standalone/cjs/succession.js +1 -148
  128. package/dist-standalone/cjs/timeouts.js +1 -412
  129. package/dist-standalone/cjs/trace-context.js +1 -424
  130. package/dist-standalone/cjs/trace-spans.js +1 -495
  131. package/dist-standalone/cjs/transport.js +1 -63
  132. package/dist-standalone/cjs/trust-registry.js +1 -991
  133. package/dist-standalone/cjs/types/error-response.js +1 -56
  134. package/dist-standalone/cjs/vault-auth.js +1 -178
  135. package/dist-standalone/cjs/vault-store-loader.js +1 -194
  136. package/dist-standalone/cjs/verify.js +1 -25
  137. package/dist-standalone/cjs/version-info.js +1 -543
  138. package/dist-standalone/cjs/xfetch.js +1 -340
  139. package/dist-standalone/cli/init.js +1 -455
  140. package/dist-standalone/cli/setup.js +1 -514
  141. package/dist-standalone/cli/types.js +1 -27
  142. package/dist-standalone/cli/xbind.js +1 -148
  143. package/dist-standalone/config-validation.js +1 -513
  144. package/dist-standalone/connect.js +1 -274
  145. package/dist-standalone/connection-pool.js +1 -500
  146. package/dist-standalone/correlation-id.js +1 -326
  147. package/dist-standalone/crypto-utils.d.ts +2 -7
  148. package/dist-standalone/crypto-utils.js +1 -157
  149. package/dist-standalone/debug-mode.js +1 -510
  150. package/dist-standalone/did-document.js +1 -96
  151. package/dist-standalone/did-privateme.js +1 -121
  152. package/dist-standalone/did-web.js +1 -196
  153. package/dist-standalone/discovery.js +1 -458
  154. package/dist-standalone/dual-mode.js +1 -247
  155. package/dist-standalone/email-templates.js +1 -309
  156. package/dist-standalone/email-transport.d.ts +2 -2
  157. package/dist-standalone/email-transport.js +1 -232
  158. package/dist-standalone/envelope.js +1 -525
  159. package/dist-standalone/errors.d.ts +13 -3
  160. package/dist-standalone/errors.js +1 -896
  161. package/dist-standalone/event-emitter.js +1 -456
  162. package/dist-standalone/gateway-state.d.ts +1 -1
  163. package/dist-standalone/gateway-state.js +1 -51
  164. package/dist-standalone/gateway-transport.js +1 -116
  165. package/dist-standalone/graceful-degradation.js +1 -396
  166. package/dist-standalone/guardrails.js +1 -216
  167. package/dist-standalone/health-check.d.ts +5 -1
  168. package/dist-standalone/health-check.js +1 -332
  169. package/dist-standalone/http-compat.d.ts +1 -1
  170. package/dist-standalone/http-compat.js +1 -267
  171. package/dist-standalone/http-status-map.js +1 -561
  172. package/dist-standalone/identity.js +1 -619
  173. package/dist-standalone/index.d.ts +15 -4
  174. package/dist-standalone/index.js +1 -78
  175. package/dist-standalone/invitation.js +1 -415
  176. package/dist-standalone/invite.js +1 -324
  177. package/dist-standalone/key-agreement.js +1 -325
  178. package/dist-standalone/lazy-init.d.ts +11 -6
  179. package/dist-standalone/lazy-init.js +1 -295
  180. package/dist-standalone/logger.js +1 -285
  181. package/dist-standalone/loopback-transport.d.ts +87 -0
  182. package/dist-standalone/loopback-transport.js +1 -0
  183. package/dist-standalone/mdns-discovery.js +1 -195
  184. package/dist-standalone/nonce-store.js +1 -76
  185. package/dist-standalone/pairing-manager.js +1 -219
  186. package/dist-standalone/plugin-system.js +1 -257
  187. package/dist-standalone/plugins/logging.js +1 -163
  188. package/dist-standalone/plugins/metrics.d.ts +4 -4
  189. package/dist-standalone/plugins/metrics.js +1 -176
  190. package/dist-standalone/plugins/validation.js +1 -297
  191. package/dist-standalone/policy.js +1 -315
  192. package/dist-standalone/progress-callbacks.js +1 -576
  193. package/dist-standalone/redis-nonce-store.js +1 -72
  194. package/dist-standalone/registry-middleware.js +1 -47
  195. package/dist-standalone/retry-strategies.js +1 -534
  196. package/dist-standalone/retry-transport.js +1 -98
  197. package/dist-standalone/runtime/browser.js +1 -516
  198. package/dist-standalone/runtime/edge.js +1 -511
  199. package/dist-standalone/runtime/react-native.d.ts +1 -1
  200. package/dist-standalone/runtime/react-native.js +1 -383
  201. package/dist-standalone/security-policy.js +1 -239
  202. package/dist-standalone/serialization.js +1 -1031
  203. package/dist-standalone/split-channel.d.ts +1 -1
  204. package/dist-standalone/split-channel.js +1 -219
  205. package/dist-standalone/subscription-proof.js +1 -224
  206. package/dist-standalone/succession.js +1 -142
  207. package/dist-standalone/timeouts.js +1 -398
  208. package/dist-standalone/trace-context.js +1 -414
  209. package/dist-standalone/trace-spans.js +1 -488
  210. package/dist-standalone/transport.d.ts +1 -1
  211. package/dist-standalone/transport.js +1 -59
  212. package/dist-standalone/trust-registry.d.ts +3 -3
  213. package/dist-standalone/trust-registry.js +1 -950
  214. package/dist-standalone/types/error-response.js +1 -52
  215. package/dist-standalone/vault-auth.js +1 -174
  216. package/dist-standalone/vault-store-loader.d.ts +9 -0
  217. package/dist-standalone/vault-store-loader.js +1 -187
  218. package/dist-standalone/verify.js +1 -16
  219. package/dist-standalone/version-info.js +1 -530
  220. package/dist-standalone/xfetch.js +1 -335
  221. package/package.json +1 -1
  222. package/share1.dat +0 -0
@@ -1,398 +1 @@
1
- /**
2
- * @module timeouts
3
- * Configurable request timeouts for xBind operations
4
- *
5
- * Provides centralized timeout management with:
6
- * - Per-operation timeout configuration
7
- * - Default timeouts for registry, gateway, transport
8
- * - Timeout inheritance and overrides
9
- * - Timeout error messages with context
10
- * - Integration with AbortController/cancellation tokens
11
- *
12
- * @example
13
- * ```typescript
14
- * import { TimeoutConfig, withTimeout, createTimeoutController } from '@private.me/xbind';
15
- *
16
- * // Create timeout configuration
17
- * const config = new TimeoutConfig({
18
- * registry: 10000,
19
- * gateway: 15000,
20
- * transport: 30000,
21
- * });
22
- *
23
- * // Use timeout with fetch
24
- * const controller = createTimeoutController(config.registry);
25
- * const response = await fetch(url, { signal: controller.signal });
26
- *
27
- * // Wrap async operation with timeout
28
- * const result = await withTimeout(
29
- * async () => performOperation(),
30
- * 5000,
31
- * 'Operation description'
32
- * );
33
- * ```
34
- */
35
- import { err, ok } from"./_deps/shared/index.js";
36
- /**
37
- * Timeout error with operation context.
38
- */
39
- export class TimeoutError extends Error {
40
- code = 'TIMEOUT';
41
- operation;
42
- timeoutMs;
43
- timestamp;
44
- constructor(operation, timeoutMs) {
45
- super(`Operation '${operation}' timed out after ${timeoutMs}ms. ` +
46
- `Actions: (1) Check network connectivity, (2) Increase timeout threshold, ` +
47
- `(3) Verify service availability, (4) Implement retry with exponential backoff.`);
48
- this.name = 'TimeoutError';
49
- this.operation = operation;
50
- this.timeoutMs = timeoutMs;
51
- this.timestamp = Date.now();
52
- }
53
- /**
54
- * Create timeout error with additional context.
55
- */
56
- static withContext(operation, timeoutMs, context) {
57
- const error = new TimeoutError(operation, timeoutMs);
58
- if (context) {
59
- Object.assign(error, { context });
60
- }
61
- return error;
62
- }
63
- }
64
- /**
65
- * Default timeout values (milliseconds).
66
- */
67
- export const DEFAULT_TIMEOUTS = {
68
- registry: 10_000, // 10 seconds for registry lookups
69
- gateway: 15_000, // 15 seconds for gateway operations
70
- transport: 30_000, // 30 seconds for transport/HTTP
71
- default: 30_000, // 30 seconds fallback
72
- };
73
- /**
74
- * Timeout configuration manager.
75
- * Provides timeout values with inheritance and override support.
76
- */
77
- export class TimeoutConfig {
78
- registry;
79
- gateway;
80
- transport;
81
- default;
82
- constructor(options = {}) {
83
- this.registry = options.registry ?? DEFAULT_TIMEOUTS.registry;
84
- this.gateway = options.gateway ?? DEFAULT_TIMEOUTS.gateway;
85
- this.transport = options.transport ?? DEFAULT_TIMEOUTS.transport;
86
- this.default = options.default ?? DEFAULT_TIMEOUTS.default;
87
- // Validate timeout values
88
- this.validateTimeout('registry', this.registry);
89
- this.validateTimeout('gateway', this.gateway);
90
- this.validateTimeout('transport', this.transport);
91
- this.validateTimeout('default', this.default);
92
- }
93
- /**
94
- * Get timeout for registry operations.
95
- * @param override - Optional override timeout
96
- */
97
- getRegistry(override) {
98
- return override ?? this.registry;
99
- }
100
- /**
101
- * Get timeout for gateway operations.
102
- * @param override - Optional override timeout
103
- */
104
- getGateway(override) {
105
- return override ?? this.gateway;
106
- }
107
- /**
108
- * Get timeout for transport operations.
109
- * @param override - Optional override timeout
110
- */
111
- getTransport(override) {
112
- return override ?? this.transport;
113
- }
114
- /**
115
- * Get default timeout.
116
- * @param override - Optional override timeout
117
- */
118
- getDefault(override) {
119
- return override ?? this.default;
120
- }
121
- /**
122
- * Get timeout for specific operation category.
123
- * @param category - Operation category
124
- * @param override - Optional override timeout
125
- */
126
- get(category, override) {
127
- switch (category) {
128
- case 'registry':
129
- return this.getRegistry(override);
130
- case 'gateway':
131
- return this.getGateway(override);
132
- case 'transport':
133
- return this.getTransport(override);
134
- case 'default':
135
- return this.getDefault(override);
136
- }
137
- }
138
- /**
139
- * Create new config with overrides.
140
- * @param overrides - Timeout overrides
141
- */
142
- with(overrides) {
143
- return new TimeoutConfig({
144
- registry: overrides.registry ?? this.registry,
145
- gateway: overrides.gateway ?? this.gateway,
146
- transport: overrides.transport ?? this.transport,
147
- default: overrides.default ?? this.default,
148
- });
149
- }
150
- /**
151
- * Validate timeout value.
152
- */
153
- validateTimeout(name, value) {
154
- if (!Number.isFinite(value) || value <= 0) {
155
- throw new Error(`Invalid timeout for '${name}': ${value}. Must be positive finite number.`);
156
- }
157
- if (value > 300_000) {
158
- // Warn if timeout > 5 minutes (likely config error)
159
- console.warn(`[xbind:timeout] Large timeout for '${name}': ${value}ms. ` +
160
- `Consider reducing to avoid indefinite hangs.`);
161
- }
162
- }
163
- /**
164
- * Export configuration as plain object.
165
- */
166
- toJSON() {
167
- return {
168
- registry: this.registry,
169
- gateway: this.gateway,
170
- transport: this.transport,
171
- default: this.default,
172
- };
173
- }
174
- }
175
- /**
176
- * Operation timeout controller with cancellation support.
177
- * Wraps AbortController with timeout semantics.
178
- */
179
- export class OperationTimeoutController {
180
- signal;
181
- timeoutMs;
182
- operation;
183
- controller;
184
- timer;
185
- timedOut = false;
186
- constructor(timeoutMs, operation = 'operation') {
187
- this.controller = new AbortController();
188
- this.signal = this.controller.signal;
189
- this.timeoutMs = timeoutMs;
190
- this.operation = operation;
191
- // Set timeout
192
- this.timer = setTimeout(() => {
193
- this.timedOut = true;
194
- this.controller.abort();
195
- }, timeoutMs);
196
- }
197
- /**
198
- * Cancel the operation (clear timeout).
199
- */
200
- cancel() {
201
- clearTimeout(this.timer);
202
- if (!this.controller.signal.aborted) {
203
- this.controller.abort();
204
- }
205
- }
206
- /**
207
- * Clear timeout if operation completed successfully.
208
- * Call this when operation finishes to prevent unnecessary abort.
209
- */
210
- clear() {
211
- clearTimeout(this.timer);
212
- }
213
- /**
214
- * Check if controller timed out (vs manual cancellation).
215
- */
216
- didTimeout() {
217
- return this.timedOut;
218
- }
219
- /**
220
- * Get timeout error if operation timed out.
221
- */
222
- getTimeoutError() {
223
- return this.timedOut ? new TimeoutError(this.operation, this.timeoutMs) : null;
224
- }
225
- }
226
- /**
227
- * Create timeout controller for operation.
228
- * @param timeoutMs - Timeout in milliseconds
229
- * @param operation - Operation description for error messages
230
- */
231
- export function createTimeoutController(timeoutMs, operation = 'operation') {
232
- return new OperationTimeoutController(timeoutMs, operation);
233
- }
234
- /**
235
- * Wrap async operation with timeout.
236
- * Rejects with TimeoutError if operation exceeds timeout.
237
- *
238
- * @param fn - Async operation to execute
239
- * @param timeoutMs - Timeout in milliseconds
240
- * @param operation - Operation description for error messages
241
- * @returns Result of operation or timeout error
242
- *
243
- * @example
244
- * ```typescript
245
- * const result = await withTimeout(
246
- * async () => fetch('https://api.example.com'),
247
- * 5000,
248
- * 'API request'
249
- * );
250
- * ```
251
- */
252
- export async function withTimeout(fn, timeoutMs, operation = 'operation') {
253
- const controller = createTimeoutController(timeoutMs, operation);
254
- try {
255
- const result = await Promise.race([
256
- fn(),
257
- new Promise((_, reject) => {
258
- controller.signal.addEventListener('abort', () => {
259
- reject(new TimeoutError(operation, timeoutMs));
260
- });
261
- }),
262
- ]);
263
- controller.clear();
264
- return result;
265
- }
266
- catch (error) {
267
- controller.clear();
268
- throw error;
269
- }
270
- }
271
- /**
272
- * Wrap async operation with timeout (Result-based).
273
- * Returns ok(result) or err('TIMEOUT') without throwing.
274
- *
275
- * @param fn - Async operation to execute
276
- * @param timeoutMs - Timeout in milliseconds
277
- * @param operation - Operation description for error messages
278
- * @returns Result with operation result or timeout error
279
- *
280
- * @example
281
- * ```typescript
282
- * const result = await withTimeoutResult(
283
- * async () => fetch('https://api.example.com'),
284
- * 5000,
285
- * 'API request'
286
- * );
287
- * if (result.ok) {
288
- * console.log('Success:', result.value);
289
- * } else {
290
- * console.error('Timeout:', result.error);
291
- * }
292
- * ```
293
- */
294
- export async function withTimeoutResult(fn, timeoutMs, operation = 'operation') {
295
- try {
296
- const result = await withTimeout(fn, timeoutMs, operation);
297
- return ok(result);
298
- }
299
- catch (error) {
300
- if (error instanceof TimeoutError) {
301
- return err('TIMEOUT');
302
- }
303
- throw error; // Re-throw non-timeout errors
304
- }
305
- }
306
- /**
307
- * Create timeout signal for use with fetch/AbortController APIs.
308
- *
309
- * @param timeoutMs - Timeout in milliseconds
310
- * @param operation - Operation description
311
- * @returns AbortSignal that aborts after timeout
312
- *
313
- * @example
314
- * ```typescript
315
- * const signal = createOperationTimeoutSignal(5000, 'fetch request');
316
- * const response = await fetch(url, { signal });
317
- * ```
318
- */
319
- export function createOperationTimeoutSignal(timeoutMs, operation = 'operation') {
320
- return createTimeoutController(timeoutMs, operation).signal;
321
- }
322
- /**
323
- * Timeout helper for operations with cancellation token.
324
- * Returns controller that can be passed to fetch, agent.send, etc.
325
- *
326
- * @param config - Timeout configuration
327
- * @param category - Operation category
328
- * @param operation - Operation description
329
- * @param override - Optional timeout override
330
- * @returns Timeout controller
331
- *
332
- * @example
333
- * ```typescript
334
- * const controller = createOperationTimeout(config, 'registry', 'lookup DID');
335
- * const response = await fetch(url, { signal: controller.signal });
336
- * controller.clear(); // Clear timeout on success
337
- * ```
338
- */
339
- export function createOperationTimeout(config, category, operation, override) {
340
- const timeoutMs = config.get(category, override);
341
- return createTimeoutController(timeoutMs, operation);
342
- }
343
- /**
344
- * Check if error is a timeout error.
345
- */
346
- export function isTimeoutError(error) {
347
- return error instanceof TimeoutError;
348
- }
349
- /**
350
- * Extract timeout from error if it's a timeout error.
351
- * Returns timeout in milliseconds or undefined.
352
- */
353
- export function getTimeoutFromError(error) {
354
- return isTimeoutError(error) ? error.timeoutMs : undefined;
355
- }
356
- /**
357
- * Create timeout configuration from environment variables.
358
- * Looks for:
359
- * - XBIND_TIMEOUT_REGISTRY
360
- * - XBIND_TIMEOUT_GATEWAY
361
- * - XBIND_TIMEOUT_TRANSPORT
362
- * - XBIND_TIMEOUT_DEFAULT
363
- *
364
- * Falls back to default values if not set.
365
- */
366
- export function createTimeoutConfigFromEnv() {
367
- const options = {};
368
- const registry = process.env.XBIND_TIMEOUT_REGISTRY;
369
- if (registry) {
370
- const parsed = parseInt(registry, 10);
371
- if (!isNaN(parsed))
372
- options.registry = parsed;
373
- }
374
- const gateway = process.env.XBIND_TIMEOUT_GATEWAY;
375
- if (gateway) {
376
- const parsed = parseInt(gateway, 10);
377
- if (!isNaN(parsed))
378
- options.gateway = parsed;
379
- }
380
- const transport = process.env.XBIND_TIMEOUT_TRANSPORT;
381
- if (transport) {
382
- const parsed = parseInt(transport, 10);
383
- if (!isNaN(parsed))
384
- options.transport = parsed;
385
- }
386
- const defaultTimeout = process.env.XBIND_TIMEOUT_DEFAULT;
387
- if (defaultTimeout) {
388
- const parsed = parseInt(defaultTimeout, 10);
389
- if (!isNaN(parsed))
390
- options.default = parsed;
391
- }
392
- return new TimeoutConfig(options);
393
- }
394
- /**
395
- * Global default timeout configuration.
396
- * Can be overridden by setting environment variables.
397
- */
398
- export const globalTimeoutConfig = createTimeoutConfigFromEnv();
1
+ import{err,ok}from"./_deps/shared/index.js";export class TimeoutError extends Error{code="TIMEOUT";operation;timeoutMs;timestamp;constructor(t,e){super(`Operation '${t}' timed out after ${e}ms. Actions: (1) Check network connectivity, (2) Increase timeout threshold, (3) Verify service availability, (4) Implement retry with exponential backoff.`),this.name="TimeoutError",this.operation=t,this.timeoutMs=e,this.timestamp=Date.now()}static withContext(t,e,r){const i=new TimeoutError(t,e);return r&&Object.assign(i,{context:r}),i}}export const DEFAULT_TIMEOUTS={registry:1e4,gateway:15e3,transport:3e4,default:3e4};export class TimeoutConfig{registry;gateway;transport;default;constructor(t={}){this.registry=t.registry??DEFAULT_TIMEOUTS.registry,this.gateway=t.gateway??DEFAULT_TIMEOUTS.gateway,this.transport=t.transport??DEFAULT_TIMEOUTS.transport,this.default=t.default??DEFAULT_TIMEOUTS.default,this.validateTimeout("registry",this.registry),this.validateTimeout("gateway",this.gateway),this.validateTimeout("transport",this.transport),this.validateTimeout("default",this.default)}getRegistry(t){return t??this.registry}getGateway(t){return t??this.gateway}getTransport(t){return t??this.transport}getDefault(t){return t??this.default}get(t,e){switch(t){case"registry":return this.getRegistry(e);case"gateway":return this.getGateway(e);case"transport":return this.getTransport(e);case"default":return this.getDefault(e)}}with(t){return new TimeoutConfig({registry:t.registry??this.registry,gateway:t.gateway??this.gateway,transport:t.transport??this.transport,default:t.default??this.default})}validateTimeout(t,e){if(!Number.isFinite(e)||e<=0)throw new Error(`Invalid timeout for '${t}': ${e}. Must be positive finite number.`);e>3e5&&console.warn(`[xbind:timeout] Large timeout for '${t}': ${e}ms. Consider reducing to avoid indefinite hangs.`)}toJSON(){return{registry:this.registry,gateway:this.gateway,transport:this.transport,default:this.default}}}export class OperationTimeoutController{signal;timeoutMs;operation;controller;timer;timedOut=!1;constructor(t,e="operation"){this.controller=new AbortController,this.signal=this.controller.signal,this.timeoutMs=t,this.operation=e,this.timer=setTimeout(()=>{this.timedOut=!0,this.controller.abort()},t)}cancel(){clearTimeout(this.timer),this.controller.signal.aborted||this.controller.abort()}clear(){clearTimeout(this.timer)}didTimeout(){return this.timedOut}getTimeoutError(){return this.timedOut?new TimeoutError(this.operation,this.timeoutMs):null}}export function createTimeoutController(t,e="operation"){return new OperationTimeoutController(t,e)}export async function withTimeout(t,e,r="operation"){const i=createTimeoutController(e,r);try{const o=await Promise.race([t(),new Promise((t,o)=>{i.signal.addEventListener("abort",()=>{o(new TimeoutError(r,e))})})]);return i.clear(),o}catch(t){throw i.clear(),t}}export async function withTimeoutResult(t,e,r="operation"){try{const i=await withTimeout(t,e,r);return ok(i)}catch(t){if(t instanceof TimeoutError)return err("TIMEOUT");throw t}}export function createOperationTimeoutSignal(t,e="operation"){return createTimeoutController(t,e).signal}export function createOperationTimeout(t,e,r,i){return createTimeoutController(t.get(e,i),r)}export function isTimeoutError(t){return t instanceof TimeoutError}export function getTimeoutFromError(t){return isTimeoutError(t)?t.timeoutMs:void 0}export function createTimeoutConfigFromEnv(){const t={},e=process.env.XBIND_TIMEOUT_REGISTRY;if(e){const r=parseInt(e,10);isNaN(r)||(t.registry=r)}const r=process.env.XBIND_TIMEOUT_GATEWAY;if(r){const e=parseInt(r,10);isNaN(e)||(t.gateway=e)}const i=process.env.XBIND_TIMEOUT_TRANSPORT;if(i){const e=parseInt(i,10);isNaN(e)||(t.transport=e)}const o=process.env.XBIND_TIMEOUT_DEFAULT;if(o){const e=parseInt(o,10);isNaN(e)||(t.default=e)}return new TimeoutConfig(t)}export const globalTimeoutConfig=createTimeoutConfigFromEnv();