@quatrain/queue-amqp 1.1.20 → 1.2.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.
- package/lib/AmqpQueueAdapter.d.ts +2 -2
- package/lib/AmqpQueueAdapter.js +45 -15
- package/package.json +4 -2
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { AbstractQueueAdapter
|
|
1
|
+
import { AbstractQueueAdapter } from '@quatrain/queue';
|
|
2
2
|
export declare class AmqpQueueAdapter extends AbstractQueueAdapter {
|
|
3
|
-
|
|
3
|
+
protected _connect(): Promise<any>;
|
|
4
4
|
send(data: any, topic: string): Promise<string>;
|
|
5
5
|
listen(topic: string | undefined, messageHandler: Function): Promise<any>;
|
|
6
6
|
}
|
package/lib/AmqpQueueAdapter.js
CHANGED
|
@@ -8,26 +8,48 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
11
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
15
|
exports.AmqpQueueAdapter = void 0;
|
|
13
16
|
const queue_1 = require("@quatrain/queue");
|
|
14
|
-
const
|
|
17
|
+
const amqplib_1 = __importDefault(require("amqplib"));
|
|
15
18
|
class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
|
|
16
|
-
constructor(params) {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
// constructor(params: QueueParameters) {
|
|
20
|
+
// super(params)
|
|
21
|
+
// const {
|
|
22
|
+
// host = 'localhost',
|
|
23
|
+
// user = 'guest',
|
|
24
|
+
// password = 'guest',
|
|
25
|
+
// port = 5672,
|
|
26
|
+
// } = params.config || {}
|
|
27
|
+
// this._client = new AMQPClient(
|
|
28
|
+
// `amqp://${user}:${password}@${host}:${port}?frameMax=0x80000000`
|
|
29
|
+
// )
|
|
30
|
+
// }
|
|
31
|
+
_connect() {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
if (this._client) {
|
|
34
|
+
return this._client;
|
|
35
|
+
}
|
|
36
|
+
const { host = 'localhost', user = 'guest', password = 'guest', port = 5672, } = this._params.config || {};
|
|
37
|
+
this._client = amqplib_1.default.connect(`amqp://${user}:${password}@${host}:${port}`);
|
|
38
|
+
});
|
|
20
39
|
}
|
|
21
40
|
send(data, topic) {
|
|
22
41
|
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
-
|
|
24
|
-
const
|
|
25
|
-
const
|
|
42
|
+
yield this._connect(); // this._client.connect()
|
|
43
|
+
const ch = yield this._client.createChannel();
|
|
44
|
+
// const channel: AMQPChannel = await connection.channel()
|
|
45
|
+
// const queue: AMQPQueue = await channel.queue(topic)
|
|
26
46
|
const dataBuffer = Buffer.from(JSON.stringify(data));
|
|
27
47
|
queue_1.Queue.info(`[AMQP] Sending message to ${topic}`);
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
48
|
+
ch.sendToQueue(topic, dataBuffer);
|
|
49
|
+
// const res = await queue.publish(dataBuffer, { deliveryMode: 1 })
|
|
50
|
+
const id = Date.now(); // fake
|
|
51
|
+
queue_1.Queue.info(`[AMQP] Message send with id ${id}`);
|
|
52
|
+
return String(id);
|
|
31
53
|
});
|
|
32
54
|
}
|
|
33
55
|
listen(topic = this._params.topic, messageHandler) {
|
|
@@ -35,10 +57,18 @@ class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
|
|
|
35
57
|
if (!topic) {
|
|
36
58
|
throw new Error(`No topic provided for listening.`);
|
|
37
59
|
}
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
return
|
|
60
|
+
yield this._connect();
|
|
61
|
+
const ch = yield this._client.createChannel();
|
|
62
|
+
yield ch.assertQueue(topic);
|
|
63
|
+
return ch.consume(topic, (message) => __awaiter(this, void 0, void 0, function* () { return yield messageHandler(message.content.toString()); }));
|
|
64
|
+
// const connection = await this._client.connect()
|
|
65
|
+
// const channel = await connection.channel()
|
|
66
|
+
// const queue = await channel.queue(topic)
|
|
67
|
+
// return queue.subscribe(
|
|
68
|
+
// { noAck: true },
|
|
69
|
+
// async (message: AMQPMessage) =>
|
|
70
|
+
// await messageHandler(message.bodyToString())
|
|
71
|
+
// )
|
|
42
72
|
});
|
|
43
73
|
}
|
|
44
74
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quatrain/queue-amqp",
|
|
3
|
-
"version": "1.1
|
|
3
|
+
"version": "1.2.1",
|
|
4
4
|
"description": "Queue adapter for AMQP compatible queue",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"license": "MIT",
|
|
13
13
|
"devDependencies": {
|
|
14
14
|
"@tsconfig/recommended": "^1.0.1",
|
|
15
|
+
"@types/amqplib": "^0",
|
|
15
16
|
"@types/fs-extra": "^11.0.4",
|
|
16
17
|
"@types/jest": "^27.0.3",
|
|
17
18
|
"@types/node": "^22.10.1",
|
|
@@ -26,7 +27,8 @@
|
|
|
26
27
|
"dependencies": {
|
|
27
28
|
"@cloudamqp/amqp-client": "^3.4.1",
|
|
28
29
|
"@quatrain/core": "^1.1.22",
|
|
29
|
-
"@quatrain/queue": "^1.1.16"
|
|
30
|
+
"@quatrain/queue": "^1.1.16",
|
|
31
|
+
"amqplib": "^0.10.9"
|
|
30
32
|
},
|
|
31
33
|
"scripts": {
|
|
32
34
|
"test-ci": "jest --runInBand",
|