@private.me/xbind 3.0.1 → 3.0.2

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 (210) hide show
  1. package/README.md +55 -14
  2. package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1920 -1
  3. package/dist-standalone/_deps/shared/cjs/errors.js +729 -1
  4. package/dist-standalone/_deps/shared/cjs/index.js +463 -1
  5. package/dist-standalone/_deps/shared/cjs/types.js +315 -1
  6. package/dist-standalone/_deps/shared/errors.js +244 -1
  7. package/dist-standalone/_deps/shared/index.js +72 -1
  8. package/dist-standalone/_deps/shared/types.js +86 -1
  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.js +659 -1
  47. package/dist-standalone/agent-sdk.js +328 -1
  48. package/dist-standalone/agent.js +1800 -1
  49. package/dist-standalone/approval.js +193 -1
  50. package/dist-standalone/async-iterators.js +382 -1
  51. package/dist-standalone/auth.js +219 -1
  52. package/dist-standalone/auto-accept.js +229 -1
  53. package/dist-standalone/backup-config.js +201 -1
  54. package/dist-standalone/backup.js +326 -1
  55. package/dist-standalone/batch-operations.js +388 -1
  56. package/dist-standalone/cancellation.js +477 -1
  57. package/dist-standalone/checkpoint.js +186 -1
  58. package/dist-standalone/circuit-breaker.js +468 -1
  59. package/dist-standalone/cjs/agent-call.js +701 -1
  60. package/dist-standalone/cjs/agent-sdk.js +332 -1
  61. package/dist-standalone/cjs/agent.js +1837 -1
  62. package/dist-standalone/cjs/approval.js +199 -1
  63. package/dist-standalone/cjs/async-iterators.js +392 -1
  64. package/dist-standalone/cjs/auth.js +225 -1
  65. package/dist-standalone/cjs/auto-accept.js +233 -1
  66. package/dist-standalone/cjs/backup-config.js +207 -1
  67. package/dist-standalone/cjs/backup.js +330 -1
  68. package/dist-standalone/cjs/batch-operations.js +397 -1
  69. package/dist-standalone/cjs/cancellation.js +490 -1
  70. package/dist-standalone/cjs/checkpoint.js +193 -1
  71. package/dist-standalone/cjs/circuit-breaker.js +476 -1
  72. package/dist-standalone/cjs/cli/init.js +492 -1
  73. package/dist-standalone/cjs/config-validation.js +522 -1
  74. package/dist-standalone/cjs/connect.js +312 -1
  75. package/dist-standalone/cjs/connection-pool.js +506 -1
  76. package/dist-standalone/cjs/correlation-id.js +339 -1
  77. package/dist-standalone/cjs/crypto-utils.js +176 -1
  78. package/dist-standalone/cjs/debug-mode.js +534 -1
  79. package/dist-standalone/cjs/did-document.js +101 -1
  80. package/dist-standalone/cjs/did-privateme.js +130 -1
  81. package/dist-standalone/cjs/did-web.js +201 -1
  82. package/dist-standalone/cjs/discovery.js +462 -1
  83. package/dist-standalone/cjs/dual-mode.js +251 -1
  84. package/dist-standalone/cjs/email-templates.js +313 -1
  85. package/dist-standalone/cjs/email-transport.js +239 -1
  86. package/dist-standalone/cjs/envelope.js +538 -1
  87. package/dist-standalone/cjs/errors.js +913 -1
  88. package/dist-standalone/cjs/event-emitter.js +461 -1
  89. package/dist-standalone/cjs/gateway-state.js +55 -1
  90. package/dist-standalone/cjs/gateway-transport.js +120 -1
  91. package/dist-standalone/cjs/graceful-degradation.js +403 -1
  92. package/dist-standalone/cjs/guardrails.js +223 -1
  93. package/dist-standalone/cjs/health-check.js +336 -1
  94. package/dist-standalone/cjs/http-compat.js +272 -1
  95. package/dist-standalone/cjs/http-status-map.js +571 -1
  96. package/dist-standalone/cjs/identity.js +645 -1
  97. package/dist-standalone/cjs/index.js +406 -1
  98. package/dist-standalone/cjs/invitation.js +421 -1
  99. package/dist-standalone/cjs/invite.js +328 -1
  100. package/dist-standalone/cjs/key-agreement.js +335 -1
  101. package/dist-standalone/cjs/lazy-init.js +300 -1
  102. package/dist-standalone/cjs/logger.js +291 -1
  103. package/dist-standalone/cjs/mdns-discovery.js +202 -1
  104. package/dist-standalone/cjs/nonce-store.js +80 -1
  105. package/dist-standalone/cjs/pairing-manager.js +223 -1
  106. package/dist-standalone/cjs/plugin-system.js +264 -1
  107. package/dist-standalone/cjs/plugins/logging.js +168 -1
  108. package/dist-standalone/cjs/plugins/metrics.js +181 -1
  109. package/dist-standalone/cjs/plugins/validation.js +302 -1
  110. package/dist-standalone/cjs/policy.js +320 -1
  111. package/dist-standalone/cjs/progress-callbacks.js +583 -1
  112. package/dist-standalone/cjs/redis-nonce-store.js +76 -1
  113. package/dist-standalone/cjs/registry-middleware.js +50 -1
  114. package/dist-standalone/cjs/retry-strategies.js +544 -1
  115. package/dist-standalone/cjs/retry-transport.js +102 -1
  116. package/dist-standalone/cjs/runtime/browser.js +533 -1
  117. package/dist-standalone/cjs/runtime/edge.js +526 -1
  118. package/dist-standalone/cjs/runtime/react-native.js +394 -1
  119. package/dist-standalone/cjs/security-policy.js +245 -1
  120. package/dist-standalone/cjs/serialization.js +1040 -1
  121. package/dist-standalone/cjs/split-channel.js +225 -1
  122. package/dist-standalone/cjs/subscription-proof.js +230 -1
  123. package/dist-standalone/cjs/succession.js +148 -1
  124. package/dist-standalone/cjs/timeouts.js +412 -1
  125. package/dist-standalone/cjs/trace-context.js +424 -1
  126. package/dist-standalone/cjs/trace-spans.js +495 -1
  127. package/dist-standalone/cjs/transport.js +63 -1
  128. package/dist-standalone/cjs/trust-registry.js +991 -1
  129. package/dist-standalone/cjs/types/error-response.js +56 -1
  130. package/dist-standalone/cjs/vault-auth.js +178 -1
  131. package/dist-standalone/cjs/vault-store-loader.js +194 -1
  132. package/dist-standalone/cjs/verify.js +25 -1
  133. package/dist-standalone/cjs/version-info.js +543 -1
  134. package/dist-standalone/cjs/xfetch.js +340 -1
  135. package/dist-standalone/cli/init.js +455 -1
  136. package/dist-standalone/cli/setup.js +514 -1
  137. package/dist-standalone/cli/types.js +27 -1
  138. package/dist-standalone/cli/xbind.js +148 -1
  139. package/dist-standalone/config-validation.js +513 -1
  140. package/dist-standalone/connect.js +274 -1
  141. package/dist-standalone/connection-pool.js +500 -1
  142. package/dist-standalone/correlation-id.js +326 -1
  143. package/dist-standalone/crypto-utils.js +157 -1
  144. package/dist-standalone/debug-mode.js +510 -1
  145. package/dist-standalone/did-document.js +96 -1
  146. package/dist-standalone/did-privateme.js +121 -1
  147. package/dist-standalone/did-web.js +196 -1
  148. package/dist-standalone/discovery.js +458 -1
  149. package/dist-standalone/dual-mode.js +247 -1
  150. package/dist-standalone/email-templates.js +309 -1
  151. package/dist-standalone/email-transport.js +232 -1
  152. package/dist-standalone/envelope.js +525 -1
  153. package/dist-standalone/errors.js +896 -1
  154. package/dist-standalone/event-emitter.js +456 -1
  155. package/dist-standalone/gateway-state.js +51 -1
  156. package/dist-standalone/gateway-transport.js +116 -1
  157. package/dist-standalone/graceful-degradation.js +396 -1
  158. package/dist-standalone/guardrails.js +216 -1
  159. package/dist-standalone/health-check.js +332 -1
  160. package/dist-standalone/http-compat.js +267 -1
  161. package/dist-standalone/http-status-map.js +561 -1
  162. package/dist-standalone/identity.js +619 -1
  163. package/dist-standalone/index.js +78 -1
  164. package/dist-standalone/invitation.js +415 -1
  165. package/dist-standalone/invite.js +324 -1
  166. package/dist-standalone/key-agreement.js +325 -1
  167. package/dist-standalone/lazy-init.js +295 -1
  168. package/dist-standalone/logger.js +285 -1
  169. package/dist-standalone/mdns-discovery.js +195 -1
  170. package/dist-standalone/nonce-store.js +76 -1
  171. package/dist-standalone/pairing-manager.js +219 -1
  172. package/dist-standalone/plugin-system.js +257 -1
  173. package/dist-standalone/plugins/logging.js +163 -1
  174. package/dist-standalone/plugins/metrics.js +176 -1
  175. package/dist-standalone/plugins/validation.js +297 -1
  176. package/dist-standalone/policy.js +315 -1
  177. package/dist-standalone/progress-callbacks.js +576 -1
  178. package/dist-standalone/redis-nonce-store.js +72 -1
  179. package/dist-standalone/registry-middleware.js +47 -1
  180. package/dist-standalone/retry-strategies.js +534 -1
  181. package/dist-standalone/retry-transport.js +98 -1
  182. package/dist-standalone/runtime/browser.js +516 -1
  183. package/dist-standalone/runtime/edge.js +511 -1
  184. package/dist-standalone/runtime/react-native.js +383 -1
  185. package/dist-standalone/security-policy.js +239 -1
  186. package/dist-standalone/serialization.js +1031 -1
  187. package/dist-standalone/split-channel.js +219 -1
  188. package/dist-standalone/subscription-proof.js +224 -1
  189. package/dist-standalone/succession.js +142 -1
  190. package/dist-standalone/timeouts.js +398 -1
  191. package/dist-standalone/trace-context.js +414 -1
  192. package/dist-standalone/trace-spans.js +488 -1
  193. package/dist-standalone/transport.js +59 -1
  194. package/dist-standalone/trust-registry.js +950 -1
  195. package/dist-standalone/types/error-response.js +52 -1
  196. package/dist-standalone/vault-auth.js +174 -1
  197. package/dist-standalone/vault-store-loader.js +187 -1
  198. package/dist-standalone/verify.js +16 -1
  199. package/dist-standalone/version-info.js +530 -1
  200. package/dist-standalone/xfetch.js +335 -1
  201. package/package.json +4 -13
  202. package/share1.dat +0 -0
  203. package/dist-standalone/_deps/mldsa-wasm/LICENSE +0 -24
  204. package/dist-standalone/_deps/mldsa-wasm/package.json +0 -46
  205. package/dist-standalone/_deps/shared/cjs/package.json +0 -1
  206. package/dist-standalone/_deps/ux-helpers/cjs/package.json +0 -1
  207. package/dist-standalone/_deps/xchange/cjs/package.json +0 -1
  208. package/dist-standalone/_deps/xregistry/cjs/package.json +0 -1
  209. package/dist-standalone/cjs/package.json +0 -3
  210. package/dist-standalone/package.json +0 -10
@@ -1 +1,412 @@
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();
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();