1.0.0 • Published 2 years ago

@fgiova/sqs-consumer v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

SQS consumer using undici

NPM version CI workflow TypeScript Maintainability Test Coverage

Description

This module allows consuming SQS messages using @fgiova/mini-sqs-client thorough the aws-json protocol with "undici" as http agent . The @fgiova/aws-signature module is used for signing requests to optimize performance.

Installation

npm install @fgiova/sqs-consumer

Usage

import {SQSConsumer} from '@fgiova/sqs-consumer'

const consumer = new SQSConsumer({
    queueARN: "arn:aws:sqs:eu-central-1:000000000000:test-queue-hooks",
    handler: async (message) => {
        console.log(message.Body);
    }
});

Options

OptionTypeDefaultDescription
queueARNstringThe ARN of the queue to consume
handler(message: Message) => PromiseThe handler function to be called for each message
loggerLoggerconsoleThe logger to be used
autoStartbooleantrueWhether to start the consumer automatically
handlerOptionsHandlerOptionsThe options for the handler
clientOptionsClientOptionsThe options for the client
consumerOptionsConsumerOptionsThe options for the consumer
hooksHooksThe hooks to be called on specific events

HandlerOptions

OptionTypeDefaultDescription
deleteMessagebooleantrueWhether to delete the message after handling (if handler execute without any error)
extendVisibilitybooleantrueWhether to extend the visibility timeout during message handling
excuteTimeoutnumber30000The timeout for the handler execution in ms
parallelExecutionbooleantrueIf true execute handler in parallel for each batch of messages received, otherwise execute consecutively

ClientOptions

OptionTypeDefaultDescription
sqsClientMiniSQSClientThe MiniSQSClient client to be used. If not provided, a new client will be created using the queueARN.
endpointstringThe endpoint to be used for the client. If not provided, the endpoint will be inferred from the queueARN.
undiciOptionsPool.OptionsThe options for the undici client.
signerSigner / SignerOptionsThe signer to be used for signing requests. If not provided, a new singleton signer will be created.
destroySignerbooleanfalseWhether to destroy the signer when the consumer is destroyed.

ConsumerOptions

OptionTypeDefaultDescription
visibilityTimeoutnumber30The visibility timeout for the messages in seconds
waitTimeSecondsnumber20The wait time for the receiveMessage call in seconds
itemsPerRequestnumber10The maximum number of messages to be received at once
messageAttributeNamesstring[][]The message attribute names to be included in the response

Hooks

OptionTypeDescription
onPoll(messages: Message[]) => Promise<Message[]>Called when the consumer polls for messages
onMessage(message: Message) => PromiseCalled when the consumer receives a message
onHandle(message: Message) => PromiseCalled when the consumer handles a message
onHandlerSuccess(message: Message) => PromiseCalled when the consumer handles a message successfully
onHandlerTimeout(message: Message) => PromiseCalled when the consumer handler execution exceed executionTimeout
onHandlerError(message: Message, error: Error) => PromiseCalled when the consumer handler execution throws an error
onSuccess(message: Message) => PromiseCalled when the consumer handler execution finishes successfully
onError( hook: HookName, message: Message, error: Error) => PromiseCalled when the consumer handler execution throws an uncaught error
onSQSError(error: Error, message?: Message) => PromiseCalled when the consumer receives an error from the SQS service

API

SQSConsumer(options: SQSConsumerOptions)
SQSConsumer.start(): Promise<void>
SQSConsumer.stop(destroyConsumer = false): Promise<void>
SQSConsumer.addHook(hookname: string, func: Function): void

License

Licensed under MIT.

1.0.0

2 years ago