@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,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client interface for communication layer
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import type { ClientConfig } from '../types/config.js';
|
|
6
|
+
import type { Request, RequestOptions } from '../types/request.js';
|
|
7
|
+
import type { Response } from '../types/response.js';
|
|
8
|
+
import type { ServiceInstance } from '../types/service.js';
|
|
9
|
+
import type { ICircuitBreaker } from './CircuitBreaker.interface.js';
|
|
10
|
+
import type { IInterceptor } from './Interceptor.interface.js';
|
|
11
|
+
import type { ILoadBalanceStrategy } from './LoadBalancer.interface.js';
|
|
12
|
+
import type { IProtocol } from './Protocol.interface.js';
|
|
13
|
+
import type { IRetryStrategy } from './RetryStrategy.interface.js';
|
|
14
|
+
import type { IServiceDiscoverer } from './ServiceDiscovery.interface.js';
|
|
15
|
+
/**
|
|
16
|
+
* Client interface for service-to-service communication
|
|
17
|
+
* This is the main interface that users interact with
|
|
18
|
+
*/
|
|
19
|
+
export interface IClient {
|
|
20
|
+
/** Client configuration */
|
|
21
|
+
readonly config: ClientConfig;
|
|
22
|
+
/** Client name/identifier */
|
|
23
|
+
readonly name: string;
|
|
24
|
+
/** Service name this client communicates with */
|
|
25
|
+
readonly serviceName: string;
|
|
26
|
+
/** Protocol being used */
|
|
27
|
+
readonly protocol: IProtocol;
|
|
28
|
+
/**
|
|
29
|
+
* Make a request to a service endpoint
|
|
30
|
+
* @param endpoint Service endpoint/path
|
|
31
|
+
* @param data Request data
|
|
32
|
+
* @param options Request options
|
|
33
|
+
* @returns Promise resolving to the response
|
|
34
|
+
* @throws {CommunicationError} If the request fails
|
|
35
|
+
*/
|
|
36
|
+
call<T = unknown>(endpoint: string, data?: unknown, options?: RequestOptions): Promise<Response<T>>;
|
|
37
|
+
/**
|
|
38
|
+
* Make a request with full control over request object
|
|
39
|
+
* @param request Complete request object
|
|
40
|
+
* @returns Promise resolving to the response
|
|
41
|
+
*/
|
|
42
|
+
request<T = unknown>(request: Request): Promise<Response<T>>;
|
|
43
|
+
/**
|
|
44
|
+
* Get service discovery instance
|
|
45
|
+
*/
|
|
46
|
+
getServiceDiscoverer?(): IServiceDiscoverer | undefined;
|
|
47
|
+
/**
|
|
48
|
+
* Get load balancer instance
|
|
49
|
+
*/
|
|
50
|
+
getLoadBalancer?(): ILoadBalanceStrategy | undefined;
|
|
51
|
+
/**
|
|
52
|
+
* Get circuit breaker instance
|
|
53
|
+
*/
|
|
54
|
+
getCircuitBreaker?(): ICircuitBreaker | undefined;
|
|
55
|
+
/**
|
|
56
|
+
* Get retry strategy instance
|
|
57
|
+
*/
|
|
58
|
+
getRetryStrategy?(): IRetryStrategy | undefined;
|
|
59
|
+
/**
|
|
60
|
+
* Add an interceptor to the client
|
|
61
|
+
* @param interceptor Interceptor to add
|
|
62
|
+
*/
|
|
63
|
+
addInterceptor(interceptor: IInterceptor): void;
|
|
64
|
+
/**
|
|
65
|
+
* Remove an interceptor from the client
|
|
66
|
+
* @param interceptorId Interceptor identifier
|
|
67
|
+
*/
|
|
68
|
+
removeInterceptor(interceptorId: string): void;
|
|
69
|
+
/**
|
|
70
|
+
* Get all interceptors
|
|
71
|
+
*/
|
|
72
|
+
getInterceptors(): IInterceptor[];
|
|
73
|
+
/**
|
|
74
|
+
* Clear all interceptors
|
|
75
|
+
*/
|
|
76
|
+
clearInterceptors(): void;
|
|
77
|
+
/**
|
|
78
|
+
* Discover service instances
|
|
79
|
+
* @returns Promise resolving to service instances
|
|
80
|
+
*/
|
|
81
|
+
discoverService?(): Promise<ServiceInstance[]>;
|
|
82
|
+
/**
|
|
83
|
+
* Get current service instances (cached)
|
|
84
|
+
*/
|
|
85
|
+
getServiceInstances?(): ServiceInstance[];
|
|
86
|
+
/**
|
|
87
|
+
* Refresh service instances cache
|
|
88
|
+
*/
|
|
89
|
+
refreshServiceInstances?(): Promise<void>;
|
|
90
|
+
/**
|
|
91
|
+
* Health check for the client
|
|
92
|
+
* @returns Promise resolving to health status
|
|
93
|
+
*/
|
|
94
|
+
healthCheck(): Promise<{
|
|
95
|
+
healthy: boolean;
|
|
96
|
+
message?: string;
|
|
97
|
+
details?: Record<string, unknown>;
|
|
98
|
+
}>;
|
|
99
|
+
/**
|
|
100
|
+
* Get client metrics
|
|
101
|
+
*/
|
|
102
|
+
getMetrics(): Record<string, unknown>;
|
|
103
|
+
/**
|
|
104
|
+
* Reset client metrics
|
|
105
|
+
*/
|
|
106
|
+
resetMetrics(): void;
|
|
107
|
+
/**
|
|
108
|
+
* Close/cleanup client resources
|
|
109
|
+
*/
|
|
110
|
+
close(): Promise<void>;
|
|
111
|
+
/**
|
|
112
|
+
* Event emitter for client events
|
|
113
|
+
*/
|
|
114
|
+
readonly events?: {
|
|
115
|
+
/**
|
|
116
|
+
* Subscribe to client events
|
|
117
|
+
* @param event Event name
|
|
118
|
+
* @param listener Event listener
|
|
119
|
+
*/
|
|
120
|
+
on(event: string, listener: (data: any) => void): void;
|
|
121
|
+
/**
|
|
122
|
+
* Unsubscribe from client events
|
|
123
|
+
* @param event Event name
|
|
124
|
+
* @param listener Event listener
|
|
125
|
+
*/
|
|
126
|
+
off(event: string, listener: (data: any) => void): void;
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Client factory interface for creating client instances
|
|
131
|
+
*/
|
|
132
|
+
export interface IClientFactory {
|
|
133
|
+
/**
|
|
134
|
+
* Create a new client instance
|
|
135
|
+
* @param serviceName Service name to communicate with
|
|
136
|
+
* @param config Client configuration
|
|
137
|
+
* @returns New client instance
|
|
138
|
+
*/
|
|
139
|
+
create(serviceName: string, config?: Partial<ClientConfig>): IClient;
|
|
140
|
+
/**
|
|
141
|
+
* Get an existing client instance
|
|
142
|
+
* @param clientId Client identifier
|
|
143
|
+
*/
|
|
144
|
+
get?(clientId: string): IClient | undefined;
|
|
145
|
+
/**
|
|
146
|
+
* Close all client instances
|
|
147
|
+
*/
|
|
148
|
+
closeAll?(): Promise<void>;
|
|
149
|
+
/**
|
|
150
|
+
* Get all client instances
|
|
151
|
+
*/
|
|
152
|
+
getAll?(): IClient[];
|
|
153
|
+
}
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compression interface for communication layer
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import type { CompressionConfig } from '../types/config.js';
|
|
6
|
+
/**
|
|
7
|
+
* Compression algorithm
|
|
8
|
+
*/
|
|
9
|
+
export type CompressionAlgorithm = 'gzip' | 'deflate' | 'brotli' | 'none';
|
|
10
|
+
/**
|
|
11
|
+
* Compression result
|
|
12
|
+
*/
|
|
13
|
+
export interface CompressionResult {
|
|
14
|
+
/** Compressed data */
|
|
15
|
+
data: Buffer | Uint8Array;
|
|
16
|
+
/** Original size in bytes */
|
|
17
|
+
originalSize: number;
|
|
18
|
+
/** Compressed size in bytes */
|
|
19
|
+
compressedSize: number;
|
|
20
|
+
/** Compression ratio (0-1) */
|
|
21
|
+
compressionRatio: number;
|
|
22
|
+
/** Compression algorithm used */
|
|
23
|
+
algorithm: CompressionAlgorithm;
|
|
24
|
+
/** Compression level used */
|
|
25
|
+
level: number;
|
|
26
|
+
/** Compression time in milliseconds */
|
|
27
|
+
compressionTime: number;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Decompression result
|
|
31
|
+
*/
|
|
32
|
+
export interface DecompressionResult {
|
|
33
|
+
/** Decompressed data */
|
|
34
|
+
data: Buffer | string;
|
|
35
|
+
/** Original compressed size in bytes */
|
|
36
|
+
originalSize: number;
|
|
37
|
+
/** Decompressed size in bytes */
|
|
38
|
+
decompressedSize: number;
|
|
39
|
+
/** Decompression time in milliseconds */
|
|
40
|
+
decompressionTime: number;
|
|
41
|
+
/** Algorithm used for decompression */
|
|
42
|
+
algorithm: CompressionAlgorithm;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Compression options
|
|
46
|
+
*/
|
|
47
|
+
export interface CompressionOptions {
|
|
48
|
+
/** Compression algorithm */
|
|
49
|
+
algorithm?: CompressionAlgorithm;
|
|
50
|
+
/** Compression level (1-11) */
|
|
51
|
+
level?: number;
|
|
52
|
+
/** Chunk size for streaming */
|
|
53
|
+
chunkSize?: number;
|
|
54
|
+
/** Custom compression options */
|
|
55
|
+
options?: Record<string, unknown>;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Compression manager interface
|
|
59
|
+
*/
|
|
60
|
+
export interface ICompressionManager {
|
|
61
|
+
/** Compression configuration */
|
|
62
|
+
readonly config: CompressionConfig;
|
|
63
|
+
/** Supported algorithms */
|
|
64
|
+
readonly supportedAlgorithms: CompressionAlgorithm[];
|
|
65
|
+
/**
|
|
66
|
+
* Compress data
|
|
67
|
+
* @param data Data to compress
|
|
68
|
+
* @param options Compression options
|
|
69
|
+
* @returns Promise resolving to compression result
|
|
70
|
+
* @throws {CommunicationError} If compression fails
|
|
71
|
+
*/
|
|
72
|
+
compress(data: Buffer | string | Uint8Array, options?: CompressionOptions): Promise<CompressionResult>;
|
|
73
|
+
/**
|
|
74
|
+
* Compress data with detailed metrics
|
|
75
|
+
* @param data Data to compress
|
|
76
|
+
* @param options Compression options
|
|
77
|
+
* @returns Promise resolving to compression result with metrics
|
|
78
|
+
*/
|
|
79
|
+
compressWithMetrics(data: Buffer | string | Uint8Array, options?: CompressionOptions): Promise<CompressionResult>;
|
|
80
|
+
/**
|
|
81
|
+
* Decompress data
|
|
82
|
+
* @param data Data to decompress
|
|
83
|
+
* @param options Decompression options
|
|
84
|
+
* @returns Promise resolving to decompression result
|
|
85
|
+
* @throws {CommunicationError} If decompression fails
|
|
86
|
+
*/
|
|
87
|
+
decompress(data: Buffer | Uint8Array, options?: CompressionOptions): Promise<DecompressionResult>;
|
|
88
|
+
/**
|
|
89
|
+
* Check if algorithm is supported
|
|
90
|
+
* @param algorithm Algorithm to check
|
|
91
|
+
* @returns True if algorithm is supported
|
|
92
|
+
*/
|
|
93
|
+
isAlgorithmSupported(algorithm: CompressionAlgorithm): boolean;
|
|
94
|
+
/**
|
|
95
|
+
* Get content encoding header for algorithm
|
|
96
|
+
* @param algorithm Compression algorithm
|
|
97
|
+
* @returns Content-Encoding header value
|
|
98
|
+
*/
|
|
99
|
+
getContentEncoding(algorithm: CompressionAlgorithm): string;
|
|
100
|
+
/**
|
|
101
|
+
* Get algorithm from content encoding header
|
|
102
|
+
* @param contentEncoding Content-Encoding header value
|
|
103
|
+
* @returns Compression algorithm or undefined
|
|
104
|
+
*/
|
|
105
|
+
getAlgorithmFromEncoding(contentEncoding: string): CompressionAlgorithm | undefined;
|
|
106
|
+
/**
|
|
107
|
+
* Should compress based on configuration
|
|
108
|
+
* @param dataSize Data size in bytes
|
|
109
|
+
* @param contentType Content type
|
|
110
|
+
* @returns True if should compress
|
|
111
|
+
*/
|
|
112
|
+
shouldCompress(dataSize: number, contentType?: string): boolean;
|
|
113
|
+
/**
|
|
114
|
+
* Create compression stream
|
|
115
|
+
* @param algorithm Compression algorithm
|
|
116
|
+
* @param options Compression options
|
|
117
|
+
* @returns Transform stream for compression
|
|
118
|
+
*/
|
|
119
|
+
createCompressionStream(algorithm: CompressionAlgorithm, options?: CompressionOptions): TransformStream;
|
|
120
|
+
/**
|
|
121
|
+
* Create decompression stream
|
|
122
|
+
* @param algorithm Compression algorithm
|
|
123
|
+
* @returns Transform stream for decompression
|
|
124
|
+
*/
|
|
125
|
+
createDecompressionStream(algorithm: CompressionAlgorithm): TransformStream;
|
|
126
|
+
/**
|
|
127
|
+
* Update compression configuration
|
|
128
|
+
* @param config New configuration
|
|
129
|
+
*/
|
|
130
|
+
updateConfig(config: Partial<CompressionConfig>): void;
|
|
131
|
+
/**
|
|
132
|
+
* Get compression statistics
|
|
133
|
+
*/
|
|
134
|
+
getStats(): {
|
|
135
|
+
totalCompressions: number;
|
|
136
|
+
totalDecompressions: number;
|
|
137
|
+
compressionErrors: number;
|
|
138
|
+
decompressionErrors: number;
|
|
139
|
+
averageCompressionTime: number;
|
|
140
|
+
averageDecompressionTime: number;
|
|
141
|
+
averageCompressionRatio: number;
|
|
142
|
+
algorithmUsage: Record<string, number>;
|
|
143
|
+
totalBytesCompressed: number;
|
|
144
|
+
totalBytesDecompressed: number;
|
|
145
|
+
bytesSaved: number;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Reset compression statistics
|
|
149
|
+
*/
|
|
150
|
+
resetStats(): void;
|
|
151
|
+
/**
|
|
152
|
+
* Health check for compression manager
|
|
153
|
+
*/
|
|
154
|
+
healthCheck(): {
|
|
155
|
+
healthy: boolean;
|
|
156
|
+
message?: string;
|
|
157
|
+
details?: Record<string, unknown>;
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Compression manager factory interface
|
|
162
|
+
*/
|
|
163
|
+
export interface ICompressionManagerFactory {
|
|
164
|
+
/**
|
|
165
|
+
* Create a new compression manager instance
|
|
166
|
+
* @param name Compression manager name
|
|
167
|
+
* @param config Compression configuration
|
|
168
|
+
* @returns New compression manager instance
|
|
169
|
+
*/
|
|
170
|
+
create(name: string, config?: Partial<CompressionConfig>): ICompressionManager;
|
|
171
|
+
/**
|
|
172
|
+
* Get an existing compression manager instance
|
|
173
|
+
* @param name Compression manager name
|
|
174
|
+
*/
|
|
175
|
+
get(name: string): ICompressionManager | undefined;
|
|
176
|
+
/**
|
|
177
|
+
* Get all compression manager instances
|
|
178
|
+
*/
|
|
179
|
+
getAll(): ICompressionManager[];
|
|
180
|
+
/**
|
|
181
|
+
* Register a custom compression manager
|
|
182
|
+
* @param name Compression manager name
|
|
183
|
+
* @param manager Compression manager instance or constructor
|
|
184
|
+
*/
|
|
185
|
+
register(name: string, manager: ICompressionManager | (new (config: CompressionConfig) => ICompressionManager)): void;
|
|
186
|
+
/**
|
|
187
|
+
* Get available compression manager names
|
|
188
|
+
*/
|
|
189
|
+
getAvailableManagers(): string[];
|
|
190
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Connection pool interface for communication layer
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import type { ConnectionPoolConfig } from '../types/config.js';
|
|
6
|
+
/**
|
|
7
|
+
* Connection interface that must be implemented by pooled connections
|
|
8
|
+
* Note: isHealthy must be a function to be compatible with @naman_deep_singh/utils
|
|
9
|
+
*/
|
|
10
|
+
export interface IConnection {
|
|
11
|
+
/** Connection ID */
|
|
12
|
+
readonly id: string;
|
|
13
|
+
/** Is connection healthy? - Must be a function for utils compatibility */
|
|
14
|
+
readonly isHealthy: () => boolean;
|
|
15
|
+
/** Connection creation time */
|
|
16
|
+
readonly createdAt: number;
|
|
17
|
+
/** Last used time */
|
|
18
|
+
readonly lastUsedAt: number;
|
|
19
|
+
/** Usage count */
|
|
20
|
+
readonly usageCount: number;
|
|
21
|
+
/** Connection metadata */
|
|
22
|
+
readonly metadata: Record<string, unknown>;
|
|
23
|
+
/**
|
|
24
|
+
* Close the connection
|
|
25
|
+
*/
|
|
26
|
+
close(): Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Health check for connection
|
|
29
|
+
*/
|
|
30
|
+
healthCheck(): Promise<boolean>;
|
|
31
|
+
/**
|
|
32
|
+
* Reset connection state
|
|
33
|
+
*/
|
|
34
|
+
reset(): Promise<void>;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Connection acquisition result
|
|
38
|
+
*/
|
|
39
|
+
export interface ConnectionAcquisition<T extends IConnection = IConnection> {
|
|
40
|
+
/** Acquired connection */
|
|
41
|
+
connection: T;
|
|
42
|
+
/** Acquisition timestamp */
|
|
43
|
+
timestamp: number;
|
|
44
|
+
/** Wait time in milliseconds */
|
|
45
|
+
waitTime: number;
|
|
46
|
+
/** Connection pool statistics */
|
|
47
|
+
poolStats: {
|
|
48
|
+
totalConnections: number;
|
|
49
|
+
activeConnections: number;
|
|
50
|
+
idleConnections: number;
|
|
51
|
+
waitingClients: number;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Connection pool interface
|
|
56
|
+
*/
|
|
57
|
+
export interface IConnectionPool<T extends IConnection = IConnection> {
|
|
58
|
+
/** Pool name */
|
|
59
|
+
readonly name: string;
|
|
60
|
+
/** Pool configuration */
|
|
61
|
+
readonly config: ConnectionPoolConfig;
|
|
62
|
+
/** Is pool healthy? */
|
|
63
|
+
readonly isHealthy: boolean;
|
|
64
|
+
/** Total connections in pool */
|
|
65
|
+
readonly totalConnections: number;
|
|
66
|
+
/** Active connections */
|
|
67
|
+
readonly activeConnections: number;
|
|
68
|
+
/** Idle connections */
|
|
69
|
+
readonly idleConnections: number;
|
|
70
|
+
/** Waiting clients */
|
|
71
|
+
readonly waitingClients: number;
|
|
72
|
+
/**
|
|
73
|
+
* Acquire a connection from the pool
|
|
74
|
+
* @param timeout Acquisition timeout in milliseconds
|
|
75
|
+
* @returns Promise resolving to connection acquisition
|
|
76
|
+
* @throws {CommunicationError} If timeout or pool exhausted
|
|
77
|
+
*/
|
|
78
|
+
acquire(timeout?: number): Promise<ConnectionAcquisition<T>>;
|
|
79
|
+
/**
|
|
80
|
+
* Release a connection back to the pool
|
|
81
|
+
* @param connection Connection to release
|
|
82
|
+
*/
|
|
83
|
+
release(connection: T): Promise<void>;
|
|
84
|
+
/**
|
|
85
|
+
* Destroy a connection (remove from pool)
|
|
86
|
+
* @param connection Connection to destroy
|
|
87
|
+
*/
|
|
88
|
+
destroy(connection: T): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Execute a function with a connection from the pool
|
|
91
|
+
* @param fn Function to execute with connection
|
|
92
|
+
* @param timeout Operation timeout in milliseconds
|
|
93
|
+
* @returns Promise resolving to function result
|
|
94
|
+
*/
|
|
95
|
+
withConnection<R>(fn: (connection: T) => Promise<R>, timeout?: number): Promise<R>;
|
|
96
|
+
/**
|
|
97
|
+
* Create a new connection (factory method)
|
|
98
|
+
* @returns New connection instance
|
|
99
|
+
*/
|
|
100
|
+
createConnection(): Promise<T>;
|
|
101
|
+
/**
|
|
102
|
+
* Validate a connection
|
|
103
|
+
* @param connection Connection to validate
|
|
104
|
+
* @returns True if connection is valid
|
|
105
|
+
*/
|
|
106
|
+
validateConnection(connection: T): Promise<boolean>;
|
|
107
|
+
/**
|
|
108
|
+
* Drain the pool (gracefully close all connections)
|
|
109
|
+
* @param force Force immediate drain
|
|
110
|
+
*/
|
|
111
|
+
drain(force?: boolean): Promise<void>;
|
|
112
|
+
/**
|
|
113
|
+
* Clear the pool (force close all connections)
|
|
114
|
+
*/
|
|
115
|
+
clear(): Promise<void>;
|
|
116
|
+
/**
|
|
117
|
+
* Health check for the pool
|
|
118
|
+
*/
|
|
119
|
+
healthCheck(): Promise<{
|
|
120
|
+
healthy: boolean;
|
|
121
|
+
message?: string;
|
|
122
|
+
details?: Record<string, unknown>;
|
|
123
|
+
}>;
|
|
124
|
+
/**
|
|
125
|
+
* Get pool statistics
|
|
126
|
+
*/
|
|
127
|
+
getStats(): {
|
|
128
|
+
totalConnections: number;
|
|
129
|
+
activeConnections: number;
|
|
130
|
+
idleConnections: number;
|
|
131
|
+
waitingClients: number;
|
|
132
|
+
totalAcquisitions: number;
|
|
133
|
+
totalReleases: number;
|
|
134
|
+
totalDestructions: number;
|
|
135
|
+
totalErrors: number;
|
|
136
|
+
acquisitionTimeAverage: number;
|
|
137
|
+
acquisitionTimeMax: number;
|
|
138
|
+
poolUtilization: number;
|
|
139
|
+
connectionStats: Array<{
|
|
140
|
+
id: string;
|
|
141
|
+
createdAt: number;
|
|
142
|
+
lastUsedAt: number;
|
|
143
|
+
usageCount: number;
|
|
144
|
+
isHealthy: boolean;
|
|
145
|
+
}>;
|
|
146
|
+
};
|
|
147
|
+
/**
|
|
148
|
+
* Reset pool statistics
|
|
149
|
+
*/
|
|
150
|
+
resetStats(): void;
|
|
151
|
+
/**
|
|
152
|
+
* Update pool configuration
|
|
153
|
+
* @param config New configuration
|
|
154
|
+
*/
|
|
155
|
+
updateConfig(config: Partial<ConnectionPoolConfig>): void;
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Connection pool factory interface
|
|
159
|
+
*/
|
|
160
|
+
export interface IConnectionPoolFactory {
|
|
161
|
+
/**
|
|
162
|
+
* Create a new connection pool instance
|
|
163
|
+
* @param name Pool name
|
|
164
|
+
* @param config Pool configuration
|
|
165
|
+
* @param createConnection Connection factory function
|
|
166
|
+
* @param validateConnection Connection validation function
|
|
167
|
+
* @returns New connection pool instance
|
|
168
|
+
*/
|
|
169
|
+
create<T extends IConnection>(name: string, config: ConnectionPoolConfig, createConnection: () => Promise<T>, validateConnection?: (connection: T) => Promise<boolean>): IConnectionPool<T>;
|
|
170
|
+
/**
|
|
171
|
+
* Get an existing connection pool instance
|
|
172
|
+
* @param name Pool name
|
|
173
|
+
*/
|
|
174
|
+
get<T extends IConnection>(name: string): IConnectionPool<T> | undefined;
|
|
175
|
+
/**
|
|
176
|
+
* Get all connection pool instances
|
|
177
|
+
*/
|
|
178
|
+
getAll(): IConnectionPool[];
|
|
179
|
+
/**
|
|
180
|
+
* Drain all connection pools
|
|
181
|
+
*/
|
|
182
|
+
drainAll(): Promise<void>;
|
|
183
|
+
/**
|
|
184
|
+
* Clear all connection pools
|
|
185
|
+
*/
|
|
186
|
+
clearAll(): Promise<void>;
|
|
187
|
+
/**
|
|
188
|
+
* Get global connection pool statistics
|
|
189
|
+
*/
|
|
190
|
+
getGlobalStats(): Record<string, unknown>;
|
|
191
|
+
}
|