@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,279 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Service discovery and instance types
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Service instance representation
|
|
7
|
+
*/
|
|
8
|
+
export interface ServiceInstance {
|
|
9
|
+
/** Unique instance identifier */
|
|
10
|
+
id: string;
|
|
11
|
+
/** Service name */
|
|
12
|
+
serviceName: string;
|
|
13
|
+
/** Host address */
|
|
14
|
+
host: string;
|
|
15
|
+
/** Port number */
|
|
16
|
+
port: number;
|
|
17
|
+
/** Protocol (http, https, grpc, etc.) */
|
|
18
|
+
protocol?: string;
|
|
19
|
+
/** Health status */
|
|
20
|
+
status: 'healthy' | 'unhealthy' | 'unknown';
|
|
21
|
+
/** Last health check timestamp */
|
|
22
|
+
lastHealthCheck?: number;
|
|
23
|
+
/** Instance metadata */
|
|
24
|
+
metadata?: Record<string, unknown>;
|
|
25
|
+
/** Tags for categorization */
|
|
26
|
+
tags?: string[];
|
|
27
|
+
/** Weight for load balancing */
|
|
28
|
+
weight?: number;
|
|
29
|
+
/** Zone/region for locality-aware routing */
|
|
30
|
+
zone?: string;
|
|
31
|
+
/** Current load/connections */
|
|
32
|
+
load?: number;
|
|
33
|
+
/** Instance version */
|
|
34
|
+
version?: string;
|
|
35
|
+
/** Instance uptime in milliseconds */
|
|
36
|
+
uptime?: number;
|
|
37
|
+
/** Custom health check data */
|
|
38
|
+
healthData?: Record<string, unknown>;
|
|
39
|
+
/** Allow additional properties */
|
|
40
|
+
[key: string]: unknown;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Service registration information
|
|
44
|
+
*/
|
|
45
|
+
export interface ServiceRegistration {
|
|
46
|
+
/** Service name */
|
|
47
|
+
name: string;
|
|
48
|
+
/** Service ID */
|
|
49
|
+
id: string;
|
|
50
|
+
/** Service address */
|
|
51
|
+
address: string;
|
|
52
|
+
/** Service port */
|
|
53
|
+
port: number;
|
|
54
|
+
/** Health check configuration */
|
|
55
|
+
healthCheck?: {
|
|
56
|
+
/** HTTP endpoint for health checks */
|
|
57
|
+
http?: string;
|
|
58
|
+
/** gRPC health check service */
|
|
59
|
+
grpc?: string;
|
|
60
|
+
/** TCP health check port */
|
|
61
|
+
tcp?: number;
|
|
62
|
+
/** Health check interval in seconds */
|
|
63
|
+
interval?: number;
|
|
64
|
+
/** Health check timeout in seconds */
|
|
65
|
+
timeout?: number;
|
|
66
|
+
/** Deregister after failed health checks */
|
|
67
|
+
deregisterAfter?: string;
|
|
68
|
+
/** Health check headers */
|
|
69
|
+
headers?: Record<string, string>;
|
|
70
|
+
/** Health check expected status */
|
|
71
|
+
expectedStatus?: number;
|
|
72
|
+
};
|
|
73
|
+
/** Service tags */
|
|
74
|
+
tags?: string[];
|
|
75
|
+
/** Service metadata */
|
|
76
|
+
meta?: Record<string, unknown>;
|
|
77
|
+
/** Service version */
|
|
78
|
+
version?: string;
|
|
79
|
+
/** Enable/disable service */
|
|
80
|
+
enabled?: boolean;
|
|
81
|
+
/** Custom registration data */
|
|
82
|
+
custom?: Record<string, unknown>;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Service discovery watch callback
|
|
86
|
+
*/
|
|
87
|
+
export type WatchCallback = (instances: ServiceInstance[]) => void;
|
|
88
|
+
/**
|
|
89
|
+
* Service discovery unwatch function
|
|
90
|
+
*/
|
|
91
|
+
export type UnwatchFunction = () => void;
|
|
92
|
+
/**
|
|
93
|
+
* Health check result
|
|
94
|
+
*/
|
|
95
|
+
export interface HealthCheckResult {
|
|
96
|
+
/** Is the instance healthy? */
|
|
97
|
+
healthy: boolean;
|
|
98
|
+
/** Health check timestamp */
|
|
99
|
+
timestamp: number;
|
|
100
|
+
/** Response time in milliseconds */
|
|
101
|
+
responseTime?: number;
|
|
102
|
+
/** Error if health check failed */
|
|
103
|
+
error?: string;
|
|
104
|
+
/** HTTP status code if applicable */
|
|
105
|
+
statusCode?: number;
|
|
106
|
+
/** Additional health details */
|
|
107
|
+
details?: Record<string, unknown>;
|
|
108
|
+
/** Service instance ID */
|
|
109
|
+
instanceId?: string;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Service endpoint configuration
|
|
113
|
+
*/
|
|
114
|
+
export interface ServiceEndpoint {
|
|
115
|
+
/** Service name */
|
|
116
|
+
service: string;
|
|
117
|
+
/** Endpoint path */
|
|
118
|
+
endpoint: string;
|
|
119
|
+
/** HTTP method */
|
|
120
|
+
method?: string;
|
|
121
|
+
/** Request timeout in milliseconds */
|
|
122
|
+
timeout?: number;
|
|
123
|
+
/** Retry configuration */
|
|
124
|
+
retry?: {
|
|
125
|
+
/** Maximum retry attempts */
|
|
126
|
+
attempts?: number;
|
|
127
|
+
/** Backoff delay in milliseconds */
|
|
128
|
+
backoff?: number;
|
|
129
|
+
/** Retry on specific status codes */
|
|
130
|
+
retryOnStatus?: number[];
|
|
131
|
+
};
|
|
132
|
+
/** Circuit breaker configuration */
|
|
133
|
+
circuitBreaker?: {
|
|
134
|
+
/** Enable circuit breaker */
|
|
135
|
+
enabled?: boolean;
|
|
136
|
+
/** Failure threshold */
|
|
137
|
+
failureThreshold?: number;
|
|
138
|
+
/** Reset timeout */
|
|
139
|
+
resetTimeout?: number;
|
|
140
|
+
};
|
|
141
|
+
/** Headers to include */
|
|
142
|
+
headers?: Record<string, string>;
|
|
143
|
+
/** Query parameters */
|
|
144
|
+
query?: Record<string, string | number | boolean>;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Service dependency configuration
|
|
148
|
+
*/
|
|
149
|
+
export interface ServiceDependency {
|
|
150
|
+
/** Dependent service name */
|
|
151
|
+
service: string;
|
|
152
|
+
/** Dependency type */
|
|
153
|
+
type: 'hard' | 'soft' | 'optional';
|
|
154
|
+
/** Required version (semver) */
|
|
155
|
+
version?: string;
|
|
156
|
+
/** Health check endpoint */
|
|
157
|
+
healthEndpoint?: string;
|
|
158
|
+
/** Timeout for dependency calls in milliseconds */
|
|
159
|
+
timeout?: number;
|
|
160
|
+
/** Circuit breaker configuration */
|
|
161
|
+
circuitBreaker?: {
|
|
162
|
+
enabled?: boolean;
|
|
163
|
+
failureThreshold?: number;
|
|
164
|
+
};
|
|
165
|
+
/** Fallback configuration */
|
|
166
|
+
fallback?: {
|
|
167
|
+
enabled?: boolean;
|
|
168
|
+
value?: unknown;
|
|
169
|
+
function?: () => Promise<unknown>;
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Service metrics
|
|
174
|
+
*/
|
|
175
|
+
export interface ServiceMetrics {
|
|
176
|
+
/** Total request count */
|
|
177
|
+
requests: number;
|
|
178
|
+
/** Success request count */
|
|
179
|
+
successes: number;
|
|
180
|
+
/** Error count */
|
|
181
|
+
errors: number;
|
|
182
|
+
/** Success rate (0-1) */
|
|
183
|
+
successRate: number;
|
|
184
|
+
/** Average response time in milliseconds */
|
|
185
|
+
avgResponseTime: number;
|
|
186
|
+
/** P50 response time in milliseconds */
|
|
187
|
+
p50ResponseTime?: number;
|
|
188
|
+
/** P95 response time in milliseconds */
|
|
189
|
+
p95ResponseTime: number;
|
|
190
|
+
/** P99 response time in milliseconds */
|
|
191
|
+
p99ResponseTime: number;
|
|
192
|
+
/** Current active connections */
|
|
193
|
+
activeConnections: number;
|
|
194
|
+
/** Total connections handled */
|
|
195
|
+
totalConnections: number;
|
|
196
|
+
/** Error rate (0-1) */
|
|
197
|
+
errorRate: number;
|
|
198
|
+
/** Throughput (requests per second) */
|
|
199
|
+
throughput?: number;
|
|
200
|
+
/** Timestamp of last metrics collection */
|
|
201
|
+
lastUpdated: number;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Service registry configuration
|
|
205
|
+
*/
|
|
206
|
+
export interface ServiceRegistryConfig {
|
|
207
|
+
/** Registry type */
|
|
208
|
+
type: 'kubernetes' | 'consul' | 'eureka' | 'static' | 'custom';
|
|
209
|
+
/** Registry endpoint */
|
|
210
|
+
endpoint?: string;
|
|
211
|
+
/** Authentication configuration */
|
|
212
|
+
auth?: {
|
|
213
|
+
/** Authentication type */
|
|
214
|
+
type: 'token' | 'basic' | 'tls' | 'none';
|
|
215
|
+
/** Token for token auth */
|
|
216
|
+
token?: string;
|
|
217
|
+
/** Username for basic auth */
|
|
218
|
+
username?: string;
|
|
219
|
+
/** Password for basic auth */
|
|
220
|
+
password?: string;
|
|
221
|
+
/** TLS certificate */
|
|
222
|
+
tlsCert?: string | Buffer;
|
|
223
|
+
/** TLS key */
|
|
224
|
+
tlsKey?: string | Buffer;
|
|
225
|
+
/** CA certificate */
|
|
226
|
+
caCert?: string | Buffer;
|
|
227
|
+
};
|
|
228
|
+
/** Cache configuration */
|
|
229
|
+
cache?: {
|
|
230
|
+
/** Enable caching */
|
|
231
|
+
enabled: boolean;
|
|
232
|
+
/** Cache TTL in milliseconds */
|
|
233
|
+
ttl: number;
|
|
234
|
+
/** Maximum cache size */
|
|
235
|
+
maxSize?: number;
|
|
236
|
+
};
|
|
237
|
+
/** Watch for changes */
|
|
238
|
+
watch?: boolean;
|
|
239
|
+
/** Watch interval in milliseconds */
|
|
240
|
+
watchInterval?: number;
|
|
241
|
+
/** Custom configuration */
|
|
242
|
+
options?: Record<string, unknown>;
|
|
243
|
+
}
|
|
244
|
+
/**
|
|
245
|
+
* Service instance filter options
|
|
246
|
+
*/
|
|
247
|
+
export interface ServiceInstanceFilter {
|
|
248
|
+
/** Filter by status */
|
|
249
|
+
status?: 'healthy' | 'unhealthy' | 'unknown';
|
|
250
|
+
/** Filter by tags */
|
|
251
|
+
tags?: string[];
|
|
252
|
+
/** Filter by zone/region */
|
|
253
|
+
zone?: string;
|
|
254
|
+
/** Filter by version */
|
|
255
|
+
version?: string;
|
|
256
|
+
/** Maximum instances to return */
|
|
257
|
+
limit?: number;
|
|
258
|
+
/** Custom filter function */
|
|
259
|
+
filter?: (instance: ServiceInstance) => boolean;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* Service discovery result
|
|
263
|
+
*/
|
|
264
|
+
export interface ServiceDiscoveryResult {
|
|
265
|
+
/** Service name */
|
|
266
|
+
serviceName: string;
|
|
267
|
+
/** Discovered instances */
|
|
268
|
+
instances: ServiceInstance[];
|
|
269
|
+
/** Total instances count */
|
|
270
|
+
total: number;
|
|
271
|
+
/** Healthy instances count */
|
|
272
|
+
healthy: number;
|
|
273
|
+
/** Timestamp of discovery */
|
|
274
|
+
timestamp: number;
|
|
275
|
+
/** Discovery source */
|
|
276
|
+
source: string;
|
|
277
|
+
/** Next refresh time */
|
|
278
|
+
nextRefresh?: number;
|
|
279
|
+
}
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utilities integration with @naman_deep_singh/utils package
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import { GenericPool, type Connection as UtilsConnection } from '@naman_deep_singh/utils';
|
|
6
|
+
import type { IConnection } from './interfaces/ConnectionPool.interface.js';
|
|
7
|
+
/**
|
|
8
|
+
* Connection interface compatible with both packages
|
|
9
|
+
* Note: The utils package expects isHealthy as a function, not a property
|
|
10
|
+
*/
|
|
11
|
+
export interface Connection extends IConnection, UtilsConnection {
|
|
12
|
+
/** Is connection healthy? - must be a function in utils package */
|
|
13
|
+
isHealthy: () => boolean;
|
|
14
|
+
/** Connection ID */
|
|
15
|
+
id: string;
|
|
16
|
+
/** Connection creation time */
|
|
17
|
+
createdAt: number;
|
|
18
|
+
/** Last used time */
|
|
19
|
+
lastUsedAt: number;
|
|
20
|
+
/** Usage count */
|
|
21
|
+
usageCount: number;
|
|
22
|
+
/** Connection metadata */
|
|
23
|
+
metadata: Record<string, unknown>;
|
|
24
|
+
/**
|
|
25
|
+
* Close the connection
|
|
26
|
+
*/
|
|
27
|
+
close(): Promise<void>;
|
|
28
|
+
/**
|
|
29
|
+
* Health check for connection
|
|
30
|
+
*/
|
|
31
|
+
healthCheck(): Promise<boolean>;
|
|
32
|
+
/**
|
|
33
|
+
* Reset connection state
|
|
34
|
+
*/
|
|
35
|
+
reset(): Promise<void>;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Base connection implementation that satisfies both interfaces
|
|
39
|
+
*/
|
|
40
|
+
export declare abstract class BaseConnection implements Connection {
|
|
41
|
+
abstract readonly id: string;
|
|
42
|
+
abstract readonly createdAt: number;
|
|
43
|
+
abstract readonly lastUsedAt: number;
|
|
44
|
+
abstract readonly usageCount: number;
|
|
45
|
+
abstract readonly metadata: Record<string, unknown>;
|
|
46
|
+
abstract isHealthy(): boolean;
|
|
47
|
+
abstract close(): Promise<void>;
|
|
48
|
+
abstract healthCheck(): Promise<boolean>;
|
|
49
|
+
abstract reset(): Promise<void>;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Timeout utilities wrapper
|
|
53
|
+
*/
|
|
54
|
+
export declare class TimeoutUtils {
|
|
55
|
+
/**
|
|
56
|
+
* Execute with timeout
|
|
57
|
+
*/
|
|
58
|
+
static withTimeout<T>(promise: Promise<T>, timeoutMs: number, errorMessage?: string): Promise<T>;
|
|
59
|
+
/**
|
|
60
|
+
* Create delay
|
|
61
|
+
*/
|
|
62
|
+
static delay(ms: number): Promise<void>;
|
|
63
|
+
/**
|
|
64
|
+
* Retry with timeout
|
|
65
|
+
*/
|
|
66
|
+
static retryWithTimeout<T>(fn: () => Promise<T>, options?: {
|
|
67
|
+
maxAttempts?: number;
|
|
68
|
+
timeoutPerAttempt?: number;
|
|
69
|
+
backoffMultiplier?: number;
|
|
70
|
+
}): Promise<T>;
|
|
71
|
+
/**
|
|
72
|
+
* Create a timeout promise
|
|
73
|
+
*/
|
|
74
|
+
static createTimeout(timeoutMs: number, message?: string): Promise<never>;
|
|
75
|
+
/**
|
|
76
|
+
* Create an abort signal for timeout
|
|
77
|
+
*/
|
|
78
|
+
static createAbortSignal(timeoutMs: number): AbortSignal;
|
|
79
|
+
/**
|
|
80
|
+
* Race promises with timeout
|
|
81
|
+
*/
|
|
82
|
+
static raceWithTimeout<T>(promises: Promise<T>[], timeoutMs: number, errorMessage?: string): Promise<T>;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Connection pool configuration
|
|
86
|
+
*/
|
|
87
|
+
export interface ConnectionPoolConfig<T extends Connection> {
|
|
88
|
+
/** Pool name */
|
|
89
|
+
name: string;
|
|
90
|
+
/** Minimum connections in pool */
|
|
91
|
+
minConnections?: number;
|
|
92
|
+
/** Maximum connections in pool */
|
|
93
|
+
maxConnections?: number;
|
|
94
|
+
/** Connection idle timeout in milliseconds */
|
|
95
|
+
idleTimeout?: number;
|
|
96
|
+
/** Connection lifetime in milliseconds */
|
|
97
|
+
maxLifetime?: number;
|
|
98
|
+
/** Connection acquisition timeout in milliseconds */
|
|
99
|
+
acquireTimeout?: number;
|
|
100
|
+
/** Connection validation interval in milliseconds */
|
|
101
|
+
validationInterval?: number;
|
|
102
|
+
/** Enable connection warmup */
|
|
103
|
+
warmup?: boolean;
|
|
104
|
+
/** Connection factory function */
|
|
105
|
+
createConnection: () => Promise<T>;
|
|
106
|
+
/** Connection validation function */
|
|
107
|
+
validateConnection?: (connection: T) => boolean;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Connection pool wrapper
|
|
111
|
+
*/
|
|
112
|
+
export declare class ConnectionPoolUtils {
|
|
113
|
+
/**
|
|
114
|
+
* Create a generic connection pool
|
|
115
|
+
*/
|
|
116
|
+
static createPool<T extends Connection>(config: ConnectionPoolConfig<T>): GenericPool<T>;
|
|
117
|
+
/**
|
|
118
|
+
* Create a simple pool with basic configuration
|
|
119
|
+
*/
|
|
120
|
+
static createSimplePool<T extends Connection>(name: string, createConnection: () => Promise<T>, options?: {
|
|
121
|
+
minConnections?: number;
|
|
122
|
+
maxConnections?: number;
|
|
123
|
+
idleTimeout?: number;
|
|
124
|
+
}): GenericPool<T>;
|
|
125
|
+
/**
|
|
126
|
+
* Create an adapter to convert property-based isHealthy to function-based
|
|
127
|
+
*/
|
|
128
|
+
static createConnectionAdapter<T extends Record<string, any>>(connection: T & {
|
|
129
|
+
isHealthy?: boolean | (() => boolean);
|
|
130
|
+
}, closeFn?: () => Promise<void>, healthCheckFn?: () => Promise<boolean>, resetFn?: () => Promise<void>): Connection;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Compression utilities wrapper
|
|
134
|
+
*/
|
|
135
|
+
export declare class CompressionUtils {
|
|
136
|
+
/**
|
|
137
|
+
* Compression algorithm type
|
|
138
|
+
*/
|
|
139
|
+
static readonly Algorithm: {
|
|
140
|
+
readonly GZIP: "gzip";
|
|
141
|
+
readonly DEFLATE: "deflate";
|
|
142
|
+
readonly BROTLI: "brotli";
|
|
143
|
+
readonly NONE: "none";
|
|
144
|
+
};
|
|
145
|
+
/**
|
|
146
|
+
* Compress data
|
|
147
|
+
*/
|
|
148
|
+
static compress(data: Buffer | string | Uint8Array, options?: {
|
|
149
|
+
algorithm?: 'gzip' | 'deflate' | 'brotli' | 'none';
|
|
150
|
+
level?: number;
|
|
151
|
+
}): Promise<Buffer>;
|
|
152
|
+
/**
|
|
153
|
+
* Decompress data
|
|
154
|
+
*/
|
|
155
|
+
static decompress(data: Buffer | Uint8Array, options?: {
|
|
156
|
+
algorithm?: 'gzip' | 'deflate' | 'brotli';
|
|
157
|
+
}): Promise<Buffer>;
|
|
158
|
+
/**
|
|
159
|
+
* Compress data with metrics
|
|
160
|
+
*/
|
|
161
|
+
static compressWithMetrics(data: Buffer | string | Uint8Array, options?: {
|
|
162
|
+
algorithm?: 'gzip' | 'deflate' | 'brotli';
|
|
163
|
+
level?: number;
|
|
164
|
+
}): Promise<{
|
|
165
|
+
data: Buffer;
|
|
166
|
+
originalSize: number;
|
|
167
|
+
compressedSize: number;
|
|
168
|
+
compressionRatio: number;
|
|
169
|
+
compressionTime: number;
|
|
170
|
+
}>;
|
|
171
|
+
/**
|
|
172
|
+
* Get optimal compression level based on data size
|
|
173
|
+
*/
|
|
174
|
+
static getOptimalLevel(dataSize: number): number;
|
|
175
|
+
/**
|
|
176
|
+
* Estimate compression ratio for data type
|
|
177
|
+
*/
|
|
178
|
+
static estimateCompressionRatio(contentType?: string): number;
|
|
179
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@naman_deep_singh/communication-core",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Core interfaces and abstract classes for building a comprehensive service-to-service communication layer with protocols, resilience patterns, service discovery, and load balancing",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/cjs/index.js",
|
|
7
|
+
"module": "./dist/esm/index.js",
|
|
8
|
+
"types": "./dist/types/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/esm/index.js",
|
|
12
|
+
"require": "./dist/cjs/index.js",
|
|
13
|
+
"types": "./dist/types/index.d.ts"
|
|
14
|
+
},
|
|
15
|
+
"./interfaces": {
|
|
16
|
+
"import": "./dist/esm/interfaces/index.js",
|
|
17
|
+
"require": "./dist/cjs/interfaces/index.js",
|
|
18
|
+
"types": "./dist/types/interfaces/index.d.ts"
|
|
19
|
+
},
|
|
20
|
+
"./abstract": {
|
|
21
|
+
"import": "./dist/esm/abstract/index.js",
|
|
22
|
+
"require": "./dist/cjs/abstract/index.js",
|
|
23
|
+
"types": "./dist/types/abstract/index.d.ts"
|
|
24
|
+
},
|
|
25
|
+
"./types": {
|
|
26
|
+
"import": "./dist/esm/types/index.js",
|
|
27
|
+
"require": "./dist/cjs/types/index.js",
|
|
28
|
+
"types": "./dist/types/types/index.d.ts"
|
|
29
|
+
},
|
|
30
|
+
"./errors": {
|
|
31
|
+
"import": "./dist/esm/errors/index.js",
|
|
32
|
+
"require": "./dist/cjs/errors/index.js",
|
|
33
|
+
"types": "./dist/types/errors/index.d.ts"
|
|
34
|
+
}
|
|
35
|
+
},
|
|
36
|
+
"sideEffects": false,
|
|
37
|
+
"files": [
|
|
38
|
+
"dist",
|
|
39
|
+
"README.md"
|
|
40
|
+
],
|
|
41
|
+
"keywords": [
|
|
42
|
+
"microservices",
|
|
43
|
+
"communication",
|
|
44
|
+
"service-discovery",
|
|
45
|
+
"load-balancing",
|
|
46
|
+
"circuit-breaker",
|
|
47
|
+
"retry",
|
|
48
|
+
"http",
|
|
49
|
+
"grpc",
|
|
50
|
+
"websocket",
|
|
51
|
+
"resilience",
|
|
52
|
+
"typescript",
|
|
53
|
+
"interfaces",
|
|
54
|
+
"abstract-classes",
|
|
55
|
+
"connection-pooling",
|
|
56
|
+
"interceptors"
|
|
57
|
+
],
|
|
58
|
+
"author": "Naman Deep Singh",
|
|
59
|
+
"license": "MIT",
|
|
60
|
+
"homepage": "https://github.com/naman_deep_singh/ts-packages/tree/main/packages/communication/core",
|
|
61
|
+
"repository": {
|
|
62
|
+
"type": "git",
|
|
63
|
+
"url": "https://github.com/naman_deep_singh/ts-packages.git",
|
|
64
|
+
"directory": "packages/communication/core"
|
|
65
|
+
},
|
|
66
|
+
"bugs": {
|
|
67
|
+
"url": "https://github.com/naman_deep_singh/ts-packages/issues"
|
|
68
|
+
},
|
|
69
|
+
"devDependencies": {
|
|
70
|
+
"rimraf": "^5.0.5",
|
|
71
|
+
"typescript": "^5.9.3"
|
|
72
|
+
},
|
|
73
|
+
"dependencies": {
|
|
74
|
+
"@naman_deep_singh/errors": "^2.3.0",
|
|
75
|
+
"@naman_deep_singh/http-response": "^3.3.1",
|
|
76
|
+
"@naman_deep_singh/utils": "^2.6.1",
|
|
77
|
+
"@types/node": "^25.0.1"
|
|
78
|
+
},
|
|
79
|
+
"publishConfig": {
|
|
80
|
+
"access": "public"
|
|
81
|
+
},
|
|
82
|
+
"scripts": {
|
|
83
|
+
"build": "pnpm run build:types && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json",
|
|
84
|
+
"build:types": "tsc -p tsconfig.types.json",
|
|
85
|
+
"clean": "rimraf dist",
|
|
86
|
+
"clean:js": "find src -type f -name \"*.js\" -delete"
|
|
87
|
+
}
|
|
88
|
+
}
|