@autofleet/rabbit 1.0.5 → 1.0.7

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 CHANGED
@@ -7,12 +7,13 @@ declare class RabbitMq {
7
7
  creatingChannel: boolean;
8
8
  static parseMsg(msg: any): any;
9
9
  constructor();
10
- ack: (msg: any) => void;
10
+ ack: (msg: any) => Promise<void>;
11
+ nack: (msg: any, queue: string, deadQueue: string, retries: number) => Promise<void>;
11
12
  assertChannel(): Promise<Channel>;
12
13
  assertExchange(exchange: string, options?: any): Promise<import("amqplib").Replies.AssertExchange>;
13
14
  assertQueue(queue: string, options?: any): Promise<import("amqplib").Replies.AssertQueue>;
14
- consume(queue: string, callback: (msg: any, ack: Function) => any): Promise<import("amqplib").Replies.Consume>;
15
- consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function) => any): Promise<import("amqplib").Replies.Consume>;
15
+ consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any): Promise<import("amqplib").Replies.Consume>;
16
+ consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any): Promise<import("amqplib").Replies.Consume>;
16
17
  publish(exchange: string, content: any): Promise<boolean>;
17
18
  sendToQueue(queue: string, content: any): Promise<boolean>;
18
19
  }
package/dist/index.js CHANGED
@@ -5,9 +5,21 @@ const events_1 = require("events");
5
5
  const channelCreatedConst = 'channelCreated';
6
6
  class RabbitMq {
7
7
  constructor() {
8
- this.ack = (msg) => {
8
+ this.ack = async (msg) => {
9
9
  if (this.channel) {
10
- this.channel.ack(msg);
10
+ await this.channel.ack(msg);
11
+ }
12
+ };
13
+ this.nack = async (msg, queue, deadQueue, retries) => {
14
+ if (this.channel) {
15
+ await this.channel.ack(msg);
16
+ if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
17
+ msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
18
+ await this.sendToQueue(queue, Buffer.from(JSON.stringify(msg.content)));
19
+ }
20
+ else {
21
+ await this.sendToQueue(deadQueue, Buffer.from(JSON.stringify(msg.content)));
22
+ }
11
23
  }
12
24
  };
13
25
  this.creatingChannel = false;
@@ -64,7 +76,7 @@ class RabbitMq {
64
76
  async consume(queue, callback) {
65
77
  const channel = await this.assertChannel();
66
78
  await this.assertQueue(queue);
67
- return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
79
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack));
68
80
  }
69
81
  async consumeFromExchange(queue, exchange, callback) {
70
82
  const channel = await this.assertChannel();
@@ -73,14 +85,16 @@ class RabbitMq {
73
85
  this.assertQueue(queue),
74
86
  ]);
75
87
  await channel.bindQueue(queue, exchange, '');
76
- return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
88
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack));
77
89
  }
78
90
  async publish(exchange, content) {
79
91
  const channel = await this.assertChannel();
92
+ await this.assertExchange(exchange);
80
93
  return channel.publish(exchange, '', Buffer.from(content));
81
94
  }
82
95
  async sendToQueue(queue, content) {
83
96
  const channel = await this.assertChannel();
97
+ await this.assertQueue(queue);
84
98
  return channel.sendToQueue(queue, Buffer.from(content));
85
99
  }
86
100
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -35,9 +35,21 @@ class RabbitMq {
35
35
  });
36
36
  }
37
37
 
38
- public ack = (msg: any) => {
38
+ public ack = async (msg: any) => {
39
39
  if (this.channel) {
40
- this.channel.ack(msg);
40
+ await this.channel.ack(msg);
41
+ }
42
+ }
43
+
44
+ public nack = async (msg: any, queue: string, deadQueue: string, retries: number) => {
45
+ if (this.channel) {
46
+ await this.channel.ack(msg);
47
+ if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
48
+ msg.content['x-retry-count'] = msg.content['x-retry-count'] ? msg.content['x-retry-count'] + 1 : 1;
49
+ await this.sendToQueue(queue, Buffer.from(JSON.stringify(msg.content)));
50
+ } else {
51
+ await this.sendToQueue(deadQueue, Buffer.from(JSON.stringify(msg.content)));
52
+ }
41
53
  }
42
54
  }
43
55
 
@@ -76,29 +88,31 @@ class RabbitMq {
76
88
  return channel.assertQueue(queue);
77
89
  }
78
90
 
79
- async consume(queue: string, callback: (msg: any, ack: Function) => any) {
91
+ async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any) {
80
92
  const channel: Channel = await this.assertChannel();
81
93
  await this.assertQueue(queue);
82
- return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
94
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack));
83
95
  }
84
96
 
85
- async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function) => any) {
97
+ async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any) {
86
98
  const channel: Channel = await this.assertChannel();
87
99
  await Promise.all([
88
100
  this.assertExchange(exchange),
89
101
  this.assertQueue(queue),
90
102
  ]);
91
103
  await channel.bindQueue(queue, exchange, '');
92
- return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
104
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack));
93
105
  }
94
106
 
95
107
  async publish(exchange: string, content: any) {
96
108
  const channel: Channel = await this.assertChannel();
109
+ await this.assertExchange(exchange);
97
110
  return channel.publish(exchange, '', Buffer.from(content));
98
111
  }
99
112
 
100
113
  async sendToQueue(queue: string, content: any) {
101
114
  const channel: Channel = await this.assertChannel();
115
+ await this.assertQueue(queue);
102
116
  return channel.sendToQueue(queue, Buffer.from(content));
103
117
  }
104
118
  }