@autofleet/rabbit 1.0.6 → 1.0.8

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,17 +85,17 @@ 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();
80
92
  await this.assertExchange(exchange);
81
- return channel.publish(exchange, '', Buffer.from(content));
93
+ return channel.publish(exchange, '', Buffer.from(JSON.stringify(content)));
82
94
  }
83
95
  async sendToQueue(queue, content) {
84
96
  const channel = await this.assertChannel();
85
97
  await this.assertQueue(queue);
86
- return channel.sendToQueue(queue, Buffer.from(content));
98
+ return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
87
99
  }
88
100
  }
89
101
  exports.default = RabbitMq;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "1.0.6",
3
+ "version": "1.0.8",
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,32 +88,32 @@ 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();
97
109
  await this.assertExchange(exchange);
98
- return channel.publish(exchange, '', Buffer.from(content));
110
+ return channel.publish(exchange, '', Buffer.from(JSON.stringify(content)));
99
111
  }
100
112
 
101
113
  async sendToQueue(queue: string, content: any) {
102
114
  const channel: Channel = await this.assertChannel();
103
115
  await this.assertQueue(queue);
104
- return channel.sendToQueue(queue, Buffer.from(content));
116
+ return channel.sendToQueue(queue, Buffer.from(JSON.stringify(content)));
105
117
  }
106
118
  }
107
119