@quatrain/queue-amqp 1.2.15 → 1.2.16

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,16 @@
1
+ import { AbstractQueueAdapter } from '@quatrain/queue';
2
+ import amqplib, { ChannelModel } from 'amqplib';
3
+ export declare class AmqpQueueAdapter extends AbstractQueueAdapter {
4
+ protected _client: ChannelModel | undefined;
5
+ protected _connect(): Promise<ChannelModel>;
6
+ protected _disconnect(): Promise<void>;
7
+ send(data: any, topic: string): Promise<string>;
8
+ /**
9
+ * Listen to given queue and process messages with messageHandler function
10
+ * @param topic
11
+ * @param messageHandler
12
+ * @param params
13
+ * @returns
14
+ */
15
+ listen(topic: string | undefined, messageHandler: Function, params?: any): Promise<amqplib.Replies.Consume>;
16
+ }
@@ -0,0 +1,100 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.AmqpQueueAdapter = void 0;
16
+ const queue_1 = require("@quatrain/queue");
17
+ const amqplib_1 = __importDefault(require("amqplib"));
18
+ class AmqpQueueAdapter extends queue_1.AbstractQueueAdapter {
19
+ _connect() {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ if (!this._client) {
22
+ const { host = 'localhost', user = 'guest', password = 'guest', port = 5672, } = this._params.config || {};
23
+ this._client = yield amqplib_1.default.connect(`amqp://${user}:${password}@${host}:${port}`);
24
+ }
25
+ return this._client;
26
+ });
27
+ }
28
+ _disconnect() {
29
+ var _a;
30
+ return __awaiter(this, void 0, void 0, function* () {
31
+ yield ((_a = this._client) === null || _a === void 0 ? void 0 : _a.close());
32
+ });
33
+ }
34
+ send(data, topic) {
35
+ var _a;
36
+ return __awaiter(this, void 0, void 0, function* () {
37
+ yield this._connect();
38
+ const channel = yield ((_a = this._client) === null || _a === void 0 ? void 0 : _a.createChannel());
39
+ const dataBuffer = Buffer.from(JSON.stringify(data));
40
+ queue_1.Queue.info(`[AMQP] Sending message to ${topic}`);
41
+ channel === null || channel === void 0 ? void 0 : channel.sendToQueue(topic, dataBuffer);
42
+ channel === null || channel === void 0 ? void 0 : channel.close();
43
+ const id = Date.now(); // fake
44
+ queue_1.Queue.info(`[AMQP] Message send with id ${id}`);
45
+ return String(id);
46
+ });
47
+ }
48
+ /**
49
+ * Listen to given queue and process messages with messageHandler function
50
+ * @param topic
51
+ * @param messageHandler
52
+ * @param params
53
+ * @returns
54
+ */
55
+ listen(topic = this._params.topic, messageHandler, params) {
56
+ return __awaiter(this, void 0, void 0, function* () {
57
+ if (!topic) {
58
+ throw new Error(`No topic provided for listening.`);
59
+ }
60
+ const concurrency = (params === null || params === void 0 ? void 0 : params.concurrency) || 0;
61
+ queue_1.Queue.info(`Starting to listen to topic ${topic} with max concurrency set to ${concurrency}`);
62
+ let concurrents = 0;
63
+ const client = yield this._connect();
64
+ const channel = yield client.createChannel();
65
+ yield channel.assertQueue(topic, { durable: true, autoDelete: false });
66
+ yield channel.prefetch(concurrency); // only get one message at a time
67
+ return channel.consume(topic, (msg) => __awaiter(this, void 0, void 0, function* () {
68
+ if (msg === null || !channel) {
69
+ return;
70
+ }
71
+ concurrents++;
72
+ queue_1.Queue.info(`Job #${concurrents} of ${concurrency > 0 ? concurrency : 'unlimited'} started`);
73
+ try {
74
+ // 1. Process the message synchronously (wait for it to finish)
75
+ const result = yield messageHandler(msg.content.toString(), params);
76
+ if (result === false) {
77
+ throw new Error('messageHandler function failed, see status log in jobExecution for more information');
78
+ }
79
+ // 2. Acknowledge the message only after successful processing
80
+ channel.ack(msg);
81
+ // 3. Check if we reached the limit of messages to process
82
+ if (concurrency > 0 && concurrents >= concurrency) {
83
+ queue_1.Queue.info(`Reached max concurrency of ${concurrency}, disconnecting from queue`);
84
+ yield channel.close();
85
+ yield client.close();
86
+ process.exit(0);
87
+ }
88
+ }
89
+ catch (err) {
90
+ queue_1.Queue.error(`messageHandler function failed with message: ${err.message}`);
91
+ // Negative acknowledge the message so it can be requeued or handled by DLQ
92
+ channel.nack(msg);
93
+ process.exit(1);
94
+ }
95
+ }), { noAck: false } // disable auto-hack
96
+ );
97
+ });
98
+ }
99
+ }
100
+ exports.AmqpQueueAdapter = AmqpQueueAdapter;
@@ -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
+ });
@@ -0,0 +1,2 @@
1
+ import { AmqpQueueAdapter } from './AmqpQueueAdapter';
2
+ export { AmqpQueueAdapter };
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AmqpQueueAdapter = void 0;
4
+ const AmqpQueueAdapter_1 = require("./AmqpQueueAdapter");
5
+ Object.defineProperty(exports, "AmqpQueueAdapter", { enumerable: true, get: function () { return AmqpQueueAdapter_1.AmqpQueueAdapter; } });
package/package.json CHANGED
@@ -1,42 +1,42 @@
1
1
  {
2
- "name": "@quatrain/queue-amqp",
3
- "version": "1.2.15",
4
- "description": "Queue adapter for AMQP compatible queue",
5
- "main": "lib/index.js",
6
- "types": "lib/index.d.ts",
7
- "bun": "src/index.ts",
8
- "files": [
9
- "LICENSE.md",
10
- "src/",
11
- "lib/",
12
- "README.md"
13
- ],
14
- "author": "Quatrain Développement SAS <developers@quatrain.com>",
15
- "license": "AGPL-3.0-only",
16
- "devDependencies": {
17
- "@tsconfig/recommended": "^1.0.1",
18
- "@types/amqplib": "^0.10.8",
19
- "@types/fs-extra": "^11.0.4",
20
- "@types/jest": "^27.0.3",
21
- "@types/node": "^22.10.1",
22
- "@types/object-hash": "^3.0.6",
23
- "jest": "^30.2.0",
24
- "jest-node-exports-resolver": "^1.1.6",
25
- "trace-unhandled": "^2.0.1",
26
- "ts-jest": "^27.1.2",
27
- "ts-node": "^10.4.0",
28
- "typescript": "^5.1.5"
29
- },
30
- "dependencies": {
31
- "@cloudamqp/amqp-client": "^3.4.1",
32
- "@quatrain/core": "^1.1.43",
33
- "@quatrain/queue": "^1.1.22",
34
- "amqplib": "^0.10.9"
35
- },
36
- "scripts": {
37
- "test-ci": "jest --runInBand",
38
- "build": "tsc",
39
- "wbuild": "tsc --watch",
40
- "bump-to": "yarn version"
41
- }
42
- }
2
+ "name": "@quatrain/queue-amqp",
3
+ "version": "1.2.16",
4
+ "description": "Queue adapter for AMQP compatible queue",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "bun": "src/index.ts",
8
+ "files": [
9
+ "LICENSE.md",
10
+ "src/",
11
+ "dist/",
12
+ "README.md"
13
+ ],
14
+ "author": "Quatrain Développement SAS <developers@quatrain.com>",
15
+ "license": "AGPL-3.0-only",
16
+ "devDependencies": {
17
+ "@tsconfig/recommended": "^1.0.1",
18
+ "@types/amqplib": "^0.10.8",
19
+ "@types/fs-extra": "^11.0.4",
20
+ "@types/jest": "^27.0.3",
21
+ "@types/node": "^22.10.1",
22
+ "@types/object-hash": "^3.0.6",
23
+ "jest": "^30.2.0",
24
+ "jest-node-exports-resolver": "^1.1.6",
25
+ "trace-unhandled": "^2.0.1",
26
+ "ts-jest": "^27.1.2",
27
+ "ts-node": "^10.4.0",
28
+ "typescript": "^5.1.5"
29
+ },
30
+ "dependencies": {
31
+ "@cloudamqp/amqp-client": "^3.4.1",
32
+ "@quatrain/core": "^1.1.45",
33
+ "@quatrain/queue": "^1.1.23",
34
+ "amqplib": "^0.10.9"
35
+ },
36
+ "scripts": {
37
+ "test-ci": "jest --runInBand",
38
+ "build": "tsc",
39
+ "wbuild": "tsc --watch",
40
+ "bump-to": "yarn version"
41
+ }
42
+ }