@private.me/xbind 3.0.2 → 3.0.3

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