@fgiova/fastify-sqs-consumer 3.2.0 → 3.3.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,7 @@ 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). |
76
77
  | events | object | Events functions for the consumer (detail in next table). |
77
78
  | 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
79
 
@@ -24,6 +24,7 @@ export type ConsumerOptions = {
24
24
  attributeNames?: string[];
25
25
  events?: HooksOptions;
26
26
  parallelExecution?: boolean;
27
+ backoff?: ((attempt: number) => number) | number;
27
28
  sqs?: MiniSQSClient | {
28
29
  endpoint?: string;
29
30
  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) {
15
15
  const meta = { pendingMessages: 0 };
16
16
  let ownedClient;
17
17
  if (!(sqs instanceof mini_sqs_client_1.MiniSQSClient)) {
@@ -30,6 +30,8 @@ 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,
33
35
  },
34
36
  handlerOptions: {
35
37
  executionTimeout: timeout,
@@ -84,7 +86,7 @@ function sqsConsumerPlugin(fastify, options, done) {
84
86
  return consumerStart;
85
87
  };
86
88
  const addSQSConsumer = (consumerOptions) => {
87
- const { name, arn: queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs, parallelExecution, credentials, } = consumerOptions;
89
+ const { name, arn: queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs, parallelExecution, credentials, backoff, } = consumerOptions;
88
90
  const consumerName = name || (0, node_crypto_1.randomUUID)();
89
91
  if (consumers[consumerName]) {
90
92
  fastify.log.warn(`[fastify-sqs-consumer] Consumer with name ${consumerName} already exists. Skipping creation.`);
@@ -96,7 +98,7 @@ function sqsConsumerPlugin(fastify, options, done) {
96
98
  }
97
99
  maxExecutionTimeout = Math.max(maxExecutionTimeout, consumerOptions.timeout ?? 0);
98
100
  try {
99
- consumers[consumerName] = createConsumer(fastify, queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, parallelExecution, events, sqs, credentials);
101
+ consumers[consumerName] = createConsumer(fastify, queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, parallelExecution, events, sqs, credentials, backoff);
100
102
  if (isReady) {
101
103
  void startConsumer(consumerName);
102
104
  }
@@ -24,6 +24,7 @@ export type ConsumerOptions = {
24
24
  attributeNames?: string[];
25
25
  events?: HooksOptions;
26
26
  parallelExecution?: boolean;
27
+ backoff?: ((attempt: number) => number) | number;
27
28
  sqs?: MiniSQSClient | {
28
29
  endpoint?: string;
29
30
  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) {
9
9
  const meta = { pendingMessages: 0 };
10
10
  let ownedClient;
11
11
  if (!(sqs instanceof MiniSQSClient)) {
@@ -24,6 +24,8 @@ 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,
27
29
  },
28
30
  handlerOptions: {
29
31
  executionTimeout: timeout,
@@ -78,7 +80,7 @@ function sqsConsumerPlugin(fastify, options, done) {
78
80
  return consumerStart;
79
81
  };
80
82
  const addSQSConsumer = (consumerOptions) => {
81
- const { name, arn: queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs, parallelExecution, credentials, } = consumerOptions;
83
+ const { name, arn: queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs, parallelExecution, credentials, backoff, } = consumerOptions;
82
84
  const consumerName = name || randomUUID();
83
85
  if (consumers[consumerName]) {
84
86
  fastify.log.warn(`[fastify-sqs-consumer] Consumer with name ${consumerName} already exists. Skipping creation.`);
@@ -90,7 +92,7 @@ function sqsConsumerPlugin(fastify, options, done) {
90
92
  }
91
93
  maxExecutionTimeout = Math.max(maxExecutionTimeout, consumerOptions.timeout ?? 0);
92
94
  try {
93
- consumers[consumerName] = createConsumer(fastify, queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, parallelExecution, events, sqs, credentials);
95
+ consumers[consumerName] = createConsumer(fastify, queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, parallelExecution, events, sqs, credentials, backoff);
94
96
  if (isReady) {
95
97
  void startConsumer(consumerName);
96
98
  }
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.3.0",
5
5
  "main": "./dist/cjs/index.js",
6
6
  "types": "./dist/cjs/index.d.ts",
7
7
  "exports": {
@@ -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.3.0",
89
89
  "@watchable/unpromise": "^1.0.2",
90
90
  "fastify-plugin": "^5.1.0"
91
91
  }