Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ on:
pull_request:
branches: [ "main" ]

permissions:
contents: read # to be able to read the repository content for testing and building

jobs:
test:
name: Test
Expand All @@ -17,16 +20,16 @@ jobs:
contents: read
strategy:
matrix:
node-version: [20, 22, 24]
node-version: [22, 24]
os: [ubuntu-latest]
steps:
- name: Check out repo
uses: actions/checkout@v3
uses: actions/checkout@v5
with:
persist-credentials: false

- name: Setup Node ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v5
with:
node-version: ${{ matrix.node-version }}

Expand All @@ -37,29 +40,29 @@ jobs:
run: npm run test:coverage

- name: publish code coverage to code-climate (duh)
if: ${{ github.event_name == 'push' && matrix.node-version == 20 }} # Push coverage only once inside the matrix.
if: ${{ github.event_name == 'push' && matrix.node-version == 22 }} # Push coverage only once inside the matrix.
uses: qltysh/qlty-action/coverage@v2
with:
token: ${{secrets.QLTY_COVERAGE_TOKEN}}
files: ${{github.workspace}}/.tap/report/lcov.info
token: ${{secrets.QLTY_COVERAGE_TOKEN}}
files: ${{github.workspace}}/.tap/report/lcov.info

build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [20.x, 22.x, 24.x]
node-version: [22.x, 24.x]

steps:

- name: Check out repo
uses: actions/checkout@v3
uses: actions/checkout@v5
with:
persist-credentials: false

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v3
uses: actions/setup-node@v5
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
Expand All @@ -69,20 +72,25 @@ jobs:
release:
runs-on: ubuntu-latest
needs: [test, build]
if: github.event_name == 'push' # Release only on Push
permissions:
contents: write # to be able to publish a GitHub release
issues: write # to be able to comment on released issues
pull-requests: write # to be able to comment on released pull requests
id-token: write # to enable use of OIDC for trusted publishing and npm provenance
steps:
- name: Check out repo
uses: actions/checkout@v3
uses: actions/checkout@v5
with:
persist-credentials: false

- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v5
with:
node-version: '20.x'
node-version: '22.x'
- run: npm ci
- run: npm run build
- name: Semantic Release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Comment thread
fgiova marked this conversation as resolved.
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,6 @@ dist/
.nyc_output/
!/.env.dev
/test-env.json
.plans/
/.tap/
/.claude/
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20
22
75 changes: 53 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# fastify sqs-consumer
[![NPM version](https://img.shields.io/npm/v/@fgiova/fastify-sqs-consumer.svg?style=flat)](https://www.npmjs.com/package/@fgiova/fastify-rest-gateway)
[![NPM version](https://img.shields.io/npm/v/@fgiova/fastify-sqs-consumer.svg?style=flat)](https://www.npmjs.com/package/@fgiova/fastify-sqs-consumer)
![CI workflow](https://github.com/fgiova/fastify-sqs-consumer/actions/workflows/node.js.yml/badge.svg)
[![TypeScript](https://img.shields.io/badge/%3C%2F%3E-TypeScript-%230074c1.svg)](http://www.typescriptlang.org/)
[![Linted with Biome](https://img.shields.io/badge/Linted_with-Biome-60a5fa?style=flat&logo=biome)](https://biomejs.dev)
Expand All @@ -18,11 +18,34 @@ This plugin uses [@fgiova/mini-sqs-client](https://www.npmjs.com/package/@fgiova
```bash
npm i @fgiova/fastify-sqs-consumer
```
### Usage

## Usage

### ESM
```js
import fastify from "fastify";
import sqsConsumer from "@fgiova/fastify-sqs-consumer";

const app = fastify();

app.register(sqsConsumer, [
{
arn: "arn:aws:sqs:eu-central-1:000000000000:MyQueue",
waitTimeSeconds: 20,
timeout: 10_000,
batchSize: 10,
handlerFunction: async (message, fastify) => {
return true;
}
}
]);
```

### CommonJS
```js
const fastify = require("fastify")()
const fastify = require("fastify")();

fastify.register(require("fastify-sqs-consumer"), [
fastify.register(require("@fgiova/fastify-sqs-consumer").default, [
{
arn: "arn:aws:sqs:eu-central-1:000000000000:MyQueue",
waitTimeSeconds: 20,
Expand All @@ -34,30 +57,38 @@ fastify.register(require("fastify-sqs-consumer"), [
}
]);
```
### Options
Options are an array of objects with the following properties (one more for each queue):

| Option | Type | Description |
|-----------------------|-----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| arn* | string | The ARN of the Amazon SQS queue from which messages are received. |
| handlerFunction* | function | The function that will be called for each message. |
| 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. |
| 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. |
| timeout | number | The duration before the message is considered as failed: default 90000ms. |
| 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. |
| messageAttributeNames | string[] | Array of caught message's attributes for each message |
| events | object | Events functions for the consumer (detail in next table) |
| sqs | SQSClient | Initialized SQS Client (useful for testing sessions) |
## Options
Options are an array of objects with the following properties (one for each queue):

| Option | Type | Description |
|-----------------------|-------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| arn* | string | The ARN of the Amazon SQS queue from which messages are received. |
| handlerFunction* | function | The function that will be called for each message. |
| 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. |
| 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. |
| timeout | number | The duration before the message is considered as failed: default 90000ms. |
| 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. |
| attributeNames | string[] | Array of message system attributes to retrieve for each message. |
| messageAttributeNames | string[] | Array of custom message attributes to retrieve for each message. |
| parallelExecution | boolean | Enable parallel execution of message handlers. |
| credentials | {accessKeyId, secretAccessKey} | Explicit AWS credentials (alternative to environment configuration). |
| events | object | Events functions for the consumer (detail in next table). |
| sqs | MiniSQSClient | Initialized [@fgiova/mini-sqs-client](https://www.npmjs.com/package/@fgiova/mini-sqs-client) instance (useful for testing sessions). |

\* required

## Decorator

* required
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.

### handlerFunction
## handlerFunction

Handler function is an async function called for each message received from the queue.
Each Error thrown by the function is caught and the message is not deleted from the queue.
Otherwise, the message is deleted from the queue.
Otherwise, the message is deleted from the queue.

#### Events
### Events

| Option | Type | Description |
|------------------|-----------------------------------------------------------------------|---------------------------------------------------------------------|
Expand All @@ -72,4 +103,4 @@ Otherwise, the message is deleted from the queue.
| onSQSError | (error: Error, message?: Message) => Promise<void> | Called when the consumer receives an error from the SQS service |

## License
Licensed under [MIT](./LICENSE).
Licensed under [MIT](./LICENSE).
Loading