@autofleet/rabbit 1.0.10 → 1.0.12

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
@@ -12,8 +12,8 @@ declare class RabbitMq {
12
12
  assertChannel(): Promise<Channel>;
13
13
  assertExchange(exchange: string, options?: any): Promise<import("amqplib").Replies.AssertExchange>;
14
14
  assertQueue(queue: string, options?: any): Promise<import("amqplib").Replies.AssertQueue>;
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>;
15
+ consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<import("amqplib").Replies.Consume>;
16
+ consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any): Promise<import("amqplib").Replies.Consume>;
17
17
  publish(exchange: string, content: any): Promise<boolean>;
18
18
  sendToQueue(queue: string, content: any): Promise<boolean>;
19
19
  }
package/dist/index.js CHANGED
@@ -12,16 +12,16 @@ class RabbitMq {
12
12
  };
13
13
  this.nack = (queue) => async (msg, retries) => {
14
14
  if (this.channel) {
15
- await this.channel.ack(msg);
16
15
  if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
17
16
  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)));
17
+ await this.sendToQueue(queue, msg.content);
19
18
  }
20
19
  else {
21
20
  const deadQueue = `${queue}-dead`;
22
21
  await this.assertQueue(deadQueue);
23
- await this.sendToQueue(deadQueue, Buffer.from(JSON.stringify(msg.content)));
22
+ await this.sendToQueue(deadQueue, msg.content);
24
23
  }
24
+ await this.channel.ack(msg);
25
25
  }
26
26
  };
27
27
  this.creatingChannel = false;
@@ -75,17 +75,21 @@ class RabbitMq {
75
75
  const channel = await this.assertChannel();
76
76
  return channel.assertQueue(queue);
77
77
  }
78
- async consume(queue, callback) {
78
+ async consume(queue, callback, options) {
79
+ const { limit } = options ? options : 1;
79
80
  const channel = await this.assertChannel();
80
81
  await this.assertQueue(queue);
82
+ channel.prefetch(limit, false);
81
83
  return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
82
84
  }
83
- async consumeFromExchange(queue, exchange, callback) {
85
+ async consumeFromExchange(queue, exchange, callback, options) {
86
+ const { limit } = options ? options : 1;
84
87
  const channel = await this.assertChannel();
85
88
  await Promise.all([
86
89
  this.assertExchange(exchange),
87
90
  this.assertQueue(queue),
88
91
  ]);
92
+ channel.prefetch(limit, false);
89
93
  await channel.bindQueue(queue, exchange, '');
90
94
  return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
91
95
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "1.0.10",
3
+ "version": "1.0.12",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -43,15 +43,15 @@ class RabbitMq {
43
43
 
44
44
  public nack = (queue: string) => async (msg: any, retries: number) => {
45
45
  if (this.channel) {
46
- await this.channel.ack(msg);
47
46
  if (!msg.content['x-retry-count'] || msg.content['x-retry-count'] <= retries) {
48
47
  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)));
48
+ await this.sendToQueue(queue, msg.content);
50
49
  } else {
51
50
  const deadQueue = `${queue}-dead`;
52
51
  await this.assertQueue(deadQueue);
53
- await this.sendToQueue(deadQueue, Buffer.from(JSON.stringify(msg.content)));
52
+ await this.sendToQueue(deadQueue, msg.content);
54
53
  }
54
+ await this.channel.ack(msg);
55
55
  }
56
56
  }
57
57
 
@@ -90,18 +90,22 @@ class RabbitMq {
90
90
  return channel.assertQueue(queue);
91
91
  }
92
92
 
93
- async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any) {
93
+ async consume(queue: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
94
+ const { limit } = options ? options : 1;
94
95
  const channel: Channel = await this.assertChannel();
95
96
  await this.assertQueue(queue);
97
+ channel.prefetch(limit, false);
96
98
  return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
97
99
  }
98
100
 
99
- async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any) {
101
+ async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function, nack: Function) => any, options?: any) {
102
+ const { limit } = options ? options : 1;
100
103
  const channel: Channel = await this.assertChannel();
101
104
  await Promise.all([
102
105
  this.assertExchange(exchange),
103
106
  this.assertQueue(queue),
104
107
  ]);
108
+ channel.prefetch(limit, false);
105
109
  await channel.bindQueue(queue, exchange, '');
106
110
  return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue)));
107
111
  }