@message-queue-toolkit/amqp
Version:
AMQP adapter for message-queue-toolkit
77 lines • 3.12 kB
JavaScript
import { isProduction } from '@message-queue-toolkit/core';
export async function checkQueueExists(connection, locatorConfig) {
// queue check breaks channel if not successful
const checkChannel = await connection.createChannel();
checkChannel.on('error', () => {
// it's OK
});
try {
await checkChannel.checkQueue(locatorConfig.queueName);
await checkChannel.close();
}
catch (_err) {
throw new Error(`Queue with queueName ${locatorConfig.queueName} does not exist.`);
}
}
export async function checkExchangeExists(connection, locatorConfig) {
// exchange check breaks channel if not successful
const checkChannel = await connection.createChannel();
checkChannel.on('error', () => {
// it's OK
});
try {
await checkChannel.checkExchange(locatorConfig.exchange);
await checkChannel.close();
}
catch (_err) {
throw new Error(`Exchange ${locatorConfig.exchange} does not exist.`);
}
}
export async function ensureAmqpQueue(connection, channel, creationConfig, locatorConfig) {
if (creationConfig) {
await channel.assertQueue(creationConfig.queueName, creationConfig.queueOptions);
}
else {
if (!locatorConfig) {
throw new Error('locatorConfig is mandatory when creationConfig is not set');
}
await checkQueueExists(connection, locatorConfig);
}
}
export async function ensureAmqpTopicSubscription(connection, channel, creationConfig, locatorConfig) {
await ensureAmqpQueue(connection, channel, creationConfig, locatorConfig);
if (creationConfig) {
await channel.assertExchange(creationConfig.exchange, 'topic');
await channel.bindQueue(creationConfig.queueName, creationConfig.exchange, creationConfig.topicPattern ?? '');
}
else {
if (!locatorConfig) {
throw new Error('locatorConfig is mandatory when creationConfig is not set');
}
await checkExchangeExists(connection, locatorConfig);
}
}
export async function ensureExchange(connection, channel, creationConfig, locatorConfig) {
if (creationConfig) {
await channel.assertExchange(creationConfig.exchange, 'topic');
}
else {
if (!locatorConfig) {
throw new Error('locatorConfig is mandatory when creationConfig is not set');
}
await checkExchangeExists(connection, locatorConfig);
}
}
export async function deleteAmqpQueue(channel, deletionConfig, creationConfig) {
if (!deletionConfig.deleteIfExists) {
return;
}
if (isProduction() && !deletionConfig.forceDeleteInProduction) {
throw new Error('You are running autodeletion in production. This can and probably will cause a loss of data. If you are absolutely sure you want to do this, please set deletionConfig.forceDeleteInProduction to true');
}
if (!creationConfig.queueName) {
throw new Error('QueueName must be set for automatic deletion');
}
await channel.deleteQueue(creationConfig.queueName);
}
//# sourceMappingURL=amqpQueueUtils.js.map