amqp-extension 3.0.0 → 3.1.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/CHANGELOG.md +15 -0
- package/dist/config/module.js +2 -0
- package/dist/config/type.d.ts +2 -0
- package/dist/consume/type.d.ts +6 -2
- package/dist/module.d.ts +12 -2
- package/dist/module.js +54 -5
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +9 -1
- package/package.json +6 -4
- package/tsconfig.eslint.json +1 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,18 @@
|
|
|
1
|
+
## [3.1.1](https://github.com/Tada5hi/amqp-extension/compare/v3.1.0...v3.1.1) (2024-03-07)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* don't register consumer multiple times ([b45defa](https://github.com/Tada5hi/amqp-extension/commit/b45defa4dbd5a7abf6d2568586e2ff5bf2e62d16))
|
|
7
|
+
|
|
8
|
+
# [3.1.0](https://github.com/Tada5hi/amqp-extension/compare/v3.0.0...v3.1.0) (2024-03-06)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Features
|
|
12
|
+
|
|
13
|
+
* reconnect strategy ([#320](https://github.com/Tada5hi/amqp-extension/issues/320)) ([4c31551](https://github.com/Tada5hi/amqp-extension/commit/4c31551029916dcaf055f78d53a8af7c626c393d))
|
|
14
|
+
* reconnect strategy ([#320](https://github.com/Tada5hi/amqp-extension/issues/320)) ([17b141f](https://github.com/Tada5hi/amqp-extension/commit/17b141f6bd6256172e8c03a1006bfc1cab6225eb))
|
|
15
|
+
|
|
1
16
|
# [3.0.0](https://github.com/Tada5hi/amqp-extension/compare/v2.0.2...v3.0.0) (2024-02-28)
|
|
2
17
|
|
|
3
18
|
|
package/dist/config/module.js
CHANGED
|
@@ -10,6 +10,8 @@ exports.buildConfig = void 0;
|
|
|
10
10
|
const exchange_1 = require("../exchange");
|
|
11
11
|
function buildConfig(input) {
|
|
12
12
|
return {
|
|
13
|
+
reconnectAttempts: 10,
|
|
14
|
+
reconnectTimeout: 1000,
|
|
13
15
|
connection: input.connection,
|
|
14
16
|
publish: input.publish || {},
|
|
15
17
|
consume: input.consume || {},
|
package/dist/config/type.d.ts
CHANGED
|
@@ -2,6 +2,8 @@ import type { Options } from 'amqplib';
|
|
|
2
2
|
import type { ExchangeOptions } from '../exchange';
|
|
3
3
|
import type { ConsumeOptions, PublishOptions } from '../type';
|
|
4
4
|
export type Config = {
|
|
5
|
+
reconnectAttempts: number;
|
|
6
|
+
reconnectTimeout: number;
|
|
5
7
|
connection: Options.Connect | string;
|
|
6
8
|
exchange: ExchangeOptions;
|
|
7
9
|
publish: PublishOptions;
|
package/dist/consume/type.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import type { Channel, ConsumeMessage } from 'amqplib';
|
|
2
2
|
import type { ConsumeHandlerAnyKey } from './static';
|
|
3
3
|
export { ConsumeMessage, };
|
|
4
|
-
export type ConsumeMessageHandler = (message: ConsumeMessage, channel: Channel) => Promise<void
|
|
4
|
+
export type ConsumeMessageHandler = (message: ConsumeMessage, channel: Channel) => Promise<void> | void;
|
|
5
5
|
export type ConsumeHandlerAnyKeyType = typeof ConsumeHandlerAnyKey;
|
|
6
|
-
export type ConsumeHandlers =
|
|
6
|
+
export type ConsumeHandlers = {
|
|
7
|
+
[ConsumeHandlerAnyKey]?: ConsumeMessageHandler;
|
|
8
|
+
} & {
|
|
9
|
+
[key: string]: ConsumeMessageHandler;
|
|
10
|
+
};
|
package/dist/module.d.ts
CHANGED
|
@@ -1,13 +1,23 @@
|
|
|
1
|
-
import type { Connection } from 'amqplib';
|
|
1
|
+
import type { Channel, Connection } from 'amqplib';
|
|
2
2
|
import type { Config, ConfigInput } from './config';
|
|
3
3
|
import type { ConsumeHandlers } from './consume';
|
|
4
4
|
import type { PublishOptionsExtended } from './publish';
|
|
5
5
|
import type { ConsumeOptions } from './type';
|
|
6
|
+
type Consumer = {
|
|
7
|
+
options: ConsumeOptions;
|
|
8
|
+
handlers: ConsumeHandlers;
|
|
9
|
+
};
|
|
6
10
|
export declare class Client {
|
|
7
11
|
protected connection: Connection | undefined;
|
|
8
12
|
protected config: Config;
|
|
13
|
+
protected reconnectAttempts: number;
|
|
14
|
+
protected consumers: Consumer[];
|
|
9
15
|
constructor(options: ConfigInput);
|
|
16
|
+
protected createConnection(): Promise<Connection>;
|
|
10
17
|
useConnection(): Promise<Connection>;
|
|
18
|
+
protected recreateConsumers(): Promise<void>;
|
|
19
|
+
protected createConsumer(options: ConsumeOptions, handlers: ConsumeHandlers): Promise<Channel>;
|
|
11
20
|
consume(options: ConsumeOptions, handlers: ConsumeHandlers): Promise<void>;
|
|
12
|
-
publish(options: PublishOptionsExtended): Promise<
|
|
21
|
+
publish(options: PublishOptionsExtended): Promise<boolean>;
|
|
13
22
|
}
|
|
23
|
+
export {};
|
package/dist/module.js
CHANGED
|
@@ -5,8 +5,12 @@
|
|
|
5
5
|
* For the full copyright and license information,
|
|
6
6
|
* view the LICENSE file that was distributed with this source code.
|
|
7
7
|
*/
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
8
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
12
|
exports.Client = void 0;
|
|
13
|
+
const node_process_1 = __importDefault(require("node:process"));
|
|
10
14
|
const amqplib_1 = require("amqplib");
|
|
11
15
|
const smob_1 = require("smob");
|
|
12
16
|
const uuid_1 = require("uuid");
|
|
@@ -14,18 +18,56 @@ const config_1 = require("./config");
|
|
|
14
18
|
const consume_1 = require("./consume");
|
|
15
19
|
const exchange_1 = require("./exchange");
|
|
16
20
|
const publish_1 = require("./publish");
|
|
21
|
+
const utils_1 = require("./utils");
|
|
17
22
|
class Client {
|
|
18
23
|
constructor(options) {
|
|
19
24
|
this.config = (0, config_1.buildConfig)(options);
|
|
25
|
+
this.reconnectAttempts = 0;
|
|
26
|
+
this.consumers = [];
|
|
27
|
+
node_process_1.default.once('SIGINT', async () => {
|
|
28
|
+
if (this.connection) {
|
|
29
|
+
await this.connection.close();
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async createConnection() {
|
|
34
|
+
let connection;
|
|
35
|
+
try {
|
|
36
|
+
connection = await (0, amqplib_1.connect)(this.config.connection);
|
|
37
|
+
this.reconnectAttempts = 0;
|
|
38
|
+
}
|
|
39
|
+
catch (e) {
|
|
40
|
+
if (this.reconnectAttempts < this.config.reconnectAttempts) {
|
|
41
|
+
this.reconnectAttempts++;
|
|
42
|
+
await (0, utils_1.wait)(this.config.reconnectTimeout);
|
|
43
|
+
return this.createConnection();
|
|
44
|
+
}
|
|
45
|
+
throw e;
|
|
46
|
+
}
|
|
47
|
+
return connection;
|
|
20
48
|
}
|
|
21
49
|
async useConnection() {
|
|
22
50
|
if (typeof this.connection !== 'undefined') {
|
|
23
51
|
return this.connection;
|
|
24
52
|
}
|
|
25
|
-
|
|
53
|
+
const connection = await this.createConnection();
|
|
54
|
+
const handleDisconnect = async (err) => {
|
|
55
|
+
if (!err)
|
|
56
|
+
return;
|
|
57
|
+
this.connection = await this.createConnection();
|
|
58
|
+
await this.recreateConsumers();
|
|
59
|
+
};
|
|
60
|
+
connection.once('close', handleDisconnect);
|
|
61
|
+
connection.once('error', handleDisconnect);
|
|
62
|
+
this.connection = connection;
|
|
26
63
|
return this.connection;
|
|
27
64
|
}
|
|
28
|
-
async
|
|
65
|
+
async recreateConsumers() {
|
|
66
|
+
for (let i = 0; i < this.consumers.length; i++) {
|
|
67
|
+
await this.createConsumer(this.consumers[i].options, this.consumers[i].handlers);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async createConsumer(options, handlers) {
|
|
29
71
|
const connection = await this.useConnection();
|
|
30
72
|
const channel = await connection.createChannel();
|
|
31
73
|
options = (0, smob_1.merge)(options, this.config.consume);
|
|
@@ -78,6 +120,14 @@ class Client {
|
|
|
78
120
|
}
|
|
79
121
|
};
|
|
80
122
|
await channel.consume(queueName, (message) => handleMessage(message), (0, consume_1.buildDriverConsumeOptions)(options));
|
|
123
|
+
return channel;
|
|
124
|
+
}
|
|
125
|
+
async consume(options, handlers) {
|
|
126
|
+
await this.createConsumer(options, handlers);
|
|
127
|
+
this.consumers.push({
|
|
128
|
+
options,
|
|
129
|
+
handlers,
|
|
130
|
+
});
|
|
81
131
|
}
|
|
82
132
|
async publish(options) {
|
|
83
133
|
let buffer;
|
|
@@ -107,11 +157,10 @@ class Client {
|
|
|
107
157
|
if (typeof exchangeOptions.routingKey === 'undefined') {
|
|
108
158
|
throw new Error('The routingKey can not be empty if a non default exchange is selected.');
|
|
109
159
|
}
|
|
110
|
-
channel.publish(this.config.exchange.name, exchangeOptions.routingKey, buffer, (0, publish_1.buildDriverPublishOptions)({
|
|
160
|
+
return channel.publish(this.config.exchange.name, exchangeOptions.routingKey, buffer, (0, publish_1.buildDriverPublishOptions)({
|
|
111
161
|
persistent: true,
|
|
112
162
|
...options,
|
|
113
163
|
}));
|
|
114
|
-
return;
|
|
115
164
|
}
|
|
116
165
|
// publish to default exchange
|
|
117
166
|
const queueName = options.queueName ||
|
|
@@ -122,7 +171,7 @@ class Client {
|
|
|
122
171
|
await channel.assertQueue(queueName, {
|
|
123
172
|
durable: true,
|
|
124
173
|
});
|
|
125
|
-
channel.sendToQueue(queueName, buffer, (0, publish_1.buildDriverPublishOptions)({
|
|
174
|
+
return channel.sendToQueue(queueName, buffer, (0, publish_1.buildDriverPublishOptions)({
|
|
126
175
|
persistent: true,
|
|
127
176
|
...options,
|
|
128
177
|
}));
|
package/dist/utils.d.ts
CHANGED
package/dist/utils.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.removeKeysFromOptions = void 0;
|
|
3
|
+
exports.wait = exports.removeKeysFromOptions = void 0;
|
|
4
4
|
const smob_1 = require("smob");
|
|
5
5
|
function removeKeysFromOptions(options, keys) {
|
|
6
6
|
for (let i = 0; i < keys.length; i++) {
|
|
@@ -11,3 +11,11 @@ function removeKeysFromOptions(options, keys) {
|
|
|
11
11
|
return options;
|
|
12
12
|
}
|
|
13
13
|
exports.removeKeysFromOptions = removeKeysFromOptions;
|
|
14
|
+
async function wait(ms) {
|
|
15
|
+
return new Promise((resolve) => {
|
|
16
|
+
setTimeout(() => {
|
|
17
|
+
resolve();
|
|
18
|
+
}, ms);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
exports.wait = wait;
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "amqp-extension",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.1.1",
|
|
4
4
|
"description": "An amqp extension with functions and utility functions to consume and publish queue messages.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"typings": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "rm -rf ./dist && tsc",
|
|
9
|
-
"test": "cross-env NODE_ENV=test jest --config ./test/jest.config.js",
|
|
9
|
+
"test": "cross-env NODE_ENV=test jest --config ./test/jest.config.js --detectOpenHandles",
|
|
10
10
|
"test:coverage": "cross-env NODE_ENV=test jest --config ./test/jest.config.js --coverage",
|
|
11
11
|
"lint": "eslint --ext .js,.vue,.ts ./src",
|
|
12
12
|
"lint:fix": "npm run lint -- --fix",
|
|
@@ -43,16 +43,18 @@
|
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@tada5hi/commitlint-config": "^1.1.7",
|
|
45
45
|
"@tada5hi/eslint-config-typescript": "^1.2.6",
|
|
46
|
-
"@tada5hi/semantic-release": "^0.3.
|
|
46
|
+
"@tada5hi/semantic-release": "^0.3.1",
|
|
47
47
|
"@tada5hi/tsconfig": "^0.5.0",
|
|
48
|
-
"@types/jest": "^29.5.
|
|
48
|
+
"@types/jest": "^29.5.12",
|
|
49
49
|
"@types/node": "^20.8.10",
|
|
50
50
|
"@types/uuid": "^9.0.8",
|
|
51
51
|
"cross-env": "^7.0.3",
|
|
52
|
+
"envix": "^1.5.0",
|
|
52
53
|
"eslint": "^8.57.0",
|
|
53
54
|
"husky": "^8.0.3",
|
|
54
55
|
"jest": "^29.7.0",
|
|
55
56
|
"semantic-release": "^22.0.7",
|
|
57
|
+
"testcontainers": "^10.7.2",
|
|
56
58
|
"ts-jest": "^29.1.2",
|
|
57
59
|
"typescript": "^5.2.2"
|
|
58
60
|
}
|