@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,180 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract base compression manager implementation
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import type { ICompressionManager, CompressionAlgorithm, CompressionOptions, CompressionResult, DecompressionResult } from '../interfaces/Compression.interface.js';
|
|
6
|
+
import type { CompressionConfig } from '../types/config.js';
|
|
7
|
+
/**
|
|
8
|
+
* Abstract base compression manager implementation
|
|
9
|
+
* Provides common functionality for compression management
|
|
10
|
+
*/
|
|
11
|
+
export declare abstract class BaseCompressionManager implements ICompressionManager {
|
|
12
|
+
/** Compression manager name */
|
|
13
|
+
readonly name: string;
|
|
14
|
+
/** Compression configuration */
|
|
15
|
+
config: CompressionConfig;
|
|
16
|
+
/** Supported algorithms */
|
|
17
|
+
readonly supportedAlgorithms: CompressionAlgorithm[];
|
|
18
|
+
/** Compression statistics */
|
|
19
|
+
protected stats: {
|
|
20
|
+
totalCompressions: number;
|
|
21
|
+
totalDecompressions: number;
|
|
22
|
+
compressionErrors: number;
|
|
23
|
+
decompressionErrors: number;
|
|
24
|
+
totalCompressionTime: number;
|
|
25
|
+
totalDecompressionTime: number;
|
|
26
|
+
totalOriginalSize: number;
|
|
27
|
+
totalCompressedSize: number;
|
|
28
|
+
totalDecompressedSize: number;
|
|
29
|
+
averageCompressionTime: number;
|
|
30
|
+
averageDecompressionTime: number;
|
|
31
|
+
averageCompressionRatio: number;
|
|
32
|
+
algorithmUsage: Record<string, number>;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Create a new base compression manager instance
|
|
36
|
+
* @param name Compression manager name
|
|
37
|
+
* @param config Compression configuration
|
|
38
|
+
*/
|
|
39
|
+
constructor(name: string, config: CompressionConfig);
|
|
40
|
+
/**
|
|
41
|
+
* Initialize compression manager
|
|
42
|
+
*/
|
|
43
|
+
protected initialize(): void;
|
|
44
|
+
/**
|
|
45
|
+
* Detect supported compression algorithms
|
|
46
|
+
* @returns Array of supported algorithms
|
|
47
|
+
*/
|
|
48
|
+
protected abstract detectSupportedAlgorithms(): CompressionAlgorithm[];
|
|
49
|
+
/**
|
|
50
|
+
* Compress data using specific algorithm
|
|
51
|
+
* @param data Data to compress
|
|
52
|
+
* @param algorithm Compression algorithm
|
|
53
|
+
* @param level Compression level
|
|
54
|
+
* @returns Compressed data
|
|
55
|
+
*/
|
|
56
|
+
protected abstract compressData(data: Buffer | Uint8Array, algorithm: CompressionAlgorithm, level: number): Promise<Buffer | Uint8Array>;
|
|
57
|
+
/**
|
|
58
|
+
* Decompress data using specific algorithm
|
|
59
|
+
* @param data Data to decompress
|
|
60
|
+
* @param algorithm Compression algorithm
|
|
61
|
+
* @returns Decompressed data
|
|
62
|
+
*/
|
|
63
|
+
protected abstract decompressData(data: Buffer | Uint8Array, algorithm: CompressionAlgorithm): Promise<Buffer>;
|
|
64
|
+
/**
|
|
65
|
+
* Compress data
|
|
66
|
+
* @param data Data to compress
|
|
67
|
+
* @param options Compression options
|
|
68
|
+
* @returns Promise resolving to compression result
|
|
69
|
+
* @throws {CommunicationError} If compression fails
|
|
70
|
+
*/
|
|
71
|
+
compress(data: Buffer | string | Uint8Array, options?: CompressionOptions): Promise<CompressionResult>;
|
|
72
|
+
/**
|
|
73
|
+
* Compress data with detailed metrics
|
|
74
|
+
* @param data Data to compress
|
|
75
|
+
* @param options Compression options
|
|
76
|
+
* @returns Promise resolving to compression result with metrics
|
|
77
|
+
*/
|
|
78
|
+
compressWithMetrics(data: Buffer | string | Uint8Array, options?: CompressionOptions): Promise<CompressionResult>;
|
|
79
|
+
/**
|
|
80
|
+
* Decompress data
|
|
81
|
+
* @param data Data to decompress
|
|
82
|
+
* @param options Decompression options
|
|
83
|
+
* @returns Promise resolving to decompression result
|
|
84
|
+
* @throws {CommunicationError} If decompression fails
|
|
85
|
+
*/
|
|
86
|
+
decompress(data: Buffer | Uint8Array, options?: CompressionOptions): Promise<DecompressionResult>;
|
|
87
|
+
/**
|
|
88
|
+
* Detect compression algorithm from data
|
|
89
|
+
* @param data Compressed data
|
|
90
|
+
* @returns Detected algorithm or undefined
|
|
91
|
+
*/
|
|
92
|
+
protected detectAlgorithm(data: Buffer | Uint8Array): CompressionAlgorithm | undefined;
|
|
93
|
+
/**
|
|
94
|
+
* Check if algorithm is supported
|
|
95
|
+
* @param algorithm Algorithm to check
|
|
96
|
+
* @returns True if algorithm is supported
|
|
97
|
+
*/
|
|
98
|
+
isAlgorithmSupported(algorithm: CompressionAlgorithm): boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Get content encoding header for algorithm
|
|
101
|
+
* @param algorithm Compression algorithm
|
|
102
|
+
* @returns Content-Encoding header value
|
|
103
|
+
*/
|
|
104
|
+
getContentEncoding(algorithm: CompressionAlgorithm): string;
|
|
105
|
+
/**
|
|
106
|
+
* Get algorithm from content encoding header
|
|
107
|
+
* @param contentEncoding Content-Encoding header value
|
|
108
|
+
* @returns Compression algorithm or undefined
|
|
109
|
+
*/
|
|
110
|
+
getAlgorithmFromEncoding(contentEncoding: string): CompressionAlgorithm | undefined;
|
|
111
|
+
/**
|
|
112
|
+
* Should compress based on configuration
|
|
113
|
+
* @param dataSize Data size in bytes
|
|
114
|
+
* @param contentType Content type
|
|
115
|
+
* @returns True if should compress
|
|
116
|
+
*/
|
|
117
|
+
shouldCompress(dataSize: number, contentType?: string): boolean;
|
|
118
|
+
/**
|
|
119
|
+
* Create compression stream
|
|
120
|
+
* @param algorithm Compression algorithm
|
|
121
|
+
* @param options Compression options
|
|
122
|
+
* @returns Transform stream for compression
|
|
123
|
+
*/
|
|
124
|
+
createCompressionStream(algorithm: CompressionAlgorithm, options?: CompressionOptions): TransformStream;
|
|
125
|
+
/**
|
|
126
|
+
* Create decompression stream
|
|
127
|
+
* @param algorithm Compression algorithm
|
|
128
|
+
* @returns Transform stream for decompression
|
|
129
|
+
*/
|
|
130
|
+
createDecompressionStream(algorithm: CompressionAlgorithm): TransformStream;
|
|
131
|
+
/**
|
|
132
|
+
* Update compression statistics
|
|
133
|
+
* @param originalSize Original size in bytes
|
|
134
|
+
* @param compressedSize Compressed size in bytes
|
|
135
|
+
* @param compressionTime Compression time in milliseconds
|
|
136
|
+
* @param algorithm Compression algorithm used
|
|
137
|
+
*/
|
|
138
|
+
protected updateCompressionStats(originalSize: number, compressedSize: number, compressionTime: number, algorithm: CompressionAlgorithm): void;
|
|
139
|
+
/**
|
|
140
|
+
* Update decompression statistics
|
|
141
|
+
* @param originalSize Original compressed size in bytes
|
|
142
|
+
* @param decompressedSize Decompressed size in bytes
|
|
143
|
+
* @param decompressionTime Decompression time in milliseconds
|
|
144
|
+
* @param algorithm Decompression algorithm used
|
|
145
|
+
*/
|
|
146
|
+
protected updateDecompressionStats(originalSize: number, decompressedSize: number, decompressionTime: number, algorithm: CompressionAlgorithm): void;
|
|
147
|
+
/**
|
|
148
|
+
* Update compression configuration
|
|
149
|
+
* @param config New configuration
|
|
150
|
+
*/
|
|
151
|
+
updateConfig(config: Partial<CompressionConfig>): void;
|
|
152
|
+
/**
|
|
153
|
+
* Get compression statistics
|
|
154
|
+
*/
|
|
155
|
+
getStats(): {
|
|
156
|
+
totalCompressions: number;
|
|
157
|
+
totalDecompressions: number;
|
|
158
|
+
compressionErrors: number;
|
|
159
|
+
decompressionErrors: number;
|
|
160
|
+
averageCompressionTime: number;
|
|
161
|
+
averageDecompressionTime: number;
|
|
162
|
+
averageCompressionRatio: number;
|
|
163
|
+
algorithmUsage: Record<string, number>;
|
|
164
|
+
totalBytesCompressed: number;
|
|
165
|
+
totalBytesDecompressed: number;
|
|
166
|
+
bytesSaved: number;
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Reset compression statistics
|
|
170
|
+
*/
|
|
171
|
+
resetStats(): void;
|
|
172
|
+
/**
|
|
173
|
+
* Health check for compression manager
|
|
174
|
+
*/
|
|
175
|
+
healthCheck(): {
|
|
176
|
+
healthy: boolean;
|
|
177
|
+
message?: string;
|
|
178
|
+
details?: Record<string, unknown>;
|
|
179
|
+
};
|
|
180
|
+
}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract base connection pool implementation
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import type { IConnection, IConnectionPool, ConnectionAcquisition } from '../interfaces/ConnectionPool.interface.js';
|
|
6
|
+
import type { ConnectionPoolConfig } from '../types/config.js';
|
|
7
|
+
/**
|
|
8
|
+
* Abstract base connection pool implementation
|
|
9
|
+
* Provides common functionality for connection pooling
|
|
10
|
+
*/
|
|
11
|
+
export declare abstract class BaseConnectionPool<T extends IConnection = IConnection> implements IConnectionPool<T> {
|
|
12
|
+
/** Pool name */
|
|
13
|
+
readonly name: string;
|
|
14
|
+
/** Pool configuration */
|
|
15
|
+
config: ConnectionPoolConfig;
|
|
16
|
+
/** Is pool healthy? */
|
|
17
|
+
isHealthy: boolean;
|
|
18
|
+
/** Total connections in pool */
|
|
19
|
+
totalConnections: number;
|
|
20
|
+
/** Active connections */
|
|
21
|
+
activeConnections: number;
|
|
22
|
+
/** Idle connections */
|
|
23
|
+
idleConnections: number;
|
|
24
|
+
/** Waiting clients */
|
|
25
|
+
waitingClients: number;
|
|
26
|
+
/** Connection factory function */
|
|
27
|
+
protected createConnectionFn: () => Promise<T>;
|
|
28
|
+
/** Connection validation function */
|
|
29
|
+
protected validateConnectionFn: (connection: T) => Promise<boolean>;
|
|
30
|
+
/** Connection pool */
|
|
31
|
+
protected pool: Set<T>;
|
|
32
|
+
/** Active connections set */
|
|
33
|
+
protected activeConnectionsSet: Set<T>;
|
|
34
|
+
/** Idle connections queue */
|
|
35
|
+
protected idleConnectionsQueue: T[];
|
|
36
|
+
/** Waiting clients queue */
|
|
37
|
+
protected waitingQueue: Array<{
|
|
38
|
+
resolve: (acquisition: ConnectionAcquisition<T>) => void;
|
|
39
|
+
reject: (error: Error) => void;
|
|
40
|
+
timeout: NodeJS.Timeout;
|
|
41
|
+
startTime: number;
|
|
42
|
+
}>;
|
|
43
|
+
/** Pool statistics */
|
|
44
|
+
protected stats: {
|
|
45
|
+
totalAcquisitions: number;
|
|
46
|
+
totalReleases: number;
|
|
47
|
+
totalDestructions: number;
|
|
48
|
+
totalErrors: number;
|
|
49
|
+
totalAcquisitionTime: number;
|
|
50
|
+
maxAcquisitionTime: number;
|
|
51
|
+
connectionStats: Map<string, {
|
|
52
|
+
createdAt: number;
|
|
53
|
+
lastUsedAt: number;
|
|
54
|
+
usageCount: number;
|
|
55
|
+
totalUsageTime: number;
|
|
56
|
+
isHealthy: boolean;
|
|
57
|
+
}>;
|
|
58
|
+
};
|
|
59
|
+
/** Cleanup interval */
|
|
60
|
+
protected cleanupInterval?: NodeJS.Timeout;
|
|
61
|
+
/** Health check interval */
|
|
62
|
+
protected healthCheckInterval?: NodeJS.Timeout;
|
|
63
|
+
/**
|
|
64
|
+
* Create a new base connection pool instance
|
|
65
|
+
* @param name Pool name
|
|
66
|
+
* @param config Pool configuration
|
|
67
|
+
* @param createConnection Connection factory function
|
|
68
|
+
* @param validateConnection Connection validation function
|
|
69
|
+
*/
|
|
70
|
+
constructor(name: string, config: ConnectionPoolConfig, createConnection: () => Promise<T>, validateConnection?: (connection: T) => Promise<boolean>);
|
|
71
|
+
/**
|
|
72
|
+
* Initialize connection pool
|
|
73
|
+
*/
|
|
74
|
+
protected initialize(): Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Warm up the pool with minimum connections
|
|
77
|
+
*/
|
|
78
|
+
protected warmupPool(): Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Acquire a connection from the pool
|
|
81
|
+
* @param timeout Acquisition timeout in milliseconds
|
|
82
|
+
* @returns Promise resolving to connection acquisition
|
|
83
|
+
* @throws {CommunicationError} If timeout or pool exhausted
|
|
84
|
+
*/
|
|
85
|
+
acquire(timeout?: number): Promise<ConnectionAcquisition<T>>;
|
|
86
|
+
/**
|
|
87
|
+
* Get or create a connection
|
|
88
|
+
* @returns Connection or null if cannot create
|
|
89
|
+
*/
|
|
90
|
+
protected getOrCreateConnection(): Promise<T | null>;
|
|
91
|
+
/**
|
|
92
|
+
* Create a new connection (factory method)
|
|
93
|
+
* @returns New connection instance
|
|
94
|
+
*/
|
|
95
|
+
abstract createConnection(): Promise<T>;
|
|
96
|
+
/**
|
|
97
|
+
* Validate a connection
|
|
98
|
+
* @param connection Connection to validate
|
|
99
|
+
* @returns True if connection is valid
|
|
100
|
+
*/
|
|
101
|
+
validateConnection(connection: T): Promise<boolean>;
|
|
102
|
+
/**
|
|
103
|
+
* Release a connection back to the pool
|
|
104
|
+
* @param connection Connection to release
|
|
105
|
+
*/
|
|
106
|
+
release(connection: T): Promise<void>;
|
|
107
|
+
/**
|
|
108
|
+
* Destroy a connection (remove from pool)
|
|
109
|
+
* @param connection Connection to destroy
|
|
110
|
+
*/
|
|
111
|
+
destroy(connection: T): Promise<void>;
|
|
112
|
+
/**
|
|
113
|
+
* Destroy a connection (internal)
|
|
114
|
+
* @param connection Connection to destroy
|
|
115
|
+
*/
|
|
116
|
+
protected destroyConnection(connection: T): Promise<void>;
|
|
117
|
+
/**
|
|
118
|
+
* Execute a function with a connection from the pool
|
|
119
|
+
* @param fn Function to execute with connection
|
|
120
|
+
* @param timeout Operation timeout in milliseconds
|
|
121
|
+
* @returns Promise resolving to function result
|
|
122
|
+
*/
|
|
123
|
+
withConnection<R>(fn: (connection: T) => Promise<R>, timeout?: number): Promise<R>;
|
|
124
|
+
/**
|
|
125
|
+
* Record connection acquisition
|
|
126
|
+
* @param startTime Acquisition start time
|
|
127
|
+
* @param connection Acquired connection
|
|
128
|
+
*/
|
|
129
|
+
protected recordAcquisition(startTime: number, connection: T): void;
|
|
130
|
+
/**
|
|
131
|
+
* Create acquisition result
|
|
132
|
+
* @param connection Acquired connection
|
|
133
|
+
* @param waitTime Wait time in milliseconds
|
|
134
|
+
* @returns Connection acquisition result
|
|
135
|
+
*/
|
|
136
|
+
protected createAcquisitionResult(connection: T, waitTime: number): ConnectionAcquisition<T>;
|
|
137
|
+
/**
|
|
138
|
+
* Update connection statistics
|
|
139
|
+
* @param connection Connection
|
|
140
|
+
* @param released Whether connection was released
|
|
141
|
+
*/
|
|
142
|
+
protected updateConnectionStats(connection: T, released: boolean): void;
|
|
143
|
+
/**
|
|
144
|
+
* Remove waiting client from queue
|
|
145
|
+
* @param reject Reject function
|
|
146
|
+
* @param startTime Start time
|
|
147
|
+
*/
|
|
148
|
+
protected removeWaitingClient(reject: (error: Error) => void, startTime: number): void;
|
|
149
|
+
/**
|
|
150
|
+
* Notify waiting clients
|
|
151
|
+
*/
|
|
152
|
+
protected notifyWaitingClients(): Promise<void>;
|
|
153
|
+
/**
|
|
154
|
+
* Clean up idle connections
|
|
155
|
+
*/
|
|
156
|
+
protected cleanupIdleConnections(): Promise<void>;
|
|
157
|
+
/**
|
|
158
|
+
* Check pool health
|
|
159
|
+
*/
|
|
160
|
+
protected checkPoolHealth(): Promise<void>;
|
|
161
|
+
/**
|
|
162
|
+
* Drain the pool (gracefully close all connections)
|
|
163
|
+
* @param force Force immediate drain
|
|
164
|
+
*/
|
|
165
|
+
drain(force?: boolean): Promise<void>;
|
|
166
|
+
/**
|
|
167
|
+
* Clear the pool (force close all connections)
|
|
168
|
+
*/
|
|
169
|
+
clear(): Promise<void>;
|
|
170
|
+
/**
|
|
171
|
+
* Health check for the pool
|
|
172
|
+
*/
|
|
173
|
+
healthCheck(): Promise<{
|
|
174
|
+
healthy: boolean;
|
|
175
|
+
message?: string;
|
|
176
|
+
details?: Record<string, unknown>;
|
|
177
|
+
}>;
|
|
178
|
+
/**
|
|
179
|
+
* Get pool statistics
|
|
180
|
+
*/
|
|
181
|
+
getStats(): {
|
|
182
|
+
totalConnections: number;
|
|
183
|
+
activeConnections: number;
|
|
184
|
+
idleConnections: number;
|
|
185
|
+
waitingClients: number;
|
|
186
|
+
totalAcquisitions: number;
|
|
187
|
+
totalReleases: number;
|
|
188
|
+
totalDestructions: number;
|
|
189
|
+
totalErrors: number;
|
|
190
|
+
acquisitionTimeAverage: number;
|
|
191
|
+
acquisitionTimeMax: number;
|
|
192
|
+
poolUtilization: number;
|
|
193
|
+
connectionStats: Array<{
|
|
194
|
+
id: string;
|
|
195
|
+
createdAt: number;
|
|
196
|
+
lastUsedAt: number;
|
|
197
|
+
usageCount: number;
|
|
198
|
+
isHealthy: boolean;
|
|
199
|
+
}>;
|
|
200
|
+
};
|
|
201
|
+
/**
|
|
202
|
+
* Reset pool statistics
|
|
203
|
+
*/
|
|
204
|
+
resetStats(): void;
|
|
205
|
+
/**
|
|
206
|
+
* Update pool configuration
|
|
207
|
+
* @param config New configuration
|
|
208
|
+
*/
|
|
209
|
+
updateConfig(config: Partial<ConnectionPoolConfig>): void;
|
|
210
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Abstract base interceptor implementation
|
|
3
|
+
* @packageDocumentation
|
|
4
|
+
*/
|
|
5
|
+
import type { CommunicationErrorType } from '../errors/CommunicationError.js';
|
|
6
|
+
import type { Request, RequestContext } from '../types/request.js';
|
|
7
|
+
import type { IInterceptor, InterceptorOrder } from '../interfaces/Interceptor.interface.js';
|
|
8
|
+
import type { Response } from '../types/response.js';
|
|
9
|
+
/**
|
|
10
|
+
* Abstract base interceptor implementation
|
|
11
|
+
* Provides common functionality for all interceptor implementations
|
|
12
|
+
*/
|
|
13
|
+
export declare abstract class BaseInterceptor<TRequest = Request, TResponse = Response> implements IInterceptor<TRequest, TResponse> {
|
|
14
|
+
/** Interceptor name */
|
|
15
|
+
readonly name: string;
|
|
16
|
+
/** Interceptor version */
|
|
17
|
+
readonly version?: string;
|
|
18
|
+
/** Interceptor description */
|
|
19
|
+
readonly description?: string;
|
|
20
|
+
/** Whether interceptor is enabled */
|
|
21
|
+
enabled: boolean;
|
|
22
|
+
/** Interceptor execution order */
|
|
23
|
+
readonly order: InterceptorOrder;
|
|
24
|
+
/** Interceptor statistics */
|
|
25
|
+
protected stats: {
|
|
26
|
+
totalRequests: number;
|
|
27
|
+
totalResponses: number;
|
|
28
|
+
totalErrors: number;
|
|
29
|
+
modifications: number;
|
|
30
|
+
totalProcessingTime: number;
|
|
31
|
+
averageProcessingTime: number;
|
|
32
|
+
lastError?: string;
|
|
33
|
+
};
|
|
34
|
+
/** Configuration */
|
|
35
|
+
protected config: Record<string, unknown>;
|
|
36
|
+
/** Last processing time */
|
|
37
|
+
protected lastProcessingTime?: number;
|
|
38
|
+
/**
|
|
39
|
+
* Create a new base interceptor instance
|
|
40
|
+
* @param name Interceptor name
|
|
41
|
+
* @param options Interceptor options
|
|
42
|
+
*/
|
|
43
|
+
constructor(name: string, options?: Record<string, unknown>);
|
|
44
|
+
/**
|
|
45
|
+
* Setup interceptor (internal initialization)
|
|
46
|
+
* @param options Initialization options
|
|
47
|
+
*/
|
|
48
|
+
protected setup(options?: Record<string, unknown>): void;
|
|
49
|
+
/**
|
|
50
|
+
* Intercept request before it's sent
|
|
51
|
+
* @param request Original request
|
|
52
|
+
* @param context Request context
|
|
53
|
+
* @returns Modified request or void
|
|
54
|
+
*/
|
|
55
|
+
abstract onRequest?(request: TRequest, context: RequestContext): TRequest | void | Promise<TRequest | void>;
|
|
56
|
+
/**
|
|
57
|
+
* Intercept response after it's received
|
|
58
|
+
* @param response Original response
|
|
59
|
+
* @param context Request context
|
|
60
|
+
* @returns Modified response or void
|
|
61
|
+
*/
|
|
62
|
+
abstract onResponse?(response: TResponse, context: RequestContext): TResponse | void | Promise<TResponse | void>;
|
|
63
|
+
/**
|
|
64
|
+
* Intercept error when request fails
|
|
65
|
+
* @param error Error that occurred
|
|
66
|
+
* @param context Request context
|
|
67
|
+
* @returns Modified error, new response, or void
|
|
68
|
+
*/
|
|
69
|
+
abstract onError?(error: CommunicationErrorType, context: RequestContext): CommunicationErrorType | TResponse | void | Promise<CommunicationErrorType | TResponse | void>;
|
|
70
|
+
/**
|
|
71
|
+
* Initialize interceptor (called when added to client/protocol)
|
|
72
|
+
* @param options Initialization options
|
|
73
|
+
*/
|
|
74
|
+
initialize(options?: Record<string, unknown>): void | Promise<void>;
|
|
75
|
+
/**
|
|
76
|
+
* Cleanup interceptor (called when removed from client/protocol)
|
|
77
|
+
*/
|
|
78
|
+
cleanup(): void | Promise<void>;
|
|
79
|
+
/**
|
|
80
|
+
* Get interceptor configuration
|
|
81
|
+
*/
|
|
82
|
+
getConfig(): Record<string, unknown>;
|
|
83
|
+
/**
|
|
84
|
+
* Update interceptor configuration
|
|
85
|
+
* @param config New configuration
|
|
86
|
+
*/
|
|
87
|
+
updateConfig(config: Record<string, unknown>): void;
|
|
88
|
+
/**
|
|
89
|
+
* Health check for the interceptor
|
|
90
|
+
*/
|
|
91
|
+
healthCheck(): {
|
|
92
|
+
healthy: boolean;
|
|
93
|
+
message?: string;
|
|
94
|
+
details?: Record<string, unknown>;
|
|
95
|
+
};
|
|
96
|
+
/**
|
|
97
|
+
* Get interceptor statistics
|
|
98
|
+
*/
|
|
99
|
+
getStats(): {
|
|
100
|
+
totalRequests: number;
|
|
101
|
+
totalResponses: number;
|
|
102
|
+
totalErrors: number;
|
|
103
|
+
modifications: number;
|
|
104
|
+
averageProcessingTime: number;
|
|
105
|
+
lastError?: string;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Reset interceptor statistics
|
|
109
|
+
*/
|
|
110
|
+
resetStats(): void;
|
|
111
|
+
/**
|
|
112
|
+
* Execute request interception with timing and error handling
|
|
113
|
+
* @param request Original request
|
|
114
|
+
* @param context Request context
|
|
115
|
+
* @returns Modified request or original if not modified
|
|
116
|
+
*/
|
|
117
|
+
protected executeOnRequest(request: TRequest, context: RequestContext): Promise<TRequest>;
|
|
118
|
+
/**
|
|
119
|
+
* Execute response interception with timing and error handling
|
|
120
|
+
* @param response Original response
|
|
121
|
+
* @param context Request context
|
|
122
|
+
* @returns Modified response or original if not modified
|
|
123
|
+
*/
|
|
124
|
+
protected executeOnResponse(response: TResponse, context: RequestContext): Promise<TResponse>;
|
|
125
|
+
/**
|
|
126
|
+
* Execute error interception with timing and error handling
|
|
127
|
+
* @param error Original error
|
|
128
|
+
* @param context Request context
|
|
129
|
+
* @returns Modified error or response, or original error if not modified
|
|
130
|
+
*/
|
|
131
|
+
protected executeOnError(error: CommunicationErrorType, context: RequestContext): Promise<CommunicationErrorType | TResponse>;
|
|
132
|
+
/**
|
|
133
|
+
* Update request statistics
|
|
134
|
+
* @param processingTime Processing time in milliseconds
|
|
135
|
+
* @param modified Whether request was modified
|
|
136
|
+
*/
|
|
137
|
+
protected updateRequestStats(processingTime: number, modified: boolean): void;
|
|
138
|
+
/**
|
|
139
|
+
* Update response statistics
|
|
140
|
+
* @param processingTime Processing time in milliseconds
|
|
141
|
+
* @param modified Whether response was modified
|
|
142
|
+
*/
|
|
143
|
+
protected updateResponseStats(processingTime: number, modified: boolean): void;
|
|
144
|
+
/**
|
|
145
|
+
* Update error statistics
|
|
146
|
+
* @param processingTime Processing time in milliseconds
|
|
147
|
+
* @param modified Whether error was modified
|
|
148
|
+
*/
|
|
149
|
+
protected updateErrorStats(processingTime: number, modified: boolean): void;
|
|
150
|
+
}
|