@push.rocks/smartregistry 2.2.3 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/dist_ts/00_commitinfo_data.js +1 -1
  2. package/dist_ts/cargo/classes.cargoregistry.d.ts +7 -1
  3. package/dist_ts/cargo/classes.cargoregistry.js +42 -4
  4. package/dist_ts/cargo/classes.cargoupstream.d.ts +44 -0
  5. package/dist_ts/cargo/classes.cargoupstream.js +129 -0
  6. package/dist_ts/cargo/index.d.ts +1 -0
  7. package/dist_ts/cargo/index.js +2 -1
  8. package/dist_ts/classes.smartregistry.d.ts +33 -2
  9. package/dist_ts/classes.smartregistry.js +45 -12
  10. package/dist_ts/composer/classes.composerregistry.d.ts +7 -1
  11. package/dist_ts/composer/classes.composerregistry.js +34 -3
  12. package/dist_ts/composer/classes.composerupstream.d.ts +40 -0
  13. package/dist_ts/composer/classes.composerupstream.js +159 -0
  14. package/dist_ts/composer/index.d.ts +1 -0
  15. package/dist_ts/composer/index.js +2 -1
  16. package/dist_ts/core/classes.authmanager.d.ts +30 -80
  17. package/dist_ts/core/classes.authmanager.js +63 -337
  18. package/dist_ts/core/classes.defaultauthprovider.d.ts +78 -0
  19. package/dist_ts/core/classes.defaultauthprovider.js +311 -0
  20. package/dist_ts/core/classes.registrystorage.d.ts +70 -4
  21. package/dist_ts/core/classes.registrystorage.js +165 -5
  22. package/dist_ts/core/index.d.ts +3 -0
  23. package/dist_ts/core/index.js +7 -2
  24. package/dist_ts/core/interfaces.auth.d.ts +83 -0
  25. package/dist_ts/core/interfaces.auth.js +2 -0
  26. package/dist_ts/core/interfaces.core.d.ts +38 -0
  27. package/dist_ts/core/interfaces.storage.d.ts +120 -0
  28. package/dist_ts/core/interfaces.storage.js +2 -0
  29. package/dist_ts/index.d.ts +1 -0
  30. package/dist_ts/index.js +3 -1
  31. package/dist_ts/maven/classes.mavenregistry.d.ts +12 -1
  32. package/dist_ts/maven/classes.mavenregistry.js +69 -4
  33. package/dist_ts/maven/classes.mavenupstream.d.ts +45 -0
  34. package/dist_ts/maven/classes.mavenupstream.js +153 -0
  35. package/dist_ts/maven/index.d.ts +1 -0
  36. package/dist_ts/maven/index.js +2 -1
  37. package/dist_ts/npm/classes.npmregistry.d.ts +3 -1
  38. package/dist_ts/npm/classes.npmregistry.js +55 -6
  39. package/dist_ts/npm/classes.npmupstream.d.ts +51 -0
  40. package/dist_ts/npm/classes.npmupstream.js +206 -0
  41. package/dist_ts/npm/index.d.ts +1 -0
  42. package/dist_ts/npm/index.js +2 -1
  43. package/dist_ts/oci/classes.ociregistry.d.ts +4 -1
  44. package/dist_ts/oci/classes.ociregistry.js +78 -17
  45. package/dist_ts/oci/classes.ociupstream.d.ts +62 -0
  46. package/dist_ts/oci/classes.ociupstream.js +206 -0
  47. package/dist_ts/oci/index.d.ts +1 -0
  48. package/dist_ts/oci/index.js +2 -1
  49. package/dist_ts/plugins.d.ts +4 -1
  50. package/dist_ts/plugins.js +6 -2
  51. package/dist_ts/pypi/classes.pypiregistry.d.ts +7 -1
  52. package/dist_ts/pypi/classes.pypiregistry.js +60 -4
  53. package/dist_ts/pypi/classes.pypiupstream.d.ts +48 -0
  54. package/dist_ts/pypi/classes.pypiupstream.js +165 -0
  55. package/dist_ts/pypi/index.d.ts +1 -0
  56. package/dist_ts/pypi/index.js +2 -1
  57. package/dist_ts/rubygems/classes.rubygemsregistry.d.ts +7 -1
  58. package/dist_ts/rubygems/classes.rubygemsregistry.js +35 -4
  59. package/dist_ts/rubygems/classes.rubygemsupstream.d.ts +47 -0
  60. package/dist_ts/rubygems/classes.rubygemsupstream.js +184 -0
  61. package/dist_ts/rubygems/index.d.ts +1 -0
  62. package/dist_ts/rubygems/index.js +2 -1
  63. package/dist_ts/upstream/classes.baseupstream.d.ts +112 -0
  64. package/dist_ts/upstream/classes.baseupstream.js +411 -0
  65. package/dist_ts/upstream/classes.circuitbreaker.d.ts +111 -0
  66. package/dist_ts/upstream/classes.circuitbreaker.js +192 -0
  67. package/dist_ts/upstream/classes.upstreamcache.d.ts +170 -0
  68. package/dist_ts/upstream/classes.upstreamcache.js +485 -0
  69. package/dist_ts/upstream/index.d.ts +6 -0
  70. package/dist_ts/upstream/index.js +7 -0
  71. package/dist_ts/upstream/interfaces.upstream.d.ts +169 -0
  72. package/dist_ts/upstream/interfaces.upstream.js +23 -0
  73. package/package.json +4 -2
  74. package/ts/00_commitinfo_data.ts +1 -1
  75. package/ts/cargo/classes.cargoregistry.ts +48 -3
  76. package/ts/cargo/classes.cargoupstream.ts +159 -0
  77. package/ts/cargo/index.ts +1 -0
  78. package/ts/classes.smartregistry.ts +88 -11
  79. package/ts/composer/classes.composerregistry.ts +39 -2
  80. package/ts/composer/classes.composerupstream.ts +200 -0
  81. package/ts/composer/index.ts +1 -0
  82. package/ts/core/classes.authmanager.ts +74 -412
  83. package/ts/core/classes.defaultauthprovider.ts +393 -0
  84. package/ts/core/classes.registrystorage.ts +199 -5
  85. package/ts/core/index.ts +8 -1
  86. package/ts/core/interfaces.auth.ts +91 -0
  87. package/ts/core/interfaces.core.ts +42 -0
  88. package/ts/core/interfaces.storage.ts +130 -0
  89. package/ts/index.ts +3 -0
  90. package/ts/maven/classes.mavenregistry.ts +84 -3
  91. package/ts/maven/classes.mavenupstream.ts +220 -0
  92. package/ts/maven/index.ts +1 -0
  93. package/ts/npm/classes.npmregistry.ts +61 -5
  94. package/ts/npm/classes.npmupstream.ts +260 -0
  95. package/ts/npm/index.ts +1 -0
  96. package/ts/oci/classes.ociregistry.ts +89 -17
  97. package/ts/oci/classes.ociupstream.ts +263 -0
  98. package/ts/oci/index.ts +1 -0
  99. package/ts/plugins.ts +7 -1
  100. package/ts/pypi/classes.pypiregistry.ts +68 -3
  101. package/ts/pypi/classes.pypiupstream.ts +211 -0
  102. package/ts/pypi/index.ts +1 -0
  103. package/ts/rubygems/classes.rubygemsregistry.ts +40 -3
  104. package/ts/rubygems/classes.rubygemsupstream.ts +230 -0
  105. package/ts/rubygems/index.ts +1 -0
  106. package/ts/upstream/classes.baseupstream.ts +526 -0
  107. package/ts/upstream/classes.circuitbreaker.ts +238 -0
  108. package/ts/upstream/classes.upstreamcache.ts +626 -0
  109. package/ts/upstream/index.ts +11 -0
  110. package/ts/upstream/interfaces.upstream.ts +195 -0
@@ -0,0 +1,238 @@
1
+ import type { TCircuitState, IUpstreamResilienceConfig } from './interfaces.upstream.js';
2
+ import { DEFAULT_RESILIENCE_CONFIG } from './interfaces.upstream.js';
3
+
4
+ /**
5
+ * Circuit breaker implementation for upstream resilience.
6
+ *
7
+ * States:
8
+ * - CLOSED: Normal operation, requests pass through
9
+ * - OPEN: Circuit is tripped, requests fail fast
10
+ * - HALF_OPEN: Testing if upstream has recovered
11
+ *
12
+ * Transitions:
13
+ * - CLOSED → OPEN: When failure count exceeds threshold
14
+ * - OPEN → HALF_OPEN: After reset timeout expires
15
+ * - HALF_OPEN → CLOSED: On successful request
16
+ * - HALF_OPEN → OPEN: On failed request
17
+ */
18
+ export class CircuitBreaker {
19
+ /** Unique identifier for logging and metrics */
20
+ public readonly id: string;
21
+
22
+ /** Current circuit state */
23
+ private state: TCircuitState = 'CLOSED';
24
+
25
+ /** Count of consecutive failures */
26
+ private failureCount: number = 0;
27
+
28
+ /** Timestamp when circuit was opened */
29
+ private openedAt: number = 0;
30
+
31
+ /** Number of successful requests in half-open state */
32
+ private halfOpenSuccesses: number = 0;
33
+
34
+ /** Configuration */
35
+ private readonly config: IUpstreamResilienceConfig;
36
+
37
+ /** Number of successes required to close circuit from half-open */
38
+ private readonly halfOpenThreshold: number = 2;
39
+
40
+ constructor(id: string, config?: Partial<IUpstreamResilienceConfig>) {
41
+ this.id = id;
42
+ this.config = { ...DEFAULT_RESILIENCE_CONFIG, ...config };
43
+ }
44
+
45
+ /**
46
+ * Get current circuit state.
47
+ */
48
+ public getState(): TCircuitState {
49
+ // Check if we should transition from OPEN to HALF_OPEN
50
+ if (this.state === 'OPEN') {
51
+ const elapsed = Date.now() - this.openedAt;
52
+ if (elapsed >= this.config.circuitBreakerResetMs) {
53
+ this.transitionTo('HALF_OPEN');
54
+ }
55
+ }
56
+ return this.state;
57
+ }
58
+
59
+ /**
60
+ * Check if circuit allows requests.
61
+ * Returns true if requests should be allowed.
62
+ */
63
+ public canRequest(): boolean {
64
+ const currentState = this.getState();
65
+ return currentState !== 'OPEN';
66
+ }
67
+
68
+ /**
69
+ * Record a successful request.
70
+ * May transition circuit from HALF_OPEN to CLOSED.
71
+ */
72
+ public recordSuccess(): void {
73
+ if (this.state === 'HALF_OPEN') {
74
+ this.halfOpenSuccesses++;
75
+ if (this.halfOpenSuccesses >= this.halfOpenThreshold) {
76
+ this.transitionTo('CLOSED');
77
+ }
78
+ } else if (this.state === 'CLOSED') {
79
+ // Reset failure count on success
80
+ this.failureCount = 0;
81
+ }
82
+ }
83
+
84
+ /**
85
+ * Record a failed request.
86
+ * May transition circuit from CLOSED/HALF_OPEN to OPEN.
87
+ */
88
+ public recordFailure(): void {
89
+ if (this.state === 'HALF_OPEN') {
90
+ // Any failure in half-open immediately opens circuit
91
+ this.transitionTo('OPEN');
92
+ } else if (this.state === 'CLOSED') {
93
+ this.failureCount++;
94
+ if (this.failureCount >= this.config.circuitBreakerThreshold) {
95
+ this.transitionTo('OPEN');
96
+ }
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Force circuit to open state.
102
+ * Useful for manual intervention or external health checks.
103
+ */
104
+ public forceOpen(): void {
105
+ this.transitionTo('OPEN');
106
+ }
107
+
108
+ /**
109
+ * Force circuit to closed state.
110
+ * Useful for manual intervention after fixing upstream issues.
111
+ */
112
+ public forceClose(): void {
113
+ this.transitionTo('CLOSED');
114
+ }
115
+
116
+ /**
117
+ * Reset circuit to initial state.
118
+ */
119
+ public reset(): void {
120
+ this.state = 'CLOSED';
121
+ this.failureCount = 0;
122
+ this.openedAt = 0;
123
+ this.halfOpenSuccesses = 0;
124
+ }
125
+
126
+ /**
127
+ * Get circuit metrics for monitoring.
128
+ */
129
+ public getMetrics(): ICircuitBreakerMetrics {
130
+ return {
131
+ id: this.id,
132
+ state: this.getState(),
133
+ failureCount: this.failureCount,
134
+ openedAt: this.openedAt > 0 ? new Date(this.openedAt) : null,
135
+ timeUntilHalfOpen: this.state === 'OPEN'
136
+ ? Math.max(0, this.config.circuitBreakerResetMs - (Date.now() - this.openedAt))
137
+ : 0,
138
+ halfOpenSuccesses: this.halfOpenSuccesses,
139
+ threshold: this.config.circuitBreakerThreshold,
140
+ resetMs: this.config.circuitBreakerResetMs,
141
+ };
142
+ }
143
+
144
+ /**
145
+ * Transition to a new state with proper cleanup.
146
+ */
147
+ private transitionTo(newState: TCircuitState): void {
148
+ const previousState = this.state;
149
+ this.state = newState;
150
+
151
+ switch (newState) {
152
+ case 'OPEN':
153
+ this.openedAt = Date.now();
154
+ this.halfOpenSuccesses = 0;
155
+ break;
156
+ case 'HALF_OPEN':
157
+ this.halfOpenSuccesses = 0;
158
+ break;
159
+ case 'CLOSED':
160
+ this.failureCount = 0;
161
+ this.openedAt = 0;
162
+ this.halfOpenSuccesses = 0;
163
+ break;
164
+ }
165
+
166
+ // Log state transition (useful for debugging and monitoring)
167
+ // In production, this would emit events or metrics
168
+ if (previousState !== newState) {
169
+ // State changed - could emit event here
170
+ }
171
+ }
172
+ }
173
+
174
+ /**
175
+ * Metrics for circuit breaker monitoring.
176
+ */
177
+ export interface ICircuitBreakerMetrics {
178
+ /** Circuit breaker identifier */
179
+ id: string;
180
+ /** Current state */
181
+ state: TCircuitState;
182
+ /** Number of consecutive failures */
183
+ failureCount: number;
184
+ /** When circuit was opened (null if never opened) */
185
+ openedAt: Date | null;
186
+ /** Milliseconds until circuit transitions to half-open (0 if not open) */
187
+ timeUntilHalfOpen: number;
188
+ /** Number of successes in half-open state */
189
+ halfOpenSuccesses: number;
190
+ /** Failure threshold for opening circuit */
191
+ threshold: number;
192
+ /** Reset timeout in milliseconds */
193
+ resetMs: number;
194
+ }
195
+
196
+ /**
197
+ * Execute a function with circuit breaker protection.
198
+ *
199
+ * @param breaker The circuit breaker to use
200
+ * @param fn The async function to execute
201
+ * @param fallback Optional fallback function when circuit is open
202
+ * @returns The result of fn or fallback
203
+ * @throws CircuitOpenError if circuit is open and no fallback provided
204
+ */
205
+ export async function withCircuitBreaker<T>(
206
+ breaker: CircuitBreaker,
207
+ fn: () => Promise<T>,
208
+ fallback?: () => Promise<T>,
209
+ ): Promise<T> {
210
+ if (!breaker.canRequest()) {
211
+ if (fallback) {
212
+ return fallback();
213
+ }
214
+ throw new CircuitOpenError(breaker.id);
215
+ }
216
+
217
+ try {
218
+ const result = await fn();
219
+ breaker.recordSuccess();
220
+ return result;
221
+ } catch (error) {
222
+ breaker.recordFailure();
223
+ throw error;
224
+ }
225
+ }
226
+
227
+ /**
228
+ * Error thrown when circuit is open and no fallback is provided.
229
+ */
230
+ export class CircuitOpenError extends Error {
231
+ public readonly circuitId: string;
232
+
233
+ constructor(circuitId: string) {
234
+ super(`Circuit breaker '${circuitId}' is open`);
235
+ this.name = 'CircuitOpenError';
236
+ this.circuitId = circuitId;
237
+ }
238
+ }