UNPKG

@nestjstools/messaging-amazon-sqs-extension

Version:

Extension to handle messages and dispatch them over Amazon SQS

82 lines 4.12 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.AmazonSqsMessagingConsumer = void 0; const amazon_sqs_channel_1 = require("../channel/amazon-sqs.channel"); const messaging_1 = require("@nestjstools/messaging"); const common_1 = require("@nestjs/common"); const client_sqs_1 = require("@aws-sdk/client-sqs"); let AmazonSqsMessagingConsumer = class AmazonSqsMessagingConsumer { channel = undefined; async consume(dispatcher, channel) { this.channel = channel; const client = this.channel.client; async function processPollMessages() { const delay = (s) => new Promise((resolve) => setTimeout(resolve, s * 1000)); while (true) { const receiveParams = { QueueUrl: channel.config.queueUrl, MaxNumberOfMessages: channel.config.maxNumberOfMessages, WaitTimeSeconds: channel.config.waitTimeSeconds, VisibilityTimeout: channel.config.visibilityTimeout, MessageAttributeNames: ['All'], }; const response = await client.send(new client_sqs_1.ReceiveMessageCommand(receiveParams)); if (response.Messages && response.Messages.length > 0) { for (const message of response.Messages) { const attrs = message.MessageAttributes; const messageBody = message.Body; const routingKey = attrs.messagingRoutingKey.StringValue; await dispatcher.dispatch(new messaging_1.ConsumerMessage(JSON.parse(messageBody), routingKey)); const deleteParams = { QueueUrl: channel.config.queueUrl, ReceiptHandle: message.ReceiptHandle, }; await client.send(new client_sqs_1.DeleteMessageCommand(deleteParams)); } } await delay(channel.config.waitTimeSeconds); } } await this.createDeadLetterQueue(channel); processPollMessages(); return Promise.resolve(); } async onError(errored, channel) { if (!channel.config.deadLetterQueue) { return Promise.resolve(); } const command = new client_sqs_1.SendMessageCommand({ QueueUrl: `${channel.config.queueUrl}_dead_letter`, MessageBody: JSON.stringify(errored.dispatchedConsumerMessage.message), MessageAttributes: { messagingRoutingKey: { DataType: 'String', StringValue: errored.dispatchedConsumerMessage.routingKey, }, }, }); await channel.client.send(command); return Promise.resolve(); } createDeadLetterQueue(channel) { if (!channel.config.deadLetterQueue) { return Promise.resolve(); } channel.client.send(new client_sqs_1.CreateQueueCommand({ QueueName: `${channel.config.queueName}_dead_letter`, })); return Promise.resolve(); } }; exports.AmazonSqsMessagingConsumer = AmazonSqsMessagingConsumer; exports.AmazonSqsMessagingConsumer = AmazonSqsMessagingConsumer = __decorate([ (0, common_1.Injectable)(), (0, messaging_1.MessageConsumer)(amazon_sqs_channel_1.AmazonSqsChannel) ], AmazonSqsMessagingConsumer); //# sourceMappingURL=amazon-sqs-messaging.consumer.js.map