@message-queue-toolkit/sns
Version:
SNS adapter for message-queue-toolkit
113 lines • 4.79 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.AbstractSnsPublisher = void 0;
const client_sns_1 = require("@aws-sdk/client-sns");
const node_core_1 = require("@lokalise/node-core");
const core_1 = require("@message-queue-toolkit/core");
const sqs_1 = require("@message-queue-toolkit/sqs");
const snsUtils_1 = require("../utils/snsUtils");
const AbstractSnsService_1 = require("./AbstractSnsService");
class AbstractSnsPublisher extends AbstractSnsService_1.AbstractSnsService {
messageSchemaContainer;
isDeduplicationEnabled;
initPromise;
constructor(dependencies, options) {
super(dependencies, options);
this.messageSchemaContainer = this.resolvePublisherMessageSchemaContainer(options);
this.isDeduplicationEnabled = !!options.enablePublisherDeduplication;
}
async publish(message, options = {}) {
const messageSchemaResult = this.resolveSchema(message);
if (messageSchemaResult.error) {
throw messageSchemaResult.error;
}
// If it's not initted yet, do the lazy init
if (!this.isInitted) {
// avoid multiple concurrent inits
if (!this.initPromise) {
this.initPromise = this.init();
}
await this.initPromise;
this.initPromise = undefined;
}
try {
const messageProcessingStartTimestamp = Date.now();
const parsedMessage = messageSchemaResult.result.parse(message);
const topicName = this.locatorConfig?.topicName ?? this.creationConfig?.topic?.Name ?? 'unknown';
if (this.logMessages) {
// @ts-ignore
const resolvedLogMessage = this.resolveMessageLog(message, message[this.messageTypeField]);
this.logMessage(resolvedLogMessage);
}
const updatedMessage = this.updateInternalProperties(message);
const maybeOffloadedPayloadMessage = await this.offloadMessagePayloadIfNeeded(updatedMessage, () => (0, snsUtils_1.calculateOutgoingMessageSize)(updatedMessage));
if (this.isDeduplicationEnabledForMessage(parsedMessage) &&
(await this.deduplicateMessage(parsedMessage, core_1.DeduplicationRequester.Publisher))
.isDuplicated) {
this.handleMessageProcessed({
message: parsedMessage,
processingResult: { status: 'published', skippedAsDuplicate: true },
messageProcessingStartTimestamp,
queueName: topicName,
});
return;
}
await this.sendMessage(maybeOffloadedPayloadMessage, options);
this.handleMessageProcessed({
message: parsedMessage,
processingResult: { status: 'published' },
messageProcessingStartTimestamp,
queueName: topicName,
});
}
catch (error) {
const err = error;
this.handleError(err);
throw new node_core_1.InternalError({
message: `Error while publishing to SNS: ${err.message}`,
errorCode: 'SNS_PUBLISH_ERROR',
details: {
publisher: this.constructor.name,
topic: this.topicArn,
// @ts-ignore
messageType: message[this.messageTypeField] ?? 'unknown',
},
cause: err,
});
}
}
resolveMessage() {
throw new Error('Not implemented for publisher');
}
resolveSchema(message) {
return this.messageSchemaContainer.resolveSchema(message);
}
/* c8 ignore start */
resolveNextFunction() {
throw new Error('Not implemented for publisher');
}
processPrehandlers() {
throw new Error('Not implemented for publisher');
}
preHandlerBarrier() {
throw new Error('Not implemented for publisher');
}
processMessage() {
throw new Error('Not implemented for publisher');
}
isDeduplicationEnabledForMessage(message) {
return this.isDeduplicationEnabled && super.isDeduplicationEnabledForMessage(message);
}
async sendMessage(payload, options) {
const attributes = (0, sqs_1.resolveOutgoingMessageAttributes)(payload);
const command = new client_sns_1.PublishCommand({
Message: JSON.stringify(payload),
MessageAttributes: attributes,
TopicArn: this.topicArn,
...options,
});
await this.snsClient.send(command);
}
}
exports.AbstractSnsPublisher = AbstractSnsPublisher;
//# sourceMappingURL=AbstractSnsPublisher.js.map
;