amqp-extension 2.0.2 → 3.0.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 CHANGED
@@ -1,3 +1,20 @@
1
+ # [3.0.0](https://github.com/Tada5hi/amqp-extension/compare/v2.0.2...v3.0.0) (2024-02-28)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **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))
7
+
8
+
9
+ ### Features
10
+
11
+ * bind consume & publish to client class & remove singleton patterns ([9160408](https://github.com/Tada5hi/amqp-extension/commit/9160408038b1d48210735c843674817bb0ac45c6))
12
+
13
+
14
+ ### BREAKING CHANGES
15
+
16
+ * refactor public api
17
+
1
18
  ## [2.0.2](https://github.com/Tada5hi/amqp-extension/compare/v2.0.1...v2.0.2) (2024-02-28)
2
19
 
3
20
 
package/README.MD CHANGED
@@ -2,12 +2,11 @@
2
2
 
3
3
  [![npm version](https://badge.fury.io/js/amqp-extension.svg)](https://badge.fury.io/js/amqp-extension)
4
4
  [![codecov](https://codecov.io/gh/Tada5hi/amqp-extension/branch/master/graph/badge.svg?token=6YELWNP9HG)](https://codecov.io/gh/Tada5hi/amqp-extension)
5
- [![Master Workflow](https://github.com/Tada5hi/amqp-extension/workflows/main/badge.svg)](https://github.com/Tada5hi/amqp-extension)
5
+ [![Master Workflow](https://github.com/tada5hi/amqp-extension/actions/workflows/main.yml/badge.svg)](https://github.com/Tada5hi/amqp-extension)
6
6
  [![Known Vulnerabilities](https://snyk.io/test/github/Tada5hi/amqp-extension/badge.svg?targetFile=package.json)](https://snyk.io/test/github/Tada5hi/amqp-extension?targetFile=package.json)
7
7
  [![semantic-release: angular](https://img.shields.io/badge/semantic--release-angular-e10079?logo=semantic-release)](https://github.com/semantic-release/semantic-release)
8
8
 
9
- This is a library on top of the famous [amqplib](https://www.npmjs.com/package/amqplib) library and defines a [message format](#message-types) for queue messages through a message broker across multiple standalone services.
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` function allows you to send messages quickly.
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
- // This will set the default connection :)
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
- consume,
66
+ Client,
75
67
  ConsumeMessage,
76
68
  ConsumeOptions,
77
- setConfig
78
69
  } from "amqp-extension";
79
70
 
80
- // This will set the default connection :)
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
- publish
314
- } from "amqp-extension";
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
- consume,
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,
@@ -1,3 +1,3 @@
1
1
  export * from './type';
2
2
  export * from './module';
3
- export * from './utils';
3
+ export * from './module';
@@ -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("./utils"), exports);
25
+ __exportStar(require("./module"), exports);
@@ -1,4 +1,2 @@
1
1
  import type { Config, ConfigInput } from './type';
2
- export declare function setConfig(key: string | ConfigInput, value?: ConfigInput): Config;
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;
@@ -1,44 +1,22 @@
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.getConfig = exports.hasConfig = exports.setConfig = void 0;
10
- const utils_1 = require("./utils");
11
- const instances = {};
12
- function setConfig(key, value) {
13
- if (typeof key === 'string') {
14
- if (typeof value === 'undefined') {
15
- throw new Error(`A config must be defined for the alias: ${key}`);
16
- }
17
- value.alias = key;
18
- instances[key] = (0, utils_1.extendConfig)(value);
19
- return instances[key];
20
- }
21
- key.alias = (0, utils_1.getConfigKey)(key.alias);
22
- instances[key.alias] = (0, utils_1.extendConfig)(key);
23
- return instances[key.alias];
9
+ exports.buildConfig = void 0;
10
+ const exchange_1 = require("../exchange");
11
+ function buildConfig(input) {
12
+ return {
13
+ connection: input.connection,
14
+ publish: input.publish || {},
15
+ consume: input.consume || {},
16
+ exchange: input.exchange || {
17
+ name: '',
18
+ type: exchange_1.ExchangeType.DEFAULT,
19
+ },
20
+ };
24
21
  }
25
- exports.setConfig = setConfig;
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;
22
+ exports.buildConfig = buildConfig;
@@ -2,7 +2,6 @@ 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
- alias: string;
6
5
  connection: Options.Connect | string;
7
6
  exchange: ExchangeOptions;
8
7
  publish: PublishOptions;
@@ -1,3 +1,3 @@
1
1
  export * from './static';
2
- export * from './module';
3
2
  export * from './type';
3
+ export * from './utils';
@@ -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/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from './config';
2
2
  export * from './consume';
3
- export * from './connection';
4
3
  export * from './exchange';
4
+ export * from './module';
5
5
  export * from './publish';
6
6
  export * from './type';
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);
@@ -0,0 +1,13 @@
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
+ export declare class Client {
7
+ protected connection: Connection | undefined;
8
+ protected config: Config;
9
+ constructor(options: ConfigInput);
10
+ useConnection(): Promise<Connection>;
11
+ consume(options: ConsumeOptions, handlers: ConsumeHandlers): Promise<void>;
12
+ publish(options: PublishOptionsExtended): Promise<void>;
13
+ }
package/dist/module.js ADDED
@@ -0,0 +1,131 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.Client = void 0;
10
+ const amqplib_1 = require("amqplib");
11
+ const smob_1 = require("smob");
12
+ const uuid_1 = require("uuid");
13
+ const config_1 = require("./config");
14
+ const consume_1 = require("./consume");
15
+ const exchange_1 = require("./exchange");
16
+ const publish_1 = require("./publish");
17
+ class Client {
18
+ constructor(options) {
19
+ this.config = (0, config_1.buildConfig)(options);
20
+ }
21
+ async useConnection() {
22
+ if (typeof this.connection !== 'undefined') {
23
+ return this.connection;
24
+ }
25
+ this.connection = await (0, amqplib_1.connect)(this.config.connection);
26
+ return this.connection;
27
+ }
28
+ async consume(options, handlers) {
29
+ const connection = await this.useConnection();
30
+ const channel = await connection.createChannel();
31
+ options = (0, smob_1.merge)(options, this.config.consume);
32
+ const exchangeOptions = (0, smob_1.merge)(options.exchange || {}, this.config.exchange);
33
+ let queueName = options.queueName || this.config.publish.queueName || '';
34
+ if ((0, exchange_1.isDefaultExchange)(exchangeOptions.type)) {
35
+ if (queueName === '') {
36
+ throw new Error('The queue name can not be empty if a non default exchange is selected.');
37
+ }
38
+ await channel.assertQueue(queueName, {
39
+ durable: true,
40
+ });
41
+ }
42
+ else {
43
+ await channel.assertExchange(exchangeOptions.name, exchangeOptions.type, (0, exchange_1.buildDriverExchangeOptions)({
44
+ durable: true,
45
+ ...exchangeOptions,
46
+ }));
47
+ const assertionQueue = await channel.assertQueue('', {
48
+ durable: false,
49
+ autoDelete: true,
50
+ exclusive: true,
51
+ });
52
+ if (typeof exchangeOptions.routingKey === 'undefined') {
53
+ throw new Error('The routingKey can not be empty if a non default exchange is selected.');
54
+ }
55
+ await channel.bindQueue(assertionQueue.queue, this.config.exchange.name, exchangeOptions.routingKey);
56
+ queueName = assertionQueue.queue;
57
+ }
58
+ if (typeof options.prefetchCount !== 'undefined') {
59
+ await channel.prefetch(options.prefetchCount);
60
+ }
61
+ const handleMessage = async (message) => {
62
+ if (!message) {
63
+ return;
64
+ }
65
+ const handler = handlers[message.properties.type] ||
66
+ handlers[consume_1.ConsumeHandlerAnyKey];
67
+ const requeueOnFailure = this.config.consume.requeueOnFailure ?? false;
68
+ if (typeof handler === 'undefined') {
69
+ channel.reject(message, requeueOnFailure);
70
+ return;
71
+ }
72
+ try {
73
+ await handler(message, channel);
74
+ channel.ack(message);
75
+ }
76
+ catch (e) {
77
+ channel.reject(message, requeueOnFailure);
78
+ }
79
+ };
80
+ await channel.consume(queueName, (message) => handleMessage(message), (0, consume_1.buildDriverConsumeOptions)(options));
81
+ }
82
+ async publish(options) {
83
+ let buffer;
84
+ if (Buffer.isBuffer(options.content)) {
85
+ buffer = options.content;
86
+ }
87
+ else if (typeof options.content === 'string') {
88
+ buffer = Buffer.from(options.content, 'utf-8');
89
+ }
90
+ else {
91
+ buffer = Buffer.from(JSON.stringify(options.content), 'utf-8');
92
+ }
93
+ const connection = await this.useConnection();
94
+ const channel = await connection.createChannel();
95
+ options = (0, smob_1.merge)(options, this.config.publish || {});
96
+ const exchangeOptions = (0, smob_1.merge)({}, options.exchange || {}, this.config.exchange);
97
+ if (!(0, exchange_1.isDefaultExchange)(exchangeOptions.type)) {
98
+ await channel.assertExchange(exchangeOptions.name, exchangeOptions.type, (0, exchange_1.buildDriverExchangeOptions)({
99
+ durable: true,
100
+ ...exchangeOptions,
101
+ }));
102
+ }
103
+ if (typeof options.messageId === 'undefined') {
104
+ options.messageId = options.id || (0, uuid_1.v4)();
105
+ }
106
+ if (!(0, exchange_1.isDefaultExchange)(exchangeOptions.type)) {
107
+ if (typeof exchangeOptions.routingKey === 'undefined') {
108
+ throw new Error('The routingKey can not be empty if a non default exchange is selected.');
109
+ }
110
+ channel.publish(this.config.exchange.name, exchangeOptions.routingKey, buffer, (0, publish_1.buildDriverPublishOptions)({
111
+ persistent: true,
112
+ ...options,
113
+ }));
114
+ return;
115
+ }
116
+ // publish to default exchange
117
+ const queueName = options.queueName ||
118
+ this.config.publish.queueName;
119
+ if (typeof queueName === 'undefined' || queueName === '') {
120
+ throw new Error('The queue name can not be empty if a non default exchange is selected.');
121
+ }
122
+ await channel.assertQueue(queueName, {
123
+ durable: true,
124
+ });
125
+ channel.sendToQueue(queueName, buffer, (0, publish_1.buildDriverPublishOptions)({
126
+ persistent: true,
127
+ ...options,
128
+ }));
129
+ }
130
+ }
131
+ exports.Client = Client;
@@ -1,2 +1,2 @@
1
- export * from './module';
2
1
  export * from './type';
2
+ export * from './utils';
@@ -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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "amqp-extension",
3
- "version": "2.0.2",
3
+ "version": "3.0.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",
@@ -35,13 +35,13 @@
35
35
  },
36
36
  "homepage": "https://github.com/Tada5hi/amqp-extension#readme",
37
37
  "dependencies": {
38
- "@types/amqplib": "^0.10.4",
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.3",
44
+ "@tada5hi/commitlint-config": "^1.1.7",
45
45
  "@tada5hi/eslint-config-typescript": "^1.2.6",
46
46
  "@tada5hi/semantic-release": "^0.3.0",
47
47
  "@tada5hi/tsconfig": "^0.5.0",
@@ -49,11 +49,11 @@
49
49
  "@types/node": "^20.8.10",
50
50
  "@types/uuid": "^9.0.8",
51
51
  "cross-env": "^7.0.3",
52
- "eslint": "^8.53.0",
52
+ "eslint": "^8.57.0",
53
53
  "husky": "^8.0.3",
54
54
  "jest": "^29.7.0",
55
55
  "semantic-release": "^22.0.7",
56
- "ts-jest": "^29.1.1",
56
+ "ts-jest": "^29.1.2",
57
57
  "typescript": "^5.2.2"
58
58
  }
59
59
  }
@@ -1,3 +0,0 @@
1
- import type { Config, ConfigInput } from './type';
2
- export declare function getConfigKey(alias?: string): string;
3
- export declare function extendConfig(input: ConfigInput): Config;
@@ -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';
@@ -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;
@@ -1,3 +0,0 @@
1
- import type { ConsumeOptions } from '../type';
2
- import type { ConsumeHandlers } from './type';
3
- export declare function consume(options: ConsumeOptions, handlers: ConsumeHandlers): Promise<void>;
@@ -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;
@@ -1,2 +0,0 @@
1
- import type { PublishOptionsExtended } from './type';
2
- export declare function publish(options: PublishOptionsExtended): Promise<void>;
@@ -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;