event-message-broker
Version:
A Message Bus that uses AWS Stack and RabbitMQ
51 lines (50 loc) • 2.14 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { v4 as uuidV4 } from 'uuid';
export class RabbitMqInfrastructure {
constructor(channel) {
this.channel = channel;
}
createQueue(queueName) {
return __awaiter(this, void 0, void 0, function* () {
yield this.channel.assertQueue(queueName, { durable: true });
});
}
bindTopic(binder) {
return __awaiter(this, void 0, void 0, function* () {
yield this.channel.assertExchange(binder.TopicName, 'direct', {
durable: true,
arguments: {
'x-delayed-type': 'direct'
}
});
yield this.channel.bindQueue(binder.QueueName, binder.TopicName, '*');
});
}
bindTemporaryTopic(queueName) {
return __awaiter(this, void 0, void 0, function* () {
const topicName = `${uuidV4}-temporary-exchange`;
yield this.channel.assertExchange(topicName, 'direct', {
durable: true,
arguments: {
'x-delayed-type': 'direct'
}
});
yield this.channel.bindQueue(queueName, topicName, '*');
return topicName;
});
}
deleteTopic(queueName, topicName) {
return __awaiter(this, void 0, void 0, function* () {
this.channel.unbindQueue(queueName, topicName, '*');
yield this.channel.deleteExchange(topicName);
});
}
}