UNPKG

@message-queue-toolkit/sqs

Version:
113 lines 4.83 kB
import { SendMessageCommand } from '@aws-sdk/client-sqs'; import { InternalError } from '@lokalise/node-core'; import { DeduplicationRequesterEnum, } from '@message-queue-toolkit/core'; import { resolveOutgoingMessageAttributes } from "../utils/messageUtils.js"; import { calculateOutgoingMessageSize } from "../utils/sqsUtils.js"; import { AbstractSqsService } from "./AbstractSqsService.js"; export const PAYLOAD_OFFLOADING_ATTRIBUTE_PREFIX = 'payloadOffloading.'; export const OFFLOADED_PAYLOAD_SIZE_ATTRIBUTE = `${PAYLOAD_OFFLOADING_ATTRIBUTE_PREFIX}size`; export class AbstractSqsPublisher extends AbstractSqsService { 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); if (this.logMessages) { // @ts-ignore const resolvedLogMessage = this.resolveMessageLog(message, message[this.messageTypeField]); this.logMessage(resolvedLogMessage); } // biome-ignore lint/style/noParameterAssign: This is expected message = this.updateInternalProperties(message); const maybeOffloadedPayloadMessage = await this.offloadMessagePayloadIfNeeded(message, () => calculateOutgoingMessageSize(message)); if (this.isDeduplicationEnabledForMessage(parsedMessage) && (await this.deduplicateMessage(parsedMessage, DeduplicationRequesterEnum.Publisher)) .isDuplicated) { this.handleMessageProcessed({ message: parsedMessage, processingResult: { status: 'published', skippedAsDuplicate: true }, messageProcessingStartTimestamp, queueName: this.queueName, }); return; } await this.sendMessage(maybeOffloadedPayloadMessage, options); this.handleMessageProcessed({ message: parsedMessage, processingResult: { status: 'published' }, messageProcessingStartTimestamp, queueName: this.queueName, }); } catch (error) { const err = error; this.handleError(err); throw new InternalError({ message: `Error while publishing to SQS: ${err.message}`, errorCode: 'SQS_PUBLISH_ERROR', details: { publisher: this.constructor.name, queueArn: this.queueArn, queueName: this.queueName, // @ts-ignore messageType: message[this.messageTypeField] ?? 'unknown', }, cause: err, }); } } /* c8 ignore start */ resolveNextFunction() { throw new Error('Not implemented for publisher'); } resolveMessage(_message) { 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'); } /* c8 ignore stop */ isDeduplicationEnabledForMessage(message) { return this.isDeduplicationEnabled && super.isDeduplicationEnabledForMessage(message); } resolveSchema(message) { return this.messageSchemaContainer.resolveSchema(message); } async sendMessage(payload, options) { const attributes = resolveOutgoingMessageAttributes(payload); const command = new SendMessageCommand({ QueueUrl: this.queueUrl, MessageBody: JSON.stringify(payload), MessageAttributes: attributes, ...options, }); await this.sqsClient.send(command); } } //# sourceMappingURL=AbstractSqsPublisher.js.map