@autofleet/rabbit 1.0.1

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.
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ const index_1 = require("./index");
12
+ const init = () => __awaiter(this, void 0, void 0, function* () {
13
+ const rabbit = yield index_1.default.init();
14
+ yield rabbit.consumeFromExchange('test-q2', 'test-e', (msg, ack) => {
15
+ console.log('msg2', msg.content);
16
+ // ack(msg);
17
+ });
18
+ yield rabbit.consumeFromExchange('test-q', 'test-e', (msg, ack) => {
19
+ console.log('msg', msg.content);
20
+ ack(msg);
21
+ });
22
+ yield rabbit.publish('test-e', 'hey');
23
+ yield rabbit.publish('test-e', 'hey1');
24
+ yield rabbit.consume('test-q-2', (msg, ack) => {
25
+ console.log('msg-q', msg.content);
26
+ ack(msg);
27
+ });
28
+ yield rabbit.sendToQueue('test-q-2', 'hey-q');
29
+ });
30
+ init();
@@ -0,0 +1,15 @@
1
+ import { Channel } from 'amqplib';
2
+ declare class RabbitMq {
3
+ channel: Channel;
4
+ static init(): Promise<RabbitMq>;
5
+ static parseMsg(msg: any): any;
6
+ constructor(channel: Channel);
7
+ ack: (msg: any) => void;
8
+ assertExchange(exchange: string, options?: any): Promise<import("amqplib").Replies.AssertExchange>;
9
+ assertQueue(queue: string, options?: any): Promise<import("amqplib").Replies.AssertQueue>;
10
+ consume(queue: string, callback: (msg: any, ack: Function) => any): Promise<import("amqplib").Replies.Consume>;
11
+ consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function) => any): Promise<import("amqplib").Replies.Consume>;
12
+ publish(exchange: string, content: any): Promise<boolean>;
13
+ sendToQueue(queue: string, content: any): Promise<boolean>;
14
+ }
15
+ export default RabbitMq;
package/dist/index.js ADDED
@@ -0,0 +1,83 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ return new (P || (P = Promise))(function (resolve, reject) {
4
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
+ function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
7
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
8
+ });
9
+ };
10
+ Object.defineProperty(exports, "__esModule", { value: true });
11
+ const amqplib_1 = require("amqplib");
12
+ class RabbitMq {
13
+ constructor(channel) {
14
+ 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();
26
+ }
27
+ catch (e) {
28
+ throw e;
29
+ }
30
+ process.once('SIGINT', function () {
31
+ console.log('Removing channel');
32
+ channel.close();
33
+ });
34
+ return new RabbitMq(channel);
35
+ });
36
+ }
37
+ static parseMsg(msg) {
38
+ let { content } = msg;
39
+ content = content.toString();
40
+ try {
41
+ content = JSON.parse(content);
42
+ }
43
+ catch (e) { }
44
+ return Object.assign({}, msg, { content });
45
+ }
46
+ assertExchange(exchange, options) {
47
+ return __awaiter(this, void 0, void 0, function* () {
48
+ return this.channel.assertExchange(exchange, 'fanout');
49
+ });
50
+ }
51
+ assertQueue(queue, options) {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ return this.channel.assertQueue(queue);
54
+ });
55
+ }
56
+ consume(queue, callback) {
57
+ return __awaiter(this, void 0, void 0, function* () {
58
+ yield this.assertQueue(queue);
59
+ return this.channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
60
+ });
61
+ }
62
+ consumeFromExchange(queue, exchange, callback) {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ yield Promise.all([
65
+ this.assertExchange(exchange),
66
+ this.assertQueue(queue),
67
+ ]);
68
+ yield this.channel.bindQueue(queue, exchange, '');
69
+ return this.channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
70
+ });
71
+ }
72
+ publish(exchange, content) {
73
+ return __awaiter(this, void 0, void 0, function* () {
74
+ return this.channel.publish(exchange, '', Buffer.from(content));
75
+ });
76
+ }
77
+ sendToQueue(queue, content) {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ return this.channel.sendToQueue(queue, Buffer.from(content));
80
+ });
81
+ }
82
+ }
83
+ exports.default = RabbitMq;
package/nodemon.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "ignore": [
3
+ "**/*.test.ts",
4
+ "**/*.spec.ts",
5
+ ".git",
6
+ "node_modules"
7
+ ],
8
+ "watch": [
9
+ "src"
10
+ ],
11
+ "exec": "npm start",
12
+ "ext": "ts"
13
+ }
package/package.json ADDED
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "@autofleet/rabbit",
3
+ "version": "1.0.1",
4
+ "description": "",
5
+ "main": "dist/index.js",
6
+ "scripts": {
7
+ "start": "ts-node src/index.ts",
8
+ "prepublish": "tsc",
9
+ "dev": "nodemon"
10
+ },
11
+ "dependencies": {
12
+ "@types/amqplib": "^0.5.9",
13
+ "amqplib": "^0.5.3"
14
+ },
15
+ "devDependencies": {
16
+ "ts-node": "^7.0.1",
17
+ "typescript": "^3.2.1"
18
+ },
19
+ "author": "",
20
+ "license": "ISC"
21
+ }
package/src/example.ts ADDED
@@ -0,0 +1,25 @@
1
+ import RabbitMq from './index';
2
+
3
+ const init = async () => {
4
+ const rabbit = await RabbitMq.init();
5
+ await rabbit.consumeFromExchange('test-q2', 'test-e', (msg, ack) => {
6
+ console.log('msg2', msg.content);
7
+ // ack(msg);
8
+ });
9
+
10
+ await rabbit.consumeFromExchange('test-q', 'test-e', (msg, ack) => {
11
+ console.log('msg', msg.content);
12
+ ack(msg);
13
+ });
14
+ await rabbit.publish('test-e', 'hey')
15
+ await rabbit.publish('test-e', 'hey1')
16
+
17
+
18
+ await rabbit.consume('test-q-2', (msg, ack) => {
19
+ console.log('msg-q', msg.content);
20
+ ack(msg);
21
+ });
22
+ await rabbit.sendToQueue('test-q-2', 'hey-q')
23
+ }
24
+
25
+ init();
package/src/index.ts ADDED
@@ -0,0 +1,75 @@
1
+ import { connect, Connection, Channel } from 'amqplib';
2
+
3
+ class RabbitMq {
4
+ channel: Channel;
5
+
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
+ static parseMsg(msg: any) {
23
+ let { content } = msg;
24
+ content = content.toString();
25
+
26
+ try {
27
+ content = JSON.parse(content);
28
+ } catch(e) { }
29
+
30
+ return {
31
+ ...msg,
32
+ content,
33
+ }
34
+ }
35
+
36
+ constructor(channel: Channel) {
37
+ this.channel = channel;
38
+ }
39
+
40
+ public ack = (msg: any) => {
41
+ this.channel.ack(msg);
42
+ }
43
+
44
+ async assertExchange(exchange: string, options?: any) {
45
+ return this.channel.assertExchange(exchange, 'fanout')
46
+ }
47
+
48
+ async assertQueue(queue: string, options?: any) {
49
+ return this.channel.assertQueue(queue);
50
+ }
51
+
52
+ async consume(queue: string, callback: (msg: any, ack: Function) => any) {
53
+ await this.assertQueue(queue);
54
+ return this.channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
55
+ }
56
+
57
+ async consumeFromExchange(queue: string, exchange: string, callback: (msg: any, ack: Function) => any) {
58
+ await Promise.all([
59
+ this.assertExchange(exchange),
60
+ this.assertQueue(queue),
61
+ ]);
62
+ await this.channel.bindQueue(queue, exchange, '');
63
+ return this.channel.consume(queue, (msg) => callback(RabbitMq.parseMsg(msg), this.ack));
64
+ }
65
+
66
+ async publish(exchange: string, content: any) {
67
+ return this.channel.publish(exchange, '', Buffer.from(content));
68
+ }
69
+
70
+ async sendToQueue(queue: string, content: any) {
71
+ return this.channel.sendToQueue(queue, Buffer.from(content));
72
+ }
73
+ }
74
+
75
+ export default RabbitMq;
package/tsconfig.json ADDED
@@ -0,0 +1,9 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "es6",
4
+ "module": "commonjs",
5
+ "declaration": true,
6
+ "outDir": "./dist",
7
+ "strict": true
8
+ }
9
+ }