amqp-extension 3.1.1 → 3.3.0
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 +19 -0
- package/dist/consume/type.d.ts +3 -2
- package/dist/consume/utils.js +0 -1
- package/dist/module.d.ts +2 -13
- package/dist/module.js +70 -94
- package/dist/publish/utils.js +0 -1
- package/dist/type.d.ts +2 -5
- package/package.json +4 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,22 @@
|
|
|
1
|
+
# [3.3.0](https://github.com/Tada5hi/amqp-extension/compare/v3.2.0...v3.3.0) (2024-03-20)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* use amqp-connection-manager for connection/reconnect buisness ([#331](https://github.com/Tada5hi/amqp-extension/issues/331)) ([aa6a21e](https://github.com/Tada5hi/amqp-extension/commit/aa6a21e391ad2c07aa645ec2241e47b177e825e9))
|
|
7
|
+
|
|
8
|
+
# [3.2.0](https://github.com/Tada5hi/amqp-extension/compare/v3.1.1...v3.2.0) (2024-03-11)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
### Bug Fixes
|
|
12
|
+
|
|
13
|
+
* remove unnecessary alias option ([8027d0a](https://github.com/Tada5hi/amqp-extension/commit/8027d0afb2063e5d0387dacf26cd77444dc933e4))
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
### Features
|
|
17
|
+
|
|
18
|
+
* allways close channel after publishing ([a9b300f](https://github.com/Tada5hi/amqp-extension/commit/a9b300f6ab64d176a656cf9fc485f31a36168a7f))
|
|
19
|
+
|
|
1
20
|
## [3.1.1](https://github.com/Tada5hi/amqp-extension/compare/v3.1.0...v3.1.1) (2024-03-07)
|
|
2
21
|
|
|
3
22
|
|
package/dist/consume/type.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ChannelWrapper } from 'amqp-connection-manager';
|
|
2
|
+
import type { ConsumeMessage } from 'amqplib';
|
|
2
3
|
import type { ConsumeHandlerAnyKey } from './static';
|
|
3
4
|
export { ConsumeMessage, };
|
|
4
|
-
export type ConsumeMessageHandler = (message: ConsumeMessage, channel:
|
|
5
|
+
export type ConsumeMessageHandler = (message: ConsumeMessage, channel: ChannelWrapper) => Promise<void> | void;
|
|
5
6
|
export type ConsumeHandlerAnyKeyType = typeof ConsumeHandlerAnyKey;
|
|
6
7
|
export type ConsumeHandlers = {
|
|
7
8
|
[ConsumeHandlerAnyKey]?: ConsumeMessageHandler;
|
package/dist/consume/utils.js
CHANGED
package/dist/module.d.ts
CHANGED
|
@@ -1,23 +1,12 @@
|
|
|
1
|
-
import type { Channel, Connection } from 'amqplib';
|
|
2
1
|
import type { Config, ConfigInput } from './config';
|
|
3
2
|
import type { ConsumeHandlers } from './consume';
|
|
4
3
|
import type { PublishOptionsExtended } from './publish';
|
|
5
|
-
import type { ConsumeOptions } from './type';
|
|
6
|
-
type Consumer = {
|
|
7
|
-
options: ConsumeOptions;
|
|
8
|
-
handlers: ConsumeHandlers;
|
|
9
|
-
};
|
|
4
|
+
import type { Connection, ConsumeOptions } from './type';
|
|
10
5
|
export declare class Client {
|
|
11
6
|
protected connection: Connection | undefined;
|
|
12
7
|
protected config: Config;
|
|
13
|
-
protected reconnectAttempts: number;
|
|
14
|
-
protected consumers: Consumer[];
|
|
15
8
|
constructor(options: ConfigInput);
|
|
16
|
-
|
|
17
|
-
useConnection(): Promise<Connection>;
|
|
18
|
-
protected recreateConsumers(): Promise<void>;
|
|
19
|
-
protected createConsumer(options: ConsumeOptions, handlers: ConsumeHandlers): Promise<Channel>;
|
|
9
|
+
useConnection(): Connection;
|
|
20
10
|
consume(options: ConsumeOptions, handlers: ConsumeHandlers): Promise<void>;
|
|
21
11
|
publish(options: PublishOptionsExtended): Promise<boolean>;
|
|
22
12
|
}
|
|
23
|
-
export {};
|
package/dist/module.js
CHANGED
|
@@ -10,105 +10,45 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
10
10
|
};
|
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
12
|
exports.Client = void 0;
|
|
13
|
+
const amqp_connection_manager_1 = require("amqp-connection-manager");
|
|
13
14
|
const node_process_1 = __importDefault(require("node:process"));
|
|
14
|
-
const amqplib_1 = require("amqplib");
|
|
15
15
|
const smob_1 = require("smob");
|
|
16
16
|
const uuid_1 = require("uuid");
|
|
17
17
|
const config_1 = require("./config");
|
|
18
18
|
const consume_1 = require("./consume");
|
|
19
19
|
const exchange_1 = require("./exchange");
|
|
20
20
|
const publish_1 = require("./publish");
|
|
21
|
-
const utils_1 = require("./utils");
|
|
22
21
|
class Client {
|
|
23
22
|
constructor(options) {
|
|
24
23
|
this.config = (0, config_1.buildConfig)(options);
|
|
25
|
-
this.reconnectAttempts = 0;
|
|
26
|
-
this.consumers = [];
|
|
27
24
|
node_process_1.default.once('SIGINT', async () => {
|
|
28
25
|
if (this.connection) {
|
|
29
26
|
await this.connection.close();
|
|
30
27
|
}
|
|
31
28
|
});
|
|
32
29
|
}
|
|
33
|
-
|
|
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;
|
|
48
|
-
}
|
|
49
|
-
async useConnection() {
|
|
30
|
+
useConnection() {
|
|
50
31
|
if (typeof this.connection !== 'undefined') {
|
|
51
32
|
return this.connection;
|
|
52
33
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
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;
|
|
34
|
+
this.connection = (0, amqp_connection_manager_1.connect)(this.config.connection, {
|
|
35
|
+
reconnectTimeInSeconds: this.config.reconnectTimeout,
|
|
36
|
+
});
|
|
63
37
|
return this.connection;
|
|
64
38
|
}
|
|
65
|
-
async
|
|
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) {
|
|
71
|
-
const connection = await this.useConnection();
|
|
72
|
-
const channel = await connection.createChannel();
|
|
39
|
+
async consume(options, handlers) {
|
|
73
40
|
options = (0, smob_1.merge)(options, this.config.consume);
|
|
74
41
|
const exchangeOptions = (0, smob_1.merge)(options.exchange || {}, this.config.exchange);
|
|
75
42
|
let queueName = options.queueName || this.config.publish.queueName || '';
|
|
76
|
-
|
|
77
|
-
if (queueName === '') {
|
|
78
|
-
throw new Error('The queue name can not be empty if a non default exchange is selected.');
|
|
79
|
-
}
|
|
80
|
-
await channel.assertQueue(queueName, {
|
|
81
|
-
durable: true,
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
await channel.assertExchange(exchangeOptions.name, exchangeOptions.type, (0, exchange_1.buildDriverExchangeOptions)({
|
|
86
|
-
durable: true,
|
|
87
|
-
...exchangeOptions,
|
|
88
|
-
}));
|
|
89
|
-
const assertionQueue = await channel.assertQueue('', {
|
|
90
|
-
durable: false,
|
|
91
|
-
autoDelete: true,
|
|
92
|
-
exclusive: true,
|
|
93
|
-
});
|
|
94
|
-
if (typeof exchangeOptions.routingKey === 'undefined') {
|
|
95
|
-
throw new Error('The routingKey can not be empty if a non default exchange is selected.');
|
|
96
|
-
}
|
|
97
|
-
await channel.bindQueue(assertionQueue.queue, this.config.exchange.name, exchangeOptions.routingKey);
|
|
98
|
-
queueName = assertionQueue.queue;
|
|
99
|
-
}
|
|
100
|
-
if (typeof options.prefetchCount !== 'undefined') {
|
|
101
|
-
await channel.prefetch(options.prefetchCount);
|
|
102
|
-
}
|
|
43
|
+
const requeueOnFailure = this.config.consume.requeueOnFailure ?? false;
|
|
103
44
|
const handleMessage = async (message) => {
|
|
104
45
|
if (!message) {
|
|
105
46
|
return;
|
|
106
47
|
}
|
|
107
48
|
const handler = handlers[message.properties.type] ||
|
|
108
49
|
handlers[consume_1.ConsumeHandlerAnyKey];
|
|
109
|
-
const requeueOnFailure = this.config.consume.requeueOnFailure ?? false;
|
|
110
50
|
if (typeof handler === 'undefined') {
|
|
111
|
-
channel.
|
|
51
|
+
channel.nack(message, undefined, requeueOnFailure);
|
|
112
52
|
return;
|
|
113
53
|
}
|
|
114
54
|
try {
|
|
@@ -116,18 +56,43 @@ class Client {
|
|
|
116
56
|
channel.ack(message);
|
|
117
57
|
}
|
|
118
58
|
catch (e) {
|
|
119
|
-
channel.
|
|
59
|
+
channel.nack(message);
|
|
120
60
|
}
|
|
121
61
|
};
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
62
|
+
const connection = this.useConnection();
|
|
63
|
+
const channel = connection.createChannel({
|
|
64
|
+
setup: async (channel) => {
|
|
65
|
+
if ((0, exchange_1.isDefaultExchange)(exchangeOptions.type)) {
|
|
66
|
+
if (queueName === '') {
|
|
67
|
+
throw new Error('The queue name can not be empty if a non default exchange is selected.');
|
|
68
|
+
}
|
|
69
|
+
await channel.assertQueue(queueName, {
|
|
70
|
+
durable: true,
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
await channel.assertExchange(exchangeOptions.name, exchangeOptions.type, (0, exchange_1.buildDriverExchangeOptions)({
|
|
75
|
+
durable: true,
|
|
76
|
+
...exchangeOptions,
|
|
77
|
+
}));
|
|
78
|
+
const assertionQueue = await channel.assertQueue('', {
|
|
79
|
+
durable: false,
|
|
80
|
+
autoDelete: true,
|
|
81
|
+
exclusive: true,
|
|
82
|
+
});
|
|
83
|
+
if (typeof exchangeOptions.routingKey === 'undefined') {
|
|
84
|
+
throw new Error('The routingKey can not be empty if a non default exchange is selected.');
|
|
85
|
+
}
|
|
86
|
+
await channel.bindQueue(assertionQueue.queue, this.config.exchange.name, exchangeOptions.routingKey);
|
|
87
|
+
queueName = assertionQueue.queue;
|
|
88
|
+
}
|
|
89
|
+
if (typeof options.prefetchCount !== 'undefined') {
|
|
90
|
+
await channel.prefetch(options.prefetchCount);
|
|
91
|
+
}
|
|
92
|
+
await channel.consume(queueName, (message) => handleMessage(message), (0, consume_1.buildDriverConsumeOptions)(options));
|
|
93
|
+
},
|
|
130
94
|
});
|
|
95
|
+
return channel.waitForConnect();
|
|
131
96
|
}
|
|
132
97
|
async publish(options) {
|
|
133
98
|
let buffer;
|
|
@@ -140,41 +105,52 @@ class Client {
|
|
|
140
105
|
else {
|
|
141
106
|
buffer = Buffer.from(JSON.stringify(options.content), 'utf-8');
|
|
142
107
|
}
|
|
143
|
-
const connection = await this.useConnection();
|
|
144
|
-
const channel = await connection.createChannel();
|
|
145
108
|
options = (0, smob_1.merge)(options, this.config.publish || {});
|
|
146
|
-
const exchangeOptions = (0, smob_1.merge)({}, options.exchange || {}, this.config.exchange);
|
|
147
|
-
if (!(0, exchange_1.isDefaultExchange)(exchangeOptions.type)) {
|
|
148
|
-
await channel.assertExchange(exchangeOptions.name, exchangeOptions.type, (0, exchange_1.buildDriverExchangeOptions)({
|
|
149
|
-
durable: true,
|
|
150
|
-
...exchangeOptions,
|
|
151
|
-
}));
|
|
152
|
-
}
|
|
153
109
|
if (typeof options.messageId === 'undefined') {
|
|
154
110
|
options.messageId = options.id || (0, uuid_1.v4)();
|
|
155
111
|
}
|
|
112
|
+
const exchangeOptions = (0, smob_1.merge)({}, options.exchange || {}, this.config.exchange);
|
|
113
|
+
// publish to default exchange
|
|
114
|
+
const queueName = options.queueName ||
|
|
115
|
+
this.config.publish.queueName;
|
|
116
|
+
const connection = this.useConnection();
|
|
117
|
+
const channel = connection.createChannel({
|
|
118
|
+
setup: async (channel) => {
|
|
119
|
+
if (!(0, exchange_1.isDefaultExchange)(exchangeOptions.type)) {
|
|
120
|
+
await channel.assertExchange(exchangeOptions.name, exchangeOptions.type, (0, exchange_1.buildDriverExchangeOptions)({
|
|
121
|
+
durable: true,
|
|
122
|
+
...exchangeOptions,
|
|
123
|
+
}));
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
if (typeof queueName === 'undefined' || queueName === '') {
|
|
127
|
+
throw new Error('The queue name can not be empty if a non default exchange is selected.');
|
|
128
|
+
}
|
|
129
|
+
await channel.assertQueue(queueName, {
|
|
130
|
+
durable: true,
|
|
131
|
+
});
|
|
132
|
+
},
|
|
133
|
+
});
|
|
156
134
|
if (!(0, exchange_1.isDefaultExchange)(exchangeOptions.type)) {
|
|
157
135
|
if (typeof exchangeOptions.routingKey === 'undefined') {
|
|
158
136
|
throw new Error('The routingKey can not be empty if a non default exchange is selected.');
|
|
159
137
|
}
|
|
160
|
-
|
|
138
|
+
const published = await channel.publish(this.config.exchange.name, exchangeOptions.routingKey, buffer, (0, publish_1.buildDriverPublishOptions)({
|
|
161
139
|
persistent: true,
|
|
162
140
|
...options,
|
|
163
141
|
}));
|
|
142
|
+
await channel.close();
|
|
143
|
+
return published;
|
|
164
144
|
}
|
|
165
|
-
// publish to default exchange
|
|
166
|
-
const queueName = options.queueName ||
|
|
167
|
-
this.config.publish.queueName;
|
|
168
145
|
if (typeof queueName === 'undefined' || queueName === '') {
|
|
169
146
|
throw new Error('The queue name can not be empty if a non default exchange is selected.');
|
|
170
147
|
}
|
|
171
|
-
await channel.
|
|
172
|
-
durable: true,
|
|
173
|
-
});
|
|
174
|
-
return channel.sendToQueue(queueName, buffer, (0, publish_1.buildDriverPublishOptions)({
|
|
148
|
+
const published = await channel.sendToQueue(queueName, buffer, (0, publish_1.buildDriverPublishOptions)({
|
|
175
149
|
persistent: true,
|
|
176
150
|
...options,
|
|
177
151
|
}));
|
|
152
|
+
await channel.close();
|
|
153
|
+
return published;
|
|
178
154
|
}
|
|
179
155
|
}
|
|
180
156
|
exports.Client = Client;
|
package/dist/publish/utils.js
CHANGED
package/dist/type.d.ts
CHANGED
|
@@ -1,11 +1,8 @@
|
|
|
1
|
+
import type { connect } from 'amqp-connection-manager';
|
|
1
2
|
import type { Options } from 'amqplib';
|
|
2
|
-
import type { ConfigInput } from './config';
|
|
3
3
|
import type { ExchangeOptions } from './exchange';
|
|
4
|
+
export type Connection = ReturnType<typeof connect>;
|
|
4
5
|
type CommonOptions = {
|
|
5
|
-
/**
|
|
6
|
-
* Config key or object.
|
|
7
|
-
*/
|
|
8
|
-
alias?: string | ConfigInput;
|
|
9
6
|
/**
|
|
10
7
|
* Exchange
|
|
11
8
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "amqp-extension",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.0",
|
|
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",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"homepage": "https://github.com/Tada5hi/amqp-extension#readme",
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@types/amqplib": "^0.10.5",
|
|
39
|
+
"amqp-connection-manager": "^4.1.14",
|
|
39
40
|
"amqplib": "^0.10.3",
|
|
40
41
|
"smob": "^1.4.1",
|
|
41
42
|
"uuid": "^9.0.1"
|
|
@@ -46,7 +47,7 @@
|
|
|
46
47
|
"@tada5hi/semantic-release": "^0.3.1",
|
|
47
48
|
"@tada5hi/tsconfig": "^0.5.0",
|
|
48
49
|
"@types/jest": "^29.5.12",
|
|
49
|
-
"@types/node": "^20.
|
|
50
|
+
"@types/node": "^20.11.30",
|
|
50
51
|
"@types/uuid": "^9.0.8",
|
|
51
52
|
"cross-env": "^7.0.3",
|
|
52
53
|
"envix": "^1.5.0",
|
|
@@ -56,6 +57,6 @@
|
|
|
56
57
|
"semantic-release": "^22.0.7",
|
|
57
58
|
"testcontainers": "^10.7.2",
|
|
58
59
|
"ts-jest": "^29.1.2",
|
|
59
|
-
"typescript": "^5.
|
|
60
|
+
"typescript": "^5.4.2"
|
|
60
61
|
}
|
|
61
62
|
}
|