event-message-broker
Version:
A Message Bus that uses AWS Stack and RabbitMQ
110 lines (109 loc) • 5.39 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 { SQSClient, SetQueueAttributesCommand, ListQueuesCommand, CreateQueueCommand, TagQueueCommand } from "@aws-sdk/client-sqs";
import { QUEUE_ARN_TEMPLATE, QUEUE_URL_TEMPLATE, TOPIC_ARN_TEMPLATE } from "../utils/constants";
import { Configuration } from "../../application/utils/configurations";
export class SQSInfrastructure {
constructor() {
this.client = new 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 CreateQueueCommand({ QueueName: queueName });
const dlqCommand = new 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: QUEUE_ARN_TEMPLATE(queueName),
QueueUrl: QUEUE_URL_TEMPLATE(queueName),
Created: !exists
};
});
}
linkTopicQueuePolicy(queueName, topicName) {
return __awaiter(this, void 0, void 0, function* () {
const queueUrl = QUEUE_URL_TEMPLATE(queueName);
const queueArn = QUEUE_ARN_TEMPLATE(queueName);
const topicArn = TOPIC_ARN_TEMPLATE(topicName);
const policy = (yield import('./policies.json')).linkTopicQueuePolicy;
policy.Statement[0].Resource = queueArn;
policy.Statement[0].Condition.ArnEquals["aws:SourceArn"] = topicArn;
const command = new 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 import('./policies.json')).redrivePolicy;
policy.deadLetterTargetAr = QUEUE_ARN_TEMPLATE(queueName + '-dlq');
const command = new SetQueueAttributesCommand({
QueueUrl: 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 (!Configuration.tags || Configuration.tags.length < 1) {
return;
}
const formatedTags = Configuration.tags.reduce((accumulator, current) => {
accumulator[current.Key] = current.Value;
return accumulator;
}, {});
const command = new TagQueueCommand({
QueueUrl: QUEUE_URL_TEMPLATE(queueName),
Tags: formatedTags
});
yield this.client.send(command);
return Configuration.tags;
});
}
check(queueName) {
return __awaiter(this, void 0, void 0, function* () {
const command = new ListQueuesCommand({ QueueNamePrefix: queueName });
const output = yield this.client.send(command);
return output.QueueUrls ? true : false;
});
}
}