UNPKG

@message-queue-toolkit/sqs

Version:
117 lines 5.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AbstractSqsPublisher = exports.OFFLOADED_PAYLOAD_SIZE_ATTRIBUTE = exports.PAYLOAD_OFFLOADING_ATTRIBUTE_PREFIX = void 0; const client_sqs_1 = require("@aws-sdk/client-sqs"); const node_core_1 = require("@lokalise/node-core"); const core_1 = require("@message-queue-toolkit/core"); const messageUtils_1 = require("../utils/messageUtils"); const sqsUtils_1 = require("../utils/sqsUtils"); const AbstractSqsService_1 = require("./AbstractSqsService"); exports.PAYLOAD_OFFLOADING_ATTRIBUTE_PREFIX = 'payloadOffloading.'; exports.OFFLOADED_PAYLOAD_SIZE_ATTRIBUTE = `${exports.PAYLOAD_OFFLOADING_ATTRIBUTE_PREFIX}size`; class AbstractSqsPublisher extends AbstractSqsService_1.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, () => (0, sqsUtils_1.calculateOutgoingMessageSize)(message)); if (this.isDeduplicationEnabledForMessage(parsedMessage) && (await this.deduplicateMessage(parsedMessage, core_1.DeduplicationRequester.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 node_core_1.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 = (0, messageUtils_1.resolveOutgoingMessageAttributes)(payload); const command = new client_sqs_1.SendMessageCommand({ QueueUrl: this.queueUrl, MessageBody: JSON.stringify(payload), MessageAttributes: attributes, ...options, }); await this.sqsClient.send(command); } } exports.AbstractSqsPublisher = AbstractSqsPublisher; //# sourceMappingURL=AbstractSqsPublisher.js.map