Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ DATABASE_DELETE_MESSAGE=true
RABBITMQ_ENABLED=false
RABBITMQ_URI=amqp://localhost
RABBITMQ_EXCHANGE_NAME=evolution
RABBITMQ_FRAME_MAX=8192
# Global events - By enabling this variable, events from all instances are sent in the same event queue.
RABBITMQ_GLOBAL_ENABLED=false
# Prefix key to queue name
Expand Down
14 changes: 13 additions & 1 deletion src/api/integrations/event/rabbitmq/rabbitmq.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,21 @@

await new Promise<void>((resolve, reject) => {
const uri = configService.get<Rabbitmq>('RABBITMQ').URI;
const frameMax = configService.get<Rabbitmq>('RABBITMQ').FRAME_MAX;
const rabbitmqExchangeName = configService.get<Rabbitmq>('RABBITMQ').EXCHANGE_NAME;

amqp.connect(uri, (error, connection) => {
const url = new URL(uri);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Parsing the URI with new URL may throw on invalid URIs

Consider adding a try/catch around new URL(uri) or validating the URI beforehand to prevent unhandled exceptions from malformed input.

const connectionOptions = {
protocol: url.protocol.slice(0, -1),
hostname: url.hostname,
port: url.port || 5672,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Convert url.port to a number and adjust default port per protocol

url.port is a string, and the default port should be 5671 for amqps. Use port: url.port ? Number(url.port) : (url.protocol === 'amqps:' ? 5671 : 5672) to set the correct type and default value.

Suggested change
port: url.port || 5672,
port: url.port ? Number(url.port) : (url.protocol === 'amqps:' ? 5671 : 5672),

username: url.username || 'guest',
password: url.password || 'guest',
vhost: url.pathname.slice(1) || '/',

Check failure on line 34 in src/api/integrations/event/rabbitmq/rabbitmq.controller.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Delete `·`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Decode percent-encoded vhost names

Wrap url.pathname.slice(1) with decodeURIComponent() to ensure special characters in vhost names are properly decoded.

Suggested change
vhost: url.pathname.slice(1) || '/',
vhost: decodeURIComponent(url.pathname.slice(1)) || '/',

frameMax: frameMax

Check failure on line 35 in src/api/integrations/event/rabbitmq/rabbitmq.controller.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Insert `,`
};

amqp.connect(connectionOptions, (error, connection) => {
if (error) {
reject(error);

Expand Down
2 changes: 2 additions & 0 deletions src/config/env.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export type EventsRabbitmq = {
export type Rabbitmq = {
ENABLED: boolean;
URI: string;
FRAME_MAX: number;
EXCHANGE_NAME: string;
GLOBAL_ENABLED: boolean;
EVENTS: EventsRabbitmq;
Expand Down Expand Up @@ -359,6 +360,7 @@ export class ConfigService {
PREFIX_KEY: process.env?.RABBITMQ_PREFIX_KEY || 'evolution',
EXCHANGE_NAME: process.env?.RABBITMQ_EXCHANGE_NAME || 'evolution_exchange',
URI: process.env.RABBITMQ_URI || '',
FRAME_MAX: Number.parseInt(process.env.RABBITMQ_FRAME_MAX) || 8192,
EVENTS: {
APPLICATION_STARTUP: process.env?.RABBITMQ_EVENTS_APPLICATION_STARTUP === 'true',
INSTANCE_CREATE: process.env?.RABBITMQ_EVENTS_INSTANCE_CREATE === 'true',
Expand Down
Loading