@fgiova/fastify-sqs-consumer 3.0.0 → 3.1.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,10 +74,39 @@ 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
  | events | object | Events functions for the consumer (detail in next table). |
77
- | sqs | MiniSQSClient | Initialized [@fgiova/mini-sqs-client](https://www.npmjs.com/package/@fgiova/mini-sqs-client) instance (useful for testing sessions). |
77
+ | 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
78
 
79
79
  \* required
80
80
 
81
+ ### sqs options object
82
+
83
+ Instead of passing a pre-built `MiniSQSClient`, you can pass an options object so the plugin builds the client for you:
84
+
85
+ | Option | Type | Description |
86
+ |---------------|---------------------------------|----------------------------------------------------------------------------------------------|
87
+ | endpoint | string | Custom SQS endpoint (e.g. a local LocalStack/ElasticMQ instance). |
88
+ | undiciOptions | Pool.Options | [undici](https://undici.nodejs.org/#/docs/api/Pool) `Pool` options for the underlying HTTP client. |
89
+ | credentials | {accessKeyId, secretAccessKey} | Explicit AWS credentials. The top-level `credentials` option takes precedence over this one. |
90
+
91
+ ```js
92
+ app.register(sqsConsumer, [
93
+ {
94
+ arn: "arn:aws:sqs:eu-central-1:000000000000:MyQueue",
95
+ sqs: {
96
+ endpoint: "http://localhost:4566",
97
+ undiciOptions: { connections: 1 },
98
+ credentials: {
99
+ accessKeyId: "AWS_ACCESS_KEY_ID",
100
+ secretAccessKey: "AWS_SECRET_ACCESS_KEY"
101
+ }
102
+ },
103
+ handlerFunction: async (message, fastify) => {
104
+ return true;
105
+ }
106
+ }
107
+ ]);
108
+ ```
109
+
81
110
  ## Decorator
82
111
 
83
112
  The plugin decorates the Fastify instance with `sqsConsumers`, a `Record<string, { consumer: SQSConsumer, meta: { pendingMessages: number } }>` that provides access to the registered consumers and their metadata.
@@ -1,6 +1,7 @@
1
- import type { Message, MiniSQSClient } from "@fgiova/mini-sqs-client";
1
+ import { type Message, MiniSQSClient } from "@fgiova/mini-sqs-client";
2
2
  import { type HooksOptions, SQSConsumer } from "@fgiova/sqs-consumer";
3
3
  import type { FastifyInstance } from "fastify";
4
+ import type { Pool } from "undici";
4
5
  declare module "fastify" {
5
6
  interface FastifyInstance {
6
7
  sqsConsumers: Record<string, {
@@ -26,7 +27,14 @@ declare function sqsConsumerPlugin(fastify: FastifyInstance, options: {
26
27
  attributeNames?: string[];
27
28
  events?: HooksOptions;
28
29
  parallelExecution?: boolean;
29
- sqs?: MiniSQSClient;
30
+ sqs?: MiniSQSClient | {
31
+ endpoint?: string;
32
+ undiciOptions?: Pool.Options;
33
+ credentials?: {
34
+ accessKeyId: string;
35
+ secretAccessKey: string;
36
+ };
37
+ };
30
38
  }[], done: (err?: Error) => void): void;
31
39
  export declare const fastifyPlugin: typeof sqsConsumerPlugin;
32
40
  export default fastifyPlugin;
package/dist/cjs/index.js CHANGED
@@ -5,12 +5,18 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.fastifyPlugin = void 0;
7
7
  const node_crypto_1 = require("node:crypto");
8
+ const mini_sqs_client_1 = require("@fgiova/mini-sqs-client");
8
9
  const sqs_consumer_1 = require("@fgiova/sqs-consumer");
9
10
  // @ts-expect-error
10
11
  const unpromise_1 = require("@watchable/unpromise");
11
12
  const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
12
13
  function createConsumer(fastify, queueArn, handlerFunction, timeout = 90000, waitTimeSeconds = 20, batchSize = 1, attributeNames = [], messageAttributeNames = [], parallelExecution, hooks, sqs, credentials) {
13
14
  const meta = { pendingMessages: 0 };
15
+ credentials =
16
+ credentials ??
17
+ (typeof sqs === "object" && !(sqs instanceof mini_sqs_client_1.MiniSQSClient)
18
+ ? sqs.credentials
19
+ : undefined);
14
20
  return {
15
21
  consumer: new sqs_consumer_1.SQSConsumer({
16
22
  queueARN: queueArn,
@@ -28,7 +34,13 @@ function createConsumer(fastify, queueArn, handlerFunction, timeout = 90000, wai
28
34
  },
29
35
  hooks,
30
36
  clientOptions: {
31
- sqsClient: sqs,
37
+ sqsClient: sqs instanceof mini_sqs_client_1.MiniSQSClient ? sqs : undefined,
38
+ endpoint: typeof sqs === "object" && !(sqs instanceof mini_sqs_client_1.MiniSQSClient)
39
+ ? sqs.endpoint
40
+ : undefined,
41
+ undiciOptions: typeof sqs === "object" && !(sqs instanceof mini_sqs_client_1.MiniSQSClient)
42
+ ? sqs.undiciOptions
43
+ : undefined,
32
44
  /* c8 ignore next 1 */
33
45
  signer: credentials ? { credentials } : undefined,
34
46
  },
@@ -74,8 +86,11 @@ function sqsConsumerPlugin(fastify, options, done) {
74
86
  }
75
87
  fastify.addHook("onReady", (done) => {
76
88
  for (const consumerName of Object.keys(consumers)) {
77
- consumers[consumerName].consumer.start().catch((e) => {
89
+ consumers[consumerName].consumer.start().catch(async (e) => {
78
90
  fastify.log.error(e);
91
+ await consumers[consumerName].consumer
92
+ .stop()
93
+ .catch((stopErr) => fastify.log.error(stopErr));
79
94
  delete consumers[consumerName];
80
95
  });
81
96
  }
@@ -1,6 +1,7 @@
1
- import type { Message, MiniSQSClient } from "@fgiova/mini-sqs-client";
1
+ import { type Message, MiniSQSClient } from "@fgiova/mini-sqs-client";
2
2
  import { type HooksOptions, SQSConsumer } from "@fgiova/sqs-consumer";
3
3
  import type { FastifyInstance } from "fastify";
4
+ import type { Pool } from "undici";
4
5
  declare module "fastify" {
5
6
  interface FastifyInstance {
6
7
  sqsConsumers: Record<string, {
@@ -26,7 +27,14 @@ declare function sqsConsumerPlugin(fastify: FastifyInstance, options: {
26
27
  attributeNames?: string[];
27
28
  events?: HooksOptions;
28
29
  parallelExecution?: boolean;
29
- sqs?: MiniSQSClient;
30
+ sqs?: MiniSQSClient | {
31
+ endpoint?: string;
32
+ undiciOptions?: Pool.Options;
33
+ credentials?: {
34
+ accessKeyId: string;
35
+ secretAccessKey: string;
36
+ };
37
+ };
30
38
  }[], done: (err?: Error) => void): void;
31
39
  export declare const fastifyPlugin: typeof sqsConsumerPlugin;
32
40
  export default fastifyPlugin;
@@ -1,10 +1,16 @@
1
1
  import { randomUUID } from "node:crypto";
2
+ import { MiniSQSClient } from "@fgiova/mini-sqs-client";
2
3
  import { SQSConsumer } from "@fgiova/sqs-consumer";
3
4
  // @ts-expect-error
4
5
  import { Unpromise } from "@watchable/unpromise";
5
6
  import fp from "fastify-plugin";
6
7
  function createConsumer(fastify, queueArn, handlerFunction, timeout = 90000, waitTimeSeconds = 20, batchSize = 1, attributeNames = [], messageAttributeNames = [], parallelExecution, hooks, sqs, credentials) {
7
8
  const meta = { pendingMessages: 0 };
9
+ credentials =
10
+ credentials ??
11
+ (typeof sqs === "object" && !(sqs instanceof MiniSQSClient)
12
+ ? sqs.credentials
13
+ : undefined);
8
14
  return {
9
15
  consumer: new SQSConsumer({
10
16
  queueARN: queueArn,
@@ -22,7 +28,13 @@ function createConsumer(fastify, queueArn, handlerFunction, timeout = 90000, wai
22
28
  },
23
29
  hooks,
24
30
  clientOptions: {
25
- sqsClient: sqs,
31
+ sqsClient: sqs instanceof MiniSQSClient ? sqs : undefined,
32
+ endpoint: typeof sqs === "object" && !(sqs instanceof MiniSQSClient)
33
+ ? sqs.endpoint
34
+ : undefined,
35
+ undiciOptions: typeof sqs === "object" && !(sqs instanceof MiniSQSClient)
36
+ ? sqs.undiciOptions
37
+ : undefined,
26
38
  /* c8 ignore next 1 */
27
39
  signer: credentials ? { credentials } : undefined,
28
40
  },
@@ -68,8 +80,11 @@ function sqsConsumerPlugin(fastify, options, done) {
68
80
  }
69
81
  fastify.addHook("onReady", (done) => {
70
82
  for (const consumerName of Object.keys(consumers)) {
71
- consumers[consumerName].consumer.start().catch((e) => {
83
+ consumers[consumerName].consumer.start().catch(async (e) => {
72
84
  fastify.log.error(e);
85
+ await consumers[consumerName].consumer
86
+ .stop()
87
+ .catch((stopErr) => fastify.log.error(stopErr));
73
88
  delete consumers[consumerName];
74
89
  });
75
90
  }
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.0.0",
4
+ "version": "3.1.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.7.4",
80
+ "fastify": "^5.8.5",
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.0.0",
88
+ "@fgiova/sqs-consumer": "^3.2.0",
89
89
  "@watchable/unpromise": "^1.0.2",
90
90
  "fastify-plugin": "^5.1.0"
91
91
  }