@quatrain/queue-amqp 1.1.19 → 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.
|
@@ -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
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const AmqpQueueAdapter_1 = require("./AmqpQueueAdapter");
|
|
13
|
+
const amqp_client_1 = require("@cloudamqp/amqp-client");
|
|
14
|
+
const queue_1 = require("@quatrain/queue");
|
|
15
|
+
// Mock the @cloudamqp/amqp-client library
|
|
16
|
+
jest.mock('@cloudamqp/amqp-client');
|
|
17
|
+
// Mock @quatrain/queue to spy on Queue.info
|
|
18
|
+
jest.mock('@quatrain/queue', () => (Object.assign(Object.assign({}, jest.requireActual('@quatrain/queue')), { Queue: {
|
|
19
|
+
info: jest.fn(),
|
|
20
|
+
} })));
|
|
21
|
+
const mockPublish = jest
|
|
22
|
+
.fn()
|
|
23
|
+
.mockResolvedValue({ confirmId: 'mock-confirm-id' });
|
|
24
|
+
const mockSubscribe = jest.fn();
|
|
25
|
+
const mockQueue = jest.fn().mockResolvedValue({
|
|
26
|
+
publish: mockPublish,
|
|
27
|
+
subscribe: mockSubscribe,
|
|
28
|
+
});
|
|
29
|
+
const mockChannel = jest.fn().mockResolvedValue({
|
|
30
|
+
queue: mockQueue,
|
|
31
|
+
});
|
|
32
|
+
const mockConnect = jest.fn().mockResolvedValue({
|
|
33
|
+
channel: mockChannel,
|
|
34
|
+
});
|
|
35
|
+
const AMQPClientMock = amqp_client_1.AMQPClient;
|
|
36
|
+
AMQPClientMock.mockImplementation(() => {
|
|
37
|
+
return {
|
|
38
|
+
connect: mockConnect,
|
|
39
|
+
};
|
|
40
|
+
});
|
|
41
|
+
describe('AmqpQueueAdapter', () => {
|
|
42
|
+
const params = {
|
|
43
|
+
config: {
|
|
44
|
+
host: 'test-host',
|
|
45
|
+
user: 'test-user',
|
|
46
|
+
password: 'test-password',
|
|
47
|
+
port: 1234,
|
|
48
|
+
},
|
|
49
|
+
topic: 'default-topic',
|
|
50
|
+
};
|
|
51
|
+
beforeEach(() => {
|
|
52
|
+
// Clear all instances and calls to constructor and all methods:
|
|
53
|
+
AMQPClientMock.mockClear();
|
|
54
|
+
mockConnect.mockClear();
|
|
55
|
+
mockChannel.mockClear();
|
|
56
|
+
mockQueue.mockClear();
|
|
57
|
+
mockPublish.mockClear();
|
|
58
|
+
mockSubscribe.mockClear();
|
|
59
|
+
queue_1.Queue.info.mockClear();
|
|
60
|
+
});
|
|
61
|
+
it('should construct with default and custom parameters', () => {
|
|
62
|
+
new AmqpQueueAdapter_1.AmqpQueueAdapter({ config: {} });
|
|
63
|
+
expect(AMQPClientMock).toHaveBeenCalledWith('amqp://guest:guest@localhost:5672?frameMax=0');
|
|
64
|
+
new AmqpQueueAdapter_1.AmqpQueueAdapter(params);
|
|
65
|
+
expect(AMQPClientMock).toHaveBeenCalledWith('amqp://test-user:test-password@test-host:1234?frameMax=0');
|
|
66
|
+
});
|
|
67
|
+
describe('send', () => {
|
|
68
|
+
it('should send a message to the specified topic', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
69
|
+
const adapter = new AmqpQueueAdapter_1.AmqpQueueAdapter(params);
|
|
70
|
+
const data = { key: 'value' };
|
|
71
|
+
const topic = 'test-topic';
|
|
72
|
+
const confirmId = yield adapter.send(data, topic);
|
|
73
|
+
expect(mockConnect).toHaveBeenCalledTimes(1);
|
|
74
|
+
expect(mockChannel).toHaveBeenCalledTimes(1);
|
|
75
|
+
expect(mockQueue).toHaveBeenCalledWith(topic);
|
|
76
|
+
expect(mockPublish).toHaveBeenCalledWith(Buffer.from(JSON.stringify(data)), { deliveryMode: 2 });
|
|
77
|
+
expect(queue_1.Queue.info).toHaveBeenCalledWith(`[AMQP] Sending message to ${topic}`);
|
|
78
|
+
expect(queue_1.Queue.info).toHaveBeenCalledWith(`[AMQP] Message send with id mock-confirm-id`);
|
|
79
|
+
expect(confirmId).toBe('mock-confirm-id');
|
|
80
|
+
}));
|
|
81
|
+
});
|
|
82
|
+
describe('listen', () => {
|
|
83
|
+
it('should listen to the default topic if none is provided', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
84
|
+
const adapter = new AmqpQueueAdapter_1.AmqpQueueAdapter(params);
|
|
85
|
+
const handler = jest.fn();
|
|
86
|
+
yield adapter.listen(undefined, handler);
|
|
87
|
+
expect(mockConnect).toHaveBeenCalledTimes(1);
|
|
88
|
+
expect(mockChannel).toHaveBeenCalledTimes(1);
|
|
89
|
+
expect(mockQueue).toHaveBeenCalledWith(params.topic);
|
|
90
|
+
expect(mockSubscribe).toHaveBeenCalledWith({ noAck: true }, expect.any(Function));
|
|
91
|
+
}));
|
|
92
|
+
it('should listen to a specific topic', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
93
|
+
const adapter = new AmqpQueueAdapter_1.AmqpQueueAdapter(params);
|
|
94
|
+
const handler = jest.fn();
|
|
95
|
+
const topic = 'specific-topic';
|
|
96
|
+
yield adapter.listen(topic, handler);
|
|
97
|
+
expect(mockQueue).toHaveBeenCalledWith(topic);
|
|
98
|
+
}));
|
|
99
|
+
it('should throw an error if no topic is available', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
100
|
+
const adapter = new AmqpQueueAdapter_1.AmqpQueueAdapter({ config: {} });
|
|
101
|
+
const handler = jest.fn();
|
|
102
|
+
yield expect(adapter.listen(undefined, handler)).rejects.toThrow('No topic provided for listening.');
|
|
103
|
+
}));
|
|
104
|
+
it('should call the message handler with the message body', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
105
|
+
const adapter = new AmqpQueueAdapter_1.AmqpQueueAdapter(params);
|
|
106
|
+
const handler = jest.fn();
|
|
107
|
+
const messageContent = { data: 'test message' };
|
|
108
|
+
const message = {
|
|
109
|
+
bodyToString: () => JSON.stringify(messageContent),
|
|
110
|
+
};
|
|
111
|
+
mockSubscribe.mockImplementation((options, callback) => __awaiter(void 0, void 0, void 0, function* () {
|
|
112
|
+
yield callback(message);
|
|
113
|
+
}));
|
|
114
|
+
yield adapter.listen('any-topic', handler);
|
|
115
|
+
expect(handler).toHaveBeenCalledWith(JSON.stringify(messageContent));
|
|
116
|
+
}));
|
|
117
|
+
});
|
|
118
|
+
});
|
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",
|