@message-queue-toolkit/sns
Version:
SNS adapter for message-queue-toolkit
109 lines • 4.62 kB
JavaScript
import { PublishCommand } from '@aws-sdk/client-sns';
import { InternalError } from '@lokalise/node-core';
import { DeduplicationRequesterEnum, } from '@message-queue-toolkit/core';
import { resolveOutgoingMessageAttributes } from '@message-queue-toolkit/sqs';
import { calculateOutgoingMessageSize } from "../utils/snsUtils.js";
import { AbstractSnsService } from "./AbstractSnsService.js";
export class AbstractSnsPublisher extends 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, () => calculateOutgoingMessageSize(updatedMessage));
if (this.isDeduplicationEnabledForMessage(parsedMessage) &&
(await this.deduplicateMessage(parsedMessage, DeduplicationRequesterEnum.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 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 = resolveOutgoingMessageAttributes(payload);
const command = new PublishCommand({
Message: JSON.stringify(payload),
MessageAttributes: attributes,
TopicArn: this.topicArn,
...options,
});
await this.snsClient.send(command);
}
}
//# sourceMappingURL=AbstractSnsPublisher.js.map