@xnestjs/rabbitmq 1.10.4 → 1.11.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/README.md CHANGED
@@ -72,19 +72,21 @@ configuration. By default, variables are prefixed with `RMQ_`.
72
72
 
73
73
  <!--- BEGIN env --->
74
74
 
75
- | Environment Variable | Type | Default | Description |
76
- |--------------------------|-----------|---------|-----------------------------------------------------------------------------------------------------|
77
- | `RMQ_URLS` | String[]! | | A list of RabbitMQ server URLs to connect to. |
78
- | `RMQ_USERNAME` | String | | Username used for authenticating against the server |
79
- | `RMQ_PASSWORD` | String | | Password used for authenticating against the server |
80
- | `RMQ_HEARTBEAT_INTERVAL` | Number | `5` | Interval in seconds to send heartbeats to broker. Defaults to 5 seconds. |
81
- | `RMQ_RECONNECT_TIME` | Number | | The time to wait before trying to reconnect. If not specified, defaults to `RMQ_HEARTBEAT_INTERVAL` |
82
- | `RMQ_NO_DELAY` | Boolean | | when true sets TCP_NODELAY on the underlying socket. |
83
- | `RMQ_CONNECT_TIMEOUT` | Number | | Specifies the socket timeout in milliseconds while establishing the connection |
84
- | `RMQ_KEEP_ALIVE` | Boolean | | |
85
- | `RMQ_KEEP_ALIVE_DELAY` | Number | | |
86
- | `RMQ_CONNECTION_NAME` | String | | Name of the connection. (Used for debugging) |
87
- | `RMQ_LAZY_CONNECT` | Boolean | `false` | If true, defers connecting to RabbitMQ until a message is |
75
+ | Environment Variable | Type | Default | Description |
76
+ |--------------------------|-----------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
77
+ | `RMQ_URLS` | String[]! | | A list of RabbitMQ server URLs to connect to. |
78
+ | `RMQ_USERNAME` | String | | Username used for authenticating against the server |
79
+ | `RMQ_PASSWORD` | String | | Password used for authenticating against the server |
80
+ | `RMQ_ACQUIRE_TIMEOUT` | Number | `20000` | Milliseconds to wait before aborting a Channel creation attempt. |
81
+ | `RMQ_CONNECTION_NAME` | String | | Custom name for the connection, visible in the server's management UI |
82
+ | `RMQ_CONNECTION_TIMEOUT` | Number | `10000` | Max wait time, in milliseconds, for a connection attempt |
83
+ | `RMQ_FRAME_MAX` | Number | `8192` | Max size, in bytes, of AMQP data frames. Protocol max is 2^32-1. Actual value is negotiated with the server. |
84
+ | `RMQ_HEARTBEAT_INTERVAL` | Number | `60` | Period of time, in seconds, after which the TCP connection should be considered unreachable. Server may have its own max value, in which case the lowest of the two is used. A value of 0 will disable this feature. Heartbeats are sent every heartbeat / 2 seconds, so two missed heartbeats means the connection is dead. |
85
+ | `RMQ_MAX_CHANNELS` | Number | `2047` | Maximum active AMQP channels. 65535 is the protocol max. The server may also have a max value, in which case the lowest of the two is used. |
86
+ | `RMQ_RETRY_HIGH` | Number | `30000` | Max delay, in milliseconds, for exponential-backoff when reconnecting |
87
+ | `RMQ_RETRY_LOW` | Number | `1000` | Step size, in milliseconds, for exponential-backoff when reconnecting |
88
+ | `RMQ_NO_DELAY` | Boolean | `false` | Disable Nagle's algorithm for reduced latency. Disabling Nagle’s algorithm will enable the application to have many small packets in flight on the network at once, instead of a smaller number of large packets, which may increase load on the network, and may or may not benefit the application performance. |
89
+ | `RMQ_LAZY_CONNECT` | Boolean | `false` | If true, defers connecting to RabbitMQ until a message is |
88
90
 
89
91
  <!--- END env --->
90
92
 
@@ -4,42 +4,33 @@ exports.getRabbitmqConfig = getRabbitmqConfig;
4
4
  const tslib_1 = require("tslib");
5
5
  const node_process_1 = tslib_1.__importDefault(require("node:process"));
6
6
  const objects_1 = require("@jsopen/objects");
7
- const amqplib_1 = tslib_1.__importDefault(require("amqplib"));
8
7
  const putil_varhelpers_1 = require("putil-varhelpers");
9
- function getRabbitmqConfig(moduleOptions, prefix = 'RMQ_') {
10
- const options = (0, objects_1.clone)(moduleOptions);
8
+ function getRabbitmqConfig(urls, prefix = 'RMQ_') {
11
9
  const env = node_process_1.default.env;
12
- options.urls =
13
- options.urls ||
14
- (env[prefix + 'URLS'] ?? 'amqp://localhost:5672').split(/\s*,\s*/);
15
- options.heartbeatIntervalInSeconds =
16
- options.heartbeatIntervalInSeconds ??
17
- (0, putil_varhelpers_1.toInt)(env[prefix + 'HEARTBEAT_INTERVAL']);
18
- options.reconnectTimeInSeconds =
19
- options.reconnectTimeInSeconds ??
20
- (0, putil_varhelpers_1.toInt)(env[prefix + 'MAX_CONNECTION_ATTEMPTS']);
21
- options.connectionOptions = options.connectionOptions || {};
22
- const username = env[prefix + 'USERNAME'];
23
- if (username) {
24
- options.connectionOptions.credentials = amqplib_1.default.credentials.plain(username, env[prefix + 'PASSWORD'] || '');
25
- }
26
- options.connectionOptions.noDelay =
27
- options.connectionOptions.noDelay ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'NO_DELAY']);
28
- options.connectionOptions.timeout =
29
- options.connectionOptions.timeout ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'CONNECT_TIMEOUT']);
30
- options.connectionOptions.keepAlive =
31
- options.connectionOptions.keepAlive ??
32
- (0, putil_varhelpers_1.toBoolean)(env[prefix + 'KEEP_ALIVE']);
33
- options.connectionOptions.keepAliveDelay =
34
- options.connectionOptions.keepAliveDelay ??
35
- (0, putil_varhelpers_1.toInt)(env[prefix + 'KEEP_ALIVE_DELAY']);
36
- if (env[prefix + 'CONNECTION_NAME']) {
37
- options.connectionOptions.clientProperties =
38
- options.connectionOptions.clientProperties || {};
39
- options.connectionOptions.clientProperties.connection_name =
40
- env[prefix + 'CONNECTION_NAME'];
10
+ const options = {};
11
+ if (Array.isArray(urls))
12
+ options.urls = urls;
13
+ else if (typeof urls === 'object') {
14
+ (0, objects_1.merge)(options, urls, { deep: true });
41
15
  }
16
+ else
17
+ options.urls = (urls ||
18
+ env[prefix + 'URLS'] ||
19
+ 'amqp://localhost:5672').split(/\s*,\s*/) || ['amqp://guest:guest@localhost:5672'];
20
+ options.username = options.username ?? env[prefix + 'USERNAME'];
21
+ options.password = options.password ?? env[prefix + 'PASSWORD'];
22
+ options.acquireTimeout =
23
+ options.acquireTimeout ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'ACQUIRE_TIMEOUT']);
24
+ options.connectionTimeout =
25
+ options.connectionTimeout ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'CONNECTION_TIMEOUT']);
26
+ options.frameMax = options.frameMax ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'FRAME_MAX']);
27
+ options.heartbeat =
28
+ options.heartbeat ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'HEARTBEAT_INTERVAL']);
29
+ options.maxChannels =
30
+ options.maxChannels ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'MAX_CHANNELS']);
31
+ options.retryHigh = options.retryHigh ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'RETRY_HIGH']);
32
+ options.retryLow = options.retryLow ?? (0, putil_varhelpers_1.toInt)(env[prefix + 'NO_DELAY']);
42
33
  options.lazyConnect =
43
34
  options.lazyConnect ?? (0, putil_varhelpers_1.toBoolean)(env[prefix + 'LAZY_CONNECT']);
44
- return options;
35
+ return (0, objects_1.omitNullish)(options);
45
36
  }
@@ -50,7 +50,11 @@ let RabbitmqCoreModule = RabbitmqCoreModule_1 = class RabbitmqCoreModule {
50
50
  const token = opts.token ?? types_js_1.RmqClient;
51
51
  // const name = typeof token === 'string' ? token : 'RabbitMQ';
52
52
  const logger = typeof opts.logger === 'string' ? new common_1.Logger(opts.logger) : opts.logger;
53
- const exports = [constants_js_1.RMQ_CONNECTION_OPTIONS, ...(metadata.exports ?? [])];
53
+ const exports = [
54
+ constants_js_1.RMQ_CONNECTION_OPTIONS,
55
+ token,
56
+ ...(metadata.exports ?? []),
57
+ ];
54
58
  const providers = [
55
59
  ...(metadata.providers ?? []),
56
60
  {
@@ -69,19 +73,13 @@ let RabbitmqCoreModule = RabbitmqCoreModule_1 = class RabbitmqCoreModule {
69
73
  provide: token,
70
74
  inject: [constants_js_1.RMQ_CONNECTION_OPTIONS],
71
75
  useFactory: async (connectionOptions) => {
72
- const client = new types_js_1.RmqClient(connectionOptions.urls, connectionOptions);
76
+ const client = new types_js_1.RmqClient(connectionOptions);
73
77
  if (logger) {
74
- client.on('connect', ({ url }) => {
75
- logger.log('RabbitMQ connected to ' + url);
76
- });
77
- client.on('connectFailed', ({ err }) => {
78
- logger.error('RabbitMQ connection failed: ' + err?.message);
78
+ client.on('connection', () => {
79
+ logger.log('RabbitMQ client connected');
79
80
  });
80
- client.on('disconnect', ({ err }) => {
81
- if (err)
82
- logger.error('RabbitMQ client disconnected upon error. ' + err?.message);
83
- else
84
- logger.log('RabbitMQ client disconnected.');
81
+ client.on('error', err => {
82
+ logger.error('RabbitMQ connection error: ' + err?.message);
85
83
  });
86
84
  }
87
85
  return client;
@@ -89,6 +87,7 @@ let RabbitmqCoreModule = RabbitmqCoreModule_1 = class RabbitmqCoreModule {
89
87
  },
90
88
  ];
91
89
  return {
90
+ global: opts.global,
92
91
  module: RabbitmqCoreModule_1,
93
92
  providers,
94
93
  exports,
@@ -109,7 +108,7 @@ let RabbitmqCoreModule = RabbitmqCoreModule_1 = class RabbitmqCoreModule {
109
108
  return;
110
109
  this.logger?.log('Connecting to RabbitMQ at ' + ansi_colors_1.default.blue(options.urls.toString()));
111
110
  common_1.Logger.flush();
112
- await this.client.connect();
111
+ await this.client.onConnect();
113
112
  }
114
113
  onApplicationShutdown() {
115
114
  return this.client.close();
package/cjs/types.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.RmqClient = void 0;
4
- const amqp_connection_manager_1 = require("amqp-connection-manager");
5
- exports.RmqClient = amqp_connection_manager_1.AmqpConnectionManagerClass;
4
+ const tslib_1 = require("tslib");
5
+ const rabbit = tslib_1.__importStar(require("rabbitmq-client"));
6
+ exports.RmqClient = rabbit.Connection;
@@ -1,41 +1,32 @@
1
1
  import process from 'node:process';
2
- import { clone } from '@jsopen/objects';
3
- import amqplib from 'amqplib';
2
+ import { merge, omitNullish } from '@jsopen/objects';
4
3
  import { toBoolean, toInt } from 'putil-varhelpers';
5
- export function getRabbitmqConfig(moduleOptions, prefix = 'RMQ_') {
6
- const options = clone(moduleOptions);
4
+ export function getRabbitmqConfig(urls, prefix = 'RMQ_') {
7
5
  const env = process.env;
8
- options.urls =
9
- options.urls ||
10
- (env[prefix + 'URLS'] ?? 'amqp://localhost:5672').split(/\s*,\s*/);
11
- options.heartbeatIntervalInSeconds =
12
- options.heartbeatIntervalInSeconds ??
13
- toInt(env[prefix + 'HEARTBEAT_INTERVAL']);
14
- options.reconnectTimeInSeconds =
15
- options.reconnectTimeInSeconds ??
16
- toInt(env[prefix + 'MAX_CONNECTION_ATTEMPTS']);
17
- options.connectionOptions = options.connectionOptions || {};
18
- const username = env[prefix + 'USERNAME'];
19
- if (username) {
20
- options.connectionOptions.credentials = amqplib.credentials.plain(username, env[prefix + 'PASSWORD'] || '');
21
- }
22
- options.connectionOptions.noDelay =
23
- options.connectionOptions.noDelay ?? toBoolean(env[prefix + 'NO_DELAY']);
24
- options.connectionOptions.timeout =
25
- options.connectionOptions.timeout ?? toInt(env[prefix + 'CONNECT_TIMEOUT']);
26
- options.connectionOptions.keepAlive =
27
- options.connectionOptions.keepAlive ??
28
- toBoolean(env[prefix + 'KEEP_ALIVE']);
29
- options.connectionOptions.keepAliveDelay =
30
- options.connectionOptions.keepAliveDelay ??
31
- toInt(env[prefix + 'KEEP_ALIVE_DELAY']);
32
- if (env[prefix + 'CONNECTION_NAME']) {
33
- options.connectionOptions.clientProperties =
34
- options.connectionOptions.clientProperties || {};
35
- options.connectionOptions.clientProperties.connection_name =
36
- env[prefix + 'CONNECTION_NAME'];
6
+ const options = {};
7
+ if (Array.isArray(urls))
8
+ options.urls = urls;
9
+ else if (typeof urls === 'object') {
10
+ merge(options, urls, { deep: true });
37
11
  }
12
+ else
13
+ options.urls = (urls ||
14
+ env[prefix + 'URLS'] ||
15
+ 'amqp://localhost:5672').split(/\s*,\s*/) || ['amqp://guest:guest@localhost:5672'];
16
+ options.username = options.username ?? env[prefix + 'USERNAME'];
17
+ options.password = options.password ?? env[prefix + 'PASSWORD'];
18
+ options.acquireTimeout =
19
+ options.acquireTimeout ?? toInt(env[prefix + 'ACQUIRE_TIMEOUT']);
20
+ options.connectionTimeout =
21
+ options.connectionTimeout ?? toInt(env[prefix + 'CONNECTION_TIMEOUT']);
22
+ options.frameMax = options.frameMax ?? toInt(env[prefix + 'FRAME_MAX']);
23
+ options.heartbeat =
24
+ options.heartbeat ?? toInt(env[prefix + 'HEARTBEAT_INTERVAL']);
25
+ options.maxChannels =
26
+ options.maxChannels ?? toInt(env[prefix + 'MAX_CHANNELS']);
27
+ options.retryHigh = options.retryHigh ?? toInt(env[prefix + 'RETRY_HIGH']);
28
+ options.retryLow = options.retryLow ?? toInt(env[prefix + 'NO_DELAY']);
38
29
  options.lazyConnect =
39
30
  options.lazyConnect ?? toBoolean(env[prefix + 'LAZY_CONNECT']);
40
- return options;
31
+ return omitNullish(options);
41
32
  }
@@ -47,7 +47,11 @@ let RabbitmqCoreModule = RabbitmqCoreModule_1 = class RabbitmqCoreModule {
47
47
  const token = opts.token ?? RmqClient;
48
48
  // const name = typeof token === 'string' ? token : 'RabbitMQ';
49
49
  const logger = typeof opts.logger === 'string' ? new Logger(opts.logger) : opts.logger;
50
- const exports = [RMQ_CONNECTION_OPTIONS, ...(metadata.exports ?? [])];
50
+ const exports = [
51
+ RMQ_CONNECTION_OPTIONS,
52
+ token,
53
+ ...(metadata.exports ?? []),
54
+ ];
51
55
  const providers = [
52
56
  ...(metadata.providers ?? []),
53
57
  {
@@ -66,19 +70,13 @@ let RabbitmqCoreModule = RabbitmqCoreModule_1 = class RabbitmqCoreModule {
66
70
  provide: token,
67
71
  inject: [RMQ_CONNECTION_OPTIONS],
68
72
  useFactory: async (connectionOptions) => {
69
- const client = new RmqClient(connectionOptions.urls, connectionOptions);
73
+ const client = new RmqClient(connectionOptions);
70
74
  if (logger) {
71
- client.on('connect', ({ url }) => {
72
- logger.log('RabbitMQ connected to ' + url);
73
- });
74
- client.on('connectFailed', ({ err }) => {
75
- logger.error('RabbitMQ connection failed: ' + err?.message);
75
+ client.on('connection', () => {
76
+ logger.log('RabbitMQ client connected');
76
77
  });
77
- client.on('disconnect', ({ err }) => {
78
- if (err)
79
- logger.error('RabbitMQ client disconnected upon error. ' + err?.message);
80
- else
81
- logger.log('RabbitMQ client disconnected.');
78
+ client.on('error', err => {
79
+ logger.error('RabbitMQ connection error: ' + err?.message);
82
80
  });
83
81
  }
84
82
  return client;
@@ -86,6 +84,7 @@ let RabbitmqCoreModule = RabbitmqCoreModule_1 = class RabbitmqCoreModule {
86
84
  },
87
85
  ];
88
86
  return {
87
+ global: opts.global,
89
88
  module: RabbitmqCoreModule_1,
90
89
  providers,
91
90
  exports,
@@ -106,7 +105,7 @@ let RabbitmqCoreModule = RabbitmqCoreModule_1 = class RabbitmqCoreModule {
106
105
  return;
107
106
  this.logger?.log('Connecting to RabbitMQ at ' + colors.blue(options.urls.toString()));
108
107
  Logger.flush();
109
- await this.client.connect();
108
+ await this.client.onConnect();
110
109
  }
111
110
  onApplicationShutdown() {
112
111
  return this.client.close();
package/esm/types.js CHANGED
@@ -1,2 +1,2 @@
1
- import { AmqpConnectionManagerClass, } from 'amqp-connection-manager';
2
- export const RmqClient = AmqpConnectionManagerClass;
1
+ import * as rabbit from 'rabbitmq-client';
2
+ export const RmqClient = rabbit.Connection;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xnestjs/rabbitmq",
3
- "version": "1.10.4",
3
+ "version": "1.11.1",
4
4
  "description": "NestJS extension library for RabbitMQ",
5
5
  "author": "Panates",
6
6
  "license": "MIT",
@@ -14,8 +14,7 @@
14
14
  "@nestjs/common": "^10.0.0 || ^11.0.0",
15
15
  "@nestjs/core": "^10.0.0 || ^11.0.0",
16
16
  "@nestjs/microservices": "^10.0.0 || ^11.0.0",
17
- "amqp-connection-manager": "*",
18
- "amqplib": "*"
17
+ "rabbitmq-client": ">=5.0.0 <6"
19
18
  },
20
19
  "type": "module",
21
20
  "exports": {
@@ -1,2 +1,2 @@
1
1
  import type { RabbitmqConnectionOptions } from './types';
2
- export declare function getRabbitmqConfig(moduleOptions: Partial<RabbitmqConnectionOptions>, prefix?: string): RabbitmqConnectionOptions;
2
+ export declare function getRabbitmqConfig(urls: string | string[] | Partial<RabbitmqConnectionOptions>, prefix?: string): RabbitmqConnectionOptions;
package/types/types.d.ts CHANGED
@@ -1,20 +1,19 @@
1
1
  import type { LoggerService } from '@nestjs/common';
2
2
  import type { ModuleMetadata } from '@nestjs/common/interfaces';
3
3
  import type { InjectionToken } from '@nestjs/common/interfaces/modules/injection-token.interface';
4
- import { type AmqpConnectionManager, AmqpConnectionManagerClass, type AmqpConnectionManagerOptions, type Channel, type ChannelWrapper, type ConnectionUrl, type CreateChannelOpts, type SetupFunc } from 'amqp-connection-manager';
5
- export type RmqClient = AmqpConnectionManager;
6
- export declare const RmqClient: typeof AmqpConnectionManagerClass;
7
- export { AmqpConnectionManagerOptions, Channel, ChannelWrapper, ConnectionUrl, CreateChannelOpts, SetupFunc, };
8
- export interface RabbitmqConnectionOptions extends AmqpConnectionManagerOptions {
9
- urls?: ConnectionUrl[];
4
+ import * as rabbit from 'rabbitmq-client';
5
+ export type RmqClient = rabbit.Connection;
6
+ export declare const RmqClient: typeof rabbit.Connection;
7
+ export interface RabbitmqConnectionOptions extends Pick<rabbit.ConnectionOptions, 'username' | 'password' | 'acquireTimeout' | 'connectionName' | 'connectionTimeout' | 'frameMax' | 'heartbeat' | 'maxChannels' | 'retryHigh' | 'retryLow' | 'noDelay' | 'tls' | 'socket'> {
8
+ urls?: string[];
10
9
  lazyConnect?: boolean;
11
10
  }
12
11
  export interface RabbitmqModuleOptions extends BaseModuleOptions {
13
- useValue?: RabbitmqConnectionOptions;
12
+ useValue?: string | string[] | RabbitmqConnectionOptions;
14
13
  }
15
14
  export interface RabbitmqModuleAsyncOptions extends BaseModuleOptions, Pick<ModuleMetadata, 'imports'> {
16
15
  inject?: any[];
17
- useFactory: (...args: any[]) => Promise<RabbitmqConnectionOptions> | RabbitmqConnectionOptions;
16
+ useFactory: (...args: any[]) => Promise<string | string[] | RabbitmqConnectionOptions> | string | string[] | RabbitmqConnectionOptions;
18
17
  }
19
18
  interface BaseModuleOptions {
20
19
  token?: InjectionToken;
@@ -22,3 +21,4 @@ interface BaseModuleOptions {
22
21
  logger?: LoggerService | string;
23
22
  global?: boolean;
24
23
  }
24
+ export {};