@autofleet/rabbit 1.0.23 → 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 CHANGED
@@ -2,6 +2,7 @@
2
2
  import { AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
3
3
  import { EventEmitter } from 'events';
4
4
  declare class RabbitMq {
5
+ static parseMsg(msg: any): any;
5
6
  channel: ChannelWrapper | null;
6
7
  connection: AmqpConnectionManager | null;
7
8
  em: EventEmitter;
package/dist/index.js CHANGED
@@ -29,6 +29,15 @@ class RabbitMq {
29
29
  this.connection = null;
30
30
  this.creatingConnection = false;
31
31
  }
32
+ static parseMsg(msg) {
33
+ let { content } = msg;
34
+ content = content.toString();
35
+ try {
36
+ content = JSON.parse(content);
37
+ }
38
+ catch (e) { }
39
+ return Object.assign({}, msg, { content });
40
+ }
32
41
  async getConnection() {
33
42
  return new Promise(async (resolve) => {
34
43
  if (this.creatingConnection) {
@@ -78,8 +87,8 @@ class RabbitMq {
78
87
  await this.assertQueue(queue);
79
88
  return channel.addSetup((c) => {
80
89
  return Promise.all([
81
- c.prefetch(limit, false),
82
- c.consume(queue, (msg) => callback(JSON.parse(msg.toString()), this.ack, this.nack(queue))),
90
+ c.prefetch(limit, true),
91
+ c.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
83
92
  ]);
84
93
  });
85
94
  }
@@ -92,9 +101,9 @@ class RabbitMq {
92
101
  ]);
93
102
  return channel.addSetup((c) => {
94
103
  return Promise.all([
95
- c.prefetch(limit, false),
104
+ c.prefetch(limit, true),
96
105
  c.bindQueue(queue, exchange, ''),
97
- c.consume(queue, (msg) => callback(JSON.parse(msg.toString()), this.ack, this.nack(queue))),
106
+ c.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
98
107
  ]);
99
108
  });
100
109
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "1.0.23",
3
+ "version": "1.1.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -4,6 +4,20 @@ import { EventEmitter } from 'events';
4
4
 
5
5
  const connectionCreatedConst = 'connectionCreated';
6
6
  class RabbitMq {
7
+ static parseMsg(msg: any) {
8
+ let { content } = msg;
9
+ content = content.toString();
10
+
11
+ try {
12
+ content = JSON.parse(content);
13
+ } catch (e) { }
14
+
15
+ return {
16
+ ...msg,
17
+ content,
18
+ }
19
+ }
20
+
7
21
  channel: ChannelWrapper | null;
8
22
  connection: AmqpConnectionManager | null;
9
23
  em: EventEmitter;
@@ -90,8 +104,8 @@ class RabbitMq {
90
104
  await this.assertQueue(queue);
91
105
  return channel.addSetup((c: ConfirmChannel) => {
92
106
  return Promise.all([
93
- c.prefetch(limit, false),
94
- c.consume(queue, (msg: any) => callback(JSON.parse(msg.toString()), this.ack, this.nack(queue))),
107
+ c.prefetch(limit, true),
108
+ c.consume(queue, (msg: any) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
95
109
  ]);
96
110
  });
97
111
  }
@@ -106,9 +120,9 @@ class RabbitMq {
106
120
 
107
121
  return channel.addSetup((c: ConfirmChannel) => {
108
122
  return Promise.all([
109
- c.prefetch(limit, false),
123
+ c.prefetch(limit, true),
110
124
  c.bindQueue(queue, exchange, ''),
111
- c.consume(queue, (msg: any) => callback(JSON.parse(msg.toString()), this.ack, this.nack(queue))),
125
+ c.consume(queue, (msg: any) => callback(RabbitMq.parseMsg(msg), this.ack, this.nack(queue))),
112
126
  ])
113
127
  })
114
128
  }