@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,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
+ }