@cleocode/lafs 2026.4.0 → 2026.4.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/README.md +97 -68
- package/dist/src/a2a/bindings/grpc.d.ts +117 -11
- package/dist/src/a2a/bindings/grpc.d.ts.map +1 -1
- package/dist/src/a2a/bindings/grpc.js +79 -8
- package/dist/src/a2a/bindings/grpc.js.map +1 -1
- package/dist/src/a2a/bindings/http.d.ts +129 -14
- package/dist/src/a2a/bindings/http.d.ts.map +1 -1
- package/dist/src/a2a/bindings/http.js +93 -12
- package/dist/src/a2a/bindings/http.js.map +1 -1
- package/dist/src/a2a/bindings/index.d.ts +80 -7
- package/dist/src/a2a/bindings/index.d.ts.map +1 -1
- package/dist/src/a2a/bindings/index.js +69 -2
- package/dist/src/a2a/bindings/index.js.map +1 -1
- package/dist/src/a2a/bindings/jsonrpc.d.ts +193 -9
- package/dist/src/a2a/bindings/jsonrpc.d.ts.map +1 -1
- package/dist/src/a2a/bindings/jsonrpc.js +152 -9
- package/dist/src/a2a/bindings/jsonrpc.js.map +1 -1
- package/dist/src/a2a/bridge.d.ts +232 -37
- package/dist/src/a2a/bridge.d.ts.map +1 -1
- package/dist/src/a2a/bridge.js +172 -24
- package/dist/src/a2a/bridge.js.map +1 -1
- package/dist/src/a2a/extensions.d.ts +221 -12
- package/dist/src/a2a/extensions.d.ts.map +1 -1
- package/dist/src/a2a/extensions.js +175 -11
- package/dist/src/a2a/extensions.js.map +1 -1
- package/dist/src/a2a/index.d.ts +2 -0
- package/dist/src/a2a/index.d.ts.map +1 -1
- package/dist/src/a2a/index.js +2 -0
- package/dist/src/a2a/index.js.map +1 -1
- package/dist/src/a2a/streaming.d.ts +274 -2
- package/dist/src/a2a/streaming.d.ts.map +1 -1
- package/dist/src/a2a/streaming.js +245 -2
- package/dist/src/a2a/streaming.js.map +1 -1
- package/dist/src/a2a/task-lifecycle.d.ts +339 -19
- package/dist/src/a2a/task-lifecycle.d.ts.map +1 -1
- package/dist/src/a2a/task-lifecycle.js +302 -19
- package/dist/src/a2a/task-lifecycle.js.map +1 -1
- package/dist/src/budgetEnforcement.d.ts +88 -14
- package/dist/src/budgetEnforcement.d.ts.map +1 -1
- package/dist/src/budgetEnforcement.js +132 -19
- package/dist/src/budgetEnforcement.js.map +1 -1
- package/dist/src/circuit-breaker/index.d.ts +254 -9
- package/dist/src/circuit-breaker/index.d.ts.map +1 -1
- package/dist/src/circuit-breaker/index.js +218 -9
- package/dist/src/circuit-breaker/index.js.map +1 -1
- package/dist/src/compliance.d.ts +176 -0
- package/dist/src/compliance.d.ts.map +1 -1
- package/dist/src/compliance.js +100 -0
- package/dist/src/compliance.js.map +1 -1
- package/dist/src/conformance.d.ts +52 -0
- package/dist/src/conformance.d.ts.map +1 -1
- package/dist/src/conformance.js +41 -0
- package/dist/src/conformance.js.map +1 -1
- package/dist/src/conformanceProfiles.d.ts +66 -0
- package/dist/src/conformanceProfiles.d.ts.map +1 -1
- package/dist/src/conformanceProfiles.js +51 -0
- package/dist/src/conformanceProfiles.js.map +1 -1
- package/dist/src/deprecationRegistry.d.ts +80 -0
- package/dist/src/deprecationRegistry.d.ts.map +1 -1
- package/dist/src/deprecationRegistry.js +50 -0
- package/dist/src/deprecationRegistry.js.map +1 -1
- package/dist/src/discovery.d.ts +344 -63
- package/dist/src/discovery.d.ts.map +1 -1
- package/dist/src/discovery.js +67 -13
- package/dist/src/discovery.js.map +1 -1
- package/dist/src/envelope.d.ts +252 -0
- package/dist/src/envelope.d.ts.map +1 -1
- package/dist/src/envelope.js +165 -0
- package/dist/src/envelope.js.map +1 -1
- package/dist/src/errorRegistry.d.ts +159 -0
- package/dist/src/errorRegistry.d.ts.map +1 -1
- package/dist/src/errorRegistry.js +115 -0
- package/dist/src/errorRegistry.js.map +1 -1
- package/dist/src/fieldExtraction.d.ts +125 -25
- package/dist/src/fieldExtraction.d.ts.map +1 -1
- package/dist/src/fieldExtraction.js +85 -16
- package/dist/src/fieldExtraction.js.map +1 -1
- package/dist/src/flagResolver.d.ts +75 -9
- package/dist/src/flagResolver.d.ts.map +1 -1
- package/dist/src/flagResolver.js +20 -4
- package/dist/src/flagResolver.js.map +1 -1
- package/dist/src/flagSemantics.d.ts +76 -1
- package/dist/src/flagSemantics.d.ts.map +1 -1
- package/dist/src/flagSemantics.js +66 -0
- package/dist/src/flagSemantics.js.map +1 -1
- package/dist/src/health/index.d.ts +87 -6
- package/dist/src/health/index.d.ts.map +1 -1
- package/dist/src/health/index.js +54 -6
- package/dist/src/health/index.js.map +1 -1
- package/dist/src/index.d.ts +12 -1
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +12 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/mviProjection.d.ts +42 -6
- package/dist/src/mviProjection.d.ts.map +1 -1
- package/dist/src/mviProjection.js +31 -5
- package/dist/src/mviProjection.js.map +1 -1
- package/dist/src/native-loader.d.ts +49 -0
- package/dist/src/native-loader.d.ts.map +1 -0
- package/dist/src/native-loader.js +56 -0
- package/dist/src/native-loader.js.map +1 -0
- package/dist/src/problemDetails.d.ts +70 -4
- package/dist/src/problemDetails.d.ts.map +1 -1
- package/dist/src/problemDetails.js +21 -3
- package/dist/src/problemDetails.js.map +1 -1
- package/dist/src/shutdown/index.d.ts +96 -7
- package/dist/src/shutdown/index.d.ts.map +1 -1
- package/dist/src/shutdown/index.js +72 -7
- package/dist/src/shutdown/index.js.map +1 -1
- package/dist/src/tokenEstimator.d.ts +97 -11
- package/dist/src/tokenEstimator.d.ts.map +1 -1
- package/dist/src/tokenEstimator.js +90 -11
- package/dist/src/tokenEstimator.js.map +1 -1
- package/dist/src/types.d.ts +467 -2
- package/dist/src/types.d.ts.map +1 -1
- package/dist/src/types.js +64 -0
- package/dist/src/types.js.map +1 -1
- package/dist/src/validateEnvelope.d.ts +59 -1
- package/dist/src/validateEnvelope.d.ts.map +1 -1
- package/dist/src/validateEnvelope.js +75 -9
- package/dist/src/validateEnvelope.js.map +1 -1
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/lafs.md +3 -4
- package/package.json +6 -3
- package/dist/src/mcpAdapter.d.ts +0 -29
- package/dist/src/mcpAdapter.d.ts.map +0 -1
- package/dist/src/mcpAdapter.js +0 -286
- package/dist/src/mcpAdapter.js.map +0 -1
- package/schemas/v1/conformance-profiles.d.ts +0 -15
- package/schemas/v1/envelope.schema.d.ts +0 -14
- package/schemas/v1/error-registry.d.ts +0 -24
|
@@ -1,16 +1,48 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* LAFS Circuit Breaker Module
|
|
3
3
|
*
|
|
4
|
-
* Provides circuit breaker pattern for resilient service calls
|
|
4
|
+
* Provides circuit breaker pattern for resilient service calls.
|
|
5
|
+
*
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Error thrown when a circuit breaker rejects a call.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* This error is raised when the circuit is in the OPEN state or when the
|
|
13
|
+
* HALF_OPEN call limit has been reached. Callers should catch this to
|
|
14
|
+
* implement fallback logic or return a 503 response.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* try {
|
|
19
|
+
* await breaker.execute(() => fetch('/api'));
|
|
20
|
+
* } catch (err) {
|
|
21
|
+
* if (err instanceof CircuitBreakerError) {
|
|
22
|
+
* console.log('Circuit open, using fallback');
|
|
23
|
+
* }
|
|
24
|
+
* }
|
|
25
|
+
* ```
|
|
5
26
|
*/
|
|
6
27
|
export class CircuitBreakerError extends Error {
|
|
28
|
+
/**
|
|
29
|
+
* Creates a new CircuitBreakerError.
|
|
30
|
+
*
|
|
31
|
+
* @param message - Descriptive error message indicating why the call was rejected
|
|
32
|
+
*/
|
|
7
33
|
constructor(message) {
|
|
8
34
|
super(message);
|
|
9
35
|
this.name = 'CircuitBreakerError';
|
|
10
36
|
}
|
|
11
37
|
}
|
|
12
38
|
/**
|
|
13
|
-
* Circuit breaker for protecting against cascading failures
|
|
39
|
+
* Circuit breaker for protecting against cascading failures.
|
|
40
|
+
*
|
|
41
|
+
* @remarks
|
|
42
|
+
* Implements the circuit breaker pattern with three states: CLOSED (normal),
|
|
43
|
+
* OPEN (rejecting calls), and HALF_OPEN (testing recovery). The breaker
|
|
44
|
+
* automatically transitions between states based on failure and success
|
|
45
|
+
* thresholds, and schedules reset timers when the circuit opens.
|
|
14
46
|
*
|
|
15
47
|
* @example
|
|
16
48
|
* ```typescript
|
|
@@ -35,14 +67,27 @@ export class CircuitBreakerError extends Error {
|
|
|
35
67
|
*/
|
|
36
68
|
export class CircuitBreaker {
|
|
37
69
|
config;
|
|
70
|
+
/** Current circuit state. */
|
|
38
71
|
state = 'CLOSED';
|
|
72
|
+
/** Total failure count since last reset. */
|
|
39
73
|
failures = 0;
|
|
74
|
+
/** Total success count since last reset. */
|
|
40
75
|
successes = 0;
|
|
76
|
+
/** Timestamp of the most recent failure. */
|
|
41
77
|
lastFailureTime;
|
|
78
|
+
/** Consecutive successes since the last failure. */
|
|
42
79
|
consecutiveSuccesses = 0;
|
|
80
|
+
/** Lifetime call count. */
|
|
43
81
|
totalCalls = 0;
|
|
82
|
+
/** Number of calls made while in the HALF_OPEN state. */
|
|
44
83
|
halfOpenCalls = 0;
|
|
84
|
+
/** Timer handle for the scheduled OPEN-to-HALF_OPEN transition. */
|
|
45
85
|
resetTimer;
|
|
86
|
+
/**
|
|
87
|
+
* Creates a new CircuitBreaker with the given configuration.
|
|
88
|
+
*
|
|
89
|
+
* @param config - Circuit breaker configuration with name, thresholds, and timeouts
|
|
90
|
+
*/
|
|
46
91
|
constructor(config) {
|
|
47
92
|
this.config = config;
|
|
48
93
|
this.config = {
|
|
@@ -54,7 +99,24 @@ export class CircuitBreaker {
|
|
|
54
99
|
};
|
|
55
100
|
}
|
|
56
101
|
/**
|
|
57
|
-
* Execute a function with circuit breaker protection
|
|
102
|
+
* Execute a function with circuit breaker protection.
|
|
103
|
+
*
|
|
104
|
+
* @remarks
|
|
105
|
+
* When the circuit is CLOSED, calls pass through normally. When OPEN, calls
|
|
106
|
+
* are rejected with a {@link CircuitBreakerError} unless the reset timeout has
|
|
107
|
+
* elapsed (triggering HALF_OPEN). In HALF_OPEN, a limited number of trial
|
|
108
|
+
* calls are permitted; successes may close the circuit while failures re-open it.
|
|
109
|
+
*
|
|
110
|
+
* @typeParam T - Return type of the wrapped function
|
|
111
|
+
* @param fn - Async function to execute under circuit breaker protection
|
|
112
|
+
* @returns The result of invoking `fn`
|
|
113
|
+
*
|
|
114
|
+
* @example
|
|
115
|
+
* ```typescript
|
|
116
|
+
* const result = await breaker.execute(async () => {
|
|
117
|
+
* return await fetch('https://api.example.com/data');
|
|
118
|
+
* });
|
|
119
|
+
* ```
|
|
58
120
|
*/
|
|
59
121
|
async execute(fn) {
|
|
60
122
|
this.totalCalls++;
|
|
@@ -83,13 +145,40 @@ export class CircuitBreaker {
|
|
|
83
145
|
}
|
|
84
146
|
}
|
|
85
147
|
/**
|
|
86
|
-
* Get current circuit breaker state
|
|
148
|
+
* Get the current circuit breaker state.
|
|
149
|
+
*
|
|
150
|
+
* @remarks
|
|
151
|
+
* Returns one of `'CLOSED'`, `'OPEN'`, or `'HALF_OPEN'`. Useful for
|
|
152
|
+
* dashboards or conditional logic that needs to know whether calls will
|
|
153
|
+
* be accepted.
|
|
154
|
+
*
|
|
155
|
+
* @returns The current {@link CircuitState}
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
* ```typescript
|
|
159
|
+
* if (breaker.getState() === 'OPEN') {
|
|
160
|
+
* console.log('Circuit is open, requests will be rejected');
|
|
161
|
+
* }
|
|
162
|
+
* ```
|
|
87
163
|
*/
|
|
88
164
|
getState() {
|
|
89
165
|
return this.state;
|
|
90
166
|
}
|
|
91
167
|
/**
|
|
92
|
-
* Get circuit breaker metrics
|
|
168
|
+
* Get a snapshot of the circuit breaker's runtime metrics.
|
|
169
|
+
*
|
|
170
|
+
* @remarks
|
|
171
|
+
* Returns a copy of the internal counters including failure/success counts,
|
|
172
|
+
* the current state, and the timestamp of the last failure. Useful for
|
|
173
|
+
* monitoring and observability.
|
|
174
|
+
*
|
|
175
|
+
* @returns A {@link CircuitBreakerMetrics} snapshot
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```typescript
|
|
179
|
+
* const metrics = breaker.getMetrics();
|
|
180
|
+
* console.log(`State: ${metrics.state}, Failures: ${metrics.failures}`);
|
|
181
|
+
* ```
|
|
93
182
|
*/
|
|
94
183
|
getMetrics() {
|
|
95
184
|
return {
|
|
@@ -102,18 +191,41 @@ export class CircuitBreaker {
|
|
|
102
191
|
};
|
|
103
192
|
}
|
|
104
193
|
/**
|
|
105
|
-
* Manually open the circuit breaker
|
|
194
|
+
* Manually open the circuit breaker, rejecting all subsequent calls.
|
|
195
|
+
*
|
|
196
|
+
* @remarks
|
|
197
|
+
* Forces the circuit into the OPEN state regardless of the current failure
|
|
198
|
+
* count. Useful for administrative controls or when an external signal
|
|
199
|
+
* indicates the downstream service is unavailable.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```typescript
|
|
203
|
+
* breaker.forceOpen();
|
|
204
|
+
* console.log(breaker.getState()); // 'OPEN'
|
|
205
|
+
* ```
|
|
106
206
|
*/
|
|
107
207
|
forceOpen() {
|
|
108
208
|
this.transitionTo('OPEN');
|
|
109
209
|
}
|
|
110
210
|
/**
|
|
111
|
-
* Manually close the circuit breaker
|
|
211
|
+
* Manually close the circuit breaker and reset all counters.
|
|
212
|
+
*
|
|
213
|
+
* @remarks
|
|
214
|
+
* Forces the circuit into the CLOSED state and clears failure/success
|
|
215
|
+
* counters and any pending reset timer. Useful for administrative recovery
|
|
216
|
+
* after a known issue has been resolved.
|
|
217
|
+
*
|
|
218
|
+
* @example
|
|
219
|
+
* ```typescript
|
|
220
|
+
* breaker.forceClose();
|
|
221
|
+
* console.log(breaker.getState()); // 'CLOSED'
|
|
222
|
+
* ```
|
|
112
223
|
*/
|
|
113
224
|
forceClose() {
|
|
114
225
|
this.transitionTo('CLOSED');
|
|
115
226
|
this.reset();
|
|
116
227
|
}
|
|
228
|
+
/** Records a successful call and may transition from HALF_OPEN to CLOSED. */
|
|
117
229
|
onSuccess() {
|
|
118
230
|
this.successes++;
|
|
119
231
|
this.consecutiveSuccesses++;
|
|
@@ -124,6 +236,7 @@ export class CircuitBreaker {
|
|
|
124
236
|
}
|
|
125
237
|
}
|
|
126
238
|
}
|
|
239
|
+
/** Records a failed call and may trip the circuit to OPEN. */
|
|
127
240
|
onFailure() {
|
|
128
241
|
this.failures++;
|
|
129
242
|
this.consecutiveSuccesses = 0;
|
|
@@ -139,6 +252,7 @@ export class CircuitBreaker {
|
|
|
139
252
|
}
|
|
140
253
|
}
|
|
141
254
|
}
|
|
255
|
+
/** Transitions the circuit to the given state, resetting HALF_OPEN call count when entering HALF_OPEN. */
|
|
142
256
|
transitionTo(newState) {
|
|
143
257
|
console.log(`Circuit breaker '${this.config.name}': ${this.state} -> ${newState}`);
|
|
144
258
|
this.state = newState;
|
|
@@ -146,12 +260,14 @@ export class CircuitBreaker {
|
|
|
146
260
|
this.halfOpenCalls = 0;
|
|
147
261
|
}
|
|
148
262
|
}
|
|
263
|
+
/** Returns `true` if enough time has elapsed since the last failure to attempt a reset. */
|
|
149
264
|
shouldAttemptReset() {
|
|
150
265
|
if (!this.lastFailureTime)
|
|
151
266
|
return true;
|
|
152
267
|
const elapsed = Date.now() - this.lastFailureTime.getTime();
|
|
153
268
|
return elapsed >= (this.config.resetTimeout || 30000);
|
|
154
269
|
}
|
|
270
|
+
/** Schedules a timer to transition from OPEN to HALF_OPEN after the configured reset timeout. */
|
|
155
271
|
scheduleReset() {
|
|
156
272
|
if (this.resetTimer) {
|
|
157
273
|
clearTimeout(this.resetTimer);
|
|
@@ -162,6 +278,7 @@ export class CircuitBreaker {
|
|
|
162
278
|
}
|
|
163
279
|
}, this.config.resetTimeout || 30000);
|
|
164
280
|
}
|
|
281
|
+
/** Resets all failure/success counters and clears the pending reset timer. */
|
|
165
282
|
reset() {
|
|
166
283
|
this.failures = 0;
|
|
167
284
|
this.consecutiveSuccesses = 0;
|
|
@@ -173,7 +290,12 @@ export class CircuitBreaker {
|
|
|
173
290
|
}
|
|
174
291
|
}
|
|
175
292
|
/**
|
|
176
|
-
*
|
|
293
|
+
* Registry for managing multiple named circuit breakers.
|
|
294
|
+
*
|
|
295
|
+
* @remarks
|
|
296
|
+
* Provides centralized creation, lookup, and metrics aggregation for
|
|
297
|
+
* circuit breakers. Each breaker is stored by name and can be retrieved
|
|
298
|
+
* or lazily created via {@link CircuitBreakerRegistry.getOrCreate}.
|
|
177
299
|
*
|
|
178
300
|
* @example
|
|
179
301
|
* ```typescript
|
|
@@ -188,15 +310,65 @@ export class CircuitBreaker {
|
|
|
188
310
|
* ```
|
|
189
311
|
*/
|
|
190
312
|
export class CircuitBreakerRegistry {
|
|
313
|
+
/** Internal map of circuit breaker name to instance. */
|
|
191
314
|
breakers = new Map();
|
|
315
|
+
/**
|
|
316
|
+
* Register a new circuit breaker with the given name and configuration.
|
|
317
|
+
*
|
|
318
|
+
* @remarks
|
|
319
|
+
* Creates a new {@link CircuitBreaker}, stores it in the registry, and
|
|
320
|
+
* returns it. If a breaker with the same name already exists, it is replaced.
|
|
321
|
+
*
|
|
322
|
+
* @param name - Unique name for the circuit breaker
|
|
323
|
+
* @param config - Configuration options (name is set automatically)
|
|
324
|
+
* @returns The newly created {@link CircuitBreaker}
|
|
325
|
+
*
|
|
326
|
+
* @example
|
|
327
|
+
* ```typescript
|
|
328
|
+
* const breaker = registry.add('user-service', { failureThreshold: 3 });
|
|
329
|
+
* ```
|
|
330
|
+
*/
|
|
192
331
|
add(name, config) {
|
|
193
332
|
const breaker = new CircuitBreaker({ ...config, name });
|
|
194
333
|
this.breakers.set(name, breaker);
|
|
195
334
|
return breaker;
|
|
196
335
|
}
|
|
336
|
+
/**
|
|
337
|
+
* Retrieve a circuit breaker by name.
|
|
338
|
+
*
|
|
339
|
+
* @remarks
|
|
340
|
+
* Returns `undefined` if no breaker with the given name has been registered.
|
|
341
|
+
*
|
|
342
|
+
* @param name - Name of the circuit breaker to look up
|
|
343
|
+
* @returns The matching {@link CircuitBreaker}, or `undefined` if not found
|
|
344
|
+
*
|
|
345
|
+
* @example
|
|
346
|
+
* ```typescript
|
|
347
|
+
* const breaker = registry.get('payment-api');
|
|
348
|
+
* if (breaker) {
|
|
349
|
+
* await breaker.execute(() => callPaymentApi());
|
|
350
|
+
* }
|
|
351
|
+
* ```
|
|
352
|
+
*/
|
|
197
353
|
get(name) {
|
|
198
354
|
return this.breakers.get(name);
|
|
199
355
|
}
|
|
356
|
+
/**
|
|
357
|
+
* Retrieve an existing circuit breaker or create one if it does not exist.
|
|
358
|
+
*
|
|
359
|
+
* @remarks
|
|
360
|
+
* This is useful when callers want a breaker but do not know whether it
|
|
361
|
+
* has already been registered, avoiding duplicate creation.
|
|
362
|
+
*
|
|
363
|
+
* @param name - Name of the circuit breaker
|
|
364
|
+
* @param config - Configuration to use if a new breaker must be created
|
|
365
|
+
* @returns The existing or newly created {@link CircuitBreaker}
|
|
366
|
+
*
|
|
367
|
+
* @example
|
|
368
|
+
* ```typescript
|
|
369
|
+
* const breaker = registry.getOrCreate('cache-api', { failureThreshold: 10 });
|
|
370
|
+
* ```
|
|
371
|
+
*/
|
|
200
372
|
getOrCreate(name, config) {
|
|
201
373
|
let breaker = this.breakers.get(name);
|
|
202
374
|
if (!breaker) {
|
|
@@ -204,6 +376,23 @@ export class CircuitBreakerRegistry {
|
|
|
204
376
|
}
|
|
205
377
|
return breaker;
|
|
206
378
|
}
|
|
379
|
+
/**
|
|
380
|
+
* Collect metrics from all registered circuit breakers.
|
|
381
|
+
*
|
|
382
|
+
* @remarks
|
|
383
|
+
* Returns a record keyed by breaker name with each value being the
|
|
384
|
+
* corresponding {@link CircuitBreakerMetrics} snapshot.
|
|
385
|
+
*
|
|
386
|
+
* @returns A record mapping breaker names to their current metrics
|
|
387
|
+
*
|
|
388
|
+
* @example
|
|
389
|
+
* ```typescript
|
|
390
|
+
* const allMetrics = registry.getAllMetrics();
|
|
391
|
+
* for (const [name, metrics] of Object.entries(allMetrics)) {
|
|
392
|
+
* console.log(`${name}: ${metrics.state}`);
|
|
393
|
+
* }
|
|
394
|
+
* ```
|
|
395
|
+
*/
|
|
207
396
|
getAllMetrics() {
|
|
208
397
|
const metrics = {};
|
|
209
398
|
this.breakers.forEach((breaker, name) => {
|
|
@@ -211,6 +400,18 @@ export class CircuitBreakerRegistry {
|
|
|
211
400
|
});
|
|
212
401
|
return metrics;
|
|
213
402
|
}
|
|
403
|
+
/**
|
|
404
|
+
* Force-close all registered circuit breakers, resetting their counters.
|
|
405
|
+
*
|
|
406
|
+
* @remarks
|
|
407
|
+
* Iterates over every registered breaker and calls {@link CircuitBreaker.forceClose}.
|
|
408
|
+
* Useful for administrative recovery or test teardown.
|
|
409
|
+
*
|
|
410
|
+
* @example
|
|
411
|
+
* ```typescript
|
|
412
|
+
* registry.resetAll();
|
|
413
|
+
* ```
|
|
414
|
+
*/
|
|
214
415
|
resetAll() {
|
|
215
416
|
this.breakers.forEach((breaker) => {
|
|
216
417
|
breaker.forceClose();
|
|
@@ -218,7 +419,15 @@ export class CircuitBreakerRegistry {
|
|
|
218
419
|
}
|
|
219
420
|
}
|
|
220
421
|
/**
|
|
221
|
-
* Create a circuit breaker
|
|
422
|
+
* Create an Express middleware that wraps downstream handlers with a circuit breaker.
|
|
423
|
+
*
|
|
424
|
+
* @remarks
|
|
425
|
+
* Instantiates a {@link CircuitBreaker} from the provided config and wraps the
|
|
426
|
+
* `next()` call. When the circuit is open, the middleware responds with a 503
|
|
427
|
+
* status and a JSON error body instead of forwarding the request.
|
|
428
|
+
*
|
|
429
|
+
* @param config - Circuit breaker configuration for the middleware instance
|
|
430
|
+
* @returns An Express-compatible middleware function
|
|
222
431
|
*
|
|
223
432
|
* @example
|
|
224
433
|
* ```typescript
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/circuit-breaker/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/circuit-breaker/index.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAkEH;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,OAAO,mBAAoB,SAAQ,KAAK;IAC5C;;;;OAIG;IACH,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,qBAAqB,CAAC;IACpC,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,MAAM,OAAO,cAAc;IA8BL;IA7BpB,6BAA6B;IACrB,KAAK,GAAiB,QAAQ,CAAC;IAEvC,4CAA4C;IACpC,QAAQ,GAAG,CAAC,CAAC;IAErB,4CAA4C;IACpC,SAAS,GAAG,CAAC,CAAC;IAEtB,4CAA4C;IACpC,eAAe,CAAQ;IAE/B,oDAAoD;IAC5C,oBAAoB,GAAG,CAAC,CAAC;IAEjC,2BAA2B;IACnB,UAAU,GAAG,CAAC,CAAC;IAEvB,yDAAyD;IACjD,aAAa,GAAG,CAAC,CAAC;IAE1B,mEAAmE;IAC3D,UAAU,CAAkB;IAEpC;;;;OAIG;IACH,YAAoB,MAA4B;QAA5B,WAAM,GAAN,MAAM,CAAsB;QAC9C,IAAI,CAAC,MAAM,GAAG;YACZ,gBAAgB,EAAE,CAAC;YACnB,YAAY,EAAE,KAAK;YACnB,gBAAgB,EAAE,CAAC;YACnB,gBAAgB,EAAE,CAAC;YACnB,GAAG,MAAM;SACV,CAAC;IACJ,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,KAAK,CAAC,OAAO,CAAI,EAAoB;QACnC,IAAI,CAAC,UAAU,EAAE,CAAC;QAElB,IAAI,IAAI,CAAC,KAAK,KAAK,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AAU,EAAE,CAAC;QACvB,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,wBAAwB,CAAC,MAA4B;IACnE,MAAM,OAAO,GAAG,IAAI,cAAc,CAAC,MAAM,CAAC,CAAC;IAE3C,OAAO,KAAK,EACV,IAAa,EACb,GAAoE,EACpE,IAAgB,EAChB,EAAE;QACF,IAAI,CAAC;YACH,MAAM,OAAO,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE;gBAC/B,IAAI,EAAE,CAAC;YACT,CAAC,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,KAAK,YAAY,mBAAmB,EAAE,CAAC;gBACzC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC;oBACnB,KAAK,EAAE,iCAAiC;oBACxC,MAAM,EAAE,yBAAyB;iBAClC,CAAC,CAAC;YACL,CAAC;iBAAM,CAAC;gBACN,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/src/compliance.d.ts
CHANGED
|
@@ -1,32 +1,208 @@
|
|
|
1
1
|
import type { ConformanceReport, FlagInput, LAFSEnvelope } from './types.js';
|
|
2
2
|
import { type EnvelopeValidationResult } from './validateEnvelope.js';
|
|
3
|
+
/**
|
|
4
|
+
* Identifies which stage of the compliance pipeline produced an issue.
|
|
5
|
+
*
|
|
6
|
+
* @remarks
|
|
7
|
+
* Used by {@link ComplianceIssue} to classify where a failure originated
|
|
8
|
+
* during multi-stage LAFS compliance enforcement.
|
|
9
|
+
*/
|
|
3
10
|
export type ComplianceStage = 'schema' | 'envelope' | 'flags' | 'format';
|
|
11
|
+
/**
|
|
12
|
+
* Describes a single compliance failure detected during enforcement.
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* Each issue maps to a specific pipeline stage and includes a human-readable
|
|
16
|
+
* message with an optional detail string for diagnostics.
|
|
17
|
+
*/
|
|
4
18
|
export interface ComplianceIssue {
|
|
19
|
+
/** The pipeline stage that produced this issue. */
|
|
5
20
|
stage: ComplianceStage;
|
|
21
|
+
/** A short, human-readable description of the failure. */
|
|
6
22
|
message: string;
|
|
23
|
+
/**
|
|
24
|
+
* Additional diagnostic information about the failure.
|
|
25
|
+
* @defaultValue undefined
|
|
26
|
+
*/
|
|
7
27
|
detail?: string;
|
|
8
28
|
}
|
|
29
|
+
/**
|
|
30
|
+
* Options controlling which compliance stages are executed.
|
|
31
|
+
*
|
|
32
|
+
* @remarks
|
|
33
|
+
* All options default to safe values so callers can pass an empty object
|
|
34
|
+
* and still get schema validation.
|
|
35
|
+
*/
|
|
9
36
|
export interface EnforceComplianceOptions {
|
|
37
|
+
/**
|
|
38
|
+
* Whether to run envelope conformance checks after schema validation.
|
|
39
|
+
* @defaultValue true
|
|
40
|
+
*/
|
|
10
41
|
checkConformance?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Whether to run flag conformance checks.
|
|
44
|
+
* @defaultValue false
|
|
45
|
+
*/
|
|
11
46
|
checkFlags?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Flag input to validate when {@link checkFlags} is enabled.
|
|
49
|
+
* @defaultValue undefined
|
|
50
|
+
*/
|
|
12
51
|
flags?: FlagInput;
|
|
52
|
+
/**
|
|
53
|
+
* When true, asserts that the resolved output format is JSON.
|
|
54
|
+
* @defaultValue false
|
|
55
|
+
*/
|
|
13
56
|
requireJsonOutput?: boolean;
|
|
14
57
|
}
|
|
58
|
+
/**
|
|
59
|
+
* Aggregated result of a full LAFS compliance run.
|
|
60
|
+
*
|
|
61
|
+
* @remarks
|
|
62
|
+
* Contains the overall pass/fail status, per-stage reports, and the
|
|
63
|
+
* parsed envelope when schema validation succeeds.
|
|
64
|
+
*/
|
|
15
65
|
export interface ComplianceResult {
|
|
66
|
+
/** True when every executed stage passes with zero issues. */
|
|
16
67
|
ok: boolean;
|
|
68
|
+
/**
|
|
69
|
+
* The parsed envelope, present only when schema validation succeeds.
|
|
70
|
+
* @defaultValue undefined
|
|
71
|
+
*/
|
|
17
72
|
envelope?: LAFSEnvelope;
|
|
73
|
+
/** Schema validation result from the native validator (or AJV fallback). */
|
|
18
74
|
validation: EnvelopeValidationResult;
|
|
75
|
+
/**
|
|
76
|
+
* Envelope conformance report, present when {@link EnforceComplianceOptions.checkConformance} is true.
|
|
77
|
+
* @defaultValue undefined
|
|
78
|
+
*/
|
|
19
79
|
envelopeConformance?: ConformanceReport;
|
|
80
|
+
/**
|
|
81
|
+
* Flag conformance report, present when {@link EnforceComplianceOptions.checkFlags} is true.
|
|
82
|
+
* @defaultValue undefined
|
|
83
|
+
*/
|
|
20
84
|
flagConformance?: ConformanceReport;
|
|
85
|
+
/** All issues collected across every executed stage. */
|
|
21
86
|
issues: ComplianceIssue[];
|
|
22
87
|
}
|
|
88
|
+
/**
|
|
89
|
+
* Error thrown when {@link assertCompliance} or {@link withCompliance} detects failures.
|
|
90
|
+
*
|
|
91
|
+
* @remarks
|
|
92
|
+
* Extends `Error` with a structured `issues` array so callers can
|
|
93
|
+
* programmatically inspect each failure without parsing the message string.
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* try {
|
|
98
|
+
* assertCompliance(envelope);
|
|
99
|
+
* } catch (err) {
|
|
100
|
+
* if (err instanceof ComplianceError) {
|
|
101
|
+
* console.log(err.issues);
|
|
102
|
+
* }
|
|
103
|
+
* }
|
|
104
|
+
* ```
|
|
105
|
+
*/
|
|
23
106
|
export declare class ComplianceError extends Error {
|
|
107
|
+
/** The structured list of compliance issues that caused this error. */
|
|
24
108
|
readonly issues: ComplianceIssue[];
|
|
109
|
+
/**
|
|
110
|
+
* Creates a new ComplianceError from a list of issues.
|
|
111
|
+
*
|
|
112
|
+
* @param issues - The compliance issues that triggered this error.
|
|
113
|
+
*/
|
|
25
114
|
constructor(issues: ComplianceIssue[]);
|
|
26
115
|
}
|
|
116
|
+
/**
|
|
117
|
+
* Runs the full LAFS compliance pipeline against an unknown input value.
|
|
118
|
+
*
|
|
119
|
+
* @remarks
|
|
120
|
+
* Executes stages in order: schema validation, envelope conformance,
|
|
121
|
+
* flag conformance, and output-format assertion. Each stage is gated
|
|
122
|
+
* by the corresponding option. Schema validation always runs first;
|
|
123
|
+
* if it fails, later stages are skipped.
|
|
124
|
+
*
|
|
125
|
+
* @param input - The raw value to validate as a LAFS envelope.
|
|
126
|
+
* @param options - Controls which optional stages execute.
|
|
127
|
+
* @returns A {@link ComplianceResult} with the aggregate pass/fail status and per-stage reports.
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* ```ts
|
|
131
|
+
* const result = enforceCompliance(rawJson, { checkFlags: true, flags: { jsonFlag: true } });
|
|
132
|
+
* if (!result.ok) {
|
|
133
|
+
* console.error(result.issues);
|
|
134
|
+
* }
|
|
135
|
+
* ```
|
|
136
|
+
*/
|
|
27
137
|
export declare function enforceCompliance(input: unknown, options?: EnforceComplianceOptions): ComplianceResult;
|
|
138
|
+
/**
|
|
139
|
+
* Validates input and throws {@link ComplianceError} on any failure.
|
|
140
|
+
*
|
|
141
|
+
* @remarks
|
|
142
|
+
* Thin wrapper around {@link enforceCompliance} that converts a non-ok
|
|
143
|
+
* result into an exception. Useful in pipelines where compliance is a
|
|
144
|
+
* hard gate.
|
|
145
|
+
*
|
|
146
|
+
* @param input - The raw value to validate as a LAFS envelope.
|
|
147
|
+
* @param options - Controls which optional stages execute.
|
|
148
|
+
* @returns The validated {@link LAFSEnvelope} when all stages pass.
|
|
149
|
+
* @throws {@link ComplianceError} When any compliance stage fails.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```ts
|
|
153
|
+
* const envelope = assertCompliance(rawJson);
|
|
154
|
+
* ```
|
|
155
|
+
*/
|
|
28
156
|
export declare function assertCompliance(input: unknown, options?: EnforceComplianceOptions): LAFSEnvelope;
|
|
157
|
+
/**
|
|
158
|
+
* Wraps an envelope-producing function with automatic compliance enforcement.
|
|
159
|
+
*
|
|
160
|
+
* @remarks
|
|
161
|
+
* Returns a new async function that calls the producer, then pipes the
|
|
162
|
+
* result through {@link assertCompliance}. If the producer returns a
|
|
163
|
+
* non-compliant envelope, the wrapper throws {@link ComplianceError}.
|
|
164
|
+
*
|
|
165
|
+
* @typeParam TArgs - Argument types forwarded to the producer function.
|
|
166
|
+
* @typeParam TResult - The envelope subtype returned by the producer.
|
|
167
|
+
* @param producer - A sync or async function that produces a LAFS envelope.
|
|
168
|
+
* @param options - Compliance options forwarded to {@link assertCompliance}.
|
|
169
|
+
* @returns An async function with the same signature that enforces compliance on every call.
|
|
170
|
+
*
|
|
171
|
+
* @example
|
|
172
|
+
* ```ts
|
|
173
|
+
* const safeFetch = withCompliance(fetchEnvelope, { checkConformance: true });
|
|
174
|
+
* const envelope = await safeFetch('/api/data');
|
|
175
|
+
* ```
|
|
176
|
+
*/
|
|
29
177
|
export declare function withCompliance<TArgs extends unknown[], TResult extends LAFSEnvelope>(producer: (...args: TArgs) => TResult | Promise<TResult>, options?: EnforceComplianceOptions): (...args: TArgs) => Promise<LAFSEnvelope>;
|
|
178
|
+
/**
|
|
179
|
+
* Middleware signature for intercepting LAFS envelopes in a pipeline.
|
|
180
|
+
*
|
|
181
|
+
* @remarks
|
|
182
|
+
* Follows a standard middleware pattern: receive the current envelope,
|
|
183
|
+
* call `next()` to continue the chain, then optionally transform the result.
|
|
184
|
+
*
|
|
185
|
+
* @param envelope - The envelope entering this middleware.
|
|
186
|
+
* @param next - Callback that invokes the next middleware or terminal handler.
|
|
187
|
+
* @returns The (possibly transformed) envelope to pass upstream.
|
|
188
|
+
*/
|
|
30
189
|
export type ComplianceMiddleware = (envelope: LAFSEnvelope, next: () => LAFSEnvelope | Promise<LAFSEnvelope>) => Promise<LAFSEnvelope> | LAFSEnvelope;
|
|
190
|
+
/**
|
|
191
|
+
* Creates a {@link ComplianceMiddleware} that enforces LAFS compliance on the next handler's output.
|
|
192
|
+
*
|
|
193
|
+
* @remarks
|
|
194
|
+
* The returned middleware calls `next()`, then pipes the candidate envelope
|
|
195
|
+
* through {@link assertCompliance}. Non-compliant envelopes cause a
|
|
196
|
+
* {@link ComplianceError} to propagate.
|
|
197
|
+
*
|
|
198
|
+
* @param options - Compliance options forwarded to {@link assertCompliance}.
|
|
199
|
+
* @returns A middleware function that validates the downstream envelope.
|
|
200
|
+
*
|
|
201
|
+
* @example
|
|
202
|
+
* ```ts
|
|
203
|
+
* const mw = createComplianceMiddleware({ checkConformance: true });
|
|
204
|
+
* const result = await mw(currentEnvelope, () => produceEnvelope());
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
31
207
|
export declare function createComplianceMiddleware(options?: EnforceComplianceOptions): ComplianceMiddleware;
|
|
32
208
|
//# sourceMappingURL=compliance.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compliance.d.ts","sourceRoot":"","sources":["../../src/compliance.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,iBAAiB,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAC7E,OAAO,EAEL,KAAK,wBAAwB,EAE9B,MAAM,uBAAuB,CAAC;AAE/B,MAAM,MAAM,eAAe,GAAG,QAAQ,GAAG,UAAU,GAAG,OAAO,GAAG,QAAQ,CAAC;AAEzE,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,eAAe,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,wBAAwB;IACvC,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,KAAK,CAAC,EAAE,SAAS,CAAC;IAClB,iBAAiB,CAAC,EAAE,OAAO,CAAC;CAC7B;AAED,MAAM,WAAW,gBAAgB;IAC/B,EAAE,EAAE,OAAO,CAAC;IACZ,QAAQ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