@fgiova/fastify-sqs-consumer 3.2.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
@@ -73,6 +73,8 @@ Options are an array of objects with the following properties (one for each queu
73
73
  | messageAttributeNames | string[] | Array of custom message attributes to retrieve for each message. |
74
74
  | parallelExecution | boolean | Enable parallel execution of message handlers. |
75
75
  | credentials | {accessKeyId, secretAccessKey} | Explicit AWS credentials (alternative to environment configuration). |
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. |
76
78
  | events | object | Events functions for the consumer (detail in next table). |
77
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). |
78
80
 
@@ -156,6 +158,7 @@ Otherwise, the message is deleted from the queue.
156
158
  | onHandlerSuccess | (message: Message) => Promise<Message> | Called when the consumer handles a message successfully |
157
159
  | onHandlerTimeout | (message: Message) => Promise<Message> | Called when the consumer handler execution exceed executionTimeout |
158
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 |
159
162
  | onSuccess | (message: Message) => Promise<Message> | Called when the consumer handler execution finishes successfully |
160
163
  | onError | ( hook: HookName, message: Message, error: Error) => Promise<Boolean> | Called when the consumer handler execution throws an uncaught error |
161
164
  | onSQSError | (error: Error, message?: Message) => Promise<void> | Called when the consumer receives an error from the SQS service |
@@ -24,6 +24,8 @@ export type ConsumerOptions = {
24
24
  attributeNames?: string[];
25
25
  events?: HooksOptions;
26
26
  parallelExecution?: boolean;
27
+ backoff?: ((attempt: number) => number) | number;
28
+ maxRetries?: ((attempt: number) => number) | number;
27
29
  sqs?: MiniSQSClient | {
28
30
  endpoint?: string;
29
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) {
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)) {
@@ -30,6 +30,9 @@ function createConsumer(fastify, queueArn, handlerFunction, timeout = 90000, wai
30
30
  attributeNames,
31
31
  messageAttributeNames,
32
32
  visibilityTimeout: timeout / 1000,
33
+ errorDelaySeconds: typeof backoff === "number" ? backoff : undefined,
34
+ errorDelayBackoff: typeof backoff === "function" ? backoff : undefined,
35
+ maxReceiveCount: maxRetries,
33
36
  },
34
37
  handlerOptions: {
35
38
  executionTimeout: timeout,
@@ -84,7 +87,7 @@ function sqsConsumerPlugin(fastify, options, done) {
84
87
  return consumerStart;
85
88
  };
86
89
  const addSQSConsumer = (consumerOptions) => {
87
- const { name, arn: queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs, parallelExecution, credentials, } = consumerOptions;
90
+ const { name, arn: queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs, parallelExecution, credentials, backoff, maxRetries, } = consumerOptions;
88
91
  const consumerName = name || (0, node_crypto_1.randomUUID)();
89
92
  if (consumers[consumerName]) {
90
93
  fastify.log.warn(`[fastify-sqs-consumer] Consumer with name ${consumerName} already exists. Skipping creation.`);
@@ -96,7 +99,7 @@ function sqsConsumerPlugin(fastify, options, done) {
96
99
  }
97
100
  maxExecutionTimeout = Math.max(maxExecutionTimeout, consumerOptions.timeout ?? 0);
98
101
  try {
99
- consumers[consumerName] = createConsumer(fastify, queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, parallelExecution, events, sqs, credentials);
102
+ consumers[consumerName] = createConsumer(fastify, queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, parallelExecution, events, sqs, credentials, backoff, maxRetries);
100
103
  if (isReady) {
101
104
  void startConsumer(consumerName);
102
105
  }
@@ -24,6 +24,8 @@ export type ConsumerOptions = {
24
24
  attributeNames?: string[];
25
25
  events?: HooksOptions;
26
26
  parallelExecution?: boolean;
27
+ backoff?: ((attempt: number) => number) | number;
28
+ maxRetries?: ((attempt: number) => number) | number;
27
29
  sqs?: MiniSQSClient | {
28
30
  endpoint?: string;
29
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) {
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)) {
@@ -24,6 +24,9 @@ function createConsumer(fastify, queueArn, handlerFunction, timeout = 90000, wai
24
24
  attributeNames,
25
25
  messageAttributeNames,
26
26
  visibilityTimeout: timeout / 1000,
27
+ errorDelaySeconds: typeof backoff === "number" ? backoff : undefined,
28
+ errorDelayBackoff: typeof backoff === "function" ? backoff : undefined,
29
+ maxReceiveCount: maxRetries,
27
30
  },
28
31
  handlerOptions: {
29
32
  executionTimeout: timeout,
@@ -78,7 +81,7 @@ function sqsConsumerPlugin(fastify, options, done) {
78
81
  return consumerStart;
79
82
  };
80
83
  const addSQSConsumer = (consumerOptions) => {
81
- const { name, arn: queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs, parallelExecution, credentials, } = consumerOptions;
84
+ const { name, arn: queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs, parallelExecution, credentials, backoff, maxRetries, } = consumerOptions;
82
85
  const consumerName = name || randomUUID();
83
86
  if (consumers[consumerName]) {
84
87
  fastify.log.warn(`[fastify-sqs-consumer] Consumer with name ${consumerName} already exists. Skipping creation.`);
@@ -90,7 +93,7 @@ function sqsConsumerPlugin(fastify, options, done) {
90
93
  }
91
94
  maxExecutionTimeout = Math.max(maxExecutionTimeout, consumerOptions.timeout ?? 0);
92
95
  try {
93
- consumers[consumerName] = createConsumer(fastify, queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, parallelExecution, events, sqs, credentials);
96
+ consumers[consumerName] = createConsumer(fastify, queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, parallelExecution, events, sqs, credentials, backoff, maxRetries);
94
97
  if (isReady) {
95
98
  void startConsumer(consumerName);
96
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.2.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.2.1",
88
+ "@fgiova/sqs-consumer": "^3.4.0",
89
89
  "@watchable/unpromise": "^1.0.2",
90
90
  "fastify-plugin": "^5.1.0"
91
91
  }