amqp-extension 3.2.0 → 4.0.0-beta.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/.eslintrc +1 -1
- package/CHANGELOG.md +27 -0
- package/README.MD +11 -147
- package/dist/config/module.js +1 -1
- package/dist/config/type.d.ts +4 -3
- package/dist/consume/index.d.ts +1 -1
- package/dist/consume/index.js +1 -1
- package/dist/consume/type.d.ts +19 -4
- package/dist/consume/utils.d.ts +2 -3
- package/dist/consume/utils.js +14 -10
- package/dist/exchange/constants.d.ts +0 -1
- package/dist/exchange/constants.js +0 -1
- package/dist/exchange/type.d.ts +5 -4
- package/dist/exchange/utils.d.ts +3 -4
- package/dist/exchange/utils.js +8 -11
- package/dist/module.d.ts +16 -21
- package/dist/module.js +123 -126
- package/dist/publish/index.d.ts +0 -1
- package/dist/publish/index.js +0 -1
- package/dist/publish/type.d.ts +7 -13
- package/dist/type.d.ts +12 -26
- package/dist/utils.d.ts +2 -2
- package/dist/utils.js +19 -17
- package/package.json +10 -9
- package/dist/publish/utils.d.ts +0 -3
- package/dist/publish/utils.js +0 -13
- /package/dist/consume/{static.d.ts → constants.d.ts} +0 -0
- /package/dist/consume/{static.js → constants.js} +0 -0
package/.eslintrc
CHANGED
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,30 @@
|
|
|
1
|
+
# [4.0.0-beta.1](https://github.com/Tada5hi/amqp-extension/compare/v3.3.0...v4.0.0-beta.1) (2024-06-06)
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
### Bug Fixes
|
|
5
|
+
|
|
6
|
+
* **deps:** bump amqplib from 0.10.3 to 0.10.4 ([#368](https://github.com/Tada5hi/amqp-extension/issues/368)) ([dae6c84](https://github.com/Tada5hi/amqp-extension/commit/dae6c840ad69e74a6c5dc37ea7f0fbb6c8ea1bae))
|
|
7
|
+
* **deps:** bump smob from 1.4.1 to 1.5.0 ([#334](https://github.com/Tada5hi/amqp-extension/issues/334)) ([0b2935a](https://github.com/Tada5hi/amqp-extension/commit/0b2935af145d4844510c19b6d3f9d23ac0e3386a))
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* adjusted test suite ([a5cc771](https://github.com/Tada5hi/amqp-extension/commit/a5cc7714a141d671d1e97ed67216df0ffc53af49))
|
|
13
|
+
* change publish/consume signature ([ff316ec](https://github.com/Tada5hi/amqp-extension/commit/ff316ec27ac1077c7a6949ea5e5adf6ad1e5e5f5))
|
|
14
|
+
* initial refactoring of config, consume- &p publish-behaviour ([1102aa8](https://github.com/Tada5hi/amqp-extension/commit/1102aa8a8b000336312d89c92434d8c77e7a0424))
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
### BREAKING CHANGES
|
|
18
|
+
|
|
19
|
+
* public api changed
|
|
20
|
+
|
|
21
|
+
# [3.3.0](https://github.com/Tada5hi/amqp-extension/compare/v3.2.0...v3.3.0) (2024-03-20)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
### Features
|
|
25
|
+
|
|
26
|
+
* 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))
|
|
27
|
+
|
|
1
28
|
# [3.2.0](https://github.com/Tada5hi/amqp-extension/compare/v3.1.1...v3.2.0) (2024-03-11)
|
|
2
29
|
|
|
3
30
|
|
package/README.MD
CHANGED
|
@@ -12,13 +12,8 @@ This is a library on top of the [amqplib](https://www.npmjs.com/package/amqplib)
|
|
|
12
12
|
|
|
13
13
|
- [Installation](#installation)
|
|
14
14
|
- [Usage](#usage)
|
|
15
|
-
- [Publish](#publish
|
|
16
|
-
- [Consume](#consume
|
|
17
|
-
- [Functions](#functions)
|
|
18
|
-
- [publish](#publish)
|
|
19
|
-
- [consume](#consume)
|
|
20
|
-
- [Types](#types)
|
|
21
|
-
- [Config](#config-types)
|
|
15
|
+
- [Publish](#publish)
|
|
16
|
+
- [Consume](#consume)
|
|
22
17
|
- [License](#license)
|
|
23
18
|
|
|
24
19
|
|
|
@@ -30,16 +25,15 @@ npm install amqp-extension --save
|
|
|
30
25
|
|
|
31
26
|
## Usage
|
|
32
27
|
|
|
33
|
-
### Publish
|
|
28
|
+
### Publish
|
|
34
29
|
|
|
35
30
|
The `publish` method allows you to send messages quickly.
|
|
36
|
-
Existing options can be added or overwritten
|
|
37
31
|
|
|
38
32
|
```typescript
|
|
39
33
|
import { Client } from "amqp-extension";
|
|
40
34
|
|
|
41
35
|
const client = new Client({
|
|
42
|
-
|
|
36
|
+
connectionOptions: 'amqp://<user>:<password>@<host>',
|
|
43
37
|
exchange: {
|
|
44
38
|
name: '<name>',
|
|
45
39
|
type: 'topic'
|
|
@@ -47,20 +41,15 @@ const client = new Client({
|
|
|
47
41
|
});
|
|
48
42
|
|
|
49
43
|
(async () => {
|
|
50
|
-
await client.publish({
|
|
51
|
-
|
|
52
|
-
type: 'resourceCreated',
|
|
53
|
-
name: 'foo'
|
|
54
|
-
}
|
|
44
|
+
await client.publish('<routing-key>', {
|
|
45
|
+
foo: 'bar'
|
|
55
46
|
});
|
|
56
47
|
})();
|
|
57
48
|
```
|
|
58
49
|
|
|
59
|
-
### Consume
|
|
60
|
-
|
|
61
|
-
To consume a queue use the `consume` function. As first argument it accepts a configuration object
|
|
62
|
-
and as second argument and object to specify an async callback function handler for a specific message `type`.
|
|
50
|
+
### Consume
|
|
63
51
|
|
|
52
|
+
To consume a queue use the `consume` function.
|
|
64
53
|
```typescript
|
|
65
54
|
import {
|
|
66
55
|
Client,
|
|
@@ -69,22 +58,16 @@ import {
|
|
|
69
58
|
} from "amqp-extension";
|
|
70
59
|
|
|
71
60
|
const client = new Client({
|
|
72
|
-
|
|
61
|
+
connectionOptions: 'amqp://<user>:<password>@<host>',
|
|
73
62
|
exchange: {
|
|
74
63
|
name: '<name>',
|
|
75
64
|
type: 'topic'
|
|
76
65
|
}
|
|
77
66
|
});
|
|
78
67
|
|
|
79
|
-
const options: ConsumeOptions = {
|
|
80
|
-
exchange: {
|
|
81
|
-
routingKey: '<routing-key>'
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
68
|
(async () => {
|
|
86
|
-
await client.consume(
|
|
87
|
-
|
|
69
|
+
await client.consume('<routing-key>', {
|
|
70
|
+
$any: async (message: ConsumeMessage) => {
|
|
88
71
|
const content = message.content.toString('utf-8');
|
|
89
72
|
const payload = JSON.parse(content);
|
|
90
73
|
console.log(payload);
|
|
@@ -94,125 +77,6 @@ const options: ConsumeOptions = {
|
|
|
94
77
|
})();
|
|
95
78
|
```
|
|
96
79
|
|
|
97
|
-
## Functions
|
|
98
|
-
|
|
99
|
-
### publish
|
|
100
|
-
|
|
101
|
-
▸ `function` **publish**(`message: Message`, `options?: PublishOptions`): `Promise<void>`
|
|
102
|
-
|
|
103
|
-
Send the constructed queue message to the message broker.
|
|
104
|
-
As second parameter a registered config can be used by specifying the alias or provide the full config object.
|
|
105
|
-
#### Example
|
|
106
|
-
**`Simple`**
|
|
107
|
-
|
|
108
|
-
```typescript
|
|
109
|
-
import { Client } from "amqp-extension";
|
|
110
|
-
|
|
111
|
-
const client = new Client({
|
|
112
|
-
// ...
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
(async () => {
|
|
116
|
-
await client.publish({
|
|
117
|
-
content: {
|
|
118
|
-
type: 'resourceCreated'
|
|
119
|
-
}
|
|
120
|
-
});
|
|
121
|
-
})();
|
|
122
|
-
```
|
|
123
|
-
|
|
124
|
-
#### Type parameters
|
|
125
|
-
|
|
126
|
-
| Name | Description |
|
|
127
|
-
|:------|:------------|
|
|
128
|
-
|
|
129
|
-
#### Parameters
|
|
130
|
-
|
|
131
|
-
| Name | Type | Description |
|
|
132
|
-
|:----------|:-----------------|:----------------------------|
|
|
133
|
-
| `message` | `Message` | Constructed message object. |
|
|
134
|
-
| `options` | `PublishOptions` | Publish options. |
|
|
135
|
-
|
|
136
|
-
#### Returns
|
|
137
|
-
|
|
138
|
-
`Promise<void>`
|
|
139
|
-
|
|
140
|
-
The function does not return a value.
|
|
141
|
-
|
|
142
|
-
### consume
|
|
143
|
-
|
|
144
|
-
▸ `function` **consume**(`options: ConsumeOptions`, `cb: ConsumeHandlers`): `Promise<void>`
|
|
145
|
-
|
|
146
|
-
Send the constructed queue message to the message broker.
|
|
147
|
-
As second parameter a registered config can be used by specifying the alias or provide the full config object.
|
|
148
|
-
#### Example
|
|
149
|
-
**`Simple`**
|
|
150
|
-
|
|
151
|
-
```typescript
|
|
152
|
-
import {
|
|
153
|
-
Client,
|
|
154
|
-
ConsumeOptions,
|
|
155
|
-
ConsumeMessage,
|
|
156
|
-
} from "amqp-extension";
|
|
157
|
-
|
|
158
|
-
const client = new Client({
|
|
159
|
-
// ...
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
const options: ConsumeOptions = {
|
|
163
|
-
routingKey: '<routing-key>'
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
(async () => {
|
|
167
|
-
await client.consume(options, {
|
|
168
|
-
'<type>': async (message: ConsumeMessage) => {
|
|
169
|
-
// do some async action :)
|
|
170
|
-
}
|
|
171
|
-
});
|
|
172
|
-
})();
|
|
173
|
-
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
#### Type parameters
|
|
177
|
-
|
|
178
|
-
| Name | Description |
|
|
179
|
-
|:-------|:------------|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
#### Parameters
|
|
184
|
-
|
|
185
|
-
| Name | Type | Description |
|
|
186
|
-
|:-----------|:------------------|:-------------------|
|
|
187
|
-
| `options` | `ConsumeOptions` | Consume options. ) |
|
|
188
|
-
| `handlers` | `ConsumeHandlers` | Handlers object. |
|
|
189
|
-
|
|
190
|
-
#### Returns
|
|
191
|
-
|
|
192
|
-
`Promise<void>`
|
|
193
|
-
|
|
194
|
-
The function does not return a value.
|
|
195
|
-
|
|
196
|
-
## Types
|
|
197
|
-
|
|
198
|
-
### Config Types
|
|
199
|
-
|
|
200
|
-
```typescript
|
|
201
|
-
import { Options } from 'amqplib';
|
|
202
|
-
import { ExchangeOptions } from '../exchange';
|
|
203
|
-
import { ConsumeOptions, PublishOptions } from '../type';
|
|
204
|
-
|
|
205
|
-
export type Config = {
|
|
206
|
-
connection: Options.Connect | string,
|
|
207
|
-
exchange: ExchangeOptions,
|
|
208
|
-
publish: PublishOptions,
|
|
209
|
-
consume: ConsumeOptions
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
export type ConfigInput = Partial<Exclude<Config, 'connection'>> &
|
|
213
|
-
Pick<Config, 'connection'>;
|
|
214
|
-
```
|
|
215
|
-
|
|
216
80
|
## License
|
|
217
81
|
|
|
218
82
|
Made with 💚
|
package/dist/config/module.js
CHANGED
package/dist/config/type.d.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { Options } from 'amqplib';
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
2
|
+
import type { ConsumeOptions } from '../consume';
|
|
3
|
+
import type { Exchange } from '../exchange';
|
|
4
|
+
import type { PublishOptions } from '../publish';
|
|
4
5
|
export type Config = {
|
|
5
6
|
reconnectAttempts: number;
|
|
6
7
|
reconnectTimeout: number;
|
|
7
8
|
connection: Options.Connect | string;
|
|
8
|
-
exchange:
|
|
9
|
+
exchange: Exchange;
|
|
9
10
|
publish: PublishOptions;
|
|
10
11
|
consume: ConsumeOptions;
|
|
11
12
|
};
|
package/dist/consume/index.d.ts
CHANGED
package/dist/consume/index.js
CHANGED
|
@@ -20,6 +20,6 @@ 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("./
|
|
23
|
+
__exportStar(require("./constants"), exports);
|
|
24
24
|
__exportStar(require("./type"), exports);
|
|
25
25
|
__exportStar(require("./utils"), exports);
|
package/dist/consume/type.d.ts
CHANGED
|
@@ -1,10 +1,25 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
1
|
+
import type { ChannelWrapper } from 'amqp-connection-manager';
|
|
2
|
+
import type { ConsumeMessage, Options } from 'amqplib';
|
|
3
|
+
import type { ConsumeHandlerAnyKey } from './constants';
|
|
3
4
|
export { ConsumeMessage, };
|
|
4
|
-
export type ConsumeMessageHandler = (message: ConsumeMessage, channel:
|
|
5
|
-
export type ConsumeHandlerAnyKeyType = typeof ConsumeHandlerAnyKey;
|
|
5
|
+
export type ConsumeMessageHandler = (message: ConsumeMessage, channel: ChannelWrapper) => Promise<void> | void;
|
|
6
6
|
export type ConsumeHandlers = {
|
|
7
7
|
[ConsumeHandlerAnyKey]?: ConsumeMessageHandler;
|
|
8
8
|
} & {
|
|
9
9
|
[key: string]: ConsumeMessageHandler;
|
|
10
10
|
};
|
|
11
|
+
export type ConsumeOptions = Options.Consume & {
|
|
12
|
+
/**
|
|
13
|
+
* Default: false
|
|
14
|
+
*/
|
|
15
|
+
requeueOnFailure?: boolean;
|
|
16
|
+
/**
|
|
17
|
+
* Default: undefined
|
|
18
|
+
*/
|
|
19
|
+
prefetchCount?: number;
|
|
20
|
+
};
|
|
21
|
+
export type ConsumeContext = {
|
|
22
|
+
from: string;
|
|
23
|
+
options?: ConsumeOptions;
|
|
24
|
+
handlers: ConsumeHandlers;
|
|
25
|
+
};
|
package/dist/consume/utils.d.ts
CHANGED
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
|
|
3
|
-
export declare function buildDriverConsumeOptions(options: ConsumeOptions): Options.Consume;
|
|
1
|
+
import type { ConsumeHandlers } from './type';
|
|
2
|
+
export declare function isConsumeHandlers(input: unknown): input is ConsumeHandlers;
|
package/dist/consume/utils.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
function
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
3
|
+
exports.isConsumeHandlers = void 0;
|
|
4
|
+
const smob_1 = require("smob");
|
|
5
|
+
function isConsumeHandlers(input) {
|
|
6
|
+
if (!(0, smob_1.isObject)(input)) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
const keys = Object.keys(input);
|
|
10
|
+
for (let i = 0; i < keys.length; i++) {
|
|
11
|
+
if (typeof input[keys[i]] !== 'function') {
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
return true;
|
|
12
16
|
}
|
|
13
|
-
exports.
|
|
17
|
+
exports.isConsumeHandlers = isConsumeHandlers;
|
|
@@ -3,7 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ExchangeType = void 0;
|
|
4
4
|
var ExchangeType;
|
|
5
5
|
(function (ExchangeType) {
|
|
6
|
-
ExchangeType["DEFAULT"] = "default";
|
|
7
6
|
ExchangeType["FANOUT"] = "fanout";
|
|
8
7
|
ExchangeType["DIRECT"] = "direct";
|
|
9
8
|
ExchangeType["TOPIC"] = "topic";
|
package/dist/exchange/type.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type { Options } from 'amqplib';
|
|
2
2
|
import type { ExchangeType } from './constants';
|
|
3
|
-
export type ExchangeOptions =
|
|
4
|
-
|
|
3
|
+
export type ExchangeOptions = Options.AssertExchange;
|
|
4
|
+
export type Exchange = {
|
|
5
5
|
type: `${ExchangeType}`;
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
name?: string;
|
|
7
|
+
options?: ExchangeOptions;
|
|
8
|
+
};
|
package/dist/exchange/utils.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import type { Options } from 'amqplib';
|
|
2
1
|
import { ExchangeType } from './constants';
|
|
3
|
-
import type {
|
|
4
|
-
export declare function isDefaultExchange(
|
|
5
|
-
export declare function
|
|
2
|
+
import type { Exchange } from './type';
|
|
3
|
+
export declare function isDefaultExchange(input?: `${ExchangeType}`): input is ExchangeType.DIRECT;
|
|
4
|
+
export declare function isExchange(input: unknown): input is Exchange;
|
package/dist/exchange/utils.js
CHANGED
|
@@ -1,17 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
3
|
+
exports.isExchange = exports.isDefaultExchange = void 0;
|
|
4
|
+
const smob_1 = require("smob");
|
|
5
5
|
const constants_1 = require("./constants");
|
|
6
|
-
function isDefaultExchange(
|
|
7
|
-
return typeof
|
|
6
|
+
function isDefaultExchange(input) {
|
|
7
|
+
return typeof input === 'undefined' || input === constants_1.ExchangeType.DIRECT;
|
|
8
8
|
}
|
|
9
9
|
exports.isDefaultExchange = isDefaultExchange;
|
|
10
|
-
function
|
|
11
|
-
return (0,
|
|
12
|
-
'
|
|
13
|
-
'type',
|
|
14
|
-
'routingKey',
|
|
15
|
-
]);
|
|
10
|
+
function isExchange(input) {
|
|
11
|
+
return (0, smob_1.isObject)(input) &&
|
|
12
|
+
typeof input.type === 'string';
|
|
16
13
|
}
|
|
17
|
-
exports.
|
|
14
|
+
exports.isExchange = isExchange;
|
package/dist/module.d.ts
CHANGED
|
@@ -1,23 +1,18 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type { ConsumeHandlers } from './consume';
|
|
4
|
-
import type {
|
|
5
|
-
import type { ConsumeOptions } from './type';
|
|
6
|
-
type Consumer = {
|
|
7
|
-
options: ConsumeOptions;
|
|
8
|
-
handlers: ConsumeHandlers;
|
|
9
|
-
};
|
|
1
|
+
import type { PublishContext, PublishOptions } from './publish';
|
|
2
|
+
import type { ClientContext, Connection } from './type';
|
|
3
|
+
import type { ConsumeContext, ConsumeHandlers, ConsumeOptions } from './consume';
|
|
4
|
+
import type { Exchange } from './exchange';
|
|
10
5
|
export declare class Client {
|
|
11
|
-
|
|
12
|
-
protected
|
|
13
|
-
protected
|
|
14
|
-
protected
|
|
15
|
-
constructor(
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
publish(
|
|
6
|
+
readonly connection: Connection;
|
|
7
|
+
protected publishOptions: PublishOptions;
|
|
8
|
+
protected consumeOptions: ConsumeOptions;
|
|
9
|
+
protected exchange: Exchange;
|
|
10
|
+
constructor(ctx: ClientContext);
|
|
11
|
+
of(exchange: Exchange): Client;
|
|
12
|
+
consume(ctx: ConsumeContext): Promise<void>;
|
|
13
|
+
consume(from: string, handlers: ConsumeHandlers): Promise<void>;
|
|
14
|
+
consume(from: string, options: ConsumeOptions, handlers: ConsumeHandlers): Promise<void>;
|
|
15
|
+
publish(ctx: PublishContext): Promise<boolean>;
|
|
16
|
+
publish(to: string, data: any): Promise<boolean>;
|
|
17
|
+
publish(to: string, data: any, options: PublishOptions): Promise<boolean>;
|
|
22
18
|
}
|
|
23
|
-
export {};
|
package/dist/module.js
CHANGED
|
@@ -10,105 +10,75 @@ 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
|
-
const smob_1 = require("smob");
|
|
16
|
-
const uuid_1 = require("uuid");
|
|
17
|
-
const config_1 = require("./config");
|
|
18
15
|
const consume_1 = require("./consume");
|
|
19
16
|
const exchange_1 = require("./exchange");
|
|
20
|
-
const publish_1 = require("./publish");
|
|
21
17
|
const utils_1 = require("./utils");
|
|
22
18
|
class Client {
|
|
23
|
-
constructor(
|
|
24
|
-
|
|
25
|
-
|
|
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;
|
|
19
|
+
constructor(ctx) {
|
|
20
|
+
if (ctx.connection) {
|
|
21
|
+
this.connection = ctx.connection;
|
|
38
22
|
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
async useConnection() {
|
|
50
|
-
if (typeof this.connection !== 'undefined') {
|
|
51
|
-
return this.connection;
|
|
23
|
+
else {
|
|
24
|
+
this.connection = (0, amqp_connection_manager_1.connect)(ctx.connectionOptions, {
|
|
25
|
+
reconnectTimeInSeconds: 60,
|
|
26
|
+
});
|
|
27
|
+
node_process_1.default.once('SIGINT', async () => {
|
|
28
|
+
if (this.connection) {
|
|
29
|
+
await this.connection.close();
|
|
30
|
+
}
|
|
31
|
+
});
|
|
52
32
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
await this.recreateConsumers();
|
|
33
|
+
this.publishOptions = ctx.publishOptions || {};
|
|
34
|
+
this.consumeOptions = ctx.consumeOptions || {};
|
|
35
|
+
this.exchange = ctx.exchange || {
|
|
36
|
+
type: exchange_1.ExchangeType.DIRECT,
|
|
37
|
+
name: '',
|
|
59
38
|
};
|
|
60
|
-
connection.once('close', handleDisconnect);
|
|
61
|
-
connection.once('error', handleDisconnect);
|
|
62
|
-
this.connection = connection;
|
|
63
|
-
return this.connection;
|
|
64
39
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
40
|
+
of(exchange) {
|
|
41
|
+
return new Client({
|
|
42
|
+
connection: this.connection,
|
|
43
|
+
publishOptions: this.publishOptions,
|
|
44
|
+
consumeOptions: this.consumeOptions,
|
|
45
|
+
exchange,
|
|
46
|
+
});
|
|
69
47
|
}
|
|
70
|
-
async
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
48
|
+
async consume(_ctxOrFrom, _optionsOrHandlers, _handlers) {
|
|
49
|
+
let from;
|
|
50
|
+
let handlers;
|
|
51
|
+
let options;
|
|
52
|
+
if (typeof _ctxOrFrom === 'string') {
|
|
53
|
+
from = _ctxOrFrom;
|
|
54
|
+
if ((0, consume_1.isConsumeHandlers)(_optionsOrHandlers)) {
|
|
55
|
+
handlers = _optionsOrHandlers;
|
|
56
|
+
options = this.consumeOptions;
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
handlers = _handlers;
|
|
60
|
+
options = (0, utils_1.mergeOptions)(_optionsOrHandlers, this.consumeOptions);
|
|
79
61
|
}
|
|
80
|
-
await channel.assertQueue(queueName, {
|
|
81
|
-
durable: true,
|
|
82
|
-
});
|
|
83
62
|
}
|
|
84
63
|
else {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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.');
|
|
64
|
+
from = _ctxOrFrom.from;
|
|
65
|
+
handlers = _ctxOrFrom.handlers;
|
|
66
|
+
if (_ctxOrFrom.options) {
|
|
67
|
+
options = (0, utils_1.mergeOptions)(_ctxOrFrom.options, this.consumeOptions);
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
options = this.consumeOptions;
|
|
96
71
|
}
|
|
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
72
|
}
|
|
73
|
+
const requeueOnFailure = options.requeueOnFailure ?? false;
|
|
103
74
|
const handleMessage = async (message) => {
|
|
104
75
|
if (!message) {
|
|
105
76
|
return;
|
|
106
77
|
}
|
|
107
78
|
const handler = handlers[message.properties.type] ||
|
|
108
79
|
handlers[consume_1.ConsumeHandlerAnyKey];
|
|
109
|
-
const requeueOnFailure = this.config.consume.requeueOnFailure ?? false;
|
|
110
80
|
if (typeof handler === 'undefined') {
|
|
111
|
-
channel.
|
|
81
|
+
channel.nack(message, undefined, requeueOnFailure);
|
|
112
82
|
return;
|
|
113
83
|
}
|
|
114
84
|
try {
|
|
@@ -116,67 +86,94 @@ class Client {
|
|
|
116
86
|
channel.ack(message);
|
|
117
87
|
}
|
|
118
88
|
catch (e) {
|
|
119
|
-
channel.
|
|
89
|
+
channel.nack(message, undefined, requeueOnFailure);
|
|
120
90
|
}
|
|
121
91
|
};
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
92
|
+
const channel = this.connection.createChannel({
|
|
93
|
+
setup: async (channel) => {
|
|
94
|
+
let queueName = from;
|
|
95
|
+
if ((0, exchange_1.isDefaultExchange)(this.exchange.type)) {
|
|
96
|
+
if (queueName === '') {
|
|
97
|
+
throw new Error('The queue name can not be empty if the default exchange is selected.');
|
|
98
|
+
}
|
|
99
|
+
await channel.assertQueue(queueName, {
|
|
100
|
+
durable: true,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
await channel.assertExchange(this.exchange.name || '', this.exchange.type, this.exchange.options);
|
|
105
|
+
const assertionQueue = await channel.assertQueue('', {
|
|
106
|
+
durable: false,
|
|
107
|
+
autoDelete: true,
|
|
108
|
+
exclusive: true,
|
|
109
|
+
});
|
|
110
|
+
await channel.bindQueue(assertionQueue.queue, this.exchange.name || '', from || '');
|
|
111
|
+
queueName = assertionQueue.queue;
|
|
112
|
+
}
|
|
113
|
+
if (typeof options.prefetchCount === 'number') {
|
|
114
|
+
await channel.prefetch(options.prefetchCount);
|
|
115
|
+
}
|
|
116
|
+
await channel.consume(queueName, (message) => handleMessage(message), options);
|
|
117
|
+
},
|
|
130
118
|
});
|
|
119
|
+
return channel.waitForConnect();
|
|
131
120
|
}
|
|
132
|
-
async publish(
|
|
121
|
+
async publish(_toOrCtx, _data, _options) {
|
|
133
122
|
let buffer;
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
123
|
+
let to;
|
|
124
|
+
let data;
|
|
125
|
+
let options;
|
|
126
|
+
if (typeof _toOrCtx === 'string') {
|
|
127
|
+
to = _toOrCtx;
|
|
128
|
+
data = _data;
|
|
129
|
+
if (_options) {
|
|
130
|
+
options = (0, utils_1.mergeOptions)(_options, this.publishOptions);
|
|
131
|
+
}
|
|
132
|
+
else {
|
|
133
|
+
options = this.publishOptions;
|
|
134
|
+
}
|
|
139
135
|
}
|
|
140
136
|
else {
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
durable: true,
|
|
150
|
-
...exchangeOptions,
|
|
151
|
-
}));
|
|
137
|
+
to = _toOrCtx.to;
|
|
138
|
+
data = _toOrCtx.data;
|
|
139
|
+
if (_toOrCtx.options) {
|
|
140
|
+
options = (0, utils_1.mergeOptions)(_toOrCtx.options, this.publishOptions);
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
options = this.publishOptions;
|
|
144
|
+
}
|
|
152
145
|
}
|
|
153
|
-
if (
|
|
154
|
-
|
|
146
|
+
if (Buffer.isBuffer(data)) {
|
|
147
|
+
buffer = data;
|
|
155
148
|
}
|
|
156
|
-
if (
|
|
157
|
-
|
|
158
|
-
throw new Error('The routingKey can not be empty if a non default exchange is selected.');
|
|
159
|
-
}
|
|
160
|
-
const published = channel.publish(this.config.exchange.name, exchangeOptions.routingKey, buffer, (0, publish_1.buildDriverPublishOptions)({
|
|
161
|
-
persistent: true,
|
|
162
|
-
...options,
|
|
163
|
-
}));
|
|
164
|
-
await channel.close();
|
|
165
|
-
return published;
|
|
149
|
+
else if (typeof data === 'string') {
|
|
150
|
+
buffer = Buffer.from(data, 'utf-8');
|
|
166
151
|
}
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
this.config.publish.queueName;
|
|
170
|
-
if (typeof queueName === 'undefined' || queueName === '') {
|
|
171
|
-
throw new Error('The queue name can not be empty if a non default exchange is selected.');
|
|
152
|
+
else {
|
|
153
|
+
buffer = Buffer.from(JSON.stringify(data), 'utf-8');
|
|
172
154
|
}
|
|
173
|
-
|
|
174
|
-
|
|
155
|
+
const { connection } = this;
|
|
156
|
+
const channel = connection.createChannel({
|
|
157
|
+
setup: async (channel) => {
|
|
158
|
+
if ((0, exchange_1.isDefaultExchange)(this.exchange.type)) {
|
|
159
|
+
if (!to) {
|
|
160
|
+
throw new Error('The queue name can not be empty if the default exchange is selected.');
|
|
161
|
+
}
|
|
162
|
+
await channel.assertQueue(to, {
|
|
163
|
+
durable: true,
|
|
164
|
+
});
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (!this.exchange.name) {
|
|
168
|
+
throw new SyntaxError('A non default exchange requires a name.');
|
|
169
|
+
}
|
|
170
|
+
await channel.assertExchange(this.exchange.name, this.exchange.type, this.exchange.options);
|
|
171
|
+
},
|
|
175
172
|
});
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
173
|
+
if (!to) {
|
|
174
|
+
throw new SyntaxError('The publish operation requires a destination.');
|
|
175
|
+
}
|
|
176
|
+
const published = await channel.publish(this.exchange.name || '', to, buffer, options);
|
|
180
177
|
await channel.close();
|
|
181
178
|
return published;
|
|
182
179
|
}
|
package/dist/publish/index.d.ts
CHANGED
package/dist/publish/index.js
CHANGED
package/dist/publish/type.d.ts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
export type
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
id?: string;
|
|
9
|
-
/**
|
|
10
|
-
* The message data.
|
|
11
|
-
*/
|
|
12
|
-
content: T;
|
|
13
|
-
} & PublishOptions;
|
|
1
|
+
import type { Options } from 'amqplib';
|
|
2
|
+
export type PublishOptions = Options.Publish;
|
|
3
|
+
export type PublishContext = {
|
|
4
|
+
to: string;
|
|
5
|
+
data: any;
|
|
6
|
+
options?: PublishOptions;
|
|
7
|
+
};
|
package/dist/type.d.ts
CHANGED
|
@@ -1,28 +1,14 @@
|
|
|
1
|
+
import type { connect } from 'amqp-connection-manager';
|
|
1
2
|
import type { Options } from 'amqplib';
|
|
2
|
-
import type {
|
|
3
|
-
type
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
* default: ''
|
|
14
|
-
*/
|
|
15
|
-
queueName?: string;
|
|
3
|
+
import type { ConsumeOptions } from './consume';
|
|
4
|
+
import type { Exchange } from './exchange';
|
|
5
|
+
import type { PublishOptions } from './publish';
|
|
6
|
+
export type Connection = ReturnType<typeof connect>;
|
|
7
|
+
export type ConnectionOptions = Options.Connect | string;
|
|
8
|
+
export type ClientContext = {
|
|
9
|
+
connection?: Connection;
|
|
10
|
+
connectionOptions?: ConnectionOptions;
|
|
11
|
+
exchange?: Exchange;
|
|
12
|
+
publishOptions?: PublishOptions;
|
|
13
|
+
consumeOptions?: ConsumeOptions;
|
|
16
14
|
};
|
|
17
|
-
export type PublishOptions = CommonOptions & Options.Publish;
|
|
18
|
-
export type ConsumeOptions = CommonOptions & Options.Consume & {
|
|
19
|
-
/**
|
|
20
|
-
* Default: false
|
|
21
|
-
*/
|
|
22
|
-
requeueOnFailure?: boolean;
|
|
23
|
-
/**
|
|
24
|
-
* Default: undefined
|
|
25
|
-
*/
|
|
26
|
-
prefetchCount?: number;
|
|
27
|
-
};
|
|
28
|
-
export {};
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
1
|
+
declare const mergeOptions: import("smob").Merger;
|
|
2
|
+
export { mergeOptions, };
|
package/dist/utils.js
CHANGED
|
@@ -1,21 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.mergeOptions = void 0;
|
|
4
4
|
const smob_1 = require("smob");
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
const exchange_1 = require("./exchange");
|
|
6
|
+
const mergeOptions = (0, smob_1.createMerger)({
|
|
7
|
+
priority: 'left',
|
|
8
|
+
inPlace: false,
|
|
9
|
+
clone: true,
|
|
10
|
+
array: true,
|
|
11
|
+
arrayDistinct: true,
|
|
12
|
+
strategy(target, key, value) {
|
|
13
|
+
if (key !== 'exchange') {
|
|
14
|
+
return undefined;
|
|
9
15
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}, ms);
|
|
19
|
-
});
|
|
20
|
-
}
|
|
21
|
-
exports.wait = wait;
|
|
16
|
+
if (!(0, exchange_1.isExchange)(target[key]) ||
|
|
17
|
+
!(0, exchange_1.isExchange)(value)) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
return target;
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
exports.mergeOptions = mergeOptions;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "amqp-extension",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0-beta.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",
|
|
@@ -36,17 +36,18 @@
|
|
|
36
36
|
"homepage": "https://github.com/Tada5hi/amqp-extension#readme",
|
|
37
37
|
"dependencies": {
|
|
38
38
|
"@types/amqplib": "^0.10.5",
|
|
39
|
-
"
|
|
40
|
-
"
|
|
39
|
+
"amqp-connection-manager": "^4.1.14",
|
|
40
|
+
"amqplib": "^0.10.4",
|
|
41
|
+
"smob": "^1.5.0",
|
|
41
42
|
"uuid": "^9.0.1"
|
|
42
43
|
},
|
|
43
44
|
"devDependencies": {
|
|
44
45
|
"@tada5hi/commitlint-config": "^1.1.7",
|
|
45
|
-
"@tada5hi/eslint-config-typescript": "^1.2.
|
|
46
|
+
"@tada5hi/eslint-config-typescript": "^1.2.10",
|
|
46
47
|
"@tada5hi/semantic-release": "^0.3.1",
|
|
47
|
-
"@tada5hi/tsconfig": "^0.5.
|
|
48
|
+
"@tada5hi/tsconfig": "^0.5.1",
|
|
48
49
|
"@types/jest": "^29.5.12",
|
|
49
|
-
"@types/node": "^20.
|
|
50
|
+
"@types/node": "^20.14.2",
|
|
50
51
|
"@types/uuid": "^9.0.8",
|
|
51
52
|
"cross-env": "^7.0.3",
|
|
52
53
|
"envix": "^1.5.0",
|
|
@@ -54,8 +55,8 @@
|
|
|
54
55
|
"husky": "^8.0.3",
|
|
55
56
|
"jest": "^29.7.0",
|
|
56
57
|
"semantic-release": "^22.0.7",
|
|
57
|
-
"testcontainers": "^10.
|
|
58
|
-
"ts-jest": "^29.1.
|
|
59
|
-
"typescript": "^5.
|
|
58
|
+
"testcontainers": "^10.9.0",
|
|
59
|
+
"ts-jest": "^29.1.4",
|
|
60
|
+
"typescript": "^5.4.2"
|
|
60
61
|
}
|
|
61
62
|
}
|
package/dist/publish/utils.d.ts
DELETED
package/dist/publish/utils.js
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.buildDriverPublishOptions = void 0;
|
|
4
|
-
const utils_1 = require("../utils");
|
|
5
|
-
function buildDriverPublishOptions(options) {
|
|
6
|
-
return (0, utils_1.removeKeysFromOptions)({ ...options }, [
|
|
7
|
-
'exchange',
|
|
8
|
-
'queueName',
|
|
9
|
-
'content',
|
|
10
|
-
'id',
|
|
11
|
-
]);
|
|
12
|
-
}
|
|
13
|
-
exports.buildDriverPublishOptions = buildDriverPublishOptions;
|
|
File without changes
|
|
File without changes
|