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