@autofleet/rabbit 1.0.18 → 1.0.20

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
@@ -5,6 +5,7 @@ declare class RabbitMq {
5
5
  channel: ChannelWrapper | null;
6
6
  connection: AmqpConnectionManager | null;
7
7
  em: EventEmitter;
8
+ creatingConnection: Boolean;
8
9
  constructor();
9
10
  ack: (msg: any) => Promise<void>;
10
11
  nack: (queue: string) => (msg: any, retries: number) => Promise<void>;
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const amqp_connection_manager_1 = require("amqp-connection-manager");
4
4
  const events_1 = require("events");
5
+ const connectionCreatedConst = 'connectionCreated';
5
6
  class RabbitMq {
6
7
  constructor() {
7
8
  this.ack = async (msg) => {
@@ -26,15 +27,23 @@ class RabbitMq {
26
27
  this.em = new events_1.EventEmitter;
27
28
  this.channel = null;
28
29
  this.connection = null;
30
+ this.creatingConnection = false;
29
31
  }
30
32
  async getConnection() {
31
- if (this.connection !== null) {
32
- return this.connection;
33
- }
34
- let host = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
35
- let connection = await amqp_connection_manager_1.connect([`amqp://${host}`]);
36
- this.connection = connection;
37
- return connection;
33
+ return new Promise(async (resolve) => {
34
+ if (this.creatingConnection) {
35
+ this.em.on(connectionCreatedConst, resolve);
36
+ }
37
+ if (this.connection !== null) {
38
+ return this.connection;
39
+ }
40
+ let host = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
41
+ let connection = await amqp_connection_manager_1.connect([`amqp://${host}`]);
42
+ this.connection = connection;
43
+ this.creatingConnection = false;
44
+ this.em.emit(connectionCreatedConst, connection);
45
+ resolve(connection);
46
+ });
38
47
  }
39
48
  async assertChannel() {
40
49
  return new Promise(async (resolve, reject) => {
@@ -69,7 +78,7 @@ class RabbitMq {
69
78
  return channel.addSetup((c) => {
70
79
  return Promise.all([
71
80
  c.prefetch(limit, false),
72
- c.consume(queue, (msg) => callback(JSON.parse(msg), this.ack, this.nack(queue))),
81
+ c.consume(queue, (msg) => callback(JSON.parse(msg.toString()), this.ack, this.nack(queue))),
73
82
  ]);
74
83
  });
75
84
  }
@@ -84,7 +93,7 @@ class RabbitMq {
84
93
  return Promise.all([
85
94
  c.prefetch(limit, false),
86
95
  c.bindQueue(queue, exchange, ''),
87
- c.consume(queue, (msg) => callback(JSON.parse(msg), this.ack, this.nack(queue))),
96
+ c.consume(queue, (msg) => callback(JSON.parse(msg.toString()), this.ack, this.nack(queue))),
88
97
  ]);
89
98
  });
90
99
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/src/index.ts CHANGED
@@ -2,15 +2,18 @@ import { ConfirmChannel } from 'amqplib';
2
2
  import { connect, AmqpConnectionManager, ChannelWrapper } from 'amqp-connection-manager';
3
3
  import { EventEmitter } from 'events';
4
4
 
5
+ const connectionCreatedConst = 'connectionCreated';
5
6
  class RabbitMq {
6
7
  channel: ChannelWrapper | null;
7
8
  connection: AmqpConnectionManager | null;
8
9
  em: EventEmitter;
10
+ creatingConnection: Boolean;
9
11
 
10
12
  constructor() {
11
13
  this.em = new EventEmitter;
12
14
  this.channel = null;
13
15
  this.connection = null;
16
+ this.creatingConnection = false;
14
17
  }
15
18
 
16
19
  public ack = async (msg: any) => {
@@ -34,14 +37,22 @@ class RabbitMq {
34
37
  }
35
38
 
36
39
  async getConnection() {
37
- if (this.connection !== null) {
38
- return this.connection;
39
- }
40
+ return new Promise<AmqpConnectionManager>(async (resolve) => {
41
+ if (this.creatingConnection) {
42
+ this.em.on(connectionCreatedConst, resolve);
43
+ }
40
44
 
41
- let host: string = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
42
- let connection: AmqpConnectionManager = await connect([`amqp://${host}`]);
43
- this.connection = connection;
44
- return connection;
45
+ if (this.connection !== null) {
46
+ return this.connection;
47
+ }
48
+
49
+ let host: string = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
50
+ let connection: AmqpConnectionManager = await connect([`amqp://${host}`]);
51
+ this.connection = connection;
52
+ this.creatingConnection = false;
53
+ this.em.emit(connectionCreatedConst, connection);
54
+ resolve(connection);
55
+ })
45
56
  }
46
57
 
47
58
  async assertChannel() {
@@ -81,7 +92,7 @@ class RabbitMq {
81
92
  return channel.addSetup((c: ConfirmChannel) => {
82
93
  return Promise.all([
83
94
  c.prefetch(limit, false),
84
- c.consume(queue, (msg : any) => callback(JSON.parse(msg), this.ack, this.nack(queue))),
95
+ c.consume(queue, (msg: any) => callback(JSON.parse(msg.toString()), this.ack, this.nack(queue))),
85
96
  ]);
86
97
  });
87
98
  }
@@ -98,7 +109,7 @@ class RabbitMq {
98
109
  return Promise.all([
99
110
  c.prefetch(limit, false),
100
111
  c.bindQueue(queue, exchange, ''),
101
- c.consume(queue, (msg: any) => callback(JSON.parse(msg), this.ack, this.nack(queue))),
112
+ c.consume(queue, (msg: any) => callback(JSON.parse(msg.toString()), this.ack, this.nack(queue))),
102
113
  ])
103
114
  })
104
115
  }