amqp-extension 2.0.2 → 3.1.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 +25 -0
- package/README.MD +21 -219
- package/dist/config/index.d.ts +1 -1
- package/dist/config/index.js +1 -1
- package/dist/config/module.d.ts +1 -3
- package/dist/config/module.js +16 -36
- package/dist/config/type.d.ts +2 -1
- package/dist/consume/index.d.ts +1 -1
- package/dist/consume/index.js +1 -1
- package/dist/consume/type.d.ts +6 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/module.d.ts +22 -0
- package/dist/module.js +176 -0
- package/dist/publish/index.d.ts +1 -1
- package/dist/publish/index.js +1 -1
- package/dist/utils.d.ts +1 -0
- package/dist/utils.js +9 -1
- package/package.json +10 -8
- package/tsconfig.eslint.json +1 -0
- package/dist/config/utils.d.ts +0 -3
- package/dist/config/utils.js +0 -27
- package/dist/connection/index.d.ts +0 -1
- package/dist/connection/index.js +0 -23
- package/dist/connection/module.d.ts +0 -7
- package/dist/connection/module.js +0 -48
- package/dist/consume/module.d.ts +0 -3
- package/dist/consume/module.js +0 -72
- package/dist/publish/module.d.ts +0 -2
- package/dist/publish/module.js +0 -67
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,28 @@
|
|
|
1
|
+
# [3.1.0](https://github.com/Tada5hi/amqp-extension/compare/v3.0.0...v3.1.0) (2024-03-06)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Features
|
|
5
|
+
|
|
6
|
+
* reconnect strategy ([#320](https://github.com/Tada5hi/amqp-extension/issues/320)) ([4c31551](https://github.com/Tada5hi/amqp-extension/commit/4c31551029916dcaf055f78d53a8af7c626c393d))
|
|
7
|
+
* reconnect strategy ([#320](https://github.com/Tada5hi/amqp-extension/issues/320)) ([17b141f](https://github.com/Tada5hi/amqp-extension/commit/17b141f6bd6256172e8c03a1006bfc1cab6225eb))
|
|
8
|
+
|
|
9
|
+
# [3.0.0](https://github.com/Tada5hi/amqp-extension/compare/v2.0.2...v3.0.0) (2024-02-28)
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
### Bug Fixes
|
|
13
|
+
|
|
14
|
+
* **deps:** bump @types/amqplib from 0.10.4 to 0.10.5 ([#310](https://github.com/Tada5hi/amqp-extension/issues/310)) ([984294e](https://github.com/Tada5hi/amqp-extension/commit/984294e7ac9dd04853ebd75e70b09841340b3239))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### Features
|
|
18
|
+
|
|
19
|
+
* bind consume & publish to client class & remove singleton patterns ([9160408](https://github.com/Tada5hi/amqp-extension/commit/9160408038b1d48210735c843674817bb0ac45c6))
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
### BREAKING CHANGES
|
|
23
|
+
|
|
24
|
+
* refactor public api
|
|
25
|
+
|
|
1
26
|
## [2.0.2](https://github.com/Tada5hi/amqp-extension/compare/v2.0.1...v2.0.2) (2024-02-28)
|
|
2
27
|
|
|
3
28
|
|
package/README.MD
CHANGED
|
@@ -2,12 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://badge.fury.io/js/amqp-extension)
|
|
4
4
|
[](https://codecov.io/gh/Tada5hi/amqp-extension)
|
|
5
|
-
[](https://github.com/Tada5hi/amqp-extension)
|
|
6
6
|
[](https://snyk.io/test/github/Tada5hi/amqp-extension?targetFile=package.json)
|
|
7
7
|
[](https://github.com/semantic-release/semantic-release)
|
|
8
8
|
|
|
9
|
-
This is a library on top of the
|
|
10
|
-
All utility functions support the usage of multiple registered connections.
|
|
9
|
+
This is a library on top of the [amqplib](https://www.npmjs.com/package/amqplib) library and is meant to simplify the process of consuming & publishing queue messages.
|
|
11
10
|
|
|
12
11
|
**Table of Contents**
|
|
13
12
|
|
|
@@ -16,8 +15,6 @@ All utility functions support the usage of multiple registered connections.
|
|
|
16
15
|
- [Publish](#publish-to-queue)
|
|
17
16
|
- [Consume](#consume-queue)
|
|
18
17
|
- [Functions](#functions)
|
|
19
|
-
- [setConfig](#setconfig)
|
|
20
|
-
- [useConnection](#useconnection)
|
|
21
18
|
- [publish](#publish)
|
|
22
19
|
- [consume](#consume)
|
|
23
20
|
- [Types](#types)
|
|
@@ -35,18 +32,13 @@ npm install amqp-extension --save
|
|
|
35
32
|
|
|
36
33
|
### Publish to Queue
|
|
37
34
|
|
|
38
|
-
The `publish`
|
|
35
|
+
The `publish` method allows you to send messages quickly.
|
|
39
36
|
Existing options can be added or overwritten
|
|
40
37
|
|
|
41
38
|
```typescript
|
|
42
|
-
import {
|
|
43
|
-
useConnection,
|
|
44
|
-
publish,
|
|
45
|
-
setConfig
|
|
46
|
-
} from "amqp-extension";
|
|
39
|
+
import { Client } from "amqp-extension";
|
|
47
40
|
|
|
48
|
-
|
|
49
|
-
setConfig({
|
|
41
|
+
const client = new Client({
|
|
50
42
|
connection: 'amqp://<user>:<password>@<host>',
|
|
51
43
|
exchange: {
|
|
52
44
|
name: '<name>',
|
|
@@ -55,7 +47,7 @@ setConfig({
|
|
|
55
47
|
});
|
|
56
48
|
|
|
57
49
|
(async () => {
|
|
58
|
-
await publish({
|
|
50
|
+
await client.publish({
|
|
59
51
|
content: {
|
|
60
52
|
type: 'resourceCreated',
|
|
61
53
|
name: 'foo'
|
|
@@ -71,14 +63,12 @@ and as second argument and object to specify an async callback function handler
|
|
|
71
63
|
|
|
72
64
|
```typescript
|
|
73
65
|
import {
|
|
74
|
-
|
|
66
|
+
Client,
|
|
75
67
|
ConsumeMessage,
|
|
76
68
|
ConsumeOptions,
|
|
77
|
-
setConfig
|
|
78
69
|
} from "amqp-extension";
|
|
79
70
|
|
|
80
|
-
|
|
81
|
-
setConfig({
|
|
71
|
+
const client = new Client({
|
|
82
72
|
connection: 'amqp://<user>:<password>@<host>',
|
|
83
73
|
exchange: {
|
|
84
74
|
name: '<name>',
|
|
@@ -93,7 +83,7 @@ const options: ConsumeOptions = {
|
|
|
93
83
|
}
|
|
94
84
|
|
|
95
85
|
(async () => {
|
|
96
|
-
await consume(options, {
|
|
86
|
+
await client.consume(options, {
|
|
97
87
|
resourceCreated: async (message: ConsumeMessage) => {
|
|
98
88
|
const content = message.content.toString('utf-8');
|
|
99
89
|
const payload = JSON.parse(content);
|
|
@@ -104,201 +94,8 @@ const options: ConsumeOptions = {
|
|
|
104
94
|
})();
|
|
105
95
|
```
|
|
106
96
|
|
|
107
|
-
### Multiple Connections
|
|
108
|
-
|
|
109
|
-
To define multiple concurrent connections just specify a different `alias` in the configuration object, so it does not
|
|
110
|
-
get overwritten.
|
|
111
|
-
|
|
112
|
-
```typescript
|
|
113
|
-
import {
|
|
114
|
-
ConsumeOptions,
|
|
115
|
-
publish,
|
|
116
|
-
PublishOptionsExtended,
|
|
117
|
-
setConfig
|
|
118
|
-
} from "amqp-extension";
|
|
119
|
-
|
|
120
|
-
// This will set the default connection :)
|
|
121
|
-
setConfig({
|
|
122
|
-
connection: 'amqp://<user>:<password>@<host>',
|
|
123
|
-
exchange: {
|
|
124
|
-
name: '<name>',
|
|
125
|
-
type: 'topic'
|
|
126
|
-
}
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
setConfig({
|
|
130
|
-
alias: 'foo',
|
|
131
|
-
connection: 'amqp://<user>:<password>@<host>',
|
|
132
|
-
exchange: {
|
|
133
|
-
name: '<name>',
|
|
134
|
-
type: 'topic'
|
|
135
|
-
}
|
|
136
|
-
});
|
|
137
|
-
(async () => {
|
|
138
|
-
await consume(
|
|
139
|
-
{
|
|
140
|
-
routingKey: '<routing-key>',
|
|
141
|
-
alias: 'foo' // <--- use another connection :)
|
|
142
|
-
},
|
|
143
|
-
{
|
|
144
|
-
// ... handlers
|
|
145
|
-
}
|
|
146
|
-
);
|
|
147
|
-
|
|
148
|
-
await publish({
|
|
149
|
-
routingKey: '<routing-key>',
|
|
150
|
-
alias: 'foo', // <--- use another connection :)
|
|
151
|
-
content: {
|
|
152
|
-
foo: 'bar'
|
|
153
|
-
}
|
|
154
|
-
});
|
|
155
|
-
})();
|
|
156
|
-
|
|
157
|
-
```
|
|
158
|
-
|
|
159
97
|
## Functions
|
|
160
98
|
|
|
161
|
-
### setConfig
|
|
162
|
-
|
|
163
|
-
▸ `function` **setConfig**(`key?: string | ConfigInput`, `value?: ConfigInput`): `Config`
|
|
164
|
-
|
|
165
|
-
Register a connection as `default` alias or specify an `<alias>` as config property.
|
|
166
|
-
|
|
167
|
-
#### Example
|
|
168
|
-
**`Simple`**
|
|
169
|
-
|
|
170
|
-
Define the default connection config.
|
|
171
|
-
|
|
172
|
-
```typescript
|
|
173
|
-
import {setConfig, useConnection} from "amqp-extension";
|
|
174
|
-
|
|
175
|
-
(async () => {
|
|
176
|
-
setConfig({
|
|
177
|
-
connection: 'amqp://<user>:<password>@<host>',
|
|
178
|
-
exchange: {
|
|
179
|
-
name: '<name>',
|
|
180
|
-
type: 'topic'
|
|
181
|
-
}
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
const connection = await useConnection();
|
|
185
|
-
})();
|
|
186
|
-
|
|
187
|
-
```
|
|
188
|
-
|
|
189
|
-
Define a non default connection.
|
|
190
|
-
|
|
191
|
-
```typescript
|
|
192
|
-
import { setConfig, useConnection } from "amqp-extension";
|
|
193
|
-
|
|
194
|
-
(async () => {
|
|
195
|
-
setConfig({
|
|
196
|
-
alias: '<alias>',
|
|
197
|
-
connection: 'amqp://<user>:<password>@<host>',
|
|
198
|
-
exchange: {
|
|
199
|
-
name: '<name>',
|
|
200
|
-
type: 'topic'
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
setConfig('foo', {
|
|
205
|
-
connection: 'amqp://<user>:<password>@<host>',
|
|
206
|
-
exchange: {
|
|
207
|
-
name: '<name>',
|
|
208
|
-
type: 'topic'
|
|
209
|
-
}
|
|
210
|
-
});
|
|
211
|
-
|
|
212
|
-
const connection = await useConnection('<alias>');
|
|
213
|
-
const fooConnection = await useConnection('foo');
|
|
214
|
-
})();
|
|
215
|
-
|
|
216
|
-
```
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
#### Type parameters
|
|
220
|
-
|
|
221
|
-
| Name | Description |
|
|
222
|
-
|:------|:------------|
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
#### Parameters
|
|
227
|
-
|
|
228
|
-
| Name | Type | Description |
|
|
229
|
-
|:--------|:--------------------------|:-----------------------------------------------------------|
|
|
230
|
-
| `key` | `string` or `ConfigInput` | Config object or alias of config. [more](#config-types) |
|
|
231
|
-
| `value` | `Config` | Config object. [more](#config-types) |
|
|
232
|
-
|
|
233
|
-
#### Returns
|
|
234
|
-
|
|
235
|
-
`Config`
|
|
236
|
-
|
|
237
|
-
The function returns the Config object.
|
|
238
|
-
|
|
239
|
-
### useConnection
|
|
240
|
-
|
|
241
|
-
▸ `function` **useConnection**(`key?: string | ConfigInput`): `Promise<Connection>`
|
|
242
|
-
|
|
243
|
-
Either register a connection as `default` alias or specify an `alias` as config property.
|
|
244
|
-
If you have registered a connection you can receive the connection by specifying no arguments or provide an alias name if specified.
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
#### Example
|
|
248
|
-
**`Simple`**
|
|
249
|
-
|
|
250
|
-
Receive underlying driver connection.
|
|
251
|
-
|
|
252
|
-
```typescript
|
|
253
|
-
import {useConnection} from "amqp-extension";
|
|
254
|
-
|
|
255
|
-
(async () => {
|
|
256
|
-
const connection = await useConnection();
|
|
257
|
-
})();
|
|
258
|
-
|
|
259
|
-
```
|
|
260
|
-
|
|
261
|
-
**`Advanced`**
|
|
262
|
-
|
|
263
|
-
Use a none `default` connection.
|
|
264
|
-
|
|
265
|
-
```typescript
|
|
266
|
-
import {setConfig, useConnection} from "amqp-extension";
|
|
267
|
-
|
|
268
|
-
(async () => {
|
|
269
|
-
setConfig({
|
|
270
|
-
alias: '<alias>',
|
|
271
|
-
connection: 'amqp://<user>:<password>@<host>',
|
|
272
|
-
exchange: {
|
|
273
|
-
name: '<name>',
|
|
274
|
-
type: 'topic'
|
|
275
|
-
}
|
|
276
|
-
});
|
|
277
|
-
|
|
278
|
-
const connection = await useConnection('<alias>');
|
|
279
|
-
})();
|
|
280
|
-
|
|
281
|
-
```
|
|
282
|
-
|
|
283
|
-
#### Type parameters
|
|
284
|
-
|
|
285
|
-
| Name | Description |
|
|
286
|
-
|:------|:--------------|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
#### Parameters
|
|
291
|
-
|
|
292
|
-
| Name | Type | Description |
|
|
293
|
-
|:------|:---------------------|:-------------------------------------------------|
|
|
294
|
-
| `key` | `string` or `Config` | Config or alias of config. [more](#config-types) |
|
|
295
|
-
|
|
296
|
-
#### Returns
|
|
297
|
-
|
|
298
|
-
`Promise<Connection>`
|
|
299
|
-
|
|
300
|
-
The function returns the Connection object of the `amqplib`.
|
|
301
|
-
|
|
302
99
|
### publish
|
|
303
100
|
|
|
304
101
|
▸ `function` **publish**(`message: Message`, `options?: PublishOptions`): `Promise<void>`
|
|
@@ -309,12 +106,14 @@ As second parameter a registered config can be used by specifying the alias or p
|
|
|
309
106
|
**`Simple`**
|
|
310
107
|
|
|
311
108
|
```typescript
|
|
312
|
-
import {
|
|
313
|
-
|
|
314
|
-
|
|
109
|
+
import { Client } from "amqp-extension";
|
|
110
|
+
|
|
111
|
+
const client = new Client({
|
|
112
|
+
// ...
|
|
113
|
+
});
|
|
315
114
|
|
|
316
115
|
(async () => {
|
|
317
|
-
await publish({
|
|
116
|
+
await client.publish({
|
|
318
117
|
content: {
|
|
319
118
|
type: 'resourceCreated'
|
|
320
119
|
}
|
|
@@ -351,17 +150,21 @@ As second parameter a registered config can be used by specifying the alias or p
|
|
|
351
150
|
|
|
352
151
|
```typescript
|
|
353
152
|
import {
|
|
354
|
-
|
|
153
|
+
Client,
|
|
355
154
|
ConsumeOptions,
|
|
356
155
|
ConsumeMessage,
|
|
357
156
|
} from "amqp-extension";
|
|
358
157
|
|
|
158
|
+
const client = new Client({
|
|
159
|
+
// ...
|
|
160
|
+
});
|
|
161
|
+
|
|
359
162
|
const options: ConsumeOptions = {
|
|
360
163
|
routingKey: '<routing-key>'
|
|
361
164
|
}
|
|
362
165
|
|
|
363
166
|
(async () => {
|
|
364
|
-
await consume(options, {
|
|
167
|
+
await client.consume(options, {
|
|
365
168
|
'<type>': async (message: ConsumeMessage) => {
|
|
366
169
|
// do some async action :)
|
|
367
170
|
}
|
|
@@ -400,7 +203,6 @@ import { ExchangeOptions } from '../exchange';
|
|
|
400
203
|
import { ConsumeOptions, PublishOptions } from '../type';
|
|
401
204
|
|
|
402
205
|
export type Config = {
|
|
403
|
-
alias: string,
|
|
404
206
|
connection: Options.Connect | string,
|
|
405
207
|
exchange: ExchangeOptions,
|
|
406
208
|
publish: PublishOptions,
|
package/dist/config/index.d.ts
CHANGED
package/dist/config/index.js
CHANGED
|
@@ -22,4 +22,4 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
23
|
__exportStar(require("./type"), exports);
|
|
24
24
|
__exportStar(require("./module"), exports);
|
|
25
|
-
__exportStar(require("./
|
|
25
|
+
__exportStar(require("./module"), exports);
|
package/dist/config/module.d.ts
CHANGED
|
@@ -1,4 +1,2 @@
|
|
|
1
1
|
import type { Config, ConfigInput } from './type';
|
|
2
|
-
export declare function
|
|
3
|
-
export declare function hasConfig(key?: string): boolean;
|
|
4
|
-
export declare function getConfig(key?: string | ConfigInput): Config;
|
|
2
|
+
export declare function buildConfig(input: ConfigInput): Config;
|
package/dist/config/module.js
CHANGED
|
@@ -1,44 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/*
|
|
3
|
-
* Copyright (c) 2022.
|
|
3
|
+
* Copyright (c) 2022-2022.
|
|
4
4
|
* Author Peter Placzek (tada5hi)
|
|
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
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return instances[key.alias];
|
|
9
|
+
exports.buildConfig = void 0;
|
|
10
|
+
const exchange_1 = require("../exchange");
|
|
11
|
+
function buildConfig(input) {
|
|
12
|
+
return {
|
|
13
|
+
reconnectAttempts: 10,
|
|
14
|
+
reconnectTimeout: 1000,
|
|
15
|
+
connection: input.connection,
|
|
16
|
+
publish: input.publish || {},
|
|
17
|
+
consume: input.consume || {},
|
|
18
|
+
exchange: input.exchange || {
|
|
19
|
+
name: '',
|
|
20
|
+
type: exchange_1.ExchangeType.DEFAULT,
|
|
21
|
+
},
|
|
22
|
+
};
|
|
24
23
|
}
|
|
25
|
-
exports.
|
|
26
|
-
function hasConfig(key) {
|
|
27
|
-
key = (0, utils_1.getConfigKey)(key);
|
|
28
|
-
return Object.prototype.hasOwnProperty.call(instances, key);
|
|
29
|
-
}
|
|
30
|
-
exports.hasConfig = hasConfig;
|
|
31
|
-
function getConfig(key) {
|
|
32
|
-
if (typeof key === 'string' || typeof key === 'undefined') {
|
|
33
|
-
key = (0, utils_1.getConfigKey)(key);
|
|
34
|
-
const data = instances[key];
|
|
35
|
-
if (typeof data === 'undefined') {
|
|
36
|
-
throw new Error(`A config must be defined for the alias: ${key}`);
|
|
37
|
-
}
|
|
38
|
-
return data;
|
|
39
|
-
}
|
|
40
|
-
const config = (0, utils_1.extendConfig)(key);
|
|
41
|
-
setConfig(config);
|
|
42
|
-
return config;
|
|
43
|
-
}
|
|
44
|
-
exports.getConfig = getConfig;
|
|
24
|
+
exports.buildConfig = buildConfig;
|
package/dist/config/type.d.ts
CHANGED
|
@@ -2,7 +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
|
-
|
|
5
|
+
reconnectAttempts: number;
|
|
6
|
+
reconnectTimeout: number;
|
|
6
7
|
connection: Options.Connect | string;
|
|
7
8
|
exchange: ExchangeOptions;
|
|
8
9
|
publish: PublishOptions;
|
package/dist/consume/index.d.ts
CHANGED
package/dist/consume/index.js
CHANGED
|
@@ -21,5 +21,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
23
|
__exportStar(require("./static"), exports);
|
|
24
|
-
__exportStar(require("./module"), exports);
|
|
25
24
|
__exportStar(require("./type"), exports);
|
|
25
|
+
__exportStar(require("./utils"), exports);
|
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/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -22,7 +22,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
23
|
__exportStar(require("./config"), exports);
|
|
24
24
|
__exportStar(require("./consume"), exports);
|
|
25
|
-
__exportStar(require("./connection"), exports);
|
|
26
25
|
__exportStar(require("./exchange"), exports);
|
|
26
|
+
__exportStar(require("./module"), exports);
|
|
27
27
|
__exportStar(require("./publish"), exports);
|
|
28
28
|
__exportStar(require("./type"), exports);
|
package/dist/module.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Connection } from 'amqplib';
|
|
2
|
+
import type { Config, ConfigInput } from './config';
|
|
3
|
+
import type { ConsumeHandlers } from './consume';
|
|
4
|
+
import type { PublishOptionsExtended } from './publish';
|
|
5
|
+
import type { ConsumeOptions } from './type';
|
|
6
|
+
type Consumer = {
|
|
7
|
+
options: ConsumeOptions;
|
|
8
|
+
handlers: ConsumeHandlers;
|
|
9
|
+
};
|
|
10
|
+
export declare class Client {
|
|
11
|
+
protected connection: Connection | undefined;
|
|
12
|
+
protected config: Config;
|
|
13
|
+
protected reconnectAttempts: number;
|
|
14
|
+
protected consumers: Consumer[];
|
|
15
|
+
constructor(options: ConfigInput);
|
|
16
|
+
protected createConnection(): Promise<Connection>;
|
|
17
|
+
useConnection(): Promise<Connection>;
|
|
18
|
+
protected recreateConsumers(): Promise<void>;
|
|
19
|
+
consume(options: ConsumeOptions, handlers: ConsumeHandlers): Promise<void>;
|
|
20
|
+
publish(options: PublishOptionsExtended): Promise<boolean>;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
package/dist/module.js
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/*
|
|
3
|
+
* Copyright (c) 2024.
|
|
4
|
+
* Author Peter Placzek (tada5hi)
|
|
5
|
+
* For the full copyright and license information,
|
|
6
|
+
* view the LICENSE file that was distributed with this source code.
|
|
7
|
+
*/
|
|
8
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.Client = void 0;
|
|
13
|
+
const node_process_1 = __importDefault(require("node:process"));
|
|
14
|
+
const amqplib_1 = require("amqplib");
|
|
15
|
+
const smob_1 = require("smob");
|
|
16
|
+
const uuid_1 = require("uuid");
|
|
17
|
+
const config_1 = require("./config");
|
|
18
|
+
const consume_1 = require("./consume");
|
|
19
|
+
const exchange_1 = require("./exchange");
|
|
20
|
+
const publish_1 = require("./publish");
|
|
21
|
+
const utils_1 = require("./utils");
|
|
22
|
+
class Client {
|
|
23
|
+
constructor(options) {
|
|
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;
|
|
48
|
+
}
|
|
49
|
+
async useConnection() {
|
|
50
|
+
if (typeof this.connection !== 'undefined') {
|
|
51
|
+
return this.connection;
|
|
52
|
+
}
|
|
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;
|
|
63
|
+
return this.connection;
|
|
64
|
+
}
|
|
65
|
+
async recreateConsumers() {
|
|
66
|
+
for (let i = 0; i < this.consumers.length; i++) {
|
|
67
|
+
await this.consume(this.consumers[i].options, this.consumers[i].handlers);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
async consume(options, handlers) {
|
|
71
|
+
const connection = await this.useConnection();
|
|
72
|
+
const channel = await connection.createChannel();
|
|
73
|
+
options = (0, smob_1.merge)(options, this.config.consume);
|
|
74
|
+
const exchangeOptions = (0, smob_1.merge)(options.exchange || {}, this.config.exchange);
|
|
75
|
+
let queueName = options.queueName || this.config.publish.queueName || '';
|
|
76
|
+
if ((0, exchange_1.isDefaultExchange)(exchangeOptions.type)) {
|
|
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
|
+
}
|
|
103
|
+
const handleMessage = async (message) => {
|
|
104
|
+
if (!message) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
const handler = handlers[message.properties.type] ||
|
|
108
|
+
handlers[consume_1.ConsumeHandlerAnyKey];
|
|
109
|
+
const requeueOnFailure = this.config.consume.requeueOnFailure ?? false;
|
|
110
|
+
if (typeof handler === 'undefined') {
|
|
111
|
+
channel.reject(message, requeueOnFailure);
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
try {
|
|
115
|
+
await handler(message, channel);
|
|
116
|
+
channel.ack(message);
|
|
117
|
+
}
|
|
118
|
+
catch (e) {
|
|
119
|
+
channel.reject(message, requeueOnFailure);
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
await channel.consume(queueName, (message) => handleMessage(message), (0, consume_1.buildDriverConsumeOptions)(options));
|
|
123
|
+
this.consumers.push({
|
|
124
|
+
options,
|
|
125
|
+
handlers,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
async publish(options) {
|
|
129
|
+
let buffer;
|
|
130
|
+
if (Buffer.isBuffer(options.content)) {
|
|
131
|
+
buffer = options.content;
|
|
132
|
+
}
|
|
133
|
+
else if (typeof options.content === 'string') {
|
|
134
|
+
buffer = Buffer.from(options.content, 'utf-8');
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
buffer = Buffer.from(JSON.stringify(options.content), 'utf-8');
|
|
138
|
+
}
|
|
139
|
+
const connection = await this.useConnection();
|
|
140
|
+
const channel = await connection.createChannel();
|
|
141
|
+
options = (0, smob_1.merge)(options, this.config.publish || {});
|
|
142
|
+
const exchangeOptions = (0, smob_1.merge)({}, options.exchange || {}, this.config.exchange);
|
|
143
|
+
if (!(0, exchange_1.isDefaultExchange)(exchangeOptions.type)) {
|
|
144
|
+
await channel.assertExchange(exchangeOptions.name, exchangeOptions.type, (0, exchange_1.buildDriverExchangeOptions)({
|
|
145
|
+
durable: true,
|
|
146
|
+
...exchangeOptions,
|
|
147
|
+
}));
|
|
148
|
+
}
|
|
149
|
+
if (typeof options.messageId === 'undefined') {
|
|
150
|
+
options.messageId = options.id || (0, uuid_1.v4)();
|
|
151
|
+
}
|
|
152
|
+
if (!(0, exchange_1.isDefaultExchange)(exchangeOptions.type)) {
|
|
153
|
+
if (typeof exchangeOptions.routingKey === 'undefined') {
|
|
154
|
+
throw new Error('The routingKey can not be empty if a non default exchange is selected.');
|
|
155
|
+
}
|
|
156
|
+
return channel.publish(this.config.exchange.name, exchangeOptions.routingKey, buffer, (0, publish_1.buildDriverPublishOptions)({
|
|
157
|
+
persistent: true,
|
|
158
|
+
...options,
|
|
159
|
+
}));
|
|
160
|
+
}
|
|
161
|
+
// publish to default exchange
|
|
162
|
+
const queueName = options.queueName ||
|
|
163
|
+
this.config.publish.queueName;
|
|
164
|
+
if (typeof queueName === 'undefined' || queueName === '') {
|
|
165
|
+
throw new Error('The queue name can not be empty if a non default exchange is selected.');
|
|
166
|
+
}
|
|
167
|
+
await channel.assertQueue(queueName, {
|
|
168
|
+
durable: true,
|
|
169
|
+
});
|
|
170
|
+
return channel.sendToQueue(queueName, buffer, (0, publish_1.buildDriverPublishOptions)({
|
|
171
|
+
persistent: true,
|
|
172
|
+
...options,
|
|
173
|
+
}));
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
exports.Client = Client;
|
package/dist/publish/index.d.ts
CHANGED
package/dist/publish/index.js
CHANGED
|
@@ -20,5 +20,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
20
20
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
__exportStar(require("./module"), exports);
|
|
24
23
|
__exportStar(require("./type"), exports);
|
|
24
|
+
__exportStar(require("./utils"), exports);
|
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
|
+
"version": "3.1.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",
|
|
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",
|
|
@@ -35,25 +35,27 @@
|
|
|
35
35
|
},
|
|
36
36
|
"homepage": "https://github.com/Tada5hi/amqp-extension#readme",
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@types/amqplib": "^0.10.
|
|
38
|
+
"@types/amqplib": "^0.10.5",
|
|
39
39
|
"amqplib": "^0.10.3",
|
|
40
40
|
"smob": "^1.4.1",
|
|
41
41
|
"uuid": "^9.0.1"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
|
-
"@tada5hi/commitlint-config": "^1.1.
|
|
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
|
-
"
|
|
52
|
+
"envix": "^1.5.0",
|
|
53
|
+
"eslint": "^8.57.0",
|
|
53
54
|
"husky": "^8.0.3",
|
|
54
55
|
"jest": "^29.7.0",
|
|
55
56
|
"semantic-release": "^22.0.7",
|
|
56
|
-
"
|
|
57
|
+
"testcontainers": "^10.7.2",
|
|
58
|
+
"ts-jest": "^29.1.2",
|
|
57
59
|
"typescript": "^5.2.2"
|
|
58
60
|
}
|
|
59
61
|
}
|
package/tsconfig.eslint.json
CHANGED
package/dist/config/utils.d.ts
DELETED
package/dist/config/utils.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright (c) 2022-2022.
|
|
4
|
-
* Author Peter Placzek (tada5hi)
|
|
5
|
-
* For the full copyright and license information,
|
|
6
|
-
* view the LICENSE file that was distributed with this source code.
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.extendConfig = exports.getConfigKey = void 0;
|
|
10
|
-
const exchange_1 = require("../exchange");
|
|
11
|
-
function getConfigKey(alias) {
|
|
12
|
-
return alias || 'default';
|
|
13
|
-
}
|
|
14
|
-
exports.getConfigKey = getConfigKey;
|
|
15
|
-
function extendConfig(input) {
|
|
16
|
-
return {
|
|
17
|
-
alias: getConfigKey(input.alias),
|
|
18
|
-
connection: input.connection,
|
|
19
|
-
publish: input.publish || {},
|
|
20
|
-
consume: input.consume || {},
|
|
21
|
-
exchange: input.exchange || {
|
|
22
|
-
name: '',
|
|
23
|
-
type: exchange_1.ExchangeType.DEFAULT,
|
|
24
|
-
},
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
exports.extendConfig = extendConfig;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './module';
|
package/dist/connection/index.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright (c) 2022.
|
|
4
|
-
* Author Peter Placzek (tada5hi)
|
|
5
|
-
* For the full copyright and license information,
|
|
6
|
-
* view the LICENSE file that was distributed with this source code.
|
|
7
|
-
*/
|
|
8
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
|
-
if (k2 === undefined) k2 = k;
|
|
10
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
11
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
12
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
13
|
-
}
|
|
14
|
-
Object.defineProperty(o, k2, desc);
|
|
15
|
-
}) : (function(o, m, k, k2) {
|
|
16
|
-
if (k2 === undefined) k2 = k;
|
|
17
|
-
o[k2] = m[k];
|
|
18
|
-
}));
|
|
19
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
20
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
21
|
-
};
|
|
22
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
__exportStar(require("./module"), exports);
|
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import type { Connection, Options } from 'amqplib';
|
|
2
|
-
import type { ConfigInput } from '../config';
|
|
3
|
-
export declare function clearConnections(): Promise<void>;
|
|
4
|
-
export declare function hasConnection(key?: string): boolean;
|
|
5
|
-
export declare function getConnections(): Record<string, Connection>;
|
|
6
|
-
export declare function useConnection(key: string | ConfigInput): Promise<Connection>;
|
|
7
|
-
export declare function createConnection(config: Options.Connect | string): Promise<Connection>;
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright (c) 2022.
|
|
4
|
-
* Author Peter Placzek (tada5hi)
|
|
5
|
-
* For the full copyright and license information,
|
|
6
|
-
* view the LICENSE file that was distributed with this source code.
|
|
7
|
-
*/
|
|
8
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
9
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.createConnection = exports.useConnection = exports.getConnections = exports.hasConnection = exports.clearConnections = void 0;
|
|
13
|
-
const amqplib_1 = __importDefault(require("amqplib"));
|
|
14
|
-
const config_1 = require("../config");
|
|
15
|
-
const instances = {};
|
|
16
|
-
async function clearConnections() {
|
|
17
|
-
const keys = Object.keys(instances);
|
|
18
|
-
for (let i = 0; i < keys.length; i++) {
|
|
19
|
-
await instances[keys[i]].close();
|
|
20
|
-
delete instances[keys[i]];
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
exports.clearConnections = clearConnections;
|
|
24
|
-
function hasConnection(key) {
|
|
25
|
-
key = (0, config_1.getConfigKey)(key);
|
|
26
|
-
return Object.prototype.hasOwnProperty.call(instances, key);
|
|
27
|
-
}
|
|
28
|
-
exports.hasConnection = hasConnection;
|
|
29
|
-
function getConnections() {
|
|
30
|
-
return instances;
|
|
31
|
-
}
|
|
32
|
-
exports.getConnections = getConnections;
|
|
33
|
-
/* istanbul ignore next */
|
|
34
|
-
async function useConnection(key) {
|
|
35
|
-
const config = (0, config_1.getConfig)(key);
|
|
36
|
-
if (Object.prototype.hasOwnProperty.call(instances, config.alias)) {
|
|
37
|
-
return instances[config.alias];
|
|
38
|
-
}
|
|
39
|
-
const connection = await createConnection(config.connection);
|
|
40
|
-
instances[config.alias] = connection;
|
|
41
|
-
return connection;
|
|
42
|
-
}
|
|
43
|
-
exports.useConnection = useConnection;
|
|
44
|
-
/* istanbul ignore next */
|
|
45
|
-
async function createConnection(config) {
|
|
46
|
-
return amqplib_1.default.connect(config);
|
|
47
|
-
}
|
|
48
|
-
exports.createConnection = createConnection;
|
package/dist/consume/module.d.ts
DELETED
package/dist/consume/module.js
DELETED
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright (c) 2022.
|
|
4
|
-
* Author Peter Placzek (tada5hi)
|
|
5
|
-
* For the full copyright and license information,
|
|
6
|
-
* view the LICENSE file that was distributed with this source code.
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.consume = void 0;
|
|
10
|
-
const smob_1 = require("smob");
|
|
11
|
-
const config_1 = require("../config");
|
|
12
|
-
const connection_1 = require("../connection");
|
|
13
|
-
const exchange_1 = require("../exchange");
|
|
14
|
-
const static_1 = require("./static");
|
|
15
|
-
const utils_1 = require("./utils");
|
|
16
|
-
/* istanbul ignore next */
|
|
17
|
-
async function consume(options, handlers) {
|
|
18
|
-
const config = (0, config_1.getConfig)(options.alias);
|
|
19
|
-
const connection = await (0, connection_1.useConnection)(config.alias);
|
|
20
|
-
const channel = await connection.createChannel();
|
|
21
|
-
options = (0, smob_1.merge)(options, config.consume);
|
|
22
|
-
const exchangeOptions = (0, smob_1.merge)(options.exchange || {}, config.exchange);
|
|
23
|
-
let queueName = options.queueName || config.publish.queueName || '';
|
|
24
|
-
if ((0, exchange_1.isDefaultExchange)(exchangeOptions.type)) {
|
|
25
|
-
if (queueName === '') {
|
|
26
|
-
throw new Error('The queue name can not be empty if a non default exchange is selected.');
|
|
27
|
-
}
|
|
28
|
-
await channel.assertQueue(queueName, {
|
|
29
|
-
durable: true,
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
else {
|
|
33
|
-
await channel.assertExchange(exchangeOptions.name, exchangeOptions.type, (0, exchange_1.buildDriverExchangeOptions)({
|
|
34
|
-
durable: true,
|
|
35
|
-
...exchangeOptions,
|
|
36
|
-
}));
|
|
37
|
-
const assertionQueue = await channel.assertQueue('', {
|
|
38
|
-
durable: false,
|
|
39
|
-
autoDelete: true,
|
|
40
|
-
exclusive: true,
|
|
41
|
-
});
|
|
42
|
-
if (typeof exchangeOptions.routingKey === 'undefined') {
|
|
43
|
-
throw new Error('The routingKey can not be empty if a non default exchange is selected.');
|
|
44
|
-
}
|
|
45
|
-
await channel.bindQueue(assertionQueue.queue, config.exchange.name, exchangeOptions.routingKey);
|
|
46
|
-
queueName = assertionQueue.queue;
|
|
47
|
-
}
|
|
48
|
-
if (typeof options.prefetchCount !== 'undefined') {
|
|
49
|
-
await channel.prefetch(options.prefetchCount);
|
|
50
|
-
}
|
|
51
|
-
const handleMessage = async (message) => {
|
|
52
|
-
if (!message) {
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
const handler = handlers[message.properties.type] ||
|
|
56
|
-
handlers[static_1.ConsumeHandlerAnyKey];
|
|
57
|
-
const requeueOnFailure = config.consume.requeueOnFailure ?? false;
|
|
58
|
-
if (typeof handler === 'undefined') {
|
|
59
|
-
channel.reject(message, requeueOnFailure);
|
|
60
|
-
return;
|
|
61
|
-
}
|
|
62
|
-
try {
|
|
63
|
-
await handler(message, channel);
|
|
64
|
-
channel.ack(message);
|
|
65
|
-
}
|
|
66
|
-
catch (e) {
|
|
67
|
-
channel.reject(message, requeueOnFailure);
|
|
68
|
-
}
|
|
69
|
-
};
|
|
70
|
-
await channel.consume(queueName, (message) => handleMessage(message), (0, utils_1.buildDriverConsumeOptions)(options));
|
|
71
|
-
}
|
|
72
|
-
exports.consume = consume;
|
package/dist/publish/module.d.ts
DELETED
package/dist/publish/module.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/*
|
|
3
|
-
* Copyright (c) 2022.
|
|
4
|
-
* Author Peter Placzek (tada5hi)
|
|
5
|
-
* For the full copyright and license information,
|
|
6
|
-
* view the LICENSE file that was distributed with this source code.
|
|
7
|
-
*/
|
|
8
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.publish = void 0;
|
|
10
|
-
/* istanbul ignore next */
|
|
11
|
-
const smob_1 = require("smob");
|
|
12
|
-
const uuid_1 = require("uuid");
|
|
13
|
-
const config_1 = require("../config");
|
|
14
|
-
const connection_1 = require("../connection");
|
|
15
|
-
const exchange_1 = require("../exchange");
|
|
16
|
-
const utils_1 = require("./utils");
|
|
17
|
-
/* istanbul ignore next */
|
|
18
|
-
async function publish(options) {
|
|
19
|
-
let buffer;
|
|
20
|
-
if (Buffer.isBuffer(options.content)) {
|
|
21
|
-
buffer = options.content;
|
|
22
|
-
}
|
|
23
|
-
else if (typeof options.content === 'string') {
|
|
24
|
-
buffer = Buffer.from(options.content, 'utf-8');
|
|
25
|
-
}
|
|
26
|
-
else {
|
|
27
|
-
buffer = Buffer.from(JSON.stringify(options.content), 'utf-8');
|
|
28
|
-
}
|
|
29
|
-
const config = (0, config_1.getConfig)(options.alias);
|
|
30
|
-
const connection = await (0, connection_1.useConnection)(config.alias);
|
|
31
|
-
const channel = await connection.createChannel();
|
|
32
|
-
options = (0, smob_1.merge)(options, config.publish || {});
|
|
33
|
-
const exchangeOptions = (0, smob_1.merge)({}, options.exchange || {}, config.exchange);
|
|
34
|
-
if (!(0, exchange_1.isDefaultExchange)(exchangeOptions.type)) {
|
|
35
|
-
await channel.assertExchange(exchangeOptions.name, exchangeOptions.type, (0, exchange_1.buildDriverExchangeOptions)({
|
|
36
|
-
durable: true,
|
|
37
|
-
...exchangeOptions,
|
|
38
|
-
}));
|
|
39
|
-
}
|
|
40
|
-
if (typeof options.messageId === 'undefined') {
|
|
41
|
-
options.messageId = options.id || (0, uuid_1.v4)();
|
|
42
|
-
}
|
|
43
|
-
if (!(0, exchange_1.isDefaultExchange)(exchangeOptions.type)) {
|
|
44
|
-
if (typeof exchangeOptions.routingKey === 'undefined') {
|
|
45
|
-
throw new Error('The routingKey can not be empty if a non default exchange is selected.');
|
|
46
|
-
}
|
|
47
|
-
channel.publish(config.exchange.name, exchangeOptions.routingKey, buffer, (0, utils_1.buildDriverPublishOptions)({
|
|
48
|
-
persistent: true,
|
|
49
|
-
...options,
|
|
50
|
-
}));
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
// publish to default exchange
|
|
54
|
-
const queueName = options.queueName ||
|
|
55
|
-
config.publish.queueName;
|
|
56
|
-
if (typeof queueName === 'undefined' || queueName === '') {
|
|
57
|
-
throw new Error('The queue name can not be empty if a non default exchange is selected.');
|
|
58
|
-
}
|
|
59
|
-
await channel.assertQueue(queueName, {
|
|
60
|
-
durable: true,
|
|
61
|
-
});
|
|
62
|
-
channel.sendToQueue(queueName, buffer, (0, utils_1.buildDriverPublishOptions)({
|
|
63
|
-
persistent: true,
|
|
64
|
-
...options,
|
|
65
|
-
}));
|
|
66
|
-
}
|
|
67
|
-
exports.publish = publish;
|