@fgiova/fastify-sqs-consumer 1.1.0 → 2.0.1
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 +32 -27
- package/dist/cjs/index.d.ts +23 -0
- package/dist/cjs/index.js +97 -0
- package/dist/esm/index.d.mts +23 -0
- package/dist/esm/index.mjs +91 -0
- package/package.json +49 -30
- package/dist/index.d.ts +0 -32
- package/dist/index.js +0 -134
package/README.md
CHANGED
|
@@ -2,12 +2,17 @@
|
|
|
2
2
|
[](https://www.npmjs.com/package/@fgiova/fastify-rest-gateway)
|
|
3
3
|

|
|
4
4
|
[](http://www.typescriptlang.org/)
|
|
5
|
+
[](https://biomejs.dev)
|
|
6
|
+
[](https://qlty.sh/gh/fgiova/projects/fastify-sqs-consumer)
|
|
7
|
+
[](https://qlty.sh/gh/fgiova/projects/fastify-sqs-consumer)
|
|
8
|
+
|
|
5
9
|
## Description
|
|
6
|
-
This plugin for fastify 4.x allows you to consume messages from AWS SQS queues.
|
|
10
|
+
This plugin for fastify 5.x (for fastify 4.x use 1.x ver) allows you to consume messages from AWS SQS queues.
|
|
7
11
|
On fastify shutdown a simple wait function is called to wait for the end of the processing of the messages in progress.
|
|
8
12
|
|
|
9
13
|
**Warning**<br>
|
|
10
|
-
To use this plugin, you must have correctly configured your [AWS credentials](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html)
|
|
14
|
+
To use this plugin, you must have correctly configured your [AWS credentials](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html).<br>
|
|
15
|
+
This plugin uses [@fgiova/mini-sqs-client](https://www.npmjs.com/package/@fgiova/mini-sqs-client) and [@fgiova/sqs-consumer](https://www.npmjs.com/package/@fgiova/sqs-consumer) to interact with SQS service.
|
|
11
16
|
|
|
12
17
|
## Install
|
|
13
18
|
```bash
|
|
@@ -19,7 +24,7 @@ const fastify = require("fastify")()
|
|
|
19
24
|
|
|
20
25
|
fastify.register(require("fastify-sqs-consumer"), [
|
|
21
26
|
{
|
|
22
|
-
|
|
27
|
+
arn: "arn:aws:sqs:eu-central-1:000000000000:MyQueue",
|
|
23
28
|
waitTimeSeconds: 20,
|
|
24
29
|
timeout: 10_000,
|
|
25
30
|
batchSize: 10,
|
|
@@ -32,17 +37,19 @@ fastify.register(require("fastify-sqs-consumer"), [
|
|
|
32
37
|
### Options
|
|
33
38
|
Options are an array of objects with the following properties (one more for each queue):
|
|
34
39
|
|
|
35
|
-
| Option
|
|
36
|
-
|
|
37
|
-
|
|
|
38
|
-
|
|
|
39
|
-
|
|
|
40
|
-
|
|
|
41
|
-
|
|
|
42
|
-
|
|
|
43
|
-
|
|
|
44
|
-
| events
|
|
45
|
-
| sqs
|
|
40
|
+
| Option | Type | Description |
|
|
41
|
+
|-----------------------|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
42
|
+
| arn* | string | The ARN of the Amazon SQS queue from which messages are received. |
|
|
43
|
+
| handlerFunction* | function | The function that will be called for each message. |
|
|
44
|
+
| name | string | An optional name for the consumer (useful for add [hooks](https://github.com/fgiova/sqs-consumer?tab=readme-ov-file#hooks) post config). If not provided, uuid is assigned. |
|
|
45
|
+
| waitTimeSeconds | number | The duration (in seconds, default 20s) for which the call waits for a message to arrive in the queue before returning. If a message is available, the call returns sooner than WaitTimeSeconds. If no messages are available and the wait time expires, the call returns successfully with an empty list of messages. |
|
|
46
|
+
| timeout | number | The duration before the message is considered as failed: default 90000ms. |
|
|
47
|
+
| batchSize | number | The maximum number of messages to return. Amazon SQS never returns more messages than this value (however, fewer messages might be returned). Valid values: 1 to 10. Default: 1. |
|
|
48
|
+
| messageAttributeNames | string[] | Array of caught message's attributes for each message |
|
|
49
|
+
| events | object | Events functions for the consumer (detail in next table) |
|
|
50
|
+
| sqs | SQSClient | Initialized SQS Client (useful for testing sessions) |
|
|
51
|
+
|
|
52
|
+
* required
|
|
46
53
|
|
|
47
54
|
### handlerFunction
|
|
48
55
|
|
|
@@ -52,19 +59,17 @@ Otherwise, the message is deleted from the queue.
|
|
|
52
59
|
|
|
53
60
|
#### Events
|
|
54
61
|
|
|
55
|
-
|
|
|
56
|
-
|
|
57
|
-
|
|
|
58
|
-
|
|
|
59
|
-
|
|
|
60
|
-
|
|
|
61
|
-
|
|
|
62
|
-
|
|
|
63
|
-
|
|
|
64
|
-
|
|
|
62
|
+
| Option | Type | Description |
|
|
63
|
+
|------------------|-----------------------------------------------------------------------|---------------------------------------------------------------------|
|
|
64
|
+
| onPoll | (messages: Message[]) => Promise<Message[]> | Called when the consumer polls for messages |
|
|
65
|
+
| onMessage | (message: Message) => Promise<Message> | Called when the consumer receives a message |
|
|
66
|
+
| onHandle | (message: Message) => Promise<Message> | Called when the consumer handles a message |
|
|
67
|
+
| onHandlerSuccess | (message: Message) => Promise<Message> | Called when the consumer handles a message successfully |
|
|
68
|
+
| onHandlerTimeout | (message: Message) => Promise<Message> | Called when the consumer handler execution exceed executionTimeout |
|
|
69
|
+
| onHandlerError | (message: Message, error: Error) => Promise<Boolean> | Called when the consumer handler execution throws an error |
|
|
70
|
+
| onSuccess | (message: Message) => Promise<Message> | Called when the consumer handler execution finishes successfully |
|
|
71
|
+
| onError | ( hook: HookName, message: Message, error: Error) => Promise<Boolean> | Called when the consumer handler execution throws an uncaught error |
|
|
72
|
+
| onSQSError | (error: Error, message?: Message) => Promise<void> | Called when the consumer receives an error from the SQS service |
|
|
65
73
|
|
|
66
74
|
## License
|
|
67
75
|
Licensed under [MIT](./LICENSE).
|
|
68
|
-
|
|
69
|
-
### Acknowledgements
|
|
70
|
-
This project is kindly sponsored by: isendu Srl [www.isendu.com](https://www.isendu.com)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Message, MiniSQSClient } from "@fgiova/mini-sqs-client";
|
|
2
|
+
import { type HooksOptions, SQSConsumer } from "@fgiova/sqs-consumer";
|
|
3
|
+
import type { FastifyInstance } from "fastify";
|
|
4
|
+
declare module "fastify" {
|
|
5
|
+
interface FastifyInstance {
|
|
6
|
+
sqsConsumers: Record<string, SQSConsumer>;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
declare function sqsConsumerPlugin(fastify: FastifyInstance, options: {
|
|
10
|
+
arn: string;
|
|
11
|
+
name?: string;
|
|
12
|
+
handlerFunction: (message: Message, fastify: FastifyInstance) => Promise<unknown>;
|
|
13
|
+
timeout?: number;
|
|
14
|
+
waitTimeSeconds?: number;
|
|
15
|
+
batchSize?: number;
|
|
16
|
+
messageAttributeNames?: string[];
|
|
17
|
+
attributeNames?: string[];
|
|
18
|
+
events?: HooksOptions;
|
|
19
|
+
parallelExecution?: boolean;
|
|
20
|
+
sqs?: MiniSQSClient;
|
|
21
|
+
}[], done: (err?: Error) => void): void;
|
|
22
|
+
export declare const fastifyPlugin: typeof sqsConsumerPlugin;
|
|
23
|
+
export default fastifyPlugin;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.fastifyPlugin = void 0;
|
|
7
|
+
const node_crypto_1 = require("node:crypto");
|
|
8
|
+
const sqs_consumer_1 = require("@fgiova/sqs-consumer");
|
|
9
|
+
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
|
|
10
|
+
function createConsumer(fastify, queueArn, handlerFunction, meta, timeout = 90000, waitTimeSeconds = 20, batchSize = 1, attributeNames = [], messageAttributeNames = [], parallelExecution, hooks, sqs) {
|
|
11
|
+
return new sqs_consumer_1.SQSConsumer({
|
|
12
|
+
queueARN: queueArn,
|
|
13
|
+
autostart: false,
|
|
14
|
+
consumerOptions: {
|
|
15
|
+
waitTimeSeconds,
|
|
16
|
+
itemsPerRequest: batchSize,
|
|
17
|
+
attributeNames,
|
|
18
|
+
messageAttributeNames,
|
|
19
|
+
visibilityTimeout: timeout / 1000,
|
|
20
|
+
},
|
|
21
|
+
handlerOptions: {
|
|
22
|
+
executionTimeout: timeout,
|
|
23
|
+
parallelExecution,
|
|
24
|
+
},
|
|
25
|
+
hooks,
|
|
26
|
+
clientOptions: {
|
|
27
|
+
sqsClient: sqs,
|
|
28
|
+
},
|
|
29
|
+
handler: async function handleMessageFunction(message) {
|
|
30
|
+
meta.pendingMessages++;
|
|
31
|
+
try {
|
|
32
|
+
await handlerFunction(message, fastify);
|
|
33
|
+
meta.pendingMessages--;
|
|
34
|
+
}
|
|
35
|
+
catch (e) {
|
|
36
|
+
meta.pendingMessages--;
|
|
37
|
+
fastify.log.error(e);
|
|
38
|
+
throw e;
|
|
39
|
+
}
|
|
40
|
+
return;
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
function sqsConsumerPlugin(fastify, options, done) {
|
|
45
|
+
const metaData = {
|
|
46
|
+
pendingMessages: 0,
|
|
47
|
+
};
|
|
48
|
+
const consumers = {};
|
|
49
|
+
fastify.decorate("sqsConsumers", consumers);
|
|
50
|
+
for (const handler of options) {
|
|
51
|
+
const { name, arn: queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs, parallelExecution, } = handler;
|
|
52
|
+
try {
|
|
53
|
+
consumers[name || (0, node_crypto_1.randomUUID)()] = createConsumer(fastify, queueArn, handlerFunction, metaData, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, parallelExecution, events, sqs);
|
|
54
|
+
} /* c8 ignore next 3 */
|
|
55
|
+
catch (e) {
|
|
56
|
+
fastify.log.error(e);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
fastify.addHook("onReady", (done) => {
|
|
60
|
+
for (const consumer of Object.values(consumers)) {
|
|
61
|
+
consumer.start().catch((e) => fastify.log.error(e));
|
|
62
|
+
}
|
|
63
|
+
done();
|
|
64
|
+
});
|
|
65
|
+
let allStopped = false;
|
|
66
|
+
fastify.addHook("onClose", async (fastify) => {
|
|
67
|
+
const arrayConsumers = Object.values(consumers);
|
|
68
|
+
for (const consumer of arrayConsumers) {
|
|
69
|
+
await consumer.stop();
|
|
70
|
+
}
|
|
71
|
+
const interval = setInterval(() => {
|
|
72
|
+
const isRunning = arrayConsumers.some((consumer) => consumer.isRunning);
|
|
73
|
+
/* c8 ignore next 5 */
|
|
74
|
+
if (isRunning) {
|
|
75
|
+
fastify.log.debug("[fastify-sqs-consumer] Some consumers still running... please wait");
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
else if (!allStopped) {
|
|
79
|
+
allStopped = true;
|
|
80
|
+
fastify.log.debug("[fastify-sqs-consumer] All consumers are stopped");
|
|
81
|
+
}
|
|
82
|
+
if (metaData.pendingMessages <= 0) {
|
|
83
|
+
clearInterval(interval);
|
|
84
|
+
return done();
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
fastify.log.debug(`[fastify-sqs-consumer] ${metaData.pendingMessages} pending messages`);
|
|
88
|
+
}
|
|
89
|
+
}, 50);
|
|
90
|
+
});
|
|
91
|
+
done();
|
|
92
|
+
}
|
|
93
|
+
exports.fastifyPlugin = (0, fastify_plugin_1.default)(sqsConsumerPlugin, {
|
|
94
|
+
name: "fastify-sqs-consumer",
|
|
95
|
+
fastify: ">=5.x",
|
|
96
|
+
});
|
|
97
|
+
exports.default = exports.fastifyPlugin;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Message, MiniSQSClient } from "@fgiova/mini-sqs-client";
|
|
2
|
+
import { type HooksOptions, SQSConsumer } from "@fgiova/sqs-consumer";
|
|
3
|
+
import type { FastifyInstance } from "fastify";
|
|
4
|
+
declare module "fastify" {
|
|
5
|
+
interface FastifyInstance {
|
|
6
|
+
sqsConsumers: Record<string, SQSConsumer>;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
declare function sqsConsumerPlugin(fastify: FastifyInstance, options: {
|
|
10
|
+
arn: string;
|
|
11
|
+
name?: string;
|
|
12
|
+
handlerFunction: (message: Message, fastify: FastifyInstance) => Promise<unknown>;
|
|
13
|
+
timeout?: number;
|
|
14
|
+
waitTimeSeconds?: number;
|
|
15
|
+
batchSize?: number;
|
|
16
|
+
messageAttributeNames?: string[];
|
|
17
|
+
attributeNames?: string[];
|
|
18
|
+
events?: HooksOptions;
|
|
19
|
+
parallelExecution?: boolean;
|
|
20
|
+
sqs?: MiniSQSClient;
|
|
21
|
+
}[], done: (err?: Error) => void): void;
|
|
22
|
+
export declare const fastifyPlugin: typeof sqsConsumerPlugin;
|
|
23
|
+
export default fastifyPlugin;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { randomUUID } from "node:crypto";
|
|
2
|
+
import { SQSConsumer } from "@fgiova/sqs-consumer";
|
|
3
|
+
import fp from "fastify-plugin";
|
|
4
|
+
function createConsumer(fastify, queueArn, handlerFunction, meta, timeout = 90000, waitTimeSeconds = 20, batchSize = 1, attributeNames = [], messageAttributeNames = [], parallelExecution, hooks, sqs) {
|
|
5
|
+
return new SQSConsumer({
|
|
6
|
+
queueARN: queueArn,
|
|
7
|
+
autostart: false,
|
|
8
|
+
consumerOptions: {
|
|
9
|
+
waitTimeSeconds,
|
|
10
|
+
itemsPerRequest: batchSize,
|
|
11
|
+
attributeNames,
|
|
12
|
+
messageAttributeNames,
|
|
13
|
+
visibilityTimeout: timeout / 1000,
|
|
14
|
+
},
|
|
15
|
+
handlerOptions: {
|
|
16
|
+
executionTimeout: timeout,
|
|
17
|
+
parallelExecution,
|
|
18
|
+
},
|
|
19
|
+
hooks,
|
|
20
|
+
clientOptions: {
|
|
21
|
+
sqsClient: sqs,
|
|
22
|
+
},
|
|
23
|
+
handler: async function handleMessageFunction(message) {
|
|
24
|
+
meta.pendingMessages++;
|
|
25
|
+
try {
|
|
26
|
+
await handlerFunction(message, fastify);
|
|
27
|
+
meta.pendingMessages--;
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
meta.pendingMessages--;
|
|
31
|
+
fastify.log.error(e);
|
|
32
|
+
throw e;
|
|
33
|
+
}
|
|
34
|
+
return;
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
function sqsConsumerPlugin(fastify, options, done) {
|
|
39
|
+
const metaData = {
|
|
40
|
+
pendingMessages: 0,
|
|
41
|
+
};
|
|
42
|
+
const consumers = {};
|
|
43
|
+
fastify.decorate("sqsConsumers", consumers);
|
|
44
|
+
for (const handler of options) {
|
|
45
|
+
const { name, arn: queueArn, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs, parallelExecution, } = handler;
|
|
46
|
+
try {
|
|
47
|
+
consumers[name || randomUUID()] = createConsumer(fastify, queueArn, handlerFunction, metaData, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, parallelExecution, events, sqs);
|
|
48
|
+
} /* c8 ignore next 3 */
|
|
49
|
+
catch (e) {
|
|
50
|
+
fastify.log.error(e);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
fastify.addHook("onReady", (done) => {
|
|
54
|
+
for (const consumer of Object.values(consumers)) {
|
|
55
|
+
consumer.start().catch((e) => fastify.log.error(e));
|
|
56
|
+
}
|
|
57
|
+
done();
|
|
58
|
+
});
|
|
59
|
+
let allStopped = false;
|
|
60
|
+
fastify.addHook("onClose", async (fastify) => {
|
|
61
|
+
const arrayConsumers = Object.values(consumers);
|
|
62
|
+
for (const consumer of arrayConsumers) {
|
|
63
|
+
await consumer.stop();
|
|
64
|
+
}
|
|
65
|
+
const interval = setInterval(() => {
|
|
66
|
+
const isRunning = arrayConsumers.some((consumer) => consumer.isRunning);
|
|
67
|
+
/* c8 ignore next 5 */
|
|
68
|
+
if (isRunning) {
|
|
69
|
+
fastify.log.debug("[fastify-sqs-consumer] Some consumers still running... please wait");
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
else if (!allStopped) {
|
|
73
|
+
allStopped = true;
|
|
74
|
+
fastify.log.debug("[fastify-sqs-consumer] All consumers are stopped");
|
|
75
|
+
}
|
|
76
|
+
if (metaData.pendingMessages <= 0) {
|
|
77
|
+
clearInterval(interval);
|
|
78
|
+
return done();
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
fastify.log.debug(`[fastify-sqs-consumer] ${metaData.pendingMessages} pending messages`);
|
|
82
|
+
}
|
|
83
|
+
}, 50);
|
|
84
|
+
});
|
|
85
|
+
done();
|
|
86
|
+
}
|
|
87
|
+
export const fastifyPlugin = fp(sqsConsumerPlugin, {
|
|
88
|
+
name: "fastify-sqs-consumer",
|
|
89
|
+
fastify: ">=5.x",
|
|
90
|
+
});
|
|
91
|
+
export default fastifyPlugin;
|
package/package.json
CHANGED
|
@@ -1,18 +1,35 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgiova/fastify-sqs-consumer",
|
|
3
3
|
"description": "sqs-consumer fastify plugin",
|
|
4
|
-
"version": "
|
|
5
|
-
"main": "dist/index.js",
|
|
4
|
+
"version": "2.0.1",
|
|
5
|
+
"main": "./dist/cjs/index.js",
|
|
6
|
+
"types": "./dist/cjs/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": {
|
|
10
|
+
"types": "./dist/esm/index.d.mts",
|
|
11
|
+
"default": "./dist/esm/index.mjs"
|
|
12
|
+
},
|
|
13
|
+
"require": {
|
|
14
|
+
"types": "./dist/cjs/index.d.ts",
|
|
15
|
+
"default": "./dist/cjs/index.js"
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
},
|
|
6
19
|
"files": [
|
|
7
20
|
"dist"
|
|
8
21
|
],
|
|
9
22
|
"scripts": {
|
|
10
|
-
"lint": "eslint 'src/**/*.ts'",
|
|
11
23
|
"clean": "rimraf dist",
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
24
|
+
"preBuild": "npm run clean",
|
|
25
|
+
"build": "esmBuilder",
|
|
26
|
+
"lint": "biome ci ./src ./test",
|
|
27
|
+
"fix": "biome check --write ./src ./test",
|
|
28
|
+
"test": "tap --timeout=90",
|
|
29
|
+
"test:debug": "tap --only --timeout=0",
|
|
30
|
+
"test:local": "TEST_LOCAL=true tap",
|
|
31
|
+
"test:local:debug": "TEST_LOCAL=true tap --only --timeout=0",
|
|
32
|
+
"test:coverage": "tap --coverage-report=lcovonly --coverage-report=text"
|
|
16
33
|
},
|
|
17
34
|
"repository": {
|
|
18
35
|
"type": "git",
|
|
@@ -32,39 +49,41 @@
|
|
|
32
49
|
"access": "public"
|
|
33
50
|
},
|
|
34
51
|
"engines": {
|
|
35
|
-
"node": ">=
|
|
52
|
+
"node": ">=20.14.1"
|
|
36
53
|
},
|
|
37
54
|
"tap": {
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
|
|
55
|
+
"show-full-coverage": true,
|
|
56
|
+
"before": "./test/scripts/executors/before.js",
|
|
57
|
+
"after": "./test/scripts/executors/teardown.js",
|
|
58
|
+
"exclude": [
|
|
59
|
+
"test/helpers/**/*",
|
|
60
|
+
"test/scripts/**/*"
|
|
61
|
+
]
|
|
62
|
+
},
|
|
63
|
+
"peerDependencies": {
|
|
64
|
+
"fastify": ">=5.0.0",
|
|
65
|
+
"p-map": ">=7.0.0"
|
|
44
66
|
},
|
|
45
67
|
"devDependencies": {
|
|
46
68
|
"@aws-sdk/client-sqs": "^3.398.0",
|
|
69
|
+
"@biomejs/biome": "2.2.2",
|
|
70
|
+
"@fgiova/cjs-esm-ts-builder": "^1.2.0",
|
|
47
71
|
"@semantic-release/changelog": "^6.0.3",
|
|
48
72
|
"@semantic-release/git": "^10.0.1",
|
|
49
|
-
"@
|
|
50
|
-
"@types/
|
|
51
|
-
"@
|
|
52
|
-
"
|
|
53
|
-
"conventional-changelog-conventionalcommits": "^6.1.0",
|
|
54
|
-
"cross-env": "^7.0.3",
|
|
73
|
+
"@tsconfig/node20": "^20.1.6",
|
|
74
|
+
"@types/dotenv": "^6.1.1",
|
|
75
|
+
"@types/node": "^20.19.11",
|
|
76
|
+
"conventional-changelog-conventionalcommits": "^8.0.0",
|
|
55
77
|
"dotenv": "^10.0.0",
|
|
56
|
-
"
|
|
57
|
-
"
|
|
58
|
-
"
|
|
59
|
-
"
|
|
60
|
-
"
|
|
61
|
-
"tap": "^16.3.8",
|
|
62
|
-
"testcontainers": "^10.2.1",
|
|
63
|
-
"ts-node": "^10.9.1",
|
|
78
|
+
"fastify": "^5.5.0",
|
|
79
|
+
"semantic-release": "^24.2.3",
|
|
80
|
+
"tap": "^21.1.0",
|
|
81
|
+
"testcontainers": "^11.5.1",
|
|
82
|
+
"ts-node": "^10.9.2",
|
|
64
83
|
"typescript": "^5.1.6"
|
|
65
84
|
},
|
|
66
85
|
"dependencies": {
|
|
67
|
-
"
|
|
68
|
-
"
|
|
86
|
+
"@fgiova/sqs-consumer": "^2.1.3",
|
|
87
|
+
"fastify-plugin": "^5.0.1"
|
|
69
88
|
}
|
|
70
89
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { Consumer } from "sqs-consumer";
|
|
2
|
-
import type { FastifyInstance } from "fastify";
|
|
3
|
-
import type { Message } from "@aws-sdk/client-sqs";
|
|
4
|
-
import type { SQSClient } from "@aws-sdk/client-sqs";
|
|
5
|
-
declare module "fastify" {
|
|
6
|
-
interface FastifyInstance {
|
|
7
|
-
sqsConsumers: Consumer[];
|
|
8
|
-
}
|
|
9
|
-
}
|
|
10
|
-
type eventsHandlers = {
|
|
11
|
-
error?: (error: Error, message: void | Message | Message[]) => void;
|
|
12
|
-
processingError?: (error: Error, message: Message) => void;
|
|
13
|
-
timeoutError?: (error: Error, message: Message) => void | boolean;
|
|
14
|
-
messageReceived?: (message: Message) => void;
|
|
15
|
-
messageProcessed?: (message: Message) => void;
|
|
16
|
-
responseProcessed?: () => void;
|
|
17
|
-
stopped?: () => void;
|
|
18
|
-
empty?: () => void;
|
|
19
|
-
};
|
|
20
|
-
declare function sqsConsumerPlugin(fastify: FastifyInstance, options: {
|
|
21
|
-
queueUrl: string;
|
|
22
|
-
handlerFunction: (message: Message, fastify: FastifyInstance) => Promise<any>;
|
|
23
|
-
timeout?: number;
|
|
24
|
-
waitTimeSeconds?: number;
|
|
25
|
-
batchSize?: number;
|
|
26
|
-
attributeNames?: string[];
|
|
27
|
-
messageAttributeNames?: string[];
|
|
28
|
-
events?: eventsHandlers;
|
|
29
|
-
sqs?: SQSClient;
|
|
30
|
-
}[], done: any): void;
|
|
31
|
-
declare const _default: typeof sqsConsumerPlugin;
|
|
32
|
-
export = _default;
|
package/dist/index.js
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
const fastify_plugin_1 = __importDefault(require("fastify-plugin"));
|
|
6
|
-
const sqs_consumer_1 = require("sqs-consumer");
|
|
7
|
-
const promises_1 = require("timers/promises");
|
|
8
|
-
;
|
|
9
|
-
function createConsumer(fastify, queueUrl, handlerFunction, meta, timeout = 90000, waitTimeSeconds = 20, batchSize = 1, attributeNames = [], messageAttributeNames = [], events, sqs) {
|
|
10
|
-
return sqs_consumer_1.Consumer.create({
|
|
11
|
-
queueUrl,
|
|
12
|
-
handleMessageTimeout: 0,
|
|
13
|
-
waitTimeSeconds,
|
|
14
|
-
visibilityTimeout: timeout / 1000,
|
|
15
|
-
batchSize,
|
|
16
|
-
sqs,
|
|
17
|
-
attributeNames,
|
|
18
|
-
messageAttributeNames,
|
|
19
|
-
handleMessage: async function handleMessageFunction(message) {
|
|
20
|
-
meta.pendingMessages++;
|
|
21
|
-
try {
|
|
22
|
-
const result = await Promise.race([
|
|
23
|
-
(0, promises_1.setTimeout)(timeout, "timeout"),
|
|
24
|
-
handlerFunction(message, fastify),
|
|
25
|
-
]);
|
|
26
|
-
if (result === "timeout") {
|
|
27
|
-
const TimeoutError = new Error(`Message ${message.MessageId} Timeout after ${timeout}ms`);
|
|
28
|
-
if (events?.timeoutError && typeof events.timeoutError === "function") {
|
|
29
|
-
const timeout = events.timeoutError(TimeoutError, message);
|
|
30
|
-
/* istanbul ignore else */
|
|
31
|
-
if (timeout === false) {
|
|
32
|
-
meta.pendingMessages--;
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
throw TimeoutError;
|
|
37
|
-
}
|
|
38
|
-
meta.pendingMessages--;
|
|
39
|
-
}
|
|
40
|
-
catch (e) {
|
|
41
|
-
meta.pendingMessages--;
|
|
42
|
-
fastify.log.error(e);
|
|
43
|
-
throw e;
|
|
44
|
-
}
|
|
45
|
-
return;
|
|
46
|
-
}
|
|
47
|
-
})
|
|
48
|
-
.on("error", (err, message) => {
|
|
49
|
-
/* istanbul ignore else */
|
|
50
|
-
if (events?.error) {
|
|
51
|
-
events.error(err, message);
|
|
52
|
-
}
|
|
53
|
-
else {
|
|
54
|
-
fastify.log.error(`[fastify-sqs-consumer] ${err.message}`);
|
|
55
|
-
}
|
|
56
|
-
})
|
|
57
|
-
.on("processing_error", (err, message) => {
|
|
58
|
-
/* istanbul ignore else */
|
|
59
|
-
if (events?.processingError) {
|
|
60
|
-
events.processingError(err, message);
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
fastify.log.error(`[fastify-sqs-consumer] ${err.message}`);
|
|
64
|
-
}
|
|
65
|
-
})
|
|
66
|
-
.on("message_received", (message) => {
|
|
67
|
-
if (events?.messageReceived)
|
|
68
|
-
events.messageReceived(message);
|
|
69
|
-
})
|
|
70
|
-
.on("message_processed", (message) => {
|
|
71
|
-
if (events?.messageProcessed)
|
|
72
|
-
events.messageProcessed(message);
|
|
73
|
-
})
|
|
74
|
-
.on("response_processed", () => {
|
|
75
|
-
if (events?.responseProcessed)
|
|
76
|
-
events.responseProcessed();
|
|
77
|
-
}).on("stopped", () => {
|
|
78
|
-
if (events?.stopped)
|
|
79
|
-
events.stopped();
|
|
80
|
-
})
|
|
81
|
-
.on("empty", () => {
|
|
82
|
-
/* istanbul ignore next */
|
|
83
|
-
if (events?.empty)
|
|
84
|
-
events.empty();
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
function sqsConsumerPlugin(fastify, options, done) {
|
|
88
|
-
const metaData = {
|
|
89
|
-
pendingMessages: 0
|
|
90
|
-
};
|
|
91
|
-
const consumers = [];
|
|
92
|
-
fastify.decorate("sqsConsumers", consumers);
|
|
93
|
-
for (const handler of options) {
|
|
94
|
-
const { queueUrl, handlerFunction, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs } = handler;
|
|
95
|
-
consumers.push(createConsumer(fastify, queueUrl, handlerFunction, metaData, timeout, waitTimeSeconds, batchSize, attributeNames, messageAttributeNames, events, sqs));
|
|
96
|
-
}
|
|
97
|
-
fastify.addHook("onReady", (done) => {
|
|
98
|
-
for (const consumer of consumers) {
|
|
99
|
-
consumer.start();
|
|
100
|
-
}
|
|
101
|
-
done();
|
|
102
|
-
});
|
|
103
|
-
let allStopped = false;
|
|
104
|
-
fastify.addHook("onClose", (fastify, done) => {
|
|
105
|
-
for (const consumer of consumers) {
|
|
106
|
-
consumer.stop();
|
|
107
|
-
consumer.removeAllListeners();
|
|
108
|
-
}
|
|
109
|
-
const interval = setInterval(() => {
|
|
110
|
-
const isRunning = consumers.some((consumer) => consumer.isRunning);
|
|
111
|
-
/* istanbul ignore next */
|
|
112
|
-
if (isRunning) {
|
|
113
|
-
fastify.log.debug("[fastify-sqs-consumer] Some consumers still running... please wait");
|
|
114
|
-
return null;
|
|
115
|
-
}
|
|
116
|
-
else if (!allStopped) {
|
|
117
|
-
allStopped = true;
|
|
118
|
-
fastify.log.debug("[fastify-sqs-consumer] All consumers are stopped");
|
|
119
|
-
}
|
|
120
|
-
if (metaData.pendingMessages <= 0) {
|
|
121
|
-
clearInterval(interval);
|
|
122
|
-
return done();
|
|
123
|
-
}
|
|
124
|
-
else {
|
|
125
|
-
fastify.log.debug(`[fastify-sqs-consumer] ${metaData.pendingMessages} pending messages`);
|
|
126
|
-
}
|
|
127
|
-
}, 50);
|
|
128
|
-
});
|
|
129
|
-
done();
|
|
130
|
-
}
|
|
131
|
-
module.exports = (0, fastify_plugin_1.default)(sqsConsumerPlugin, {
|
|
132
|
-
name: "fastify-sqs-consumer",
|
|
133
|
-
fastify: ">=4.x"
|
|
134
|
-
});
|