@kaushverse/rabbitmq-core 1.0.8 → 1.1.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/dist/index.d.ts +54 -9
- package/dist/index.js +143 -62
- package/package.json +6 -4
package/dist/index.d.ts
CHANGED
|
@@ -31,21 +31,66 @@ interface PublishOptions {
|
|
|
31
31
|
|
|
32
32
|
declare const rabbitEvents: EventEmitter<[never]>;
|
|
33
33
|
declare function getChannel(): Channel;
|
|
34
|
-
|
|
35
|
-
declare function
|
|
36
|
-
|
|
37
|
-
declare function publishMessage({ exchange, type, routingKey, message, headers, }: PublishOptions): Promise<void>;
|
|
34
|
+
declare function closeRabbitMQ(): Promise<void>;
|
|
35
|
+
declare function isRabbitConnected(): boolean;
|
|
38
36
|
|
|
39
37
|
type BootstrapOptions = {
|
|
40
38
|
serviceName: string;
|
|
41
39
|
start: () => void | Promise<void>;
|
|
42
|
-
rabbit:
|
|
43
|
-
url: string;
|
|
44
|
-
tls?: RabbitTLSOptions;
|
|
45
|
-
};
|
|
40
|
+
rabbit: RabbitConnectionOptions;
|
|
46
41
|
};
|
|
47
42
|
declare function bootstrap({ serviceName, rabbit, start, }: BootstrapOptions): Promise<void>;
|
|
48
43
|
|
|
44
|
+
declare function publishMessage({ exchange, type, routingKey, message, headers, }: PublishOptions): Promise<void>;
|
|
45
|
+
|
|
46
|
+
declare function consumeMessage(options: ConsumeOptions, handler: (data: any, raw: ConsumeMessage) => Promise<void> | void): Promise<void>;
|
|
47
|
+
|
|
48
|
+
type ExchangeConfig = {
|
|
49
|
+
name: string;
|
|
50
|
+
type: ExchangeType;
|
|
51
|
+
durable?: boolean;
|
|
52
|
+
autoDelete?: boolean;
|
|
53
|
+
};
|
|
54
|
+
declare function setupExchange(channel: Channel, exchange: ExchangeConfig): Promise<void>;
|
|
55
|
+
|
|
56
|
+
type QueueConfig = {
|
|
57
|
+
name: string;
|
|
58
|
+
durable?: boolean;
|
|
59
|
+
priority?: boolean;
|
|
60
|
+
maxPriority?: number;
|
|
61
|
+
ttl?: number;
|
|
62
|
+
arguments?: Record<string, any>;
|
|
63
|
+
};
|
|
64
|
+
declare function setupQueue(channel: Channel, queue: QueueConfig): Promise<void>;
|
|
65
|
+
|
|
66
|
+
type BindingConfig = {
|
|
67
|
+
queue: string;
|
|
68
|
+
exchange: string;
|
|
69
|
+
routingKeys: string | string[];
|
|
70
|
+
};
|
|
71
|
+
declare function setupBindings(channel: Channel, binding: BindingConfig): Promise<void>;
|
|
72
|
+
|
|
73
|
+
type RetryConfig = {
|
|
74
|
+
exchange: string;
|
|
75
|
+
queue: string;
|
|
76
|
+
routingKey: string;
|
|
77
|
+
delay: number;
|
|
78
|
+
};
|
|
79
|
+
declare function setupRetry(channel: Channel, config: RetryConfig): Promise<{
|
|
80
|
+
retryExchange: string;
|
|
81
|
+
retryQueue: string;
|
|
82
|
+
}>;
|
|
83
|
+
|
|
84
|
+
type DLQConfig = {
|
|
85
|
+
exchange: string;
|
|
86
|
+
queue: string;
|
|
87
|
+
routingKey?: string;
|
|
88
|
+
};
|
|
89
|
+
declare function setupDLQ(channel: Channel, config: DLQConfig): Promise<{
|
|
90
|
+
deadLetterExchange: string;
|
|
91
|
+
deadLetterQueue: string;
|
|
92
|
+
}>;
|
|
93
|
+
|
|
49
94
|
declare function rabbitHealth(): {
|
|
50
95
|
"\uD83D\uDC07": {
|
|
51
96
|
service: string;
|
|
@@ -63,4 +108,4 @@ declare const logger: {
|
|
|
63
108
|
debug: (msg: string, meta?: any) => void;
|
|
64
109
|
};
|
|
65
110
|
|
|
66
|
-
export { type ConsumeOptions, type ExchangeType, type LogLevel, type PublishOptions, type RabbitConnectionOptions, type RabbitTLSOptions, bootstrap, consumeMessage, getChannel, logger, publishMessage, rabbitEvents, rabbitHealth, setLoggerService, setPrettyLogs };
|
|
111
|
+
export { type BindingConfig, type ConsumeOptions, type DLQConfig, type ExchangeConfig, type ExchangeType, type LogLevel, type PublishOptions, type QueueConfig, type RabbitConnectionOptions, type RabbitTLSOptions, type RetryConfig, bootstrap, closeRabbitMQ, consumeMessage, getChannel, isRabbitConnected, logger, publishMessage, rabbitEvents, rabbitHealth, setLoggerService, setPrettyLogs, setupBindings, setupDLQ, setupExchange, setupQueue, setupRetry };
|
package/dist/index.js
CHANGED
|
@@ -31,14 +31,21 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
32
|
__export(index_exports, {
|
|
33
33
|
bootstrap: () => bootstrap,
|
|
34
|
+
closeRabbitMQ: () => closeRabbitMQ,
|
|
34
35
|
consumeMessage: () => consumeMessage,
|
|
35
36
|
getChannel: () => getChannel,
|
|
37
|
+
isRabbitConnected: () => isRabbitConnected,
|
|
36
38
|
logger: () => logger,
|
|
37
39
|
publishMessage: () => publishMessage,
|
|
38
40
|
rabbitEvents: () => rabbitEvents,
|
|
39
41
|
rabbitHealth: () => rabbitHealth,
|
|
40
42
|
setLoggerService: () => setLoggerService,
|
|
41
|
-
setPrettyLogs: () => setPrettyLogs
|
|
43
|
+
setPrettyLogs: () => setPrettyLogs,
|
|
44
|
+
setupBindings: () => setupBindings,
|
|
45
|
+
setupDLQ: () => setupDLQ,
|
|
46
|
+
setupExchange: () => setupExchange,
|
|
47
|
+
setupQueue: () => setupQueue,
|
|
48
|
+
setupRetry: () => setupRetry
|
|
42
49
|
});
|
|
43
50
|
module.exports = __toCommonJS(index_exports);
|
|
44
51
|
|
|
@@ -122,66 +129,6 @@ function isRabbitConnected() {
|
|
|
122
129
|
return Boolean(connection && channel);
|
|
123
130
|
}
|
|
124
131
|
|
|
125
|
-
// src/consumer.ts
|
|
126
|
-
async function consumeMessage(options, handler) {
|
|
127
|
-
const channel2 = getChannel();
|
|
128
|
-
const {
|
|
129
|
-
queue,
|
|
130
|
-
exchange,
|
|
131
|
-
type,
|
|
132
|
-
routingKey = "",
|
|
133
|
-
prefetch = 1,
|
|
134
|
-
noAck = false
|
|
135
|
-
} = options;
|
|
136
|
-
await channel2.assertExchange(exchange, type, { durable: true });
|
|
137
|
-
await channel2.assertQueue(queue, { durable: true });
|
|
138
|
-
await channel2.bindQueue(queue, exchange, routingKey);
|
|
139
|
-
channel2.prefetch(prefetch);
|
|
140
|
-
console.log(`\u{1F4E5} Waiting for messages on queue: ${queue}`);
|
|
141
|
-
await channel2.consume(
|
|
142
|
-
queue,
|
|
143
|
-
async (msg) => {
|
|
144
|
-
if (!msg) return;
|
|
145
|
-
try {
|
|
146
|
-
const content = msg.content.toString();
|
|
147
|
-
const data = tryParseJSON(content);
|
|
148
|
-
await handler(data, msg);
|
|
149
|
-
if (!noAck) channel2.ack(msg);
|
|
150
|
-
} catch (err) {
|
|
151
|
-
console.error("\u274C Message processing failed:", err);
|
|
152
|
-
if (!noAck) channel2.nack(msg, false, false);
|
|
153
|
-
}
|
|
154
|
-
},
|
|
155
|
-
{ noAck }
|
|
156
|
-
);
|
|
157
|
-
}
|
|
158
|
-
function tryParseJSON(data) {
|
|
159
|
-
try {
|
|
160
|
-
return JSON.parse(data);
|
|
161
|
-
} catch {
|
|
162
|
-
return data;
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// src/publisher.ts
|
|
167
|
-
async function publishMessage({
|
|
168
|
-
exchange,
|
|
169
|
-
type,
|
|
170
|
-
routingKey = "",
|
|
171
|
-
message,
|
|
172
|
-
headers = {}
|
|
173
|
-
}) {
|
|
174
|
-
const channel2 = getChannel();
|
|
175
|
-
await channel2.assertExchange(exchange, type, { durable: true });
|
|
176
|
-
const payload = Buffer.isBuffer(message) ? message : Buffer.from(
|
|
177
|
-
typeof message === "string" ? message : JSON.stringify(message)
|
|
178
|
-
);
|
|
179
|
-
channel2.publish(exchange, routingKey, payload, {
|
|
180
|
-
persistent: true,
|
|
181
|
-
headers
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
|
|
185
132
|
// src/logger/index.ts
|
|
186
133
|
var serviceName = "service";
|
|
187
134
|
var prettyLogs = true;
|
|
@@ -247,6 +194,133 @@ async function bootstrap({
|
|
|
247
194
|
}
|
|
248
195
|
}
|
|
249
196
|
|
|
197
|
+
// src/publisher.ts
|
|
198
|
+
async function publishMessage({
|
|
199
|
+
exchange,
|
|
200
|
+
type,
|
|
201
|
+
routingKey = "",
|
|
202
|
+
message,
|
|
203
|
+
headers = {}
|
|
204
|
+
}) {
|
|
205
|
+
const channel2 = getChannel();
|
|
206
|
+
await channel2.assertExchange(exchange, type, { durable: true });
|
|
207
|
+
const payload = Buffer.isBuffer(message) ? message : Buffer.from(
|
|
208
|
+
typeof message === "string" ? message : JSON.stringify(message)
|
|
209
|
+
);
|
|
210
|
+
channel2.publish(exchange, routingKey, payload, {
|
|
211
|
+
persistent: true,
|
|
212
|
+
headers
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// src/consumer.ts
|
|
217
|
+
async function consumeMessage(options, handler) {
|
|
218
|
+
const channel2 = getChannel();
|
|
219
|
+
const {
|
|
220
|
+
queue,
|
|
221
|
+
exchange,
|
|
222
|
+
type,
|
|
223
|
+
routingKey = "",
|
|
224
|
+
prefetch = 1,
|
|
225
|
+
noAck = false
|
|
226
|
+
} = options;
|
|
227
|
+
await channel2.assertExchange(exchange, type, { durable: true });
|
|
228
|
+
await channel2.assertQueue(queue, { durable: true });
|
|
229
|
+
await channel2.bindQueue(queue, exchange, routingKey);
|
|
230
|
+
channel2.prefetch(prefetch);
|
|
231
|
+
console.log(`\u{1F4E5} Waiting for messages on queue: ${queue}`);
|
|
232
|
+
await channel2.consume(
|
|
233
|
+
queue,
|
|
234
|
+
async (msg) => {
|
|
235
|
+
if (!msg) return;
|
|
236
|
+
try {
|
|
237
|
+
const content = msg.content.toString();
|
|
238
|
+
const data = tryParseJSON(content);
|
|
239
|
+
await handler(data, msg);
|
|
240
|
+
if (!noAck) channel2.ack(msg);
|
|
241
|
+
} catch (err) {
|
|
242
|
+
console.error("\u274C Message processing failed:", err);
|
|
243
|
+
if (!noAck) channel2.nack(msg, false, false);
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
{ noAck }
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
function tryParseJSON(data) {
|
|
250
|
+
try {
|
|
251
|
+
return JSON.parse(data);
|
|
252
|
+
} catch {
|
|
253
|
+
return data;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// src/builder/exchange.ts
|
|
258
|
+
async function setupExchange(channel2, exchange) {
|
|
259
|
+
await channel2.assertExchange(exchange.name, exchange.type, {
|
|
260
|
+
durable: exchange.durable ?? true,
|
|
261
|
+
autoDelete: exchange.autoDelete ?? false
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// src/builder/queue.ts
|
|
266
|
+
async function setupQueue(channel2, queue) {
|
|
267
|
+
await channel2.assertQueue(queue.name, {
|
|
268
|
+
durable: queue.durable ?? true,
|
|
269
|
+
arguments: {
|
|
270
|
+
...queue.priority && {
|
|
271
|
+
"x-max-priority": queue.maxPriority ?? 10
|
|
272
|
+
},
|
|
273
|
+
...queue.ttl && {
|
|
274
|
+
"x-message-ttl": queue.ttl
|
|
275
|
+
},
|
|
276
|
+
...queue.arguments ?? {}
|
|
277
|
+
}
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
// src/builder/binding.ts
|
|
282
|
+
async function setupBindings(channel2, binding) {
|
|
283
|
+
const keys = Array.isArray(binding.routingKeys) ? binding.routingKeys : [binding.routingKeys];
|
|
284
|
+
for (const key of keys) {
|
|
285
|
+
await channel2.bindQueue(binding.queue, binding.exchange, key);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// src/builder/retry.ts
|
|
290
|
+
async function setupRetry(channel2, config) {
|
|
291
|
+
const retryExchange = `${config.exchange}.retry`;
|
|
292
|
+
const retryQueue = `${config.queue}.retry.${config.delay}`;
|
|
293
|
+
await channel2.assertExchange(retryExchange, "direct", {
|
|
294
|
+
durable: true
|
|
295
|
+
});
|
|
296
|
+
await channel2.assertQueue(retryQueue, {
|
|
297
|
+
durable: true,
|
|
298
|
+
arguments: {
|
|
299
|
+
"x-message-ttl": config.delay,
|
|
300
|
+
"x-dead-letter-exchange": config.exchange,
|
|
301
|
+
"x-dead-letter-routing-key": config.routingKey
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
await channel2.bindQueue(retryQueue, retryExchange, config.routingKey);
|
|
305
|
+
return {
|
|
306
|
+
retryExchange,
|
|
307
|
+
retryQueue
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
// src/builder/dlq.ts
|
|
312
|
+
async function setupDLQ(channel2, config) {
|
|
313
|
+
const dlx = `${config.exchange}.dlx`;
|
|
314
|
+
const dlq = `${config.queue}.dlq`;
|
|
315
|
+
await channel2.assertExchange(dlx, "direct", { durable: true });
|
|
316
|
+
await channel2.assertQueue(dlq, { durable: true });
|
|
317
|
+
await channel2.bindQueue(dlq, dlx, config.routingKey ?? config.queue);
|
|
318
|
+
return {
|
|
319
|
+
deadLetterExchange: dlx,
|
|
320
|
+
deadLetterQueue: dlq
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
250
324
|
// src/health/index.ts
|
|
251
325
|
function rabbitHealth() {
|
|
252
326
|
const connected = isRabbitConnected();
|
|
@@ -261,12 +335,19 @@ function rabbitHealth() {
|
|
|
261
335
|
// Annotate the CommonJS export names for ESM import in node:
|
|
262
336
|
0 && (module.exports = {
|
|
263
337
|
bootstrap,
|
|
338
|
+
closeRabbitMQ,
|
|
264
339
|
consumeMessage,
|
|
265
340
|
getChannel,
|
|
341
|
+
isRabbitConnected,
|
|
266
342
|
logger,
|
|
267
343
|
publishMessage,
|
|
268
344
|
rabbitEvents,
|
|
269
345
|
rabbitHealth,
|
|
270
346
|
setLoggerService,
|
|
271
|
-
setPrettyLogs
|
|
347
|
+
setPrettyLogs,
|
|
348
|
+
setupBindings,
|
|
349
|
+
setupDLQ,
|
|
350
|
+
setupExchange,
|
|
351
|
+
setupQueue,
|
|
352
|
+
setupRetry
|
|
272
353
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kaushverse/rabbitmq-core",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Reusable RabbitMQ (AMQP / AMQPS) core for Node.js & microservices",
|
|
5
5
|
"author": "Kaushik (KaushVerse)",
|
|
6
6
|
"license": "MIT",
|
|
@@ -17,10 +17,12 @@
|
|
|
17
17
|
"keywords": [
|
|
18
18
|
"rabbitmq",
|
|
19
19
|
"amqp",
|
|
20
|
-
"amqps",
|
|
21
20
|
"microservices",
|
|
22
|
-
"
|
|
23
|
-
"
|
|
21
|
+
"message-queue",
|
|
22
|
+
"event-driven",
|
|
23
|
+
"dlq",
|
|
24
|
+
"retry",
|
|
25
|
+
"amqps"
|
|
24
26
|
],
|
|
25
27
|
"dependencies": {
|
|
26
28
|
"amqplib": "^0.10.9"
|