@rewaa/event-broker
Version:
A broker for all the events that Rewaa will ever produce or consume
107 lines • 4.3 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SNSProducer = void 0;
const client_sns_1 = require("@aws-sdk/client-sns");
const uuid_1 = require("uuid");
const constants_1 = require("../constants");
class SNSProducer {
constructor(logger, config) {
this.logger = logger;
this.send = async (topicArn, message) => {
return await this.sns.publish(this.getPublishInput(topicArn, message));
};
this.sendBatch = async (topicArn, messages) => {
return await this.sns.publishBatch(this.getBatchPublishInput(topicArn, messages));
};
this.createTopic = async (topicName, attributes) => {
const params = {
Name: topicName,
Attributes: attributes,
};
if (this.isFifoTopic(topicName) && params.Attributes) {
params.Attributes.FifoTopic = "true";
}
try {
const { TopicArn } = await this.sns.createTopic(params);
return TopicArn;
}
catch (error) {
this.logger.error(`Topic creation failed: ${topicName}`);
throw error;
}
};
this.subscribeToTopic = async (topicArn, queueArn, filterPolicy, deliverRawMessage) => {
const params = {
TopicArn: topicArn,
Protocol: "sqs",
Endpoint: queueArn,
Attributes: {}
};
if (params.Attributes) {
if (filterPolicy) {
params.Attributes.FilterPolicy = JSON.stringify(filterPolicy);
}
}
try {
const response = await this.sns.subscribe(params);
/**
* Always setting attributes after creating subscribtion
* This is done to avoid having to filter subscriptions to check
* if they exist
*/
if (response.SubscriptionArn) {
if (deliverRawMessage) {
await this.sns.setSubscriptionAttributes({
SubscriptionArn: response.SubscriptionArn,
AttributeName: 'RawMessageDelivery',
AttributeValue: 'false'
});
}
}
return response;
}
catch (error) {
this.logger.error(`Topic subscription failed: ${queueArn} to ${topicArn}`);
throw error;
}
};
this.isFifoTopic = (topicArn) => topicArn.includes(".fifo");
this.sns = new client_sns_1.SNS(config);
}
get client() {
return this.sns;
}
getPublishInput(topicArn, message) {
const params = {
Message: JSON.stringify(message),
TargetArn: topicArn,
};
if (this.isFifoTopic(topicArn)) {
params.MessageDeduplicationId = message.deduplicationId || (0, uuid_1.v4)();
params.MessageGroupId = message.messageGroupId;
}
params.MessageAttributes = this.getMessageAttributesForPublish(message.messageAttributes);
return params;
}
getMessageAttributesForPublish(messageAttributes) {
return Object.assign(Object.assign({}, messageAttributes), { PayloadVersion: {
DataType: "String",
StringValue: constants_1.PAYLOAD_STRUCTURE_VERSION_V1,
} });
}
getBatchPublishInput(topicArn, messages) {
const isFifo = this.isFifoTopic(topicArn);
const params = {
TopicArn: topicArn,
PublishBatchRequestEntries: messages.map((message) => {
return Object.assign({ Id: message.id, Message: JSON.stringify(message), MessageAttributes: this.getMessageAttributesForPublish(message.messageAttributes) }, (isFifo && {
MessageDeduplicationId: message.deduplicationId || (0, uuid_1.v4)(),
MessageGroupId: message.messageGroupId,
}));
}),
};
return params;
}
}
exports.SNSProducer = SNSProducer;
//# sourceMappingURL=producer.sns.js.map