@fgiova/fastify-sqs-consumer 3.3.0 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -74,6 +74,7 @@ Options are an array of objects with the following properties (one for each queu
74
74
  | parallelExecution | boolean | Enable parallel execution of message handlers. |
75
75
  | credentials | {accessKeyId, secretAccessKey} | Explicit AWS credentials (alternative to environment configuration). |
76
76
  | backoff | number \| (attempt: number) => number | Delay (in seconds, 0-43200) before a failed message becomes visible again. A number sets a fixed delay; a function receives the attempt number (`ApproximateReceiveCount`, requested automatically) and returns the delay. Default: 0 (immediate retry). |
77
+ | maxRetries | number \| (attempt: number) => number | Maximum receive count before giving up on a failed message. When `ApproximateReceiveCount` (requested automatically) reaches this value, the `onMaxReceiveCountExceeded` event is fired: returning `true` deletes the message. A function receives the attempt number and returns the limit. Default: unlimited. |
77
78
  | events | object | Events functions for the consumer (detail in next table). |
78
79
  | sqs | MiniSQSClient \| SQSOptions | Either an initialized [@fgiova/mini-sqs-client](https://www.npmjs.com/package/@fgiova/mini-sqs-client) instance (useful for testing sessions) or an options object used to build the client (see below). |
79
80
 
@@ -157,6 +158,7 @@ Otherwise, the message is deleted from the queue.
157
158
  | onHandlerSuccess | (message: Message) => Promise<Message> | Called when the consumer handles a message successfully |
158
159
  | onHandlerTimeout | (message: Message) => Promise<Message> | Called when the consumer handler execution exceed executionTimeout |
159
160
  | onHandlerError | (message: Message, error: Error) => Promise<Boolean> | Called when the consumer handler execution throws an error |
161
+ | onMaxReceiveCountExceeded | (message: Message, count: number) => Promise<Boolean> | Called when a failed message reaches `maxRetries`; return `true` to delete it |
160
162
  | onSuccess | (message: Message) => Promise<Message> | Called when the consumer handler execution finishes successfully |
161
163
  | onError | ( hook: HookName, message: Message, error: Error) => Promise<Boolean> | Called when the consumer handler execution throws an uncaught error |
162
164
  | onSQSError | (error: Error, message?: Message) => Promise<void> | Called when the consumer receives an error from the SQS service |
@@ -25,6 +25,7 @@ export type ConsumerOptions = {
25
25
  events?: HooksOptions;
26
26
  parallelExecution?: boolean;
27
27
  backoff?: ((attempt: number) => number) | number;
28
+ maxRetries?: ((attempt: number) => number) | number;
28
29
  sqs?: MiniSQSClient | {
29
30
  endpoint?: string;
30
31
  undiciOptions?: Pool.Options;
package/dist/cjs/index.js CHANGED
@@ -11,7 +11,7 @@ const sqs_consumer_1 = require("@fgiova/sqs-consumer");
11
11
  // @ts-expect-error
12
12
  const unpromise_1 = require("@watchable/unpromise");
13
13
  const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
14
- function createConsumer(fastify, queueArn, handlerFunction, timeout = 90000, waitTimeSeconds = 20, batchSize = 1, attributeNames = [], messageAttributeNames = [], parallelExecution, hooks, sqs, credentials, backoff) {
14
+ function createConsumer(fastify, queueArn, handlerFunction, timeout = 90000, waitTimeSeconds = 20, batchSize = 1, attributeNames = [], messageAttributeNames = [], parallelExecution, hooks, sqs, credentials, backoff, maxRetries) {
15
15
  const meta = { pendingMessages: 0 };
16
16
  let ownedClient;
17
17
  if (!(sqs instanceof mini_sqs_client_1.MiniSQSClient)) {
@@ -32,6 +32,7 @@ function createConsumer(fastify, queueArn, handlerFunction, timeout = 90000, wai
32
32
  visibilityTimeout: timeout / 1000,
33
33
  errorDelaySeconds: typeof backoff === "number" ? backoff : undefined,
34
34
  errorDelayBackoff: typeof backoff === "function" ? backoff : undefined,
35
+ maxReceiveCount: maxRetries,
35
36
  },
36
37
  handlerOptions: {
37
38
  executionTimeout: timeout,
@@ -86,7 +87,7 @@ function sqsConsumerPlugin(fastify, options, done) {
86
87
  return consumerStart;
87
88
  };
88
89
  const addSQSConsumer = (consumerOptions) => {
89
- const { name, arn: queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs, parallelExecution, credentials, backoff, } = consumerOptions;
90
+ const { name, arn: queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs, parallelExecution, credentials, backoff, maxRetries, } = consumerOptions;
90
91
  const consumerName = name || (0, node_crypto_1.randomUUID)();
91
92
  if (consumers[consumerName]) {
92
93
  fastify.log.warn(`[fastify-sqs-consumer] Consumer with name ${consumerName} already exists. Skipping creation.`);
@@ -98,7 +99,7 @@ function sqsConsumerPlugin(fastify, options, done) {
98
99
  }
99
100
  maxExecutionTimeout = Math.max(maxExecutionTimeout, consumerOptions.timeout ?? 0);
100
101
  try {
101
- consumers[consumerName] = createConsumer(fastify, queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, parallelExecution, events, sqs, credentials, backoff);
102
+ consumers[consumerName] = createConsumer(fastify, queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, parallelExecution, events, sqs, credentials, backoff, maxRetries);
102
103
  if (isReady) {
103
104
  void startConsumer(consumerName);
104
105
  }
@@ -25,6 +25,7 @@ export type ConsumerOptions = {
25
25
  events?: HooksOptions;
26
26
  parallelExecution?: boolean;
27
27
  backoff?: ((attempt: number) => number) | number;
28
+ maxRetries?: ((attempt: number) => number) | number;
28
29
  sqs?: MiniSQSClient | {
29
30
  endpoint?: string;
30
31
  undiciOptions?: Pool.Options;
@@ -5,7 +5,7 @@ import { SQSConsumer } from "@fgiova/sqs-consumer";
5
5
  // @ts-expect-error
6
6
  import { Unpromise } from "@watchable/unpromise";
7
7
  import fp from "fastify-plugin";
8
- function createConsumer(fastify, queueArn, handlerFunction, timeout = 90000, waitTimeSeconds = 20, batchSize = 1, attributeNames = [], messageAttributeNames = [], parallelExecution, hooks, sqs, credentials, backoff) {
8
+ function createConsumer(fastify, queueArn, handlerFunction, timeout = 90000, waitTimeSeconds = 20, batchSize = 1, attributeNames = [], messageAttributeNames = [], parallelExecution, hooks, sqs, credentials, backoff, maxRetries) {
9
9
  const meta = { pendingMessages: 0 };
10
10
  let ownedClient;
11
11
  if (!(sqs instanceof MiniSQSClient)) {
@@ -26,6 +26,7 @@ function createConsumer(fastify, queueArn, handlerFunction, timeout = 90000, wai
26
26
  visibilityTimeout: timeout / 1000,
27
27
  errorDelaySeconds: typeof backoff === "number" ? backoff : undefined,
28
28
  errorDelayBackoff: typeof backoff === "function" ? backoff : undefined,
29
+ maxReceiveCount: maxRetries,
29
30
  },
30
31
  handlerOptions: {
31
32
  executionTimeout: timeout,
@@ -80,7 +81,7 @@ function sqsConsumerPlugin(fastify, options, done) {
80
81
  return consumerStart;
81
82
  };
82
83
  const addSQSConsumer = (consumerOptions) => {
83
- const { name, arn: queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs, parallelExecution, credentials, backoff, } = consumerOptions;
84
+ const { name, arn: queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs, parallelExecution, credentials, backoff, maxRetries, } = consumerOptions;
84
85
  const consumerName = name || randomUUID();
85
86
  if (consumers[consumerName]) {
86
87
  fastify.log.warn(`[fastify-sqs-consumer] Consumer with name ${consumerName} already exists. Skipping creation.`);
@@ -92,7 +93,7 @@ function sqsConsumerPlugin(fastify, options, done) {
92
93
  }
93
94
  maxExecutionTimeout = Math.max(maxExecutionTimeout, consumerOptions.timeout ?? 0);
94
95
  try {
95
- consumers[consumerName] = createConsumer(fastify, queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, parallelExecution, events, sqs, credentials, backoff);
96
+ consumers[consumerName] = createConsumer(fastify, queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, parallelExecution, events, sqs, credentials, backoff, maxRetries);
96
97
  if (isReady) {
97
98
  void startConsumer(consumerName);
98
99
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@fgiova/fastify-sqs-consumer",
3
3
  "description": "sqs-consumer fastify plugin",
4
- "version": "3.3.0",
4
+ "version": "3.4.0",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "types": "./dist/cjs/index.d.ts",
7
7
  "exports": {
@@ -77,7 +77,7 @@
77
77
  "clock-mock": "^2.0.4",
78
78
  "conventional-changelog-conventionalcommits": "^9.1.0",
79
79
  "dotenv": "^10.0.0",
80
- "fastify": "^5.12.5",
80
+ "fastify": "^5.12.8",
81
81
  "semantic-release": "^25.0.3",
82
82
  "tap": "^21.1.0",
83
83
  "testcontainers": "^11.5.1",
@@ -85,7 +85,7 @@
85
85
  "typescript": "^5.1.6"
86
86
  },
87
87
  "dependencies": {
88
- "@fgiova/sqs-consumer": "^3.3.0",
88
+ "@fgiova/sqs-consumer": "^3.4.0",
89
89
  "@watchable/unpromise": "^1.0.2",
90
90
  "fastify-plugin": "^5.1.0"
91
91
  }