@private.me/xbind 3.0.0 → 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.
- package/README.md +55 -7
- package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1920 -1
- package/dist-standalone/_deps/shared/cjs/errors.js +729 -1
- package/dist-standalone/_deps/shared/cjs/index.js +463 -1
- package/dist-standalone/_deps/shared/cjs/types.js +315 -1
- package/dist-standalone/_deps/shared/errors.js +244 -1
- package/dist-standalone/_deps/shared/index.js +72 -1
- package/dist-standalone/_deps/shared/types.js +86 -1
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.js +1 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.js +1 -1
- package/dist-standalone/_deps/ux-helpers/errors.js +1 -1
- package/dist-standalone/_deps/ux-helpers/index.js +1 -1
- package/dist-standalone/_deps/ux-helpers/pagination.js +1 -1
- package/dist-standalone/_deps/ux-helpers/progress.js +1 -1
- package/dist-standalone/_deps/ux-helpers/search.js +1 -1
- package/dist-standalone/_deps/xchange/auto-accept.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/auto-accept.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/index.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/invite-client.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/lazy-init.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/trust-integration.js +1 -1
- package/dist-standalone/_deps/xchange/cjs/xchange.js +1 -1
- package/dist-standalone/_deps/xchange/errors.js +1 -1
- package/dist-standalone/_deps/xchange/index.js +1 -1
- package/dist-standalone/_deps/xchange/invite-client.js +1 -1
- package/dist-standalone/_deps/xchange/lazy-init.js +1 -1
- package/dist-standalone/_deps/xchange/trust-integration.js +1 -1
- package/dist-standalone/_deps/xchange/xchange.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/index.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/cjs/types.js +1 -1
- package/dist-standalone/_deps/xregistry/discovery.js +1 -1
- package/dist-standalone/_deps/xregistry/errors.js +1 -1
- package/dist-standalone/_deps/xregistry/index.js +1 -1
- package/dist-standalone/_deps/xregistry/registry.js +1 -1
- package/dist-standalone/_deps/xregistry/schema.js +1 -1
- package/dist-standalone/_deps/xregistry/types.js +1 -1
- package/dist-standalone/agent-call.js +659 -1
- package/dist-standalone/agent-sdk.js +328 -1
- package/dist-standalone/agent.js +1800 -1
- package/dist-standalone/approval.js +193 -1
- package/dist-standalone/async-iterators.js +382 -1
- package/dist-standalone/auth.js +219 -1
- package/dist-standalone/auto-accept.js +229 -1
- package/dist-standalone/backup-config.js +201 -1
- package/dist-standalone/backup.js +326 -1
- package/dist-standalone/batch-operations.js +388 -1
- package/dist-standalone/cancellation.js +477 -1
- package/dist-standalone/checkpoint.js +186 -1
- package/dist-standalone/circuit-breaker.js +468 -1
- package/dist-standalone/cjs/agent-call.js +701 -1
- package/dist-standalone/cjs/agent-sdk.js +332 -1
- package/dist-standalone/cjs/agent.js +1837 -1
- package/dist-standalone/cjs/approval.js +199 -1
- package/dist-standalone/cjs/async-iterators.js +392 -1
- package/dist-standalone/cjs/auth.js +225 -1
- package/dist-standalone/cjs/auto-accept.js +233 -1
- package/dist-standalone/cjs/backup-config.js +207 -1
- package/dist-standalone/cjs/backup.js +330 -1
- package/dist-standalone/cjs/batch-operations.js +397 -1
- package/dist-standalone/cjs/cancellation.js +490 -1
- package/dist-standalone/cjs/checkpoint.js +193 -1
- package/dist-standalone/cjs/circuit-breaker.js +476 -1
- package/dist-standalone/cjs/cli/init.js +492 -1
- package/dist-standalone/cjs/config-validation.js +522 -1
- package/dist-standalone/cjs/connect.js +312 -1
- package/dist-standalone/cjs/connection-pool.js +506 -1
- package/dist-standalone/cjs/correlation-id.js +339 -1
- package/dist-standalone/cjs/crypto-utils.js +176 -1
- package/dist-standalone/cjs/debug-mode.js +534 -1
- package/dist-standalone/cjs/did-document.js +101 -1
- package/dist-standalone/cjs/did-privateme.js +130 -1
- package/dist-standalone/cjs/did-web.js +201 -1
- package/dist-standalone/cjs/discovery.js +462 -1
- package/dist-standalone/cjs/dual-mode.js +251 -1
- package/dist-standalone/cjs/email-templates.js +313 -1
- package/dist-standalone/cjs/email-transport.js +239 -1
- package/dist-standalone/cjs/envelope.js +538 -1
- package/dist-standalone/cjs/errors.js +913 -1
- package/dist-standalone/cjs/event-emitter.js +461 -1
- package/dist-standalone/cjs/gateway-state.js +55 -1
- package/dist-standalone/cjs/gateway-transport.js +120 -1
- package/dist-standalone/cjs/graceful-degradation.js +403 -1
- package/dist-standalone/cjs/guardrails.js +223 -1
- package/dist-standalone/cjs/health-check.js +336 -1
- package/dist-standalone/cjs/http-compat.js +272 -1
- package/dist-standalone/cjs/http-status-map.js +571 -1
- package/dist-standalone/cjs/identity.js +645 -1
- package/dist-standalone/cjs/index.js +406 -1
- package/dist-standalone/cjs/invitation.js +421 -1
- package/dist-standalone/cjs/invite.js +328 -1
- package/dist-standalone/cjs/key-agreement.js +335 -1
- package/dist-standalone/cjs/lazy-init.js +300 -1
- package/dist-standalone/cjs/logger.js +291 -1
- package/dist-standalone/cjs/mdns-discovery.js +202 -1
- package/dist-standalone/cjs/nonce-store.js +80 -1
- package/dist-standalone/cjs/pairing-manager.js +223 -1
- package/dist-standalone/cjs/plugin-system.js +264 -1
- package/dist-standalone/cjs/plugins/logging.js +168 -1
- package/dist-standalone/cjs/plugins/metrics.js +181 -1
- package/dist-standalone/cjs/plugins/validation.js +302 -1
- package/dist-standalone/cjs/policy.js +320 -1
- package/dist-standalone/cjs/progress-callbacks.js +583 -1
- package/dist-standalone/cjs/redis-nonce-store.js +76 -1
- package/dist-standalone/cjs/registry-middleware.js +50 -1
- package/dist-standalone/cjs/retry-strategies.js +544 -1
- package/dist-standalone/cjs/retry-transport.js +102 -1
- package/dist-standalone/cjs/runtime/browser.js +533 -1
- package/dist-standalone/cjs/runtime/edge.js +526 -1
- package/dist-standalone/cjs/runtime/react-native.js +394 -1
- package/dist-standalone/cjs/security-policy.js +245 -1
- package/dist-standalone/cjs/serialization.js +1040 -1
- package/dist-standalone/cjs/split-channel.js +225 -1
- package/dist-standalone/cjs/subscription-proof.js +230 -1
- package/dist-standalone/cjs/succession.js +148 -1
- package/dist-standalone/cjs/timeouts.js +412 -1
- package/dist-standalone/cjs/trace-context.js +424 -1
- package/dist-standalone/cjs/trace-spans.js +495 -1
- package/dist-standalone/cjs/transport.js +63 -1
- package/dist-standalone/cjs/trust-registry.js +991 -1
- package/dist-standalone/cjs/types/error-response.js +56 -1
- package/dist-standalone/cjs/vault-auth.js +178 -1
- package/dist-standalone/cjs/vault-store-loader.js +194 -1
- package/dist-standalone/cjs/verify.js +25 -1
- package/dist-standalone/cjs/version-info.js +543 -1
- package/dist-standalone/cjs/xfetch.js +340 -1
- package/dist-standalone/cli/init.js +455 -1
- package/dist-standalone/cli/setup.js +514 -1
- package/dist-standalone/cli/types.js +27 -1
- package/dist-standalone/cli/xbind.js +148 -1
- package/dist-standalone/config-validation.js +513 -1
- package/dist-standalone/connect.js +274 -1
- package/dist-standalone/connection-pool.js +500 -1
- package/dist-standalone/correlation-id.js +326 -1
- package/dist-standalone/crypto-utils.js +157 -1
- package/dist-standalone/debug-mode.js +510 -1
- package/dist-standalone/did-document.js +96 -1
- package/dist-standalone/did-privateme.js +121 -1
- package/dist-standalone/did-web.js +196 -1
- package/dist-standalone/discovery.js +458 -1
- package/dist-standalone/dual-mode.js +247 -1
- package/dist-standalone/email-templates.js +309 -1
- package/dist-standalone/email-transport.js +232 -1
- package/dist-standalone/envelope.js +525 -1
- package/dist-standalone/errors.js +896 -1
- package/dist-standalone/event-emitter.js +456 -1
- package/dist-standalone/gateway-state.js +51 -1
- package/dist-standalone/gateway-transport.js +116 -1
- package/dist-standalone/graceful-degradation.js +396 -1
- package/dist-standalone/guardrails.js +216 -1
- package/dist-standalone/health-check.js +332 -1
- package/dist-standalone/http-compat.js +267 -1
- package/dist-standalone/http-status-map.js +561 -1
- package/dist-standalone/identity.js +619 -1
- package/dist-standalone/index.js +78 -1
- package/dist-standalone/invitation.js +415 -1
- package/dist-standalone/invite.js +324 -1
- package/dist-standalone/key-agreement.js +325 -1
- package/dist-standalone/lazy-init.js +295 -1
- package/dist-standalone/logger.js +285 -1
- package/dist-standalone/mdns-discovery.js +195 -1
- package/dist-standalone/nonce-store.js +76 -1
- package/dist-standalone/pairing-manager.js +219 -1
- package/dist-standalone/plugin-system.js +257 -1
- package/dist-standalone/plugins/logging.d.ts +84 -0
- package/dist-standalone/plugins/logging.js +163 -0
- package/dist-standalone/plugins/metrics.d.ts +111 -0
- package/dist-standalone/plugins/metrics.js +176 -0
- package/dist-standalone/plugins/validation.d.ts +104 -0
- package/dist-standalone/plugins/validation.js +297 -0
- package/dist-standalone/policy.js +315 -1
- package/dist-standalone/progress-callbacks.js +576 -1
- package/dist-standalone/redis-nonce-store.js +72 -1
- package/dist-standalone/registry-middleware.js +47 -1
- package/dist-standalone/retry-strategies.js +534 -1
- package/dist-standalone/retry-transport.js +98 -1
- package/dist-standalone/runtime/browser.d.ts +311 -0
- package/dist-standalone/runtime/browser.js +516 -0
- package/dist-standalone/runtime/edge.d.ts +282 -0
- package/dist-standalone/runtime/edge.js +511 -0
- package/dist-standalone/runtime/react-native.d.ts +157 -0
- package/dist-standalone/runtime/react-native.js +383 -0
- package/dist-standalone/security-policy.js +239 -1
- package/dist-standalone/serialization.js +1031 -1
- package/dist-standalone/split-channel.js +219 -1
- package/dist-standalone/subscription-proof.js +224 -1
- package/dist-standalone/succession.js +142 -1
- package/dist-standalone/timeouts.js +398 -1
- package/dist-standalone/trace-context.js +414 -1
- package/dist-standalone/trace-spans.js +488 -1
- package/dist-standalone/transport.js +59 -1
- package/dist-standalone/trust-registry.js +950 -1
- package/dist-standalone/types/error-response.d.ts +209 -0
- package/dist-standalone/types/error-response.js +52 -0
- package/dist-standalone/vault-auth.js +174 -1
- package/dist-standalone/vault-store-loader.js +187 -1
- package/dist-standalone/verify.js +16 -1
- package/dist-standalone/version-info.js +530 -1
- package/dist-standalone/xfetch.js +335 -1
- package/package.json +4 -10
- package/share1.dat +0 -0
- package/dist-standalone/_deps/mldsa-wasm/LICENSE +0 -24
- package/dist-standalone/_deps/mldsa-wasm/package.json +0 -46
- package/dist-standalone/_deps/shared/cjs/package.json +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/package.json +0 -1
- package/dist-standalone/_deps/xchange/cjs/package.json +0 -1
- package/dist-standalone/_deps/xregistry/cjs/package.json +0 -1
- package/dist-standalone/cjs/package.json +0 -3
- package/dist-standalone/package.json +0 -10
|
@@ -1 +1,412 @@
|
|
|
1
|
-
"use strict";
|
|
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();
|