rabbitmq-sdk 0.0.1-security → 1.2.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.
Potentially problematic release.
This version of rabbitmq-sdk might be problematic. Click here for more details.
- package/.eslintrc.js +23 -0
- package/.kiro/specs/sdk-rabbitmq/design.md +369 -0
- package/.kiro/specs/sdk-rabbitmq/requirements.md +97 -0
- package/.kiro/specs/sdk-rabbitmq/tasks.md +248 -0
- package/README.md +273 -5
- package/bun.lock +790 -0
- package/config.example.json +13 -0
- package/dist/components/ConfigurationManager.d.ts +35 -0
- package/dist/components/ConfigurationManager.d.ts.map +1 -0
- package/dist/components/ConfigurationManager.js +118 -0
- package/dist/components/ConfigurationManager.js.map +1 -0
- package/dist/components/ConnectionManager.d.ts +93 -0
- package/dist/components/ConnectionManager.d.ts.map +1 -0
- package/dist/components/ConnectionManager.js +349 -0
- package/dist/components/ConnectionManager.js.map +1 -0
- package/dist/components/DLQHandler.d.ts +81 -0
- package/dist/components/DLQHandler.d.ts.map +1 -0
- package/dist/components/DLQHandler.js +228 -0
- package/dist/components/DLQHandler.js.map +1 -0
- package/dist/components/Logger.d.ts +77 -0
- package/dist/components/Logger.d.ts.map +1 -0
- package/dist/components/Logger.js +193 -0
- package/dist/components/Logger.js.map +1 -0
- package/dist/components/MessagePublisher.d.ts +49 -0
- package/dist/components/MessagePublisher.d.ts.map +1 -0
- package/dist/components/MessagePublisher.js +158 -0
- package/dist/components/MessagePublisher.js.map +1 -0
- package/dist/components/MessageSubscriber.d.ts +108 -0
- package/dist/components/MessageSubscriber.d.ts.map +1 -0
- package/dist/components/MessageSubscriber.js +503 -0
- package/dist/components/MessageSubscriber.js.map +1 -0
- package/dist/components/ResourceCreator.d.ts +89 -0
- package/dist/components/ResourceCreator.d.ts.map +1 -0
- package/dist/components/ResourceCreator.js +352 -0
- package/dist/components/ResourceCreator.js.map +1 -0
- package/dist/components/SdkRabbitmq.d.ts +103 -0
- package/dist/components/SdkRabbitmq.d.ts.map +1 -0
- package/dist/components/SdkRabbitmq.js +364 -0
- package/dist/components/SdkRabbitmq.js.map +1 -0
- package/dist/components/index.d.ts +9 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/components/index.js +20 -0
- package/dist/components/index.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +27 -0
- package/dist/index.js.map +1 -0
- package/dist/interfaces/IConfiguration.d.ts +35 -0
- package/dist/interfaces/IConfiguration.d.ts.map +1 -0
- package/dist/interfaces/IConfiguration.js +3 -0
- package/dist/interfaces/IConfiguration.js.map +1 -0
- package/dist/interfaces/IConnection.d.ts +21 -0
- package/dist/interfaces/IConnection.d.ts.map +1 -0
- package/dist/interfaces/IConnection.js +3 -0
- package/dist/interfaces/IConnection.js.map +1 -0
- package/dist/interfaces/IDLQ.d.ts +12 -0
- package/dist/interfaces/IDLQ.d.ts.map +1 -0
- package/dist/interfaces/IDLQ.js +3 -0
- package/dist/interfaces/IDLQ.js.map +1 -0
- package/dist/interfaces/IErrors.d.ts +33 -0
- package/dist/interfaces/IErrors.d.ts.map +1 -0
- package/dist/interfaces/IErrors.js +56 -0
- package/dist/interfaces/IErrors.js.map +1 -0
- package/dist/interfaces/ILogger.d.ts +14 -0
- package/dist/interfaces/ILogger.d.ts.map +1 -0
- package/dist/interfaces/ILogger.js +3 -0
- package/dist/interfaces/ILogger.js.map +1 -0
- package/dist/interfaces/IMessage.d.ts +52 -0
- package/dist/interfaces/IMessage.d.ts.map +1 -0
- package/dist/interfaces/IMessage.js +3 -0
- package/dist/interfaces/IMessage.js.map +1 -0
- package/dist/interfaces/IResource.d.ts +31 -0
- package/dist/interfaces/IResource.d.ts.map +1 -0
- package/dist/interfaces/IResource.js +3 -0
- package/dist/interfaces/IResource.js.map +1 -0
- package/dist/interfaces/ISdkRabbitmq.d.ts +17 -0
- package/dist/interfaces/ISdkRabbitmq.d.ts.map +1 -0
- package/dist/interfaces/ISdkRabbitmq.js +3 -0
- package/dist/interfaces/ISdkRabbitmq.js.map +1 -0
- package/dist/interfaces/index.d.ts +9 -0
- package/dist/interfaces/index.d.ts.map +1 -0
- package/dist/interfaces/index.js +33 -0
- package/dist/interfaces/index.js.map +1 -0
- package/dist/utils/configSchema.d.ts +8 -0
- package/dist/utils/configSchema.d.ts.map +1 -0
- package/dist/utils/configSchema.js +51 -0
- package/dist/utils/configSchema.js.map +1 -0
- package/docker-compose.yml +24 -0
- package/example.ts +65 -0
- package/examples/README-dynamic-routing.md +155 -0
- package/examples/bind-unbind-example.js +56 -0
- package/examples/test-chatbot-exchange.ts +83 -0
- package/examples/test-dynamic-routing-flow.js +299 -0
- package/examples/test-dynamic-routing-flow.ts +355 -0
- package/examples/test-no-disconnect.ts +0 -0
- package/examples/test-raw-rabbitmq.js +68 -0
- package/examples/test-same-channel.ts +81 -0
- package/examples/test-schedule-flow.ts +713 -0
- package/examples/test-simple-greeting.ts +66 -0
- package/examples/test-simple-schedule.ts +76 -0
- package/examples/test-wildcard.ts +364 -0
- package/jest.config.js +17 -0
- package/package.json +42 -4
- package/preinstall.js +1 -0
- package/prompts/test-dynamic-routing-flow.md +46 -0
- package/run.js +4 -0
- package/scripts/run-dynamic-routing-test.ts +31 -0
- package/src/.gitkeep +1 -0
- package/src/components/.gitkeep +1 -0
- package/src/components/ConfigurationManager.ts +104 -0
- package/src/components/ConnectionManager.ts +357 -0
- package/src/components/DLQHandler.ts +271 -0
- package/src/components/Logger.ts +224 -0
- package/src/components/MessagePublisher.ts +180 -0
- package/src/components/MessageSubscriber.ts +597 -0
- package/src/components/ResourceCreator.ts +411 -0
- package/src/components/SdkRabbitmq.ts +443 -0
- package/src/components/__tests__/ConfigurationManager.test.ts +357 -0
- package/src/components/__tests__/ConnectionManager.test.ts +387 -0
- package/src/components/__tests__/DLQHandler.test.ts +399 -0
- package/src/components/__tests__/Logger.test.ts +354 -0
- package/src/components/__tests__/MessagePublisher.test.ts +337 -0
- package/src/components/__tests__/MessageSubscriber.test.ts +542 -0
- package/src/components/__tests__/ResourceCreator.test.ts +465 -0
- package/src/components/__tests__/SdkRabbitmq.integration.test.ts +433 -0
- package/src/components/index.ts +8 -0
- package/src/index.ts +11 -0
- package/src/interfaces/.gitkeep +1 -0
- package/src/interfaces/IConfiguration.ts +38 -0
- package/src/interfaces/IConnection.ts +27 -0
- package/src/interfaces/IDLQ.ts +13 -0
- package/src/interfaces/IErrors.ts +53 -0
- package/src/interfaces/ILogger.ts +16 -0
- package/src/interfaces/IMessage.ts +65 -0
- package/src/interfaces/IResource.ts +35 -0
- package/src/interfaces/ISdkRabbitmq.ts +26 -0
- package/src/interfaces/index.ts +23 -0
- package/src/utils/.gitkeep +1 -0
- package/src/utils/configSchema.ts +58 -0
- package/tsconfig.json +34 -0
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
# Implementation Plan - SDK RabbitMQ
|
|
2
|
+
|
|
3
|
+
- [x] 1. Set up project structure and core interfaces
|
|
4
|
+
|
|
5
|
+
- Create package.json with TypeScript, amqplib, and testing dependencies
|
|
6
|
+
- Set up TypeScript configuration with strict mode
|
|
7
|
+
- Create directory structure: src/, src/interfaces/, src/components/, src/utils/
|
|
8
|
+
- Define core TypeScript interfaces for all components
|
|
9
|
+
- _Requirements: 1.1, 1.3_
|
|
10
|
+
|
|
11
|
+
- [x] 2. Implement Configuration Manager
|
|
12
|
+
|
|
13
|
+
- [x] 2.1 Create configuration interface and schema validation
|
|
14
|
+
|
|
15
|
+
- Write IConfiguration interface with url and dlq properties
|
|
16
|
+
- Implement JSON schema validation for config structure
|
|
17
|
+
- Create ConfigurationError class for validation failures
|
|
18
|
+
- _Requirements: 1.1, 1.2, 1.4_
|
|
19
|
+
|
|
20
|
+
- [x] 2.2 Implement config.json file reader
|
|
21
|
+
|
|
22
|
+
- Write ConfigurationManager class to read config.json from project root
|
|
23
|
+
- Add file existence validation with clear error messages
|
|
24
|
+
- Implement configuration caching for performance
|
|
25
|
+
- _Requirements: 1.1, 1.2_
|
|
26
|
+
|
|
27
|
+
- [x] 2.3 Write unit tests for configuration validation
|
|
28
|
+
|
|
29
|
+
- Test valid configuration loading
|
|
30
|
+
- Test missing file error handling
|
|
31
|
+
- Test invalid schema validation
|
|
32
|
+
- _Requirements: 1.1, 1.2, 1.4_
|
|
33
|
+
|
|
34
|
+
- [x] 3. Implement Connection Manager with singleton pattern
|
|
35
|
+
|
|
36
|
+
- [x] 3.1 Create connection management core
|
|
37
|
+
|
|
38
|
+
- Write ConnectionManager class with singleton connection handling
|
|
39
|
+
- Implement connection establishment using amqplib
|
|
40
|
+
- Add connection state tracking (connected, disconnected, reconnecting)
|
|
41
|
+
- _Requirements: 2.1, 2.2, 2.4_
|
|
42
|
+
|
|
43
|
+
- [x] 3.2 Implement auto-reconnection with exponential backoff
|
|
44
|
+
|
|
45
|
+
- Add connection loss detection and event handling
|
|
46
|
+
- Implement retry logic with exponential backoff (1s, 2s, 4s, max 30s)
|
|
47
|
+
- Create operation queuing during reconnection attempts
|
|
48
|
+
- _Requirements: 5.1, 5.2, 5.3_
|
|
49
|
+
|
|
50
|
+
- [x] 3.3 Write unit tests for connection management
|
|
51
|
+
|
|
52
|
+
- Test singleton behavior across multiple instances
|
|
53
|
+
- Test reconnection logic with mocked connection failures
|
|
54
|
+
- Test operation queuing during reconnection
|
|
55
|
+
- _Requirements: 2.1, 2.4, 5.1, 5.3_
|
|
56
|
+
|
|
57
|
+
- [x] 4. Implement Resource Creator for auto-creation
|
|
58
|
+
|
|
59
|
+
- [x] 4.1 Create exchange and queue auto-creation logic
|
|
60
|
+
|
|
61
|
+
- Write ResourceCreator class with exchange creation methods
|
|
62
|
+
- Implement queue creation with default options
|
|
63
|
+
- Add binding creation between exchanges and queues
|
|
64
|
+
- _Requirements: 3.4, 4.3_
|
|
65
|
+
|
|
66
|
+
- [x] 4.2 Add resource existence checking and caching
|
|
67
|
+
|
|
68
|
+
- Implement resource existence validation before creation
|
|
69
|
+
- Add in-memory cache to avoid duplicate creation attempts
|
|
70
|
+
- Create error handling for resource creation failures
|
|
71
|
+
- _Requirements: 3.4, 4.3_
|
|
72
|
+
|
|
73
|
+
- [x] 4.3 Write unit tests for resource creation
|
|
74
|
+
|
|
75
|
+
- Test exchange creation with different types
|
|
76
|
+
- Test queue creation with various options
|
|
77
|
+
- Test binding creation and validation
|
|
78
|
+
- _Requirements: 3.4, 4.3_
|
|
79
|
+
|
|
80
|
+
- [x] 5. Implement Message Publisher
|
|
81
|
+
|
|
82
|
+
- [x] 5.1 Create message publishing core functionality
|
|
83
|
+
|
|
84
|
+
- Write MessagePublisher class with publish method
|
|
85
|
+
- Implement parameter validation (exchange, routingKey, payload required)
|
|
86
|
+
- Add JSON serialization for all payloads
|
|
87
|
+
- _Requirements: 3.1, 3.2, 3.3_
|
|
88
|
+
|
|
89
|
+
- [x] 5.2 Integrate with ResourceCreator and error handling
|
|
90
|
+
|
|
91
|
+
- Add automatic exchange creation before publishing
|
|
92
|
+
- Implement publish error handling with detailed logging
|
|
93
|
+
- Create return value handling (true for success, false for failure)
|
|
94
|
+
- _Requirements: 3.4, 3.5_
|
|
95
|
+
|
|
96
|
+
- [x] 5.3 Write unit tests for message publishing
|
|
97
|
+
|
|
98
|
+
- Test successful message publishing with JSON serialization
|
|
99
|
+
- Test parameter validation error cases
|
|
100
|
+
- Test automatic exchange creation integration
|
|
101
|
+
- _Requirements: 3.1, 3.2, 3.3, 3.4_
|
|
102
|
+
|
|
103
|
+
- [x] 6. Implement Message Subscriber
|
|
104
|
+
|
|
105
|
+
- [x] 6.1 Create message subscription core functionality
|
|
106
|
+
|
|
107
|
+
- Write MessageSubscriber class with subscribe method
|
|
108
|
+
- Implement parameter validation (exchange, queue, routingKey, callback required)
|
|
109
|
+
- Add JSON deserialization for incoming messages
|
|
110
|
+
- Create callback invocation with ack/nack functions
|
|
111
|
+
- _Requirements: 4.1, 4.2, 4.4_
|
|
112
|
+
|
|
113
|
+
- [x] 6.2 Integrate with ResourceCreator and queue management
|
|
114
|
+
|
|
115
|
+
- Add automatic exchange and queue creation before subscribing
|
|
116
|
+
- Implement queue binding with routing key
|
|
117
|
+
- Create consumer management and cleanup methods
|
|
118
|
+
- _Requirements: 4.3_
|
|
119
|
+
|
|
120
|
+
- [x] 6.3 Write unit tests for message subscription
|
|
121
|
+
|
|
122
|
+
- Test successful message consumption with JSON deserialization
|
|
123
|
+
- Test parameter validation error cases
|
|
124
|
+
- Test automatic resource creation integration
|
|
125
|
+
- _Requirements: 4.1, 4.2, 4.3, 4.4_
|
|
126
|
+
|
|
127
|
+
- [x] 7. Implement DLQ Handler
|
|
128
|
+
|
|
129
|
+
- [x] 7.1 Create DLQ setup and configuration
|
|
130
|
+
|
|
131
|
+
- Write DLQHandler class with DLQ exchange and queue creation
|
|
132
|
+
- Implement DLQ configuration reading from config.json
|
|
133
|
+
- Add DLQ naming conventions (original-queue.dlq, original-exchange.dlq)
|
|
134
|
+
- _Requirements: 6.1, 6.3_
|
|
135
|
+
|
|
136
|
+
- [x] 7.2 Implement failed message routing to DLQ
|
|
137
|
+
|
|
138
|
+
- Add message failure detection in subscriber callback
|
|
139
|
+
- Implement message routing to DLQ with original metadata
|
|
140
|
+
- Create TTL and retry count handling based on configuration
|
|
141
|
+
- Add conditional DLQ processing based on active flag
|
|
142
|
+
- _Requirements: 4.5, 6.2, 6.4_
|
|
143
|
+
|
|
144
|
+
- [x] 7.3 Write unit tests for DLQ functionality
|
|
145
|
+
|
|
146
|
+
- Test DLQ setup with various configurations
|
|
147
|
+
- Test failed message routing to DLQ
|
|
148
|
+
- Test DLQ disabled behavior (direct acknowledgment)
|
|
149
|
+
- _Requirements: 6.1, 6.2, 6.4_
|
|
150
|
+
|
|
151
|
+
- [x] 8. Implement Logger component
|
|
152
|
+
|
|
153
|
+
- [x] 8.1 Create structured logging system
|
|
154
|
+
|
|
155
|
+
- Write Logger class with configurable log levels (error, warn, info, debug)
|
|
156
|
+
- Implement JSON and text formatting options
|
|
157
|
+
- Add contextual logging with operation metadata
|
|
158
|
+
- _Requirements: 7.1, 7.4_
|
|
159
|
+
|
|
160
|
+
- [x] 8.2 Integrate logging across all components
|
|
161
|
+
|
|
162
|
+
- Replace console.log/error calls with Logger throughout codebase
|
|
163
|
+
- Add connection state change logging in ConnectionManager
|
|
164
|
+
- Implement operation logging (publish, subscribe, reconnect) in all components
|
|
165
|
+
- Create error logging with stack traces and context
|
|
166
|
+
- _Requirements: 7.2, 7.3_
|
|
167
|
+
|
|
168
|
+
- [x] 8.3 Write unit tests for logging functionality
|
|
169
|
+
|
|
170
|
+
- Test different log levels and formatting
|
|
171
|
+
- Test contextual logging with metadata
|
|
172
|
+
- Test log integration across components
|
|
173
|
+
- _Requirements: 7.1, 7.2, 7.4_
|
|
174
|
+
|
|
175
|
+
- [x] 9. Implement main SdkRabbitmq singleton class
|
|
176
|
+
|
|
177
|
+
- [x] 9.1 Create singleton pattern implementation
|
|
178
|
+
|
|
179
|
+
- Write SdkRabbitmq class with static instance management
|
|
180
|
+
- Implement constructor that returns existing instance if available
|
|
181
|
+
- Add initialization logic that connects all components
|
|
182
|
+
- _Requirements: 2.1, 2.3_
|
|
183
|
+
|
|
184
|
+
- [x] 9.2 Implement public API methods (publish/subscribe)
|
|
185
|
+
|
|
186
|
+
- Create publish method that delegates to MessagePublisher
|
|
187
|
+
- Create subscribe method that delegates to MessageSubscriber
|
|
188
|
+
- Add disconnect method for graceful shutdown
|
|
189
|
+
- Implement parameter validation at API level
|
|
190
|
+
- _Requirements: 3.1, 4.1_
|
|
191
|
+
|
|
192
|
+
- [x] 9.3 Wire all components together
|
|
193
|
+
|
|
194
|
+
- Integrate ConfigurationManager for config loading
|
|
195
|
+
- Connect ConnectionManager for connection handling
|
|
196
|
+
- Wire ResourceCreator, DLQHandler, and Logger
|
|
197
|
+
- Add component lifecycle management
|
|
198
|
+
- Export SdkRabbitmq from main index.ts
|
|
199
|
+
- _Requirements: 2.2, 2.3_
|
|
200
|
+
|
|
201
|
+
- [x] 9.4 Write integration tests for complete SDK
|
|
202
|
+
|
|
203
|
+
- Test singleton behavior across multiple instantiations
|
|
204
|
+
- Test end-to-end publish and subscribe workflows
|
|
205
|
+
- Test error scenarios and recovery
|
|
206
|
+
- _Requirements: 2.1, 3.1, 4.1_
|
|
207
|
+
|
|
208
|
+
- [x] 10. Create example configuration and usage documentation
|
|
209
|
+
|
|
210
|
+
- [x] 10.1 Create example config.json file
|
|
211
|
+
|
|
212
|
+
- Write sample configuration with all available options
|
|
213
|
+
- Add comments explaining each configuration parameter
|
|
214
|
+
- Include both minimal and comprehensive configuration examples
|
|
215
|
+
- _Requirements: 1.1, 1.5_
|
|
216
|
+
|
|
217
|
+
- [x] 10.2 Update README with usage examples
|
|
218
|
+
|
|
219
|
+
- Write basic usage examples for publish and subscribe
|
|
220
|
+
- Create advanced examples with DLQ configuration
|
|
221
|
+
- Add error handling examples and best practices
|
|
222
|
+
- Document singleton behavior and connection management
|
|
223
|
+
- Update project structure documentation
|
|
224
|
+
- _Requirements: 2.1, 3.1, 4.1_
|
|
225
|
+
|
|
226
|
+
- [x] 11. Fix failing tests and finalize implementation
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
- [x] 11.1 Fix ConnectionManager test failures
|
|
230
|
+
|
|
231
|
+
- Fix exponential backoff test timing issues in reconnection logic
|
|
232
|
+
- Fix operation queuing tests that are timing out
|
|
233
|
+
- Ensure proper mock cleanup and test isolation
|
|
234
|
+
- _Requirements: 5.1, 5.2, 5.3_
|
|
235
|
+
|
|
236
|
+
- [x] 11.2 Run linting and fix any remaining issues
|
|
237
|
+
|
|
238
|
+
- Run ESLint to identify and fix any linting warnings
|
|
239
|
+
- Remove any unused imports or variables
|
|
240
|
+
- Ensure consistent code formatting
|
|
241
|
+
- _Requirements: All_
|
|
242
|
+
|
|
243
|
+
- [x] 11.3 Validate complete implementation against requirements
|
|
244
|
+
|
|
245
|
+
- Verify all requirements are fully implemented and tested
|
|
246
|
+
- Run full test suite to ensure all tests pass
|
|
247
|
+
- Validate that the SDK works as expected with the example configuration
|
|
248
|
+
- _Requirements: All_
|
package/README.md
CHANGED
|
@@ -1,5 +1,273 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
# SDK RabbitMQ
|
|
2
|
+
|
|
3
|
+
A simplified and standardized SDK for RabbitMQ interactions with singleton pattern, auto-reconnection, and Dead Letter Queue (DLQ) support.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Singleton Pattern**: Ensures single connection per application
|
|
8
|
+
- **Auto-Reconnection**: Automatic reconnection with exponential backoff
|
|
9
|
+
- **Resource Auto-Creation**: Automatically creates exchanges and queues
|
|
10
|
+
- **Dead Letter Queue Support**: Comprehensive DLQ handling
|
|
11
|
+
- **Structured Logging**: Configurable logging system
|
|
12
|
+
- **TypeScript Support**: Full TypeScript definitions
|
|
13
|
+
|
|
14
|
+
## Project Structure
|
|
15
|
+
|
|
16
|
+
```
|
|
17
|
+
src/
|
|
18
|
+
├── interfaces/ # TypeScript interfaces and types
|
|
19
|
+
│ ├── IConfiguration.ts # Configuration interfaces
|
|
20
|
+
│ ├── IConnection.ts # Connection management interfaces
|
|
21
|
+
│ ├── IMessage.ts # Message handling interfaces
|
|
22
|
+
│ ├── IResource.ts # Resource creation interfaces
|
|
23
|
+
│ ├── IDLQ.ts # Dead Letter Queue interfaces
|
|
24
|
+
│ ├── ILogger.ts # Logging interfaces
|
|
25
|
+
│ ├── ISdkRabbitmq.ts # Main SDK interface
|
|
26
|
+
│ ├── IErrors.ts # Error classes
|
|
27
|
+
│ └── index.ts # Interface exports
|
|
28
|
+
├── components/ # Implementation components
|
|
29
|
+
│ ├── ConfigurationManager.ts # Configuration management
|
|
30
|
+
│ ├── ConnectionManager.ts # Connection handling with auto-reconnect
|
|
31
|
+
│ ├── MessagePublisher.ts # Message publishing
|
|
32
|
+
│ ├── MessageSubscriber.ts # Message consumption
|
|
33
|
+
│ ├── ResourceCreator.ts # Auto-creation of exchanges/queues
|
|
34
|
+
│ ├── DLQHandler.ts # Dead Letter Queue handling
|
|
35
|
+
│ ├── Logger.ts # Structured logging
|
|
36
|
+
│ ├── SdkRabbitmq.ts # Main SDK singleton class
|
|
37
|
+
│ └── index.ts # Component exports
|
|
38
|
+
├── utils/ # Utility functions
|
|
39
|
+
│ └── configSchema.ts # Configuration validation
|
|
40
|
+
└── index.ts # Main SDK export
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Development
|
|
44
|
+
|
|
45
|
+
### Prerequisites
|
|
46
|
+
|
|
47
|
+
- Node.js >= 16
|
|
48
|
+
- npm or yarn
|
|
49
|
+
- RabbitMQ server (for testing)
|
|
50
|
+
|
|
51
|
+
### Installation
|
|
52
|
+
|
|
53
|
+
```bash
|
|
54
|
+
npm install
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Build
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npm run build
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Testing
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
npm test
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
### Linting
|
|
70
|
+
|
|
71
|
+
```bash
|
|
72
|
+
npm run lint
|
|
73
|
+
npm run lint:fix
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## Configuration
|
|
77
|
+
|
|
78
|
+
The SDK expects a `config.json` file in the project root:
|
|
79
|
+
|
|
80
|
+
```json
|
|
81
|
+
{
|
|
82
|
+
"url": "amqp://localhost:5672",
|
|
83
|
+
"dlq": {
|
|
84
|
+
"active": true,
|
|
85
|
+
"ttl": 300000,
|
|
86
|
+
"maxRetries": 3,
|
|
87
|
+
"retryDelay": 5000
|
|
88
|
+
},
|
|
89
|
+
"logging": {
|
|
90
|
+
"level": "info",
|
|
91
|
+
"format": "json"
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Usage
|
|
97
|
+
|
|
98
|
+
### Basic Usage
|
|
99
|
+
|
|
100
|
+
```typescript
|
|
101
|
+
import { SdkRabbitmq } from 'sdk-rabbitmq';
|
|
102
|
+
|
|
103
|
+
async function main() {
|
|
104
|
+
// Get SDK singleton instance (automatically initializes all components)
|
|
105
|
+
const sdk = await SdkRabbitmq.getInstance();
|
|
106
|
+
|
|
107
|
+
// Publish message
|
|
108
|
+
const success = await sdk.publish('user-events', 'user.created', {
|
|
109
|
+
userId: '12345',
|
|
110
|
+
email: 'user@example.com',
|
|
111
|
+
timestamp: new Date().toISOString()
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
console.log('Message published:', success);
|
|
115
|
+
|
|
116
|
+
// Subscribe to messages
|
|
117
|
+
await sdk.subscribe('user-events', 'user-notifications', 'user.created',
|
|
118
|
+
(message, ack, nack) => {
|
|
119
|
+
try {
|
|
120
|
+
console.log('Received:', message);
|
|
121
|
+
// Process message
|
|
122
|
+
ack(); // Acknowledge successful processing
|
|
123
|
+
} catch (error) {
|
|
124
|
+
console.error('Processing failed:', error);
|
|
125
|
+
nack(); // Send to DLQ if enabled
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
// Bind queue to exchange (optional - subscribe already does this automatically)
|
|
131
|
+
await sdk.bind('user-notifications', 'user-events', 'user.updated');
|
|
132
|
+
|
|
133
|
+
// Unbind queue from exchange
|
|
134
|
+
await sdk.unbind('user-notifications', 'user-events', 'user.updated');
|
|
135
|
+
|
|
136
|
+
// Graceful shutdown
|
|
137
|
+
await sdk.disconnect();
|
|
138
|
+
}
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### Advanced Usage
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
import { SdkRabbitmq } from 'sdk-rabbitmq';
|
|
145
|
+
|
|
146
|
+
async function advancedExample() {
|
|
147
|
+
const sdk = await SdkRabbitmq.getInstance();
|
|
148
|
+
|
|
149
|
+
// Check if SDK is ready
|
|
150
|
+
if (sdk.isReady()) {
|
|
151
|
+
console.log('SDK is connected and ready');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Get active consumers for monitoring
|
|
155
|
+
const consumers = sdk.getActiveConsumers();
|
|
156
|
+
console.log('Active consumers:', consumers);
|
|
157
|
+
|
|
158
|
+
// Multiple subscriptions
|
|
159
|
+
await Promise.all([
|
|
160
|
+
sdk.subscribe('orders', 'order-processing', 'order.created', handleOrderCreated),
|
|
161
|
+
sdk.subscribe('orders', 'order-notifications', 'order.updated', handleOrderUpdated),
|
|
162
|
+
sdk.subscribe('users', 'user-analytics', 'user.*', handleUserEvents)
|
|
163
|
+
]);
|
|
164
|
+
|
|
165
|
+
// Manual queue binding (useful for complex routing scenarios)
|
|
166
|
+
await sdk.bind('analytics-queue', 'events', 'user.login');
|
|
167
|
+
await sdk.bind('analytics-queue', 'events', 'user.logout');
|
|
168
|
+
await sdk.bind('notifications-queue', 'orders', 'order.*');
|
|
169
|
+
|
|
170
|
+
// Bulk publishing
|
|
171
|
+
const messages = [
|
|
172
|
+
{ exchange: 'orders', routingKey: 'order.created', payload: { orderId: 1 } },
|
|
173
|
+
{ exchange: 'orders', routingKey: 'order.created', payload: { orderId: 2 } },
|
|
174
|
+
{ exchange: 'users', routingKey: 'user.registered', payload: { userId: 'abc' } }
|
|
175
|
+
];
|
|
176
|
+
|
|
177
|
+
for (const msg of messages) {
|
|
178
|
+
await sdk.publish(msg.exchange, msg.routingKey, msg.payload);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
// Unbind when no longer needed
|
|
182
|
+
await sdk.unbind('analytics-queue', 'events', 'user.login');
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
function handleOrderCreated(message: any, ack: () => void, nack: () => void) {
|
|
186
|
+
// Process order creation
|
|
187
|
+
ack();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function handleOrderUpdated(message: any, ack: () => void, nack: () => void) {
|
|
191
|
+
// Process order update
|
|
192
|
+
ack();
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function handleUserEvents(message: any, ack: () => void, nack: () => void) {
|
|
196
|
+
// Process user events
|
|
197
|
+
ack();
|
|
198
|
+
}
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### Queue Binding Management
|
|
202
|
+
|
|
203
|
+
```typescript
|
|
204
|
+
import { SdkRabbitmq } from 'sdk-rabbitmq';
|
|
205
|
+
|
|
206
|
+
async function bindingExample() {
|
|
207
|
+
const sdk = await SdkRabbitmq.getInstance();
|
|
208
|
+
|
|
209
|
+
// Bind queue to exchange with specific routing key
|
|
210
|
+
await sdk.bind('notifications', 'events', 'user.created');
|
|
211
|
+
await sdk.bind('notifications', 'events', 'user.updated');
|
|
212
|
+
|
|
213
|
+
// Bind to multiple exchanges
|
|
214
|
+
await sdk.bind('analytics', 'user-events', 'user.*');
|
|
215
|
+
await sdk.bind('analytics', 'order-events', 'order.*');
|
|
216
|
+
|
|
217
|
+
// Topic exchange patterns
|
|
218
|
+
await sdk.bind('logs', 'system-logs', 'error.#'); // All error logs
|
|
219
|
+
await sdk.bind('alerts', 'system-logs', '*.critical'); // Critical from any service
|
|
220
|
+
|
|
221
|
+
// Remove bindings when no longer needed
|
|
222
|
+
await sdk.unbind('notifications', 'events', 'user.updated');
|
|
223
|
+
await sdk.unbind('analytics', 'user-events', 'user.*');
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
**Note**: The `subscribe()` method automatically creates the necessary bindings, so manual binding is only needed for advanced routing scenarios or when you want to bind queues without immediately consuming from them.
|
|
228
|
+
|
|
229
|
+
### Error Handling
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
import { SdkRabbitmq } from 'sdk-rabbitmq';
|
|
233
|
+
|
|
234
|
+
async function errorHandlingExample() {
|
|
235
|
+
try {
|
|
236
|
+
const sdk = await SdkRabbitmq.getInstance();
|
|
237
|
+
|
|
238
|
+
// Publishing with error handling
|
|
239
|
+
const success = await sdk.publish('exchange', 'key', { data: 'test' });
|
|
240
|
+
if (!success) {
|
|
241
|
+
console.error('Failed to publish message');
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// Subscription with error handling
|
|
245
|
+
await sdk.subscribe('exchange', 'queue', 'key', (message, ack, nack) => {
|
|
246
|
+
try {
|
|
247
|
+
// Risky operation that might fail
|
|
248
|
+
processMessage(message);
|
|
249
|
+
ack();
|
|
250
|
+
} catch (error) {
|
|
251
|
+
console.error('Message processing failed:', error);
|
|
252
|
+
// Message will be sent to DLQ if configured
|
|
253
|
+
nack();
|
|
254
|
+
}
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
} catch (error) {
|
|
258
|
+
console.error('SDK initialization failed:', error);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function processMessage(message: any) {
|
|
263
|
+
// Simulate processing that might fail
|
|
264
|
+
if (Math.random() > 0.8) {
|
|
265
|
+
throw new Error('Random processing failure');
|
|
266
|
+
}
|
|
267
|
+
console.log('Message processed successfully:', message);
|
|
268
|
+
}
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
## License
|
|
272
|
+
|
|
273
|
+
MIT
|