@private.me/xbind 1.3.0 → 2.3.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.
- package/LICENSES.md +212 -0
- package/README.md +388 -6
- package/dist-standalone/_deps/mldsa-wasm/dist/mldsa.js +1 -1920
- package/dist-standalone/_deps/shared/cjs/errors.js +1 -275
- package/dist-standalone/_deps/shared/cjs/index.js +1 -138
- package/dist-standalone/_deps/shared/cjs/types.js +1 -90
- package/dist-standalone/_deps/shared/errors.js +1 -262
- package/dist-standalone/_deps/shared/index.js +1 -77
- package/dist-standalone/_deps/shared/types.js +1 -91
- 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 +1 -642
- package/dist-standalone/agent-sdk.js +1 -328
- package/dist-standalone/agent.d.ts +95 -5
- package/dist-standalone/agent.js +1 -1545
- package/dist-standalone/approval.js +1 -193
- package/dist-standalone/async-iterators.d.ts +275 -0
- package/dist-standalone/async-iterators.js +1 -0
- package/dist-standalone/auth.js +1 -219
- package/dist-standalone/auto-accept.js +1 -229
- package/dist-standalone/backup-config.js +1 -201
- package/dist-standalone/backup.d.ts +114 -0
- package/dist-standalone/backup.js +1 -0
- package/dist-standalone/batch-operations.d.ts +297 -0
- package/dist-standalone/batch-operations.js +1 -0
- package/dist-standalone/cancellation.d.ts +301 -0
- package/dist-standalone/cancellation.js +1 -0
- package/dist-standalone/checkpoint.js +1 -186
- package/dist-standalone/circuit-breaker.d.ts +351 -0
- package/dist-standalone/circuit-breaker.js +1 -0
- package/dist-standalone/cjs/agent-call.js +1 -651
- package/dist-standalone/cjs/agent-sdk.js +1 -332
- package/dist-standalone/cjs/agent.js +1 -1582
- package/dist-standalone/cjs/approval.js +1 -199
- package/dist-standalone/cjs/async-iterators.js +1 -0
- package/dist-standalone/cjs/auth.js +1 -225
- package/dist-standalone/cjs/auto-accept.js +1 -233
- package/dist-standalone/cjs/backup-config.js +1 -207
- package/dist-standalone/cjs/backup.js +1 -0
- package/dist-standalone/cjs/batch-operations.js +1 -0
- package/dist-standalone/cjs/cancellation.js +1 -0
- package/dist-standalone/cjs/checkpoint.js +1 -193
- package/dist-standalone/cjs/circuit-breaker.js +1 -0
- package/dist-standalone/cjs/cli/init.js +1 -486
- package/dist-standalone/cjs/config-validation.js +1 -0
- package/dist-standalone/cjs/connect.js +1 -312
- package/dist-standalone/cjs/connection-pool.js +1 -0
- package/dist-standalone/cjs/correlation-id.js +1 -339
- package/dist-standalone/cjs/crypto-utils.js +1 -0
- package/dist-standalone/cjs/debug-mode.js +1 -0
- package/dist-standalone/cjs/did-document.js +1 -101
- package/dist-standalone/cjs/did-privateme.js +1 -130
- package/dist-standalone/cjs/did-web.js +1 -201
- package/dist-standalone/cjs/discovery.js +1 -462
- package/dist-standalone/cjs/dual-mode.js +1 -251
- package/dist-standalone/cjs/email-templates.js +1 -313
- package/dist-standalone/cjs/email-transport.js +1 -239
- package/dist-standalone/cjs/envelope.js +1 -510
- package/dist-standalone/cjs/errors.js +1 -826
- package/dist-standalone/cjs/event-emitter.js +1 -0
- package/dist-standalone/cjs/gateway-state.js +1 -55
- package/dist-standalone/cjs/gateway-transport.js +1 -120
- package/dist-standalone/cjs/graceful-degradation.js +1 -0
- package/dist-standalone/cjs/guardrails.js +1 -223
- package/dist-standalone/cjs/health-check.js +1 -0
- package/dist-standalone/cjs/http-compat.js +1 -272
- package/dist-standalone/cjs/http-status-map.js +1 -571
- package/dist-standalone/cjs/identity.js +1 -541
- package/dist-standalone/cjs/index.js +1 -237
- package/dist-standalone/cjs/invitation.js +1 -421
- package/dist-standalone/cjs/invite.js +1 -328
- package/dist-standalone/cjs/key-agreement.js +1 -246
- package/dist-standalone/cjs/lazy-init.js +1 -300
- package/dist-standalone/cjs/logger.js +1 -0
- package/dist-standalone/cjs/mdns-discovery.js +1 -202
- package/dist-standalone/cjs/nonce-store.js +1 -66
- package/dist-standalone/cjs/pairing-manager.js +1 -223
- package/dist-standalone/cjs/plugin-system.js +1 -0
- package/dist-standalone/cjs/plugins/logging.js +1 -0
- package/dist-standalone/cjs/plugins/metrics.js +1 -0
- package/dist-standalone/cjs/plugins/validation.js +1 -0
- package/dist-standalone/cjs/policy.js +1 -320
- package/dist-standalone/cjs/progress-callbacks.js +1 -0
- package/dist-standalone/cjs/redis-nonce-store.js +1 -76
- package/dist-standalone/cjs/registry-middleware.js +1 -50
- package/dist-standalone/cjs/retry-strategies.js +1 -0
- package/dist-standalone/cjs/retry-transport.js +1 -102
- package/dist-standalone/cjs/runtime/browser.js +1 -0
- package/dist-standalone/cjs/runtime/edge.js +1 -0
- package/dist-standalone/cjs/runtime/react-native.js +1 -0
- package/dist-standalone/cjs/security-policy.js +1 -245
- package/dist-standalone/cjs/serialization.js +1 -0
- package/dist-standalone/cjs/split-channel.js +1 -177
- package/dist-standalone/cjs/subscription-proof.js +1 -230
- package/dist-standalone/cjs/succession.js +1 -148
- package/dist-standalone/cjs/timeouts.js +1 -0
- package/dist-standalone/cjs/trace-context.js +1 -0
- package/dist-standalone/cjs/trace-spans.js +1 -0
- package/dist-standalone/cjs/transport.js +1 -63
- package/dist-standalone/cjs/trust-registry.js +1 -742
- package/dist-standalone/cjs/types/error-response.js +1 -56
- package/dist-standalone/cjs/vault-auth.js +1 -0
- package/dist-standalone/cjs/vault-store-loader.js +1 -0
- package/dist-standalone/cjs/verify.js +1 -25
- package/dist-standalone/cjs/version-info.js +1 -0
- package/dist-standalone/cjs/xfetch.js +1 -252
- package/dist-standalone/cli/init.js +1 -449
- package/dist-standalone/cli/setup.js +1 -514
- package/dist-standalone/cli/types.js +1 -27
- package/dist-standalone/cli/xbind.js +1 -148
- package/dist-standalone/config-validation.d.ts +185 -0
- package/dist-standalone/config-validation.js +1 -0
- package/dist-standalone/connect.js +1 -274
- package/dist-standalone/connection-pool.d.ts +251 -0
- package/dist-standalone/connection-pool.js +1 -0
- package/dist-standalone/correlation-id.js +1 -326
- package/dist-standalone/crypto-utils.d.ts +60 -0
- package/dist-standalone/crypto-utils.js +1 -0
- package/dist-standalone/debug-mode.d.ts +286 -0
- package/dist-standalone/debug-mode.js +1 -0
- package/dist-standalone/did-document.js +1 -96
- package/dist-standalone/did-privateme.js +1 -121
- package/dist-standalone/did-web.js +1 -196
- package/dist-standalone/discovery.js +1 -458
- package/dist-standalone/dual-mode.js +1 -247
- package/dist-standalone/email-templates.js +1 -309
- package/dist-standalone/email-transport.js +1 -232
- package/dist-standalone/envelope.d.ts +29 -1
- package/dist-standalone/envelope.js +1 -497
- package/dist-standalone/errors.d.ts +10 -0
- package/dist-standalone/errors.js +1 -811
- package/dist-standalone/event-emitter.d.ts +395 -0
- package/dist-standalone/event-emitter.js +1 -0
- package/dist-standalone/gateway-state.js +1 -51
- package/dist-standalone/gateway-transport.js +1 -116
- package/dist-standalone/graceful-degradation.d.ts +246 -0
- package/dist-standalone/graceful-degradation.js +1 -0
- package/dist-standalone/guardrails.js +1 -216
- package/dist-standalone/health-check.d.ts +150 -0
- package/dist-standalone/health-check.js +1 -0
- package/dist-standalone/http-compat.js +1 -267
- package/dist-standalone/http-status-map.js +1 -561
- package/dist-standalone/identity.d.ts +64 -1
- package/dist-standalone/identity.js +1 -516
- package/dist-standalone/index.d.ts +45 -3
- package/dist-standalone/index.js +1 -52
- package/dist-standalone/invitation.js +1 -415
- package/dist-standalone/invite.js +1 -324
- package/dist-standalone/key-agreement.d.ts +61 -13
- package/dist-standalone/key-agreement.js +1 -236
- package/dist-standalone/lazy-init.js +1 -295
- package/dist-standalone/logger.d.ts +77 -0
- package/dist-standalone/logger.js +1 -0
- package/dist-standalone/mdns-discovery.js +1 -195
- package/dist-standalone/nonce-store.d.ts +16 -3
- package/dist-standalone/nonce-store.js +1 -62
- package/dist-standalone/package.json +0 -1
- package/dist-standalone/pairing-manager.js +1 -219
- package/dist-standalone/plugin-system.d.ts +145 -0
- package/dist-standalone/plugin-system.js +1 -0
- package/dist-standalone/policy.js +1 -315
- package/dist-standalone/progress-callbacks.d.ts +394 -0
- package/dist-standalone/progress-callbacks.js +1 -0
- package/dist-standalone/redis-nonce-store.js +1 -72
- package/dist-standalone/registry-middleware.js +1 -47
- package/dist-standalone/retry-strategies.d.ts +382 -0
- package/dist-standalone/retry-strategies.js +1 -0
- package/dist-standalone/retry-transport.js +1 -98
- package/dist-standalone/security-policy.js +1 -239
- package/dist-standalone/serialization.d.ts +244 -0
- package/dist-standalone/serialization.js +1 -0
- package/dist-standalone/split-channel.d.ts +49 -1
- package/dist-standalone/split-channel.js +1 -171
- package/dist-standalone/subscription-proof.js +1 -224
- package/dist-standalone/succession.js +1 -142
- package/dist-standalone/timeouts.d.ts +275 -0
- package/dist-standalone/timeouts.js +1 -0
- package/dist-standalone/trace-context.d.ts +252 -0
- package/dist-standalone/trace-context.js +1 -0
- package/dist-standalone/trace-spans.d.ts +360 -0
- package/dist-standalone/trace-spans.js +1 -0
- package/dist-standalone/transport.js +1 -59
- package/dist-standalone/trust-registry.d.ts +106 -5
- package/dist-standalone/trust-registry.js +1 -702
- package/dist-standalone/vault-auth.d.ts +91 -0
- package/dist-standalone/vault-auth.js +1 -0
- package/dist-standalone/vault-store-loader.d.ts +110 -0
- package/dist-standalone/vault-store-loader.js +1 -0
- package/dist-standalone/verify.js +1 -16
- package/dist-standalone/version-info.d.ts +259 -0
- package/dist-standalone/version-info.js +1 -0
- package/dist-standalone/xfetch.js +1 -247
- package/llms.txt +1 -0
- package/package.json +66 -5
- package/share1.dat +0 -0
- package/dist-standalone/_deps/crypto/base64.d.ts +0 -29
- package/dist-standalone/_deps/crypto/base64.js +0 -209
- package/dist-standalone/_deps/crypto/cjs/base64.js +0 -103
- package/dist-standalone/_deps/crypto/cjs/errors.js +0 -119
- package/dist-standalone/_deps/crypto/cjs/hmac.js +0 -71
- package/dist-standalone/_deps/crypto/cjs/index.js +0 -86
- package/dist-standalone/_deps/crypto/cjs/padding.js +0 -57
- package/dist-standalone/_deps/crypto/cjs/share-header.js +0 -68
- package/dist-standalone/_deps/crypto/cjs/shares.js +0 -152
- package/dist-standalone/_deps/crypto/cjs/tlv.js +0 -199
- package/dist-standalone/_deps/crypto/cjs/uuid.js +0 -61
- package/dist-standalone/_deps/crypto/cjs/verify.js +0 -24
- package/dist-standalone/_deps/crypto/cjs/xorida.js +0 -221
- package/dist-standalone/_deps/crypto/errors.d.ts +0 -51
- package/dist-standalone/_deps/crypto/errors.js +0 -109
- package/dist-standalone/_deps/crypto/hmac.d.ts +0 -39
- package/dist-standalone/_deps/crypto/hmac.js +0 -66
- package/dist-standalone/_deps/crypto/index.d.ts +0 -20
- package/dist-standalone/_deps/crypto/index.js +0 -45
- package/dist-standalone/_deps/crypto/padding.d.ts +0 -19
- package/dist-standalone/_deps/crypto/padding.js +0 -53
- package/dist-standalone/_deps/crypto/share-header.d.ts +0 -44
- package/dist-standalone/_deps/crypto/share-header.js +0 -63
- package/dist-standalone/_deps/crypto/shares.d.ts +0 -27
- package/dist-standalone/_deps/crypto/shares.js +0 -148
- package/dist-standalone/_deps/crypto/tlv.d.ts +0 -26
- package/dist-standalone/_deps/crypto/tlv.js +0 -195
- package/dist-standalone/_deps/crypto/uuid.d.ts +0 -22
- package/dist-standalone/_deps/crypto/uuid.js +0 -56
- package/dist-standalone/_deps/crypto/verify.d.ts +0 -15
- package/dist-standalone/_deps/crypto/verify.js +0 -15
- package/dist-standalone/_deps/crypto/xorida.d.ts +0 -44
- package/dist-standalone/_deps/crypto/xorida.js +0 -215
- package/dist-standalone/_deps/shared/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/errors.js.map +0 -1
- package/dist-standalone/_deps/shared/index.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/index.js.map +0 -1
- package/dist-standalone/_deps/shared/types.d.ts.map +0 -1
- package/dist-standalone/_deps/shared/types.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/errors.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/index.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/pagination.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/progress.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/search.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/cjs/types.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/errors.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/index.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/index.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/pagination.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/pagination.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/progress.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/progress.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/search.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/search.js.map +0 -1
- package/dist-standalone/_deps/ux-helpers/types.d.ts.map +0 -1
- package/dist-standalone/_deps/ux-helpers/types.js.map +0 -1
- package/dist-standalone/_deps/xregistry/discovery.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/discovery.js.map +0 -1
- package/dist-standalone/_deps/xregistry/errors.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/errors.js.map +0 -1
- package/dist-standalone/_deps/xregistry/index.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/index.js.map +0 -1
- package/dist-standalone/_deps/xregistry/registry.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/registry.js.map +0 -1
- package/dist-standalone/_deps/xregistry/schema.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/schema.js.map +0 -1
- package/dist-standalone/_deps/xregistry/types.d.ts.map +0 -1
- package/dist-standalone/_deps/xregistry/types.js.map +0 -1
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module circuit-breaker
|
|
3
|
+
* Circuit breaker pattern for enhanced fault tolerance
|
|
4
|
+
*
|
|
5
|
+
* Protects external service calls (registry, gateway, S3) from cascading failures
|
|
6
|
+
* by automatically opening circuits after repeated failures and closing them after
|
|
7
|
+
* recovery periods.
|
|
8
|
+
*
|
|
9
|
+
* Architecture:
|
|
10
|
+
* - CLOSED: Normal operation, all requests pass through
|
|
11
|
+
* - OPEN: Circuit tripped, fast-fail all requests without calling service
|
|
12
|
+
* - HALF_OPEN: Testing recovery, allow limited requests to probe service health
|
|
13
|
+
*
|
|
14
|
+
* Features:
|
|
15
|
+
* - Automatic state transitions based on failure thresholds
|
|
16
|
+
* - Exponential backoff for recovery attempts
|
|
17
|
+
* - Per-service circuit isolation
|
|
18
|
+
* - Metrics integration for monitoring
|
|
19
|
+
* - Type-safe error handling
|
|
20
|
+
*
|
|
21
|
+
* @example
|
|
22
|
+
* ```typescript
|
|
23
|
+
* const breaker = new CircuitBreaker({
|
|
24
|
+
* failureThreshold: 5,
|
|
25
|
+
* recoveryTimeout: 60000,
|
|
26
|
+
* halfOpenMaxCalls: 3,
|
|
27
|
+
* });
|
|
28
|
+
*
|
|
29
|
+
* const result = await breaker.execute(async () => {
|
|
30
|
+
* return await registryClient.lookup(did);
|
|
31
|
+
* });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
import type { Result } from '@private.me/shared';
|
|
35
|
+
/**
|
|
36
|
+
* Circuit breaker state.
|
|
37
|
+
*
|
|
38
|
+
* - CLOSED: Normal operation, requests pass through
|
|
39
|
+
* - OPEN: Circuit tripped, fail fast without calling service
|
|
40
|
+
* - HALF_OPEN: Recovery testing, allow limited probes
|
|
41
|
+
*/
|
|
42
|
+
export type CircuitState = 'CLOSED' | 'OPEN' | 'HALF_OPEN';
|
|
43
|
+
/**
|
|
44
|
+
* Circuit breaker configuration options.
|
|
45
|
+
*/
|
|
46
|
+
export interface CircuitBreakerOptions {
|
|
47
|
+
/**
|
|
48
|
+
* Number of consecutive failures before opening circuit.
|
|
49
|
+
* @default 5
|
|
50
|
+
*/
|
|
51
|
+
readonly failureThreshold?: number;
|
|
52
|
+
/**
|
|
53
|
+
* Time in milliseconds to wait before transitioning from OPEN to HALF_OPEN.
|
|
54
|
+
* @default 60000 (1 minute)
|
|
55
|
+
*/
|
|
56
|
+
readonly recoveryTimeout?: number;
|
|
57
|
+
/**
|
|
58
|
+
* Maximum number of requests allowed in HALF_OPEN state before closing circuit.
|
|
59
|
+
* @default 3
|
|
60
|
+
*/
|
|
61
|
+
readonly halfOpenMaxCalls?: number;
|
|
62
|
+
/**
|
|
63
|
+
* Number of successful calls in HALF_OPEN state required to close circuit.
|
|
64
|
+
* @default 2
|
|
65
|
+
*/
|
|
66
|
+
readonly successThreshold?: number;
|
|
67
|
+
/**
|
|
68
|
+
* Optional name for this circuit (used in metrics and logging).
|
|
69
|
+
* @default 'default'
|
|
70
|
+
*/
|
|
71
|
+
readonly name?: string;
|
|
72
|
+
/**
|
|
73
|
+
* Optional callback invoked on state transitions.
|
|
74
|
+
*/
|
|
75
|
+
readonly onStateChange?: (from: CircuitState, to: CircuitState, reason: string) => void;
|
|
76
|
+
/**
|
|
77
|
+
* Optional callback invoked when circuit opens.
|
|
78
|
+
*/
|
|
79
|
+
readonly onOpen?: (reason: string) => void;
|
|
80
|
+
/**
|
|
81
|
+
* Optional callback invoked when circuit closes.
|
|
82
|
+
*/
|
|
83
|
+
readonly onClose?: () => void;
|
|
84
|
+
/**
|
|
85
|
+
* Optional callback invoked when circuit enters half-open state.
|
|
86
|
+
*/
|
|
87
|
+
readonly onHalfOpen?: () => void;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Circuit breaker metrics.
|
|
91
|
+
*/
|
|
92
|
+
export interface CircuitBreakerMetrics {
|
|
93
|
+
/** Current circuit state */
|
|
94
|
+
readonly state: CircuitState;
|
|
95
|
+
/** Total number of successful calls */
|
|
96
|
+
readonly successCount: number;
|
|
97
|
+
/** Total number of failed calls */
|
|
98
|
+
readonly failureCount: number;
|
|
99
|
+
/** Total number of rejected calls (circuit open) */
|
|
100
|
+
readonly rejectedCount: number;
|
|
101
|
+
/** Consecutive failures in current state */
|
|
102
|
+
readonly consecutiveFailures: number;
|
|
103
|
+
/** Consecutive successes in HALF_OPEN state */
|
|
104
|
+
readonly consecutiveSuccesses: number;
|
|
105
|
+
/** Timestamp when circuit last opened (undefined if never opened) */
|
|
106
|
+
readonly lastOpenedAt?: number;
|
|
107
|
+
/** Timestamp when circuit last closed (undefined if never closed) */
|
|
108
|
+
readonly lastClosedAt?: number;
|
|
109
|
+
/** Circuit name */
|
|
110
|
+
readonly name: string;
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Error codes for circuit breaker failures.
|
|
114
|
+
*/
|
|
115
|
+
export type CircuitBreakerError = 'CIRCUIT_OPEN' | 'HALF_OPEN_LIMIT_EXCEEDED' | 'EXECUTION_FAILED' | 'TIMEOUT';
|
|
116
|
+
/**
|
|
117
|
+
* Circuit breaker for protecting external service calls.
|
|
118
|
+
*
|
|
119
|
+
* Implements the circuit breaker pattern to prevent cascading failures
|
|
120
|
+
* when external services (registry, gateway, S3) become unavailable.
|
|
121
|
+
*
|
|
122
|
+
* State transitions:
|
|
123
|
+
* - CLOSED → OPEN: After failureThreshold consecutive failures
|
|
124
|
+
* - OPEN → HALF_OPEN: After recoveryTimeout milliseconds
|
|
125
|
+
* - HALF_OPEN → CLOSED: After successThreshold successful calls
|
|
126
|
+
* - HALF_OPEN → OPEN: On any failure
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* ```typescript
|
|
130
|
+
* // Create circuit breaker for registry calls
|
|
131
|
+
* const registryBreaker = new CircuitBreaker({
|
|
132
|
+
* name: 'registry',
|
|
133
|
+
* failureThreshold: 5,
|
|
134
|
+
* recoveryTimeout: 60000,
|
|
135
|
+
* });
|
|
136
|
+
*
|
|
137
|
+
* // Execute protected call
|
|
138
|
+
* const result = await registryBreaker.execute(async () => {
|
|
139
|
+
* return await registry.lookup(did);
|
|
140
|
+
* });
|
|
141
|
+
*
|
|
142
|
+
* if (!result.ok) {
|
|
143
|
+
* console.error('Circuit breaker error:', result.error);
|
|
144
|
+
* }
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
export declare class CircuitBreaker {
|
|
148
|
+
private state;
|
|
149
|
+
private consecutiveFailures;
|
|
150
|
+
private consecutiveSuccesses;
|
|
151
|
+
private successCount;
|
|
152
|
+
private failureCount;
|
|
153
|
+
private rejectedCount;
|
|
154
|
+
private lastOpenedAt?;
|
|
155
|
+
private lastClosedAt?;
|
|
156
|
+
private recoveryTimer?;
|
|
157
|
+
private readonly failureThreshold;
|
|
158
|
+
private readonly recoveryTimeout;
|
|
159
|
+
private readonly halfOpenMaxCalls;
|
|
160
|
+
private readonly successThreshold;
|
|
161
|
+
private readonly name;
|
|
162
|
+
private readonly onStateChange?;
|
|
163
|
+
private readonly onOpen?;
|
|
164
|
+
private readonly onClose?;
|
|
165
|
+
private readonly onHalfOpen?;
|
|
166
|
+
/**
|
|
167
|
+
* Create a new circuit breaker.
|
|
168
|
+
*
|
|
169
|
+
* @param options - Circuit breaker configuration
|
|
170
|
+
*/
|
|
171
|
+
constructor(options?: CircuitBreakerOptions);
|
|
172
|
+
/**
|
|
173
|
+
* Execute a function with circuit breaker protection.
|
|
174
|
+
*
|
|
175
|
+
* If the circuit is OPEN, immediately returns an error without calling the function.
|
|
176
|
+
* If the circuit is HALF_OPEN, limits the number of concurrent calls.
|
|
177
|
+
* If the circuit is CLOSED, executes the function normally.
|
|
178
|
+
*
|
|
179
|
+
* @param fn - Async function to execute
|
|
180
|
+
* @returns Result of function execution or circuit breaker error
|
|
181
|
+
*/
|
|
182
|
+
execute<T>(fn: () => Promise<T>): Promise<Result<T, CircuitBreakerError | string>>;
|
|
183
|
+
/**
|
|
184
|
+
* Get current circuit breaker metrics.
|
|
185
|
+
*
|
|
186
|
+
* @returns Current metrics snapshot
|
|
187
|
+
*/
|
|
188
|
+
getMetrics(): CircuitBreakerMetrics;
|
|
189
|
+
/**
|
|
190
|
+
* Get current circuit state.
|
|
191
|
+
*
|
|
192
|
+
* @returns Current state (CLOSED, OPEN, or HALF_OPEN)
|
|
193
|
+
*/
|
|
194
|
+
getState(): CircuitState;
|
|
195
|
+
/**
|
|
196
|
+
* Manually reset the circuit breaker to CLOSED state.
|
|
197
|
+
*
|
|
198
|
+
* Useful for testing or manual recovery scenarios.
|
|
199
|
+
* Clears all counters and timers.
|
|
200
|
+
*/
|
|
201
|
+
reset(): void;
|
|
202
|
+
/**
|
|
203
|
+
* Manually open the circuit breaker.
|
|
204
|
+
*
|
|
205
|
+
* Useful for maintenance windows or manual intervention.
|
|
206
|
+
*
|
|
207
|
+
* @param reason - Optional reason for opening circuit
|
|
208
|
+
*/
|
|
209
|
+
forceOpen(reason?: string): void;
|
|
210
|
+
/**
|
|
211
|
+
* Dispose of the circuit breaker and clean up resources.
|
|
212
|
+
*
|
|
213
|
+
* Clears any pending recovery timers.
|
|
214
|
+
*/
|
|
215
|
+
dispose(): void;
|
|
216
|
+
/**
|
|
217
|
+
* Handle successful execution.
|
|
218
|
+
*/
|
|
219
|
+
private onSuccess;
|
|
220
|
+
/**
|
|
221
|
+
* Handle failed execution.
|
|
222
|
+
*
|
|
223
|
+
* @param _error - Error that occurred (unused but kept for potential future logging)
|
|
224
|
+
*/
|
|
225
|
+
private onFailure;
|
|
226
|
+
/**
|
|
227
|
+
* Transition to a new state.
|
|
228
|
+
*
|
|
229
|
+
* @param newState - Target state
|
|
230
|
+
* @param reason - Reason for transition
|
|
231
|
+
*/
|
|
232
|
+
private transitionTo;
|
|
233
|
+
/**
|
|
234
|
+
* Schedule automatic recovery from OPEN to HALF_OPEN state.
|
|
235
|
+
*/
|
|
236
|
+
private scheduleRecovery;
|
|
237
|
+
/**
|
|
238
|
+
* Clear any pending recovery timer.
|
|
239
|
+
*/
|
|
240
|
+
private clearRecoveryTimer;
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Create a circuit breaker configured for registry operations.
|
|
244
|
+
*
|
|
245
|
+
* Registry operations have:
|
|
246
|
+
* - Higher failure threshold (10) - registry is critical, tolerate transient failures
|
|
247
|
+
* - Longer recovery timeout (2 minutes) - DNS/network issues take time
|
|
248
|
+
* - More half-open probes (5) - verify stability before full recovery
|
|
249
|
+
*
|
|
250
|
+
* @param options - Optional overrides
|
|
251
|
+
* @returns Configured circuit breaker for registry calls
|
|
252
|
+
*/
|
|
253
|
+
export declare function createRegistryCircuitBreaker(options?: Partial<CircuitBreakerOptions>): CircuitBreaker;
|
|
254
|
+
/**
|
|
255
|
+
* Create a circuit breaker configured for gateway operations.
|
|
256
|
+
*
|
|
257
|
+
* Gateway operations have:
|
|
258
|
+
* - Medium failure threshold (5) - gateway should be reliable
|
|
259
|
+
* - Standard recovery timeout (1 minute)
|
|
260
|
+
* - Standard half-open probes (3)
|
|
261
|
+
*
|
|
262
|
+
* @param options - Optional overrides
|
|
263
|
+
* @returns Configured circuit breaker for gateway calls
|
|
264
|
+
*/
|
|
265
|
+
export declare function createGatewayCircuitBreaker(options?: Partial<CircuitBreakerOptions>): CircuitBreaker;
|
|
266
|
+
/**
|
|
267
|
+
* Create a circuit breaker configured for S3 storage operations.
|
|
268
|
+
*
|
|
269
|
+
* S3 operations have:
|
|
270
|
+
* - Lower failure threshold (3) - storage failures are serious
|
|
271
|
+
* - Shorter recovery timeout (30 seconds) - AWS S3 recovers quickly
|
|
272
|
+
* - Fewer half-open probes (2) - S3 is usually reliable
|
|
273
|
+
*
|
|
274
|
+
* @param options - Optional overrides
|
|
275
|
+
* @returns Configured circuit breaker for S3 calls
|
|
276
|
+
*/
|
|
277
|
+
export declare function createS3CircuitBreaker(options?: Partial<CircuitBreakerOptions>): CircuitBreaker;
|
|
278
|
+
/**
|
|
279
|
+
* Manages multiple circuit breakers for different services.
|
|
280
|
+
*
|
|
281
|
+
* Provides centralized access to service-specific circuit breakers
|
|
282
|
+
* and aggregated metrics across all circuits.
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* ```typescript
|
|
286
|
+
* const manager = new CircuitBreakerManager();
|
|
287
|
+
*
|
|
288
|
+
* // Execute registry call with protection
|
|
289
|
+
* const result = await manager.executeRegistry(async () => {
|
|
290
|
+
* return await registry.lookup(did);
|
|
291
|
+
* });
|
|
292
|
+
*
|
|
293
|
+
* // Get all metrics
|
|
294
|
+
* const metrics = manager.getAllMetrics();
|
|
295
|
+
* console.log('Registry state:', metrics.registry.state);
|
|
296
|
+
* ```
|
|
297
|
+
*/
|
|
298
|
+
export declare class CircuitBreakerManager {
|
|
299
|
+
private readonly breakers;
|
|
300
|
+
constructor();
|
|
301
|
+
/**
|
|
302
|
+
* Execute a function with registry circuit breaker protection.
|
|
303
|
+
*
|
|
304
|
+
* @param fn - Async function to execute
|
|
305
|
+
* @returns Result of function execution or circuit breaker error
|
|
306
|
+
*/
|
|
307
|
+
executeRegistry<T>(fn: () => Promise<T>): Promise<Result<T, CircuitBreakerError | string>>;
|
|
308
|
+
/**
|
|
309
|
+
* Execute a function with gateway circuit breaker protection.
|
|
310
|
+
*
|
|
311
|
+
* @param fn - Async function to execute
|
|
312
|
+
* @returns Result of function execution or circuit breaker error
|
|
313
|
+
*/
|
|
314
|
+
executeGateway<T>(fn: () => Promise<T>): Promise<Result<T, CircuitBreakerError | string>>;
|
|
315
|
+
/**
|
|
316
|
+
* Execute a function with S3 circuit breaker protection.
|
|
317
|
+
*
|
|
318
|
+
* @param fn - Async function to execute
|
|
319
|
+
* @returns Result of function execution or circuit breaker error
|
|
320
|
+
*/
|
|
321
|
+
executeS3<T>(fn: () => Promise<T>): Promise<Result<T, CircuitBreakerError | string>>;
|
|
322
|
+
/**
|
|
323
|
+
* Get metrics for a specific circuit breaker.
|
|
324
|
+
*
|
|
325
|
+
* @param name - Circuit breaker name
|
|
326
|
+
* @returns Metrics or undefined if breaker not found
|
|
327
|
+
*/
|
|
328
|
+
getMetrics(name: string): CircuitBreakerMetrics | undefined;
|
|
329
|
+
/**
|
|
330
|
+
* Get metrics for all circuit breakers.
|
|
331
|
+
*
|
|
332
|
+
* @returns Map of circuit name to metrics
|
|
333
|
+
*/
|
|
334
|
+
getAllMetrics(): Record<string, CircuitBreakerMetrics>;
|
|
335
|
+
/**
|
|
336
|
+
* Get or create a custom circuit breaker.
|
|
337
|
+
*
|
|
338
|
+
* @param name - Circuit breaker name
|
|
339
|
+
* @param options - Optional configuration (used only on first access)
|
|
340
|
+
* @returns Circuit breaker instance
|
|
341
|
+
*/
|
|
342
|
+
getOrCreate(name: string, options?: CircuitBreakerOptions): CircuitBreaker;
|
|
343
|
+
/**
|
|
344
|
+
* Reset all circuit breakers to CLOSED state.
|
|
345
|
+
*/
|
|
346
|
+
resetAll(): void;
|
|
347
|
+
/**
|
|
348
|
+
* Dispose of all circuit breakers and clean up resources.
|
|
349
|
+
*/
|
|
350
|
+
dispose(): void;
|
|
351
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{ok,err}from"./_deps/shared/index.js";export class CircuitBreaker{state="CLOSED";consecutiveFailures=0;consecutiveSuccesses=0;successCount=0;failureCount=0;rejectedCount=0;lastOpenedAt;lastClosedAt;recoveryTimer;failureThreshold;recoveryTimeout;halfOpenMaxCalls;successThreshold;name;onStateChange;onOpen;onClose;onHalfOpen;constructor(e={}){this.failureThreshold=e.failureThreshold??5,this.recoveryTimeout=e.recoveryTimeout??6e4,this.halfOpenMaxCalls=e.halfOpenMaxCalls??3,this.successThreshold=e.successThreshold??2,this.name=e.name??"default",this.onStateChange=e.onStateChange,this.onOpen=e.onOpen,this.onClose=e.onClose,this.onHalfOpen=e.onHalfOpen}async execute(e){if("OPEN"===this.state)return this.rejectedCount++,err("CIRCUIT_OPEN");if("HALF_OPEN"===this.state){if(this.consecutiveSuccesses+(this.consecutiveFailures>0?1:0)>=this.halfOpenMaxCalls)return this.rejectedCount++,err("HALF_OPEN_LIMIT_EXCEEDED")}try{const t=await e();return this.onSuccess(),ok(t)}catch(e){return this.onFailure(e),err("EXECUTION_FAILED")}}getMetrics(){return{state:this.state,successCount:this.successCount,failureCount:this.failureCount,rejectedCount:this.rejectedCount,consecutiveFailures:this.consecutiveFailures,consecutiveSuccesses:this.consecutiveSuccesses,lastOpenedAt:this.lastOpenedAt,lastClosedAt:this.lastClosedAt,name:this.name}}getState(){return this.state}reset(){this.clearRecoveryTimer(),this.transitionTo("CLOSED","manual reset"),this.consecutiveFailures=0,this.consecutiveSuccesses=0}forceOpen(e="manual intervention"){this.transitionTo("OPEN",e),this.scheduleRecovery()}dispose(){this.clearRecoveryTimer()}onSuccess(){this.successCount++,this.consecutiveFailures=0,"HALF_OPEN"===this.state&&(this.consecutiveSuccesses++,this.consecutiveSuccesses>=this.successThreshold&&(this.transitionTo("CLOSED",`${this.consecutiveSuccesses} consecutive successes`),this.consecutiveSuccesses=0))}onFailure(e){if(this.failureCount++,this.consecutiveFailures++,this.consecutiveSuccesses=0,"CLOSED"===this.state&&this.consecutiveFailures>=this.failureThreshold){const e=`${this.consecutiveFailures} consecutive failures`;this.transitionTo("OPEN",e),this.scheduleRecovery()}"HALF_OPEN"===this.state&&(this.transitionTo("OPEN","failure in HALF_OPEN state"),this.scheduleRecovery())}transitionTo(e,t){const s=this.state;s!==e&&(this.state=e,"OPEN"===e?(this.lastOpenedAt=Date.now(),this.onOpen?.(t)):"CLOSED"===e?(this.lastClosedAt=Date.now(),this.onClose?.()):"HALF_OPEN"===e&&this.onHalfOpen?.(),this.onStateChange?.(s,e,t))}scheduleRecovery(){this.clearRecoveryTimer(),this.recoveryTimer=setTimeout(()=>{"OPEN"===this.state&&this.transitionTo("HALF_OPEN","recovery timeout elapsed")},this.recoveryTimeout),this.recoveryTimer.unref&&this.recoveryTimer.unref()}clearRecoveryTimer(){this.recoveryTimer&&(clearTimeout(this.recoveryTimer),this.recoveryTimer=void 0)}}export function createRegistryCircuitBreaker(e={}){return new CircuitBreaker({name:"registry",failureThreshold:10,recoveryTimeout:12e4,halfOpenMaxCalls:5,successThreshold:3,...e})}export function createGatewayCircuitBreaker(e={}){return new CircuitBreaker({name:"gateway",failureThreshold:5,recoveryTimeout:6e4,halfOpenMaxCalls:3,successThreshold:2,...e})}export function createS3CircuitBreaker(e={}){return new CircuitBreaker({name:"s3",failureThreshold:3,recoveryTimeout:3e4,halfOpenMaxCalls:2,successThreshold:2,...e})}export class CircuitBreakerManager{breakers=new Map;constructor(){this.breakers.set("registry",createRegistryCircuitBreaker()),this.breakers.set("gateway",createGatewayCircuitBreaker()),this.breakers.set("s3",createS3CircuitBreaker())}async executeRegistry(e){const t=this.breakers.get("registry");return t?t.execute(e):err("Circuit breaker not found: registry")}async executeGateway(e){const t=this.breakers.get("gateway");return t?t.execute(e):err("Circuit breaker not found: gateway")}async executeS3(e){const t=this.breakers.get("s3");return t?t.execute(e):err("Circuit breaker not found: s3")}getMetrics(e){return this.breakers.get(e)?.getMetrics()}getAllMetrics(){const e={};for(const[t,s]of this.breakers.entries())e[t]=s.getMetrics();return e}getOrCreate(e,t){let s=this.breakers.get(e);return s||(s=new CircuitBreaker({...t,name:e}),this.breakers.set(e,s)),s}resetAll(){for(const e of this.breakers.values())e.reset()}dispose(){for(const e of this.breakers.values())e.dispose();this.breakers.clear()}}
|