@naman_deep_singh/communication-core 1.0.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.
- package/README.md +345 -0
- package/dist/cjs/abstract/BaseCircuitBreaker.js +253 -0
- package/dist/cjs/abstract/BaseClient.js +298 -0
- package/dist/cjs/abstract/BaseCompressionManager.js +377 -0
- package/dist/cjs/abstract/BaseConnectionPool.js +543 -0
- package/dist/cjs/abstract/BaseInterceptor.js +235 -0
- package/dist/cjs/abstract/BaseLoadBalancer.js +269 -0
- package/dist/cjs/abstract/BaseProtocol.js +269 -0
- package/dist/cjs/abstract/BaseRetryStrategy.js +255 -0
- package/dist/cjs/abstract/BaseSerializer.js +341 -0
- package/dist/cjs/abstract/BaseServiceDiscoverer.js +254 -0
- package/dist/cjs/abstract/BaseTimeoutManager.js +295 -0
- package/dist/cjs/abstract/index.js +25 -0
- package/dist/cjs/errors/CircuitBreakerError.js +16 -0
- package/dist/cjs/errors/CommunicationError.js +15 -0
- package/dist/cjs/errors/ConnectionError.js +15 -0
- package/dist/cjs/errors/DiscoveryError.js +15 -0
- package/dist/cjs/errors/LoadBalancerError.js +15 -0
- package/dist/cjs/errors/ProtocolError.js +15 -0
- package/dist/cjs/errors/RetryError.js +16 -0
- package/dist/cjs/errors/SerializationError.js +15 -0
- package/dist/cjs/errors/ServiceUnavailableError.js +15 -0
- package/dist/cjs/errors/TimeoutError.js +16 -0
- package/dist/cjs/errors/communicationErrorCodes.js +35 -0
- package/dist/cjs/errors/index.js +31 -0
- package/dist/cjs/index.js +38 -0
- package/dist/cjs/interfaces/CircuitBreaker.interface.js +6 -0
- package/dist/cjs/interfaces/Client.interface.js +6 -0
- package/dist/cjs/interfaces/Compression.interface.js +6 -0
- package/dist/cjs/interfaces/ConnectionPool.interface.js +6 -0
- package/dist/cjs/interfaces/Interceptor.interface.js +6 -0
- package/dist/cjs/interfaces/LoadBalancer.interface.js +2 -0
- package/dist/cjs/interfaces/Protocol.interface.js +6 -0
- package/dist/cjs/interfaces/RetryStrategy.interface.js +6 -0
- package/dist/cjs/interfaces/Serializer.interface.js +2 -0
- package/dist/cjs/interfaces/ServiceDiscovery.interface.js +6 -0
- package/dist/cjs/interfaces/Timeout.interface.js +6 -0
- package/dist/cjs/interfaces/index.js +6 -0
- package/dist/cjs/types/config.js +6 -0
- package/dist/cjs/types/events.js +6 -0
- package/dist/cjs/types/index.js +6 -0
- package/dist/cjs/types/request.js +6 -0
- package/dist/cjs/types/response.js +6 -0
- package/dist/cjs/types/service.js +6 -0
- package/dist/cjs/utils.js +200 -0
- package/dist/esm/abstract/BaseCircuitBreaker.js +249 -0
- package/dist/esm/abstract/BaseClient.js +294 -0
- package/dist/esm/abstract/BaseCompressionManager.js +373 -0
- package/dist/esm/abstract/BaseConnectionPool.js +539 -0
- package/dist/esm/abstract/BaseInterceptor.js +231 -0
- package/dist/esm/abstract/BaseLoadBalancer.js +265 -0
- package/dist/esm/abstract/BaseProtocol.js +265 -0
- package/dist/esm/abstract/BaseRetryStrategy.js +251 -0
- package/dist/esm/abstract/BaseSerializer.js +337 -0
- package/dist/esm/abstract/BaseServiceDiscoverer.js +250 -0
- package/dist/esm/abstract/BaseTimeoutManager.js +291 -0
- package/dist/esm/abstract/index.js +11 -0
- package/dist/esm/errors/CircuitBreakerError.js +12 -0
- package/dist/esm/errors/CommunicationError.js +11 -0
- package/dist/esm/errors/ConnectionError.js +11 -0
- package/dist/esm/errors/DiscoveryError.js +11 -0
- package/dist/esm/errors/LoadBalancerError.js +11 -0
- package/dist/esm/errors/ProtocolError.js +11 -0
- package/dist/esm/errors/RetryError.js +12 -0
- package/dist/esm/errors/SerializationError.js +11 -0
- package/dist/esm/errors/ServiceUnavailableError.js +11 -0
- package/dist/esm/errors/TimeoutError.js +12 -0
- package/dist/esm/errors/communicationErrorCodes.js +32 -0
- package/dist/esm/errors/index.js +17 -0
- package/dist/esm/index.js +18 -0
- package/dist/esm/interfaces/CircuitBreaker.interface.js +5 -0
- package/dist/esm/interfaces/Client.interface.js +5 -0
- package/dist/esm/interfaces/Compression.interface.js +5 -0
- package/dist/esm/interfaces/ConnectionPool.interface.js +5 -0
- package/dist/esm/interfaces/Interceptor.interface.js +5 -0
- package/dist/esm/interfaces/LoadBalancer.interface.js +1 -0
- package/dist/esm/interfaces/Protocol.interface.js +5 -0
- package/dist/esm/interfaces/RetryStrategy.interface.js +5 -0
- package/dist/esm/interfaces/Serializer.interface.js +1 -0
- package/dist/esm/interfaces/ServiceDiscovery.interface.js +5 -0
- package/dist/esm/interfaces/Timeout.interface.js +5 -0
- package/dist/esm/interfaces/index.js +5 -0
- package/dist/esm/types/config.js +5 -0
- package/dist/esm/types/events.js +5 -0
- package/dist/esm/types/index.js +5 -0
- package/dist/esm/types/request.js +5 -0
- package/dist/esm/types/response.js +5 -0
- package/dist/esm/types/service.js +5 -0
- package/dist/esm/utils.js +193 -0
- package/dist/types/abstract/BaseCircuitBreaker.d.ts +167 -0
- package/dist/types/abstract/BaseClient.d.ts +197 -0
- package/dist/types/abstract/BaseCompressionManager.d.ts +180 -0
- package/dist/types/abstract/BaseConnectionPool.d.ts +210 -0
- package/dist/types/abstract/BaseInterceptor.d.ts +150 -0
- package/dist/types/abstract/BaseLoadBalancer.d.ts +167 -0
- package/dist/types/abstract/BaseProtocol.d.ts +163 -0
- package/dist/types/abstract/BaseRetryStrategy.d.ts +130 -0
- package/dist/types/abstract/BaseSerializer.d.ts +181 -0
- package/dist/types/abstract/BaseServiceDiscoverer.d.ts +161 -0
- package/dist/types/abstract/BaseTimeoutManager.d.ts +145 -0
- package/dist/types/abstract/index.d.ts +11 -0
- package/dist/types/errors/CircuitBreakerError.d.ts +8 -0
- package/dist/types/errors/CommunicationError.d.ts +10 -0
- package/dist/types/errors/ConnectionError.d.ts +9 -0
- package/dist/types/errors/DiscoveryError.d.ts +9 -0
- package/dist/types/errors/LoadBalancerError.d.ts +9 -0
- package/dist/types/errors/ProtocolError.d.ts +9 -0
- package/dist/types/errors/RetryError.d.ts +11 -0
- package/dist/types/errors/SerializationError.d.ts +9 -0
- package/dist/types/errors/ServiceUnavailableError.d.ts +12 -0
- package/dist/types/errors/TimeoutError.d.ts +11 -0
- package/dist/types/errors/communicationErrorCodes.d.ts +27 -0
- package/dist/types/errors/index.d.ts +11 -0
- package/dist/types/index.d.ts +13 -0
- package/dist/types/interfaces/CircuitBreaker.interface.d.ts +150 -0
- package/dist/types/interfaces/Client.interface.d.ts +153 -0
- package/dist/types/interfaces/Compression.interface.d.ts +190 -0
- package/dist/types/interfaces/ConnectionPool.interface.d.ts +191 -0
- package/dist/types/interfaces/Interceptor.interface.d.ts +220 -0
- package/dist/types/interfaces/LoadBalancer.interface.d.ts +153 -0
- package/dist/types/interfaces/Protocol.interface.d.ts +117 -0
- package/dist/types/interfaces/RetryStrategy.interface.d.ts +160 -0
- package/dist/types/interfaces/Serializer.interface.d.ts +176 -0
- package/dist/types/interfaces/ServiceDiscovery.interface.d.ts +189 -0
- package/dist/types/interfaces/Timeout.interface.d.ts +135 -0
- package/dist/types/interfaces/index.d.ts +15 -0
- package/dist/types/types/config.d.ts +540 -0
- package/dist/types/types/events.d.ts +204 -0
- package/dist/types/types/index.d.ts +9 -0
- package/dist/types/types/request.d.ts +143 -0
- package/dist/types/types/response.d.ts +155 -0
- package/dist/types/types/service.d.ts +279 -0
- package/dist/types/utils.d.ts +179 -0
- package/package.json +88 -0
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract base timeout manager implementation
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import { TimeoutError } from '@naman_deep_singh/utils';
|
|
6
|
+
import { CommunicationError } from '../errors/CommunicationError.js';
|
|
7
|
+
/**
|
|
8
|
+
* Abstract base timeout manager implementation
|
|
9
|
+
* Provides common functionality for timeout management
|
|
10
|
+
*/
|
|
11
|
+
export class BaseTimeoutManager {
|
|
12
|
+
/**
|
|
13
|
+
* Create a new base timeout manager instance
|
|
14
|
+
* @param name Timeout manager name
|
|
15
|
+
* @param config Timeout configuration
|
|
16
|
+
*/
|
|
17
|
+
constructor(name, config) {
|
|
18
|
+
/** Operation-specific timeouts */
|
|
19
|
+
this.operationTimeouts = new Map();
|
|
20
|
+
/** Timeout statistics */
|
|
21
|
+
this.stats = {
|
|
22
|
+
totalOperations: 0,
|
|
23
|
+
timedOutOperations: 0,
|
|
24
|
+
totalOperationTime: 0,
|
|
25
|
+
averageOperationTime: 0,
|
|
26
|
+
timeoutRate: 0,
|
|
27
|
+
perOperationStats: new Map(),
|
|
28
|
+
};
|
|
29
|
+
this.name = name;
|
|
30
|
+
this.config = { ...config };
|
|
31
|
+
this.initialize();
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Initialize timeout manager
|
|
35
|
+
*/
|
|
36
|
+
initialize() {
|
|
37
|
+
// Can be overridden by subclasses
|
|
38
|
+
// Set up default operation timeouts
|
|
39
|
+
if (this.config.global) {
|
|
40
|
+
this.operationTimeouts.set('global', this.config.global);
|
|
41
|
+
}
|
|
42
|
+
if (this.config.connection) {
|
|
43
|
+
this.operationTimeouts.set('connection', this.config.connection);
|
|
44
|
+
}
|
|
45
|
+
if (this.config.read) {
|
|
46
|
+
this.operationTimeouts.set('read', this.config.read);
|
|
47
|
+
}
|
|
48
|
+
if (this.config.write) {
|
|
49
|
+
this.operationTimeouts.set('write', this.config.write);
|
|
50
|
+
}
|
|
51
|
+
if (this.config.request) {
|
|
52
|
+
this.operationTimeouts.set('request', this.config.request);
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Execute a function with timeout
|
|
57
|
+
* @param fn Function to execute
|
|
58
|
+
* @param options Timeout options
|
|
59
|
+
* @returns Promise resolving to function result
|
|
60
|
+
* @throws {CommunicationError} If timeout occurs
|
|
61
|
+
*/
|
|
62
|
+
async execute(fn, options) {
|
|
63
|
+
const operationName = options?.name || 'anonymous';
|
|
64
|
+
const timeoutMs = options?.timeout || this.getOperationTimeout(operationName) || this.config.global || 30000;
|
|
65
|
+
const throwOnTimeout = options?.throwOnTimeout !== false;
|
|
66
|
+
const startTime = Date.now();
|
|
67
|
+
// Create timeout promise
|
|
68
|
+
const timeoutPromise = this.createTimeout(timeoutMs, options?.errorMessage || `Operation '${operationName}' timed out after ${timeoutMs}ms`);
|
|
69
|
+
// Create cleanup function
|
|
70
|
+
const cleanup = options?.cleanup;
|
|
71
|
+
try {
|
|
72
|
+
// Race between function and timeout
|
|
73
|
+
const result = await Promise.race([
|
|
74
|
+
fn(),
|
|
75
|
+
timeoutPromise,
|
|
76
|
+
]);
|
|
77
|
+
const duration = Date.now() - startTime;
|
|
78
|
+
this.recordSuccess(operationName, duration);
|
|
79
|
+
return result;
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
const duration = Date.now() - startTime;
|
|
83
|
+
// Check if it's a timeout error
|
|
84
|
+
if (error instanceof TimeoutError || error instanceof CommunicationError) {
|
|
85
|
+
this.recordTimeout(operationName, duration);
|
|
86
|
+
// Call cleanup if provided
|
|
87
|
+
if (cleanup) {
|
|
88
|
+
try {
|
|
89
|
+
await cleanup();
|
|
90
|
+
}
|
|
91
|
+
catch (cleanupError) {
|
|
92
|
+
// Ignore cleanup errors
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
if (throwOnTimeout) {
|
|
96
|
+
throw error;
|
|
97
|
+
}
|
|
98
|
+
// If not throwing, return default value
|
|
99
|
+
return undefined;
|
|
100
|
+
}
|
|
101
|
+
// Record failure but not timeout
|
|
102
|
+
this.recordFailure(operationName, duration, error);
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Create a timeout promise
|
|
108
|
+
* @param timeout Timeout in milliseconds
|
|
109
|
+
* @param message Error message
|
|
110
|
+
* @returns Promise that rejects after timeout
|
|
111
|
+
*/
|
|
112
|
+
createTimeout(timeout, message) {
|
|
113
|
+
return new Promise((_, reject) => {
|
|
114
|
+
const timer = setTimeout(() => {
|
|
115
|
+
clearTimeout(timer);
|
|
116
|
+
reject(new TimeoutError(message || `Operation timed out after ${timeout}ms`, timeout));
|
|
117
|
+
}, timeout);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Create an abort signal for timeout
|
|
122
|
+
* @param timeout Timeout in milliseconds
|
|
123
|
+
* @returns AbortSignal
|
|
124
|
+
*/
|
|
125
|
+
createAbortSignal(timeout) {
|
|
126
|
+
const controller = new AbortController();
|
|
127
|
+
setTimeout(() => controller.abort(), timeout);
|
|
128
|
+
return controller.signal;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Execute multiple promises with timeout
|
|
132
|
+
* @param promises Array of promises
|
|
133
|
+
* @param timeout Timeout in milliseconds
|
|
134
|
+
* @returns Promise that resolves when first promise resolves or times out
|
|
135
|
+
*/
|
|
136
|
+
async race(promises, timeout) {
|
|
137
|
+
const timeoutPromise = this.createTimeout(timeout);
|
|
138
|
+
return Promise.race([...promises, timeoutPromise]);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Delay execution for specified time
|
|
142
|
+
* @param delay Delay in milliseconds
|
|
143
|
+
* @returns Promise that resolves after delay
|
|
144
|
+
*/
|
|
145
|
+
delay(delay) {
|
|
146
|
+
return new Promise(resolve => setTimeout(resolve, delay));
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Set operation timeout
|
|
150
|
+
* @param operation Operation identifier
|
|
151
|
+
* @param timeout Timeout in milliseconds
|
|
152
|
+
*/
|
|
153
|
+
setOperationTimeout(operation, timeout) {
|
|
154
|
+
this.operationTimeouts.set(operation, timeout);
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Get operation timeout
|
|
158
|
+
* @param operation Operation identifier
|
|
159
|
+
* @returns Timeout in milliseconds or undefined
|
|
160
|
+
*/
|
|
161
|
+
getOperationTimeout(operation) {
|
|
162
|
+
// Check operation-specific timeout
|
|
163
|
+
const operationTimeout = this.operationTimeouts.get(operation);
|
|
164
|
+
if (operationTimeout !== undefined) {
|
|
165
|
+
return operationTimeout;
|
|
166
|
+
}
|
|
167
|
+
// Fall back to global timeout
|
|
168
|
+
return this.config.global;
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Record successful operation
|
|
172
|
+
* @param operation Operation name
|
|
173
|
+
* @param duration Duration in milliseconds
|
|
174
|
+
*/
|
|
175
|
+
recordSuccess(operation, duration) {
|
|
176
|
+
this.stats.totalOperations++;
|
|
177
|
+
this.stats.totalOperationTime += duration;
|
|
178
|
+
this.stats.averageOperationTime = this.stats.totalOperationTime / this.stats.totalOperations;
|
|
179
|
+
// Update per-operation stats
|
|
180
|
+
this.updateOperationStats(operation, duration, false);
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Record timed out operation
|
|
184
|
+
* @param operation Operation name
|
|
185
|
+
* @param duration Duration in milliseconds
|
|
186
|
+
*/
|
|
187
|
+
recordTimeout(operation, duration) {
|
|
188
|
+
this.stats.totalOperations++;
|
|
189
|
+
this.stats.timedOutOperations++;
|
|
190
|
+
this.stats.totalOperationTime += duration;
|
|
191
|
+
this.stats.averageOperationTime = this.stats.totalOperationTime / this.stats.totalOperations;
|
|
192
|
+
this.stats.timeoutRate = this.stats.timedOutOperations / this.stats.totalOperations;
|
|
193
|
+
// Update per-operation stats
|
|
194
|
+
this.updateOperationStats(operation, duration, true);
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Record failed operation (not timeout)
|
|
198
|
+
* @param operation Operation name
|
|
199
|
+
* @param duration Duration in milliseconds
|
|
200
|
+
* @param error Error that occurred
|
|
201
|
+
*/
|
|
202
|
+
recordFailure(operation, duration, error) {
|
|
203
|
+
this.stats.totalOperations++;
|
|
204
|
+
this.stats.totalOperationTime += duration;
|
|
205
|
+
this.stats.averageOperationTime = this.stats.totalOperationTime / this.stats.totalOperations;
|
|
206
|
+
// Update per-operation stats
|
|
207
|
+
this.updateOperationStats(operation, duration, false);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Update operation-specific statistics
|
|
211
|
+
* @param operation Operation name
|
|
212
|
+
* @param duration Duration in milliseconds
|
|
213
|
+
* @param isTimeout Whether operation timed out
|
|
214
|
+
*/
|
|
215
|
+
updateOperationStats(operation, duration, isTimeout) {
|
|
216
|
+
let opStats = this.stats.perOperationStats.get(operation);
|
|
217
|
+
if (!opStats) {
|
|
218
|
+
opStats = {
|
|
219
|
+
calls: 0,
|
|
220
|
+
timeouts: 0,
|
|
221
|
+
totalTime: 0,
|
|
222
|
+
averageTime: 0,
|
|
223
|
+
};
|
|
224
|
+
this.stats.perOperationStats.set(operation, opStats);
|
|
225
|
+
}
|
|
226
|
+
opStats.calls++;
|
|
227
|
+
if (isTimeout) {
|
|
228
|
+
opStats.timeouts++;
|
|
229
|
+
}
|
|
230
|
+
opStats.totalTime += duration;
|
|
231
|
+
opStats.averageTime = opStats.totalTime / opStats.calls;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Update timeout configuration
|
|
235
|
+
* @param config New configuration
|
|
236
|
+
*/
|
|
237
|
+
updateConfig(config) {
|
|
238
|
+
this.config = { ...this.config, ...config };
|
|
239
|
+
// Re-initialize operation timeouts
|
|
240
|
+
this.initialize();
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Get timeout statistics
|
|
244
|
+
*/
|
|
245
|
+
getStats() {
|
|
246
|
+
const perOperationStats = {};
|
|
247
|
+
for (const [operation, stats] of this.stats.perOperationStats) {
|
|
248
|
+
perOperationStats[operation] = {
|
|
249
|
+
calls: stats.calls,
|
|
250
|
+
timeouts: stats.timeouts,
|
|
251
|
+
averageTime: stats.averageTime,
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
return {
|
|
255
|
+
totalOperations: this.stats.totalOperations,
|
|
256
|
+
timedOutOperations: this.stats.timedOutOperations,
|
|
257
|
+
averageOperationTime: this.stats.averageOperationTime,
|
|
258
|
+
timeoutRate: this.stats.timeoutRate,
|
|
259
|
+
perOperationStats,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
/**
|
|
263
|
+
* Reset timeout statistics
|
|
264
|
+
*/
|
|
265
|
+
resetStats() {
|
|
266
|
+
this.stats = {
|
|
267
|
+
totalOperations: 0,
|
|
268
|
+
timedOutOperations: 0,
|
|
269
|
+
totalOperationTime: 0,
|
|
270
|
+
averageOperationTime: 0,
|
|
271
|
+
timeoutRate: 0,
|
|
272
|
+
perOperationStats: new Map(),
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Health check for timeout manager
|
|
277
|
+
*/
|
|
278
|
+
healthCheck() {
|
|
279
|
+
const healthy = true; // Timeout manager is always healthy
|
|
280
|
+
return {
|
|
281
|
+
healthy,
|
|
282
|
+
message: 'Timeout manager is operational',
|
|
283
|
+
details: {
|
|
284
|
+
name: this.name,
|
|
285
|
+
config: this.config,
|
|
286
|
+
operationTimeouts: Object.fromEntries(this.operationTimeouts),
|
|
287
|
+
statistics: this.getStats(),
|
|
288
|
+
},
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export { BaseProtocol } from './BaseProtocol.js';
|
|
2
|
+
export { BaseClient } from './BaseClient.js';
|
|
3
|
+
export { BaseCircuitBreaker } from './BaseCircuitBreaker.js';
|
|
4
|
+
export { BaseRetryStrategy } from './BaseRetryStrategy.js';
|
|
5
|
+
export { BaseLoadBalancer } from './BaseLoadBalancer.js';
|
|
6
|
+
export { BaseSerializer } from './BaseSerializer.js';
|
|
7
|
+
export { BaseServiceDiscoverer } from './BaseServiceDiscoverer.js';
|
|
8
|
+
export { BaseInterceptor } from './BaseInterceptor.js';
|
|
9
|
+
export { BaseTimeoutManager } from './BaseTimeoutManager.js';
|
|
10
|
+
export { BaseConnectionPool } from './BaseConnectionPool.js';
|
|
11
|
+
export { BaseCompressionManager } from './BaseCompressionManager.js';
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CommunicationError } from "./CommunicationError.js";
|
|
2
|
+
import { COMMUNICATION_ERROR_CODES } from "./communicationErrorCodes.js";
|
|
3
|
+
/**
|
|
4
|
+
* Circuit breaker error - when circuit is open
|
|
5
|
+
*/
|
|
6
|
+
export class CircuitBreakerError extends CommunicationError {
|
|
7
|
+
constructor(details, cause) {
|
|
8
|
+
super(COMMUNICATION_ERROR_CODES.CIRCUIT_BREAKER_OPEN, 503, // Service Unavailable
|
|
9
|
+
details, cause);
|
|
10
|
+
this.name = 'CircuitBreakerError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { AppError } from "@naman_deep_singh/errors";
|
|
2
|
+
/**
|
|
3
|
+
* Base communication error class
|
|
4
|
+
* Extends AppError from errors package
|
|
5
|
+
*/
|
|
6
|
+
export class CommunicationError extends AppError {
|
|
7
|
+
constructor(code, statusCode = 500, details, cause) {
|
|
8
|
+
super(code, statusCode, details, cause);
|
|
9
|
+
this.name = 'CommunicationError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CommunicationError } from "./CommunicationError.js";
|
|
2
|
+
import { COMMUNICATION_ERROR_CODES } from "./communicationErrorCodes.js";
|
|
3
|
+
/**
|
|
4
|
+
* Connection error - when connection fails
|
|
5
|
+
*/
|
|
6
|
+
export class ConnectionError extends CommunicationError {
|
|
7
|
+
constructor(code = COMMUNICATION_ERROR_CODES.CONNECTION_ERROR, details, cause) {
|
|
8
|
+
super(code, 503, details, cause);
|
|
9
|
+
this.name = 'ConnectionError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CommunicationError } from "./CommunicationError.js";
|
|
2
|
+
import { COMMUNICATION_ERROR_CODES } from "./communicationErrorCodes.js";
|
|
3
|
+
/**
|
|
4
|
+
* Service discovery error
|
|
5
|
+
*/
|
|
6
|
+
export class DiscoveryError extends CommunicationError {
|
|
7
|
+
constructor(code = COMMUNICATION_ERROR_CODES.DISCOVERY_ERROR, details, cause) {
|
|
8
|
+
super(code, 503, details, cause); // 503 Service Unavailable
|
|
9
|
+
this.name = 'DiscoveryError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CommunicationError } from "./CommunicationError.js";
|
|
2
|
+
import { COMMUNICATION_ERROR_CODES } from "./communicationErrorCodes.js";
|
|
3
|
+
/**
|
|
4
|
+
* Load balancer error - when load balancing fails
|
|
5
|
+
*/
|
|
6
|
+
export class LoadBalancerError extends CommunicationError {
|
|
7
|
+
constructor(code = COMMUNICATION_ERROR_CODES.LOAD_BALANCER_ERROR, details, cause) {
|
|
8
|
+
super(code, 503, details, cause);
|
|
9
|
+
this.name = 'LoadBalancerError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CommunicationError } from "./CommunicationError.js";
|
|
2
|
+
import { COMMUNICATION_ERROR_CODES } from "./communicationErrorCodes.js";
|
|
3
|
+
/**
|
|
4
|
+
* Protocol error - when protocol communication fails
|
|
5
|
+
*/
|
|
6
|
+
export class ProtocolError extends CommunicationError {
|
|
7
|
+
constructor(code = COMMUNICATION_ERROR_CODES.PROTOCOL_ERROR, details, cause) {
|
|
8
|
+
super(code, 502, details, cause); // 502 Bad Gateway
|
|
9
|
+
this.name = 'ProtocolError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CommunicationError } from "./CommunicationError.js";
|
|
2
|
+
import { COMMUNICATION_ERROR_CODES } from "./communicationErrorCodes.js";
|
|
3
|
+
/**
|
|
4
|
+
* Retry exhausted error - when all retry attempts fail
|
|
5
|
+
*/
|
|
6
|
+
export class RetryError extends CommunicationError {
|
|
7
|
+
constructor(details, cause) {
|
|
8
|
+
super(COMMUNICATION_ERROR_CODES.RETRY_EXHAUSTED, 503, // Service Unavailable
|
|
9
|
+
details, cause);
|
|
10
|
+
this.name = 'RetryError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { CommunicationError } from "./CommunicationError.js";
|
|
2
|
+
import { COMMUNICATION_ERROR_CODES } from "./communicationErrorCodes.js";
|
|
3
|
+
/**
|
|
4
|
+
* Serialization error - when serialization/deserialization fails
|
|
5
|
+
*/
|
|
6
|
+
export class SerializationError extends CommunicationError {
|
|
7
|
+
constructor(code = COMMUNICATION_ERROR_CODES.SERIALIZATION_ERROR, details, cause) {
|
|
8
|
+
super(code, 500, details, cause);
|
|
9
|
+
this.name = 'SerializationError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { ServiceUnavailableError as BaseServiceUnavailableError, } from '@naman_deep_singh/errors';
|
|
2
|
+
/**
|
|
3
|
+
* Service unavailable error for communication layer
|
|
4
|
+
* Extends the existing ServiceUnavailableError
|
|
5
|
+
*/
|
|
6
|
+
export class ServiceUnavailableError extends BaseServiceUnavailableError {
|
|
7
|
+
constructor(details, cause) {
|
|
8
|
+
super(details, cause);
|
|
9
|
+
this.name = 'ServiceUnavailableError';
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CommunicationError } from "./CommunicationError.js";
|
|
2
|
+
import { COMMUNICATION_ERROR_CODES } from "./communicationErrorCodes.js";
|
|
3
|
+
/**
|
|
4
|
+
* Timeout error - when operation times out
|
|
5
|
+
*/
|
|
6
|
+
export class TimeoutError extends CommunicationError {
|
|
7
|
+
constructor(details, cause) {
|
|
8
|
+
super(COMMUNICATION_ERROR_CODES.TIMEOUT_EXHAUSTED, 504, // Gateway Timeout
|
|
9
|
+
details, cause);
|
|
10
|
+
this.name = 'TimeoutError';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Communication-specific error codes
|
|
3
|
+
* Extends the existing ERROR_CODES
|
|
4
|
+
*/
|
|
5
|
+
export const COMMUNICATION_ERROR_CODES = {
|
|
6
|
+
// Protocol errors
|
|
7
|
+
PROTOCOL_ERROR: 'Protocol communication failed',
|
|
8
|
+
HTTP_PROTOCOL_ERROR: 'HTTP protocol error occurred',
|
|
9
|
+
GRPC_PROTOCOL_ERROR: 'gRPC protocol error occurred',
|
|
10
|
+
WEBSOCKET_PROTOCOL_ERROR: 'WebSocket protocol error occurred',
|
|
11
|
+
// Discovery errors
|
|
12
|
+
DISCOVERY_ERROR: 'Service discovery failed',
|
|
13
|
+
SERVICE_NOT_FOUND: 'Service not found in registry',
|
|
14
|
+
NO_AVAILABLE_INSTANCES: 'No available service instances',
|
|
15
|
+
DISCOVERY_TIMEOUT: 'Service discovery timed out',
|
|
16
|
+
// Resilience errors
|
|
17
|
+
CIRCUIT_BREAKER_OPEN: 'Circuit breaker is open, request blocked',
|
|
18
|
+
CIRCUIT_BREAKER_ERROR: 'Circuit breaker encountered an error',
|
|
19
|
+
RETRY_EXHAUSTED: 'Retry attempts exhausted',
|
|
20
|
+
BULKHEAD_FULL: 'Bulkhead is full, request rejected',
|
|
21
|
+
TIMEOUT_EXHAUSTED: 'Operation timeout exceeded',
|
|
22
|
+
// Load balancing errors
|
|
23
|
+
LOAD_BALANCER_ERROR: 'Load balancing failed',
|
|
24
|
+
NO_HEALTHY_INSTANCES: 'No healthy instances available',
|
|
25
|
+
// Serialization errors
|
|
26
|
+
SERIALIZATION_ERROR: 'Serialization failed',
|
|
27
|
+
DESERIALIZATION_ERROR: 'Deserialization failed',
|
|
28
|
+
// Connection errors
|
|
29
|
+
CONNECTION_ERROR: 'Connection failed',
|
|
30
|
+
CONNECTION_TIMEOUT: 'Connection timeout',
|
|
31
|
+
CONNECTION_REFUSED: 'Connection refused',
|
|
32
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { errorMessageRegistry } from "@naman_deep_singh/errors";
|
|
2
|
+
import { COMMUNICATION_ERROR_CODES } from "./communicationErrorCodes.js";
|
|
3
|
+
// Register communication error messages with the global registry
|
|
4
|
+
errorMessageRegistry.register(COMMUNICATION_ERROR_CODES);
|
|
5
|
+
// Export error classes
|
|
6
|
+
export { CircuitBreakerError } from "./CircuitBreakerError.js";
|
|
7
|
+
export { CommunicationError } from "./CommunicationError.js";
|
|
8
|
+
export { ConnectionError } from "./ConnectionError.js";
|
|
9
|
+
export { DiscoveryError } from "./DiscoveryError.js";
|
|
10
|
+
export { LoadBalancerError } from "./LoadBalancerError.js";
|
|
11
|
+
export { ProtocolError } from "./ProtocolError.js";
|
|
12
|
+
export { RetryError } from "./RetryError.js";
|
|
13
|
+
export { SerializationError } from "./SerializationError.js";
|
|
14
|
+
export { ServiceUnavailableError } from "./ServiceUnavailableError.js";
|
|
15
|
+
export { TimeoutError } from "./TimeoutError.js";
|
|
16
|
+
// Also export error codes for convenience
|
|
17
|
+
export { COMMUNICATION_ERROR_CODES } from "./communicationErrorCodes.js";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Communication Core Package
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*
|
|
5
|
+
* Core interfaces and abstract implementations for service-to-service communication.
|
|
6
|
+
* This package provides the foundation for communication protocols, resilience patterns,
|
|
7
|
+
* service discovery, load balancing, and interceptors.
|
|
8
|
+
*/
|
|
9
|
+
// Core interfaces
|
|
10
|
+
export * from './interfaces/index.js';
|
|
11
|
+
// Abstract base implementations
|
|
12
|
+
export * from './abstract/index.js';
|
|
13
|
+
// Type definitions
|
|
14
|
+
export * from './types/index.js';
|
|
15
|
+
// Error classes
|
|
16
|
+
export * from './errors/index.js';
|
|
17
|
+
// Utilities (excluding ConnectionPoolConfig to avoid duplicate)
|
|
18
|
+
export { TimeoutUtils, ConnectionPoolUtils, CompressionUtils, } from './utils.js';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|