@vtexlab/planner-message-bus
Version:
A Message Bus that uses AWS SNS, AWS SQS, and AWS EventBridge
77 lines (76 loc) • 3.13 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 { QUEUE_ARN_TEMPLATE, QUEUE_URL_TEMPLATE, TOPIC_ARN_TEMPLATE } from '../utils/constants';
import { SQSClient, CreateQueueCommand, TagQueueCommand, SetQueueAttributesCommand } from '@aws-sdk/client-sqs';
const client = new SQSClient();
export function createSqsQueue(queueName) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const output = yield client.send(new CreateQueueCommand({ QueueName: queueName }));
return {
arn: QUEUE_ARN_TEMPLATE(queueName),
url: (_a = output.QueueUrl) !== null && _a !== void 0 ? _a : QUEUE_URL_TEMPLATE(queueName)
};
});
}
export function attachDqlToMainQueue(queueName) {
return __awaiter(this, void 0, void 0, function* () {
const redrivePolicy = {
deadLetterTargetArn: QUEUE_ARN_TEMPLATE(queueName + '-dlq'),
maxReceiveCount: 100
};
yield client.send(new SetQueueAttributesCommand({
QueueUrl: QUEUE_URL_TEMPLATE(queueName),
Attributes: { RedrivePolicy: JSON.stringify(redrivePolicy) }
}));
});
}
export function tagQueue(queueName, tags) {
return __awaiter(this, void 0, void 0, function* () {
const awsTags = tags.reduce((accumulator, current) => {
accumulator[current.Key] = current.Value;
return accumulator;
}, {});
yield client.send(new TagQueueCommand({
QueueUrl: QUEUE_URL_TEMPLATE(queueName),
Tags: awsTags
}));
});
}
export function setQueuePolicy(queueName, topicName) {
return __awaiter(this, void 0, void 0, function* () {
yield client.send(new SetQueueAttributesCommand({
QueueUrl: QUEUE_URL_TEMPLATE(queueName),
Attributes: {
Policy: getPolicyDocument(QUEUE_ARN_TEMPLATE(queueName), TOPIC_ARN_TEMPLATE(topicName))
}
}));
});
}
function getPolicyDocument(queueArn, topicArn) {
return JSON.stringify({
Version: '2012-10-17',
Statement: [
{
Effect: "Allow",
Principal: {
Service: "sns.amazonaws.com"
},
Action: "sqs:SendMessage",
Resource: queueArn,
Condition: {
ArnEquals: {
"aws:SourceArn": topicArn
}
}
}
]
});
}