event-message-broker
Version:
A Message Bus that uses AWS Stack and RabbitMQ
137 lines (136 loc) • 6.83 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SQSInfrastructure = void 0;
const client_sqs_1 = require("@aws-sdk/client-sqs");
const constants_1 = require("../utils/constants");
const configurations_1 = require("../../application/utils/configurations");
class SQSInfrastructure {
constructor() {
this.client = new client_sqs_1.SQSClient();
}
create(queueName, span) {
return __awaiter(this, void 0, void 0, function* () {
span.setAttribute('queue', queueName);
const exists = yield this.check(queueName);
span.addEvent(exists ? 'queue-already-created' : 'queue-does-not-exists');
if (!exists) {
const queueCommand = new client_sqs_1.CreateQueueCommand({ QueueName: queueName });
const dlqCommand = new client_sqs_1.CreateQueueCommand({ QueueName: queueName + '-dlq' });
yield Promise.all([
() => __awaiter(this, void 0, void 0, function* () {
const output = yield this.client.send(queueCommand);
span.addEvent('queue-created', { url: output.QueueUrl });
}),
() => __awaiter(this, void 0, void 0, function* () {
const output = yield this.client.send(dlqCommand);
span.addEvent('dlq-created', { url: output.QueueUrl });
})
]);
yield Promise.all([
() => __awaiter(this, void 0, void 0, function* () {
const tags = yield this.tag(queueName);
span.addEvent('queue-tags-attached', { tags: JSON.stringify(tags) });
}),
() => __awaiter(this, void 0, void 0, function* () {
const tags = yield this.tag(queueName + '-dlq');
span.addEvent('dlq-tags-attached', { tags: JSON.stringify(tags) });
})
]);
const policy = yield this.createDlqPolicy(queueName);
span.addEvent('dlq-policy-created', { policy: JSON.stringify(policy) });
}
return {
QueueArn: (0, constants_1.QUEUE_ARN_TEMPLATE)(queueName),
QueueUrl: (0, constants_1.QUEUE_URL_TEMPLATE)(queueName),
Created: !exists
};
});
}
linkTopicQueuePolicy(queueName, topicName) {
return __awaiter(this, void 0, void 0, function* () {
const queueUrl = (0, constants_1.QUEUE_URL_TEMPLATE)(queueName);
const queueArn = (0, constants_1.QUEUE_ARN_TEMPLATE)(queueName);
const topicArn = (0, constants_1.TOPIC_ARN_TEMPLATE)(topicName);
const policy = (yield Promise.resolve().then(() => __importStar(require('./policies.json')))).linkTopicQueuePolicy;
policy.Statement[0].Resource = queueArn;
policy.Statement[0].Condition.ArnEquals["aws:SourceArn"] = topicArn;
const command = new client_sqs_1.SetQueueAttributesCommand({
QueueUrl: queueUrl,
Attributes: {
Policy: JSON.stringify(policy)
}
});
yield this.client.send(command);
return policy;
});
}
createDlqPolicy(queueName) {
return __awaiter(this, void 0, void 0, function* () {
const policy = (yield Promise.resolve().then(() => __importStar(require('./policies.json')))).redrivePolicy;
policy.deadLetterTargetAr = (0, constants_1.QUEUE_ARN_TEMPLATE)(queueName + '-dlq');
const command = new client_sqs_1.SetQueueAttributesCommand({
QueueUrl: (0, constants_1.QUEUE_URL_TEMPLATE)(queueName),
Attributes: { RedrivePolicy: JSON.stringify(policy) }
});
yield this.client.send(command);
return policy;
});
}
tag(queueName) {
return __awaiter(this, void 0, void 0, function* () {
if (!configurations_1.Configuration.tags || configurations_1.Configuration.tags.length < 1) {
return;
}
const formatedTags = configurations_1.Configuration.tags.reduce((accumulator, current) => {
accumulator[current.Key] = current.Value;
return accumulator;
}, {});
const command = new client_sqs_1.TagQueueCommand({
QueueUrl: (0, constants_1.QUEUE_URL_TEMPLATE)(queueName),
Tags: formatedTags
});
yield this.client.send(command);
return configurations_1.Configuration.tags;
});
}
check(queueName) {
return __awaiter(this, void 0, void 0, function* () {
const command = new client_sqs_1.ListQueuesCommand({ QueueNamePrefix: queueName });
const output = yield this.client.send(command);
return output.QueueUrls ? true : false;
});
}
}
exports.SQSInfrastructure = SQSInfrastructure;