@autofleet/rabbit 1.0.1 → 1.0.2

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/example.js CHANGED
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
11
  const index_1 = require("./index");
12
12
  const init = () => __awaiter(this, void 0, void 0, function* () {
13
- const rabbit = yield index_1.default.init();
13
+ const rabbit = new index_1.default();
14
14
  yield rabbit.consumeFromExchange('test-q2', 'test-e', (msg, ack) => {
15
15
  console.log('msg2', msg.content);
16
16
  // ack(msg);
package/dist/index.d.ts CHANGED
@@ -1,10 +1,14 @@
1
+ /// <reference types="node" />
1
2
  import { Channel } from 'amqplib';
3
+ import { EventEmitter } from 'events';
2
4
  declare class RabbitMq {
3
- channel: Channel;
4
- static init(): Promise<RabbitMq>;
5
+ channel: Channel | null;
6
+ em: EventEmitter;
7
+ creatingChannel: boolean;
5
8
  static parseMsg(msg: any): any;
6
- constructor(channel: Channel);
9
+ constructor();
7
10
  ack: (msg: any) => void;
11
+ assertChannel(): Promise<Channel>;
8
12
  assertExchange(exchange: string, options?: any): Promise<import("amqplib").Replies.AssertExchange>;
9
13
  assertQueue(queue: string, options?: any): Promise<import("amqplib").Replies.AssertQueue>;
10
14
  consume(queue: string, callback: (msg: any, ack: Function) => any): Promise<import("amqplib").Replies.Consume>;
package/dist/index.js CHANGED
@@ -9,29 +9,23 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  };
10
10
  Object.defineProperty(exports, "__esModule", { value: true });
11
11
  const amqplib_1 = require("amqplib");
12
+ const events_1 = require("events");
13
+ const channelCreatedConst = 'channelCreated';
12
14
  class RabbitMq {
13
- constructor(channel) {
15
+ constructor() {
14
16
  this.ack = (msg) => {
15
- this.channel.ack(msg);
16
- };
17
- this.channel = channel;
18
- }
19
- static init() {
20
- return __awaiter(this, void 0, void 0, function* () {
21
- let connection, channel;
22
- let host = process.env.RABBITMQ_SERVICE_HOST || '104.155.17.91';
23
- try {
24
- connection = yield amqplib_1.connect(`amqp://${host}`);
25
- channel = yield connection.createChannel();
17
+ if (this.channel) {
18
+ this.channel.ack(msg);
26
19
  }
27
- catch (e) {
28
- throw e;
20
+ };
21
+ this.creatingChannel = false;
22
+ this.em = new events_1.EventEmitter;
23
+ this.channel = null;
24
+ process.once('SIGINT', () => {
25
+ console.log('Removing channel');
26
+ if (this.channel) {
27
+ this.channel.close();
29
28
  }
30
- process.once('SIGINT', function () {
31
- console.log('Removing channel');
32
- channel.close();
33
- });
34
- return new RabbitMq(channel);
35
29
  });
36
30
  }
37
31
  static parseMsg(msg) {
@@ -43,40 +37,72 @@ class RabbitMq {
43
37
  catch (e) { }
44
38
  return Object.assign({}, msg, { content });
45
39
  }
40
+ assertChannel() {
41
+ return __awaiter(this, void 0, void 0, function* () {
42
+ return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
43
+ if (this.channel) {
44
+ return resolve(this.channel);
45
+ }
46
+ if (this.creatingChannel) {
47
+ return this.em.on(channelCreatedConst, resolve);
48
+ }
49
+ this.creatingChannel = true;
50
+ let connection, channel;
51
+ let host = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
52
+ try {
53
+ connection = yield amqplib_1.connect(`amqp://${host}`);
54
+ channel = yield connection.createChannel();
55
+ this.channel = channel;
56
+ this.creatingChannel = false;
57
+ this.em.emit(channelCreatedConst, channel);
58
+ resolve(channel);
59
+ }
60
+ catch (e) {
61
+ reject(e);
62
+ }
63
+ }));
64
+ });
65
+ }
46
66
  assertExchange(exchange, options) {
47
67
  return __awaiter(this, void 0, void 0, function* () {
48
- return this.channel.assertExchange(exchange, 'fanout');
68
+ const channel = yield this.assertChannel();
69
+ return channel.assertExchange(exchange, 'fanout');
49
70
  });
50
71
  }
51
72
  assertQueue(queue, options) {
52
73
  return __awaiter(this, void 0, void 0, function* () {
53
- return this.channel.assertQueue(queue);
74
+ const channel = yield this.assertChannel();
75
+ return channel.assertQueue(queue);
54
76
  });
55
77
  }
56
78
  consume(queue, callback) {
57
79
  return __awaiter(this, void 0, void 0, function* () {
80
+ const channel = yield this.assertChannel();
58
81
  yield this.assertQueue(queue);
59
- return this.channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
82
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
60
83
  });
61
84
  }
62
85
  consumeFromExchange(queue, exchange, callback) {
63
86
  return __awaiter(this, void 0, void 0, function* () {
87
+ const channel = yield this.assertChannel();
64
88
  yield Promise.all([
65
89
  this.assertExchange(exchange),
66
90
  this.assertQueue(queue),
67
91
  ]);
68
- yield this.channel.bindQueue(queue, exchange, '');
69
- return this.channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
92
+ yield channel.bindQueue(queue, exchange, '');
93
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
70
94
  });
71
95
  }
72
96
  publish(exchange, content) {
73
97
  return __awaiter(this, void 0, void 0, function* () {
74
- return this.channel.publish(exchange, '', Buffer.from(content));
98
+ const channel = yield this.assertChannel();
99
+ return channel.publish(exchange, '', Buffer.from(content));
75
100
  });
76
101
  }
77
102
  sendToQueue(queue, content) {
78
103
  return __awaiter(this, void 0, void 0, function* () {
79
- return this.channel.sendToQueue(queue, Buffer.from(content));
104
+ const channel = yield this.assertChannel();
105
+ return channel.sendToQueue(queue, Buffer.from(content));
80
106
  });
81
107
  }
82
108
  }
package/package.json CHANGED
@@ -1,10 +1,11 @@
1
1
  {
2
2
  "name": "@autofleet/rabbit",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
7
7
  "start": "ts-node src/index.ts",
8
+ "example": "ts-node src/example.ts",
8
9
  "prepublish": "tsc",
9
10
  "dev": "nodemon"
10
11
  },
package/src/example.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import RabbitMq from './index';
2
2
 
3
3
  const init = async () => {
4
- const rabbit = await RabbitMq.init();
4
+ const rabbit = new RabbitMq();
5
5
  await rabbit.consumeFromExchange('test-q2', 'test-e', (msg, ack) => {
6
6
  console.log('msg2', msg.content);
7
7
  // ack(msg);
package/src/index.ts CHANGED
@@ -1,24 +1,13 @@
1
1
  import { connect, Connection, Channel } from 'amqplib';
2
+ import { EventEmitter } from 'events';
3
+ import { reject } from 'bluebird';
2
4
 
5
+ const channelCreatedConst: string = 'channelCreated';
3
6
  class RabbitMq {
4
- channel: Channel;
7
+ channel: Channel | null;
8
+ em: EventEmitter;
9
+ creatingChannel: boolean;
5
10
 
6
- static async init() {
7
- let connection: Connection, channel: Channel;
8
- let host: string = process.env.RABBITMQ_SERVICE_HOST || '104.155.17.91';
9
- try {
10
- connection = await connect(`amqp://${host}`);
11
- channel = await connection.createChannel();
12
- } catch(e) {
13
- throw e;
14
- }
15
- process.once('SIGINT', function () {
16
- console.log('Removing channel');
17
- channel.close();
18
- });
19
-
20
- return new RabbitMq(channel);
21
- }
22
11
  static parseMsg(msg: any) {
23
12
  let { content } = msg;
24
13
  content = content.toString();
@@ -33,42 +22,84 @@ class RabbitMq {
33
22
  }
34
23
  }
35
24
 
36
- constructor(channel: Channel) {
37
- this.channel = channel;
25
+ constructor() {
26
+ this.creatingChannel = false;
27
+ this.em = new EventEmitter;
28
+ this.channel = null;
29
+
30
+ process.once('SIGINT', () => {
31
+ console.log('Removing channel');
32
+ if(this.channel) {
33
+ this.channel.close();
34
+ }
35
+ });
38
36
  }
39
37
 
40
38
  public ack = (msg: any) => {
41
- this.channel.ack(msg);
39
+ if (this.channel) {
40
+ this.channel.ack(msg);
41
+ }
42
+ }
43
+
44
+ async assertChannel() {
45
+ return new Promise<Channel>(async (resolve, reject) => {
46
+ if(this.channel) {
47
+ return resolve(this.channel);
48
+ } if (this.creatingChannel) {
49
+ return this.em.on(channelCreatedConst, resolve);
50
+ }
51
+
52
+ this.creatingChannel = true;
53
+ let connection: Connection, channel: Channel;
54
+ let host: string = process.env.RABBITMQ_SERVICE_HOST || '35.240.13.28';
55
+
56
+ try {
57
+ connection = await connect(`amqp://${host}`);
58
+ channel = await connection.createChannel();
59
+ this.channel = channel;
60
+ this.creatingChannel = false;
61
+ this.em.emit(channelCreatedConst, channel);
62
+ resolve(channel);
63
+ } catch (e) {
64
+ reject(e)
65
+ }
66
+ })
42
67
  }
43
68
 
44
69
  async assertExchange(exchange: string, options?: any) {
45
- return this.channel.assertExchange(exchange, 'fanout')
70
+ const channel: Channel = await this.assertChannel();
71
+ return channel.assertExchange(exchange, 'fanout')
46
72
  }
47
73
 
48
74
  async assertQueue(queue: string, options?: any) {
49
- return this.channel.assertQueue(queue);
75
+ const channel: Channel = await this.assertChannel();
76
+ return channel.assertQueue(queue);
50
77
  }
51
78
 
52
79
  async consume(queue: string, callback: (msg: any, ack: Function) => any) {
80
+ const channel: Channel = await this.assertChannel();
53
81
  await this.assertQueue(queue);
54
- return this.channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
82
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
55
83
  }
56
84
 
57
85
  async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function) => any) {
86
+ const channel: Channel = await this.assertChannel();
58
87
  await Promise.all([
59
88
  this.assertExchange(exchange),
60
89
  this.assertQueue(queue),
61
90
  ]);
62
- await this.channel.bindQueue(queue, exchange, '');
63
- return this.channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
91
+ await channel.bindQueue(queue, exchange, '');
92
+ return channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
64
93
  }
65
94
 
66
95
  async publish(exchange: string, content: any) {
67
- return this.channel.publish(exchange, '', Buffer.from(content));
96
+ const channel: Channel = await this.assertChannel();
97
+ return channel.publish(exchange, '', Buffer.from(content));
68
98
  }
69
99
 
70
100
  async sendToQueue(queue: string, content: any) {
71
- return this.channel.sendToQueue(queue, Buffer.from(content));
101
+ const channel: Channel = await this.assertChannel();
102
+ return channel.sendToQueue(queue, Buffer.from(content));
72
103
  }
73
104
  }
74
105