sqs-consumer 5.7.0 → 5.8.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/.github/CODEOWNERS +2 -2
- package/.github/CODE_OF_CONDUCT.md +74 -0
- package/.github/CONTRIBUTING.md +14 -12
- package/.github/ISSUE_TEMPLATE/bug-report.md +2 -2
- package/.github/ISSUE_TEMPLATE/feature-request.md +0 -1
- package/.github/ISSUE_TEMPLATE/technical-question.md +0 -1
- package/.github/SECURITY.md +9 -0
- package/.github/pull_request_template.md +20 -11
- package/.github/workflows/coverage.yml +35 -0
- package/.github/workflows/stale.yml +20 -0
- package/.github/workflows/test.yml +34 -0
- package/.prettierignore +4 -0
- package/.prettierrc.js +5 -0
- package/README.md +41 -39
- package/dist/consumer.d.ts +10 -10
- package/dist/consumer.js +27 -29
- package/dist/index.js +1 -0
- package/package.json +47 -29
- package/src/consumer.ts +90 -50
- package/src/errors.ts +1 -4
- package/test/{consumer.ts → consumer.test.ts} +74 -36
- package/tsconfig.json +3 -8
- package/.travis.yml +0 -10
package/.github/CODEOWNERS
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
# All changes should be reviewed by
|
|
2
|
-
* @bbc/ibl
|
|
1
|
+
# All changes should be reviewed by codeowners
|
|
2
|
+
* @bbc/ibl @bbc/detl-engineers
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct (CoC)
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
|
6
|
+
|
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
|
8
|
+
|
|
9
|
+
## Our Standards
|
|
10
|
+
|
|
11
|
+
Examples of behaviour that contributes to a positive environment for our community include:
|
|
12
|
+
|
|
13
|
+
- Respect different opinions, perspectives, and experiences
|
|
14
|
+
- Giving and appreciating constructive feedback
|
|
15
|
+
- Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
|
16
|
+
- Focusing on what is best for us as individuals and the overall community
|
|
17
|
+
- Demonstrating kindness toward other people
|
|
18
|
+
|
|
19
|
+
Examples of unacceptable behaviour include:
|
|
20
|
+
|
|
21
|
+
- The use of sexualised language or imagery, and sexual attention or advances of any kind
|
|
22
|
+
- Trolling, insulting or derogatory comments, and personal or political attacks
|
|
23
|
+
- Public or private harassment
|
|
24
|
+
- Publishing others’ private information, such as a physical or email address, without their explicit permission
|
|
25
|
+
- Other conduct which could reasonably be considered inappropriate in a professional setting
|
|
26
|
+
|
|
27
|
+
## Enforcement Responsibilities
|
|
28
|
+
|
|
29
|
+
Project maintainers are responsible for clarifying and enforcing our standards of acceptable behaviour and will take appropriate and fair corrective action in response to any behaviour that they deem inappropriate, threatening, offensive, or harmful.
|
|
30
|
+
|
|
31
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
|
32
|
+
|
|
33
|
+
## Scope
|
|
34
|
+
|
|
35
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
|
36
|
+
|
|
37
|
+
## Enforcement
|
|
38
|
+
|
|
39
|
+
Instances of abusive, harassing, or otherwise unacceptable behaviour may be reported to the community leaders responsible for enforcement. All complaints will be reviewed and investigated promptly and fairly.
|
|
40
|
+
|
|
41
|
+
[Project maintainers](https://github.com/bbc/sqs-consumber/blob/main/.github/CODEOWNERS) are obligated to respect the privacy and security of the reporter of any incident.
|
|
42
|
+
|
|
43
|
+
## Enforcement Guidelines
|
|
44
|
+
|
|
45
|
+
### 1. Correction
|
|
46
|
+
|
|
47
|
+
Community Impact: Use of inappropriate language or other behaviour deemed unprofessional or unwelcome in the community.
|
|
48
|
+
|
|
49
|
+
Consequence: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behaviour was inappropriate. A public apology may be requested.
|
|
50
|
+
|
|
51
|
+
### 2. Warning
|
|
52
|
+
|
|
53
|
+
Community Impact: A violation through a single incident or series of actions.
|
|
54
|
+
|
|
55
|
+
Consequence: A warning with consequences for continued behaviour. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
|
56
|
+
|
|
57
|
+
### 3. Temporary Ban
|
|
58
|
+
|
|
59
|
+
Community Impact: A serious violation of community standards, including sustained inappropriate behaviour.
|
|
60
|
+
|
|
61
|
+
Consequence: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
|
62
|
+
|
|
63
|
+
### 4. Permanent Ban
|
|
64
|
+
|
|
65
|
+
Community Impact: Demonstrating a pattern of violation of community standards, including sustained inappropriate behaviour, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
|
66
|
+
|
|
67
|
+
Consequence: A permanent ban from any sort of public interaction within the community.
|
|
68
|
+
|
|
69
|
+
## Attribution
|
|
70
|
+
|
|
71
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, available at [https://www.contributor-covenant.org/version/2/0/code_of_conduct.html][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: https://contributor-covenant.org
|
|
74
|
+
[version]: https://www.contributor-covenant.org/version/2/0/code_of_conduct/
|
package/.github/CONTRIBUTING.md
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
# Contributing
|
|
2
2
|
|
|
3
|
-
Thank you for your interest in contributing to the sqs-consumer.
|
|
4
|
-
|
|
5
|
-
* If you're unsure if a feature would make a good addition, you can always [create an issue](https://github.com/bbc/sqs-consumer/issues/new) first. Raising an issue before creating a pull request is recommended.
|
|
6
|
-
* We aim for 100% test coverage. Please write tests for any new functionality or changes.
|
|
7
|
-
* Any API changes should be fully documented.
|
|
8
|
-
* Make sure your code meets our linting standards. Run `npm run lint` to check your code.
|
|
9
|
-
* Maintain the existing coding style. There are some settings in `.jsbeautifyrc` to help.
|
|
10
|
-
* Be mindful of others when making suggestions and/or code reviewing.
|
|
11
|
-
|
|
3
|
+
Thank you for your interest in contributing to the sqs-consumer.
|
|
12
4
|
|
|
5
|
+
- If you're unsure if a feature would make a good addition, you can always [create an issue](https://github.com/bbc/sqs-consumer/issues/new) first. Raising an issue before creating a pull request is recommended.
|
|
6
|
+
- We aim for 100% test coverage. Please write tests for any new functionality or changes.
|
|
7
|
+
- Any API changes should be fully documented.
|
|
8
|
+
- Make sure your code meets our linting standards. Run `npm run lint` to check your code.
|
|
9
|
+
- Maintain the existing coding style. There are some settings in `.jsbeautifyrc` to help.
|
|
10
|
+
- Be mindful of others when making suggestions and/or code reviewing.
|
|
13
11
|
|
|
14
12
|
## Reporting Issues
|
|
13
|
+
|
|
15
14
|
Before opening a new issue, first check that there is not already an [open issue or Pull Request](https://github.com/bbc/sqs-consumer/issues?utf8=%E2%9C%93&q=is%3Aopen) that addresses it.
|
|
16
15
|
|
|
17
16
|
If there is, make relevant comments and add your reaction. Use a reaction in place of a "+1" comment:
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
|
|
18
|
+
- 👍 - upvote
|
|
19
|
+
- 👎 - downvote
|
|
20
20
|
|
|
21
21
|
If you cannot find an existing issue that describes your bug or feature, create a new issue using the guidelines below.
|
|
22
22
|
|
|
@@ -25,7 +25,8 @@ If you cannot find an existing issue that describes your bug or feature, create
|
|
|
25
25
|
3. Follow your issue in the issue tracking workflow
|
|
26
26
|
|
|
27
27
|
## Contributing Code
|
|
28
|
-
|
|
28
|
+
|
|
29
|
+
If you do not have push access to the repository, please [fork it](https://help.github.com/en/articles/fork-a-repo). You should then work on your own `main` branch.
|
|
29
30
|
|
|
30
31
|
Otherwise, you may clone this repository and create a working branch with a _kebab-case_ name reflecting what you are working on (e.g. `fix-the-thing`).
|
|
31
32
|
|
|
@@ -34,6 +35,7 @@ Follow the setup instructions in the [README](../README.md).
|
|
|
34
35
|
Ensure all your code is thoroughly tested and that this testing is detailed in the pull request.
|
|
35
36
|
|
|
36
37
|
## Pull Request Process
|
|
38
|
+
|
|
37
39
|
1. Make sure you have opened an issue and it was approved by a project maintainer before working on a PR
|
|
38
40
|
2. Read and complete all relevant sections of the PR template
|
|
39
41
|
3. Wait for the PR get approved
|
|
@@ -4,7 +4,6 @@ about: Create a report to help us improve
|
|
|
4
4
|
title: ''
|
|
5
5
|
labels: bug
|
|
6
6
|
assignees: ''
|
|
7
|
-
|
|
8
7
|
---
|
|
9
8
|
|
|
10
9
|
**Describe the bug**
|
|
@@ -12,6 +11,7 @@ A clear and concise description of what the bug is.
|
|
|
12
11
|
|
|
13
12
|
**To Reproduce**
|
|
14
13
|
Steps to reproduce the behaviour:
|
|
14
|
+
|
|
15
15
|
1. Go to '...'
|
|
16
16
|
2. Select '....'
|
|
17
17
|
3. Scroll down to '....'
|
|
@@ -24,4 +24,4 @@ A clear and concise description of what you expected to happen.
|
|
|
24
24
|
If applicable, add screenshots to help explain your problem.
|
|
25
25
|
|
|
26
26
|
**Additional context**
|
|
27
|
-
Add any other context about the problem here, such as specific device information.
|
|
27
|
+
Add any other context about the problem here, such as specific device information.
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Security Policy
|
|
2
|
+
|
|
3
|
+
## Reporting a Vulnerability
|
|
4
|
+
|
|
5
|
+
Our full security policy and vulnerability reporting procedure is documented on [this external website](https://www.bbc.com/backstage/security-disclosure-policy/#reportingavulnerability).
|
|
6
|
+
|
|
7
|
+
Please note that this is a general BBC process. Communication will not be direct with the team responsible for this repo.
|
|
8
|
+
|
|
9
|
+
If you would like to, you can also open an issue in this repo regarding your disclosure, but please never share any details of the vulnerability in the GitHub issue.
|
|
@@ -1,17 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
<!--- What types of changes does your code introduce? Put an `x` in all the boxes that apply: -->
|
|
1
|
+
Resolves #NUMBER
|
|
2
|
+
|
|
3
|
+
**Description:**
|
|
4
|
+
_A very high-level summary of easily-reproducible changes that can be understood by non-devs._
|
|
5
|
+
|
|
6
|
+
**Type of change:**
|
|
7
|
+
|
|
9
8
|
- [ ] Bug fix (non-breaking change which fixes an issue)
|
|
10
9
|
- [ ] New feature (non-breaking change which adds functionality)
|
|
11
10
|
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
|
|
12
|
+
**Why is this change required?:**
|
|
13
|
+
_A simple explanation of what the problem is and how this PR solves it_
|
|
14
|
+
|
|
15
|
+
**Code changes:**
|
|
16
|
+
|
|
17
|
+
- _A bullet point list of key code changes that have been made._
|
|
18
|
+
- _When describing code changes, try to communicate **how** and **why** you implemented something a specific way, not just **what** has changed._
|
|
19
|
+
|
|
20
|
+
---
|
|
21
|
+
|
|
22
|
+
**Checklist:**
|
|
23
|
+
|
|
15
24
|
- [ ] My code follows the code style of this project.
|
|
16
25
|
- [ ] My change requires a change to the documentation.
|
|
17
26
|
- [ ] I have updated the documentation accordingly.
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
name: Report Coverage
|
|
2
|
+
on:
|
|
3
|
+
pull_request:
|
|
4
|
+
branches:
|
|
5
|
+
- 'main'
|
|
6
|
+
push:
|
|
7
|
+
branches:
|
|
8
|
+
- main
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
strategy:
|
|
16
|
+
matrix:
|
|
17
|
+
node-version: [14.x]
|
|
18
|
+
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v3
|
|
21
|
+
|
|
22
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
23
|
+
uses: actions/setup-node@v3
|
|
24
|
+
with:
|
|
25
|
+
node-version: ${{ matrix.node-version }}
|
|
26
|
+
|
|
27
|
+
- name: Install Node Modules
|
|
28
|
+
run: npm ci
|
|
29
|
+
|
|
30
|
+
- name: Report Coverage
|
|
31
|
+
uses: paambaati/codeclimate-action@v3.2.0
|
|
32
|
+
env:
|
|
33
|
+
CC_TEST_REPORTER_ID: 2d851f8f3a9348ac4f43262305037f80a730c2660fda50af8ae4d445fd89333b
|
|
34
|
+
with:
|
|
35
|
+
coverageCommand: npm run lcov
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
name: 'Close stale issues and PRs'
|
|
2
|
+
on:
|
|
3
|
+
schedule:
|
|
4
|
+
- cron: '30 1 * * *'
|
|
5
|
+
|
|
6
|
+
jobs:
|
|
7
|
+
stale:
|
|
8
|
+
runs-on: ubuntu-latest
|
|
9
|
+
steps:
|
|
10
|
+
- uses: actions/stale@v6
|
|
11
|
+
with:
|
|
12
|
+
stale-issue-message: 'This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.'
|
|
13
|
+
stale-pr-message: 'This PR is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 10 days.'
|
|
14
|
+
close-issue-message: 'This issue was closed because it has been stalled for 5 days with no activity.'
|
|
15
|
+
close-pr-message: 'This PR was closed because it has been stalled for 10 days with no activity.'
|
|
16
|
+
days-before-issue-stale: 30
|
|
17
|
+
days-before-pr-stale: 45
|
|
18
|
+
days-before-issue-close: 5
|
|
19
|
+
days-before-pr-close: 10
|
|
20
|
+
operations-per-run: 90
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
name: Run Tests
|
|
2
|
+
on:
|
|
3
|
+
pull_request:
|
|
4
|
+
branches:
|
|
5
|
+
- 'main'
|
|
6
|
+
push:
|
|
7
|
+
branches:
|
|
8
|
+
- main
|
|
9
|
+
permissions:
|
|
10
|
+
contents: read
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
strategy:
|
|
16
|
+
matrix:
|
|
17
|
+
node-version: [14.x, 16.x, 18.x]
|
|
18
|
+
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/checkout@v3
|
|
21
|
+
|
|
22
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
23
|
+
uses: actions/setup-node@v3
|
|
24
|
+
with:
|
|
25
|
+
node-version: ${{ matrix.node-version }}
|
|
26
|
+
|
|
27
|
+
- name: Install Node Modules
|
|
28
|
+
run: npm ci
|
|
29
|
+
|
|
30
|
+
- name: NPM Audit
|
|
31
|
+
run: npx audit-ci
|
|
32
|
+
|
|
33
|
+
- name: Run Tests and Linting
|
|
34
|
+
run: npm run test
|
package/.prettierignore
ADDED
package/.prettierrc.js
ADDED
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# sqs-consumer
|
|
2
2
|
|
|
3
3
|
[](https://npmjs.org/package/sqs-consumer)
|
|
4
|
-
[](https://codeclimate.com/github/BBC/sqs-consumer)
|
|
4
|
+
[](https://github.com/bbc/sqs-consumer/actions/workflows/test.yml)
|
|
5
|
+
[](https://codeclimate.com/github/BBC/sqs-consumer)
|
|
6
6
|
[](https://codeclimate.com/github/BBC/sqs-consumer)
|
|
7
7
|
|
|
8
8
|
Build SQS-based applications without the boilerplate. Just define an async function that handles the SQS message processing.
|
|
@@ -10,7 +10,7 @@ Build SQS-based applications without the boilerplate. Just define an async funct
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
12
12
|
```bash
|
|
13
|
-
npm install sqs-consumer --save
|
|
13
|
+
npm install sqs-consumer --save-dev
|
|
14
14
|
```
|
|
15
15
|
|
|
16
16
|
## Usage
|
|
@@ -36,11 +36,12 @@ app.on('processing_error', (err) => {
|
|
|
36
36
|
app.start();
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
39
|
+
- The queue is polled continuously for messages using [long polling](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html).
|
|
40
|
+
- Messages are deleted from the queue once the handler function has completed successfully.
|
|
41
|
+
- Throwing an error (or returning a rejected promise) from the handler function will cause the message to be left on the queue. An [SQS redrive policy](http://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/SQSDeadLetterQueue.html) can be used to move messages that cannot be processed to a dead letter queue.
|
|
42
|
+
- By default messages are processed one at a time – a new message won't be received until the first one has been processed. To process messages in parallel, use the `batchSize` option [detailed below](#options).
|
|
43
|
+
- By default, the default Node.js HTTP/HTTPS SQS agent creates a new TCP connection for every new request ([AWS SQS documentation](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/node-reusing-connections.html)). To avoid the cost of establishing a new connection, you can reuse an existing connection by passing a new SQS instance with `keepAlive: true`.
|
|
44
|
+
|
|
44
45
|
```js
|
|
45
46
|
const { Consumer } = require('sqs-consumer');
|
|
46
47
|
const AWS = require('aws-sdk');
|
|
@@ -82,7 +83,6 @@ export AWS_ACCESS_KEY_ID=...
|
|
|
82
83
|
|
|
83
84
|
If you need to specify your credentials manually, you can use a pre-configured instance of the [AWS SQS](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html) client:
|
|
84
85
|
|
|
85
|
-
|
|
86
86
|
```js
|
|
87
87
|
const { Consumer } = require('sqs-consumer');
|
|
88
88
|
const AWS = require('aws-sdk');
|
|
@@ -110,7 +110,7 @@ app.on('processing_error', (err) => {
|
|
|
110
110
|
});
|
|
111
111
|
|
|
112
112
|
app.on('timeout_error', (err) => {
|
|
113
|
-
|
|
113
|
+
console.error(err.message);
|
|
114
114
|
});
|
|
115
115
|
|
|
116
116
|
app.start();
|
|
@@ -124,22 +124,23 @@ Creates a new SQS consumer.
|
|
|
124
124
|
|
|
125
125
|
#### Options
|
|
126
126
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
127
|
+
- `queueUrl` - _String_ - The SQS queue URL
|
|
128
|
+
- `region` - _String_ - The AWS region (default `eu-west-1`)
|
|
129
|
+
- `handleMessage` - _Function_ - An `async` function (or function that returns a `Promise`) to be called whenever a message is received. Receives an SQS message object as it's first argument.
|
|
130
|
+
- `handleMessageBatch` - _Function_ - An `async` function (or function that returns a `Promise`) to be called whenever a batch of messages is received. Similar to `handleMessage` but will receive the list of messages, not each message individually. **If both are set, `handleMessageBatch` overrides `handleMessage`**.
|
|
131
|
+
- `handleMessageTimeout` - _Number_ - Time in ms to wait for `handleMessage` to process a message before timing out. Emits `timeout_error` on timeout. By default, if `handleMessage` times out, the unprocessed message returns to the end of the queue.
|
|
132
|
+
- `attributeNames` - _Array_ - List of queue attributes to retrieve (i.e. `['All', 'ApproximateFirstReceiveTimestamp', 'ApproximateReceiveCount']`).
|
|
133
|
+
- `messageAttributeNames` - _Array_ - List of message attributes to retrieve (i.e. `['name', 'address']`).
|
|
134
|
+
- `batchSize` - _Number_ - The number of messages to request from SQS when polling (default `1`). This cannot be higher than the AWS limit of 10.
|
|
135
|
+
- `visibilityTimeout` - _Number_ - The duration (in seconds) that the received messages are hidden from subsequent retrieve requests after being retrieved by a ReceiveMessage request.
|
|
136
|
+
- `heartbeatInterval` - _Number_ - The interval (in seconds) between requests to extend the message visibility timeout. On each heartbeat the visibility is extended by adding `visibilityTimeout` to the number of seconds since the start of the handler function. This value must less than `visibilityTimeout`.
|
|
137
|
+
- `terminateVisibilityTimeout` - _Boolean_ - If true, sets the message visibility timeout to 0 after a `processing_error` (defaults to `false`).
|
|
138
|
+
- `waitTimeSeconds` - _Number_ - The duration (in seconds) for which the call will wait for a message to arrive in the queue before returning (defaults to `20`).
|
|
139
|
+
- `authenticationErrorTimeout` - _Number_ - The duration (in milliseconds) to wait before retrying after an authentication error (defaults to `10000`).
|
|
140
|
+
- `pollingWaitTimeMs` - _Number_ - The duration (in milliseconds) to wait before repolling the queue (defaults to `0`).
|
|
141
|
+
- `sqs` - _Object_ - An optional [AWS SQS](http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SQS.html) object to use if you need to configure the client manually
|
|
142
|
+
- `shouldDeleteMessages` - _Boolean_ - Default to `true`, if you don't want the package to delete messages from sqs set this to `false`
|
|
143
|
+
|
|
143
144
|
### `consumer.start()`
|
|
144
145
|
|
|
145
146
|
Start polling the queue for messages.
|
|
@@ -148,7 +149,7 @@ Start polling the queue for messages.
|
|
|
148
149
|
|
|
149
150
|
Stop polling the queue for messages.
|
|
150
151
|
|
|
151
|
-
### `consumer.isRunning`
|
|
152
|
+
### `consumer.isRunning`
|
|
152
153
|
|
|
153
154
|
Returns the current polling state of the consumer: `true` if it is actively polling, `false` if it is not.
|
|
154
155
|
|
|
@@ -156,20 +157,21 @@ Returns the current polling state of the consumer: `true` if it is actively poll
|
|
|
156
157
|
|
|
157
158
|
Each consumer is an [`EventEmitter`](http://nodejs.org/api/events.html) and emits the following events:
|
|
158
159
|
|
|
159
|
-
|Event|Params|Description|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
160
|
+
| Event | Params | Description |
|
|
161
|
+
| -------------------- | ------------------ | ------------------------------------------------------------------------------------------------------------------------------- |
|
|
162
|
+
| `error` | `err`, `[message]` | Fired when an error occurs interacting with the queue. If the error correlates to a message, that message is included in Params |
|
|
163
|
+
| `processing_error` | `err`, `message` | Fired when an error occurs processing the message. |
|
|
164
|
+
| `timeout_error` | `err`, `message` | Fired when `handleMessageTimeout` is supplied as an option and if `handleMessage` times out. |
|
|
165
|
+
| `message_received` | `message` | Fired when a message is received. |
|
|
166
|
+
| `message_processed` | `message` | Fired when a message is successfully processed and removed from the queue. |
|
|
167
|
+
| `response_processed` | None | Fired after one batch of items (up to `batchSize`) has been successfully processed. |
|
|
168
|
+
| `stopped` | None | Fired when the consumer finally stops its work. |
|
|
169
|
+
| `empty` | None | Fired when the queue is empty (All messages have been consumed). |
|
|
169
170
|
|
|
170
171
|
### AWS IAM Permissions
|
|
171
172
|
|
|
172
173
|
Consumer will receive and delete messages from the SQS queue. Ensure `sqs:ReceiveMessage`, `sqs:DeleteMessage`, `sqs:DeleteMessageBatch`, `sqs:ChangeMessageVisibility` and `sqs:ChangeMessageVisibilityBatch` access is granted on the queue being consumed.
|
|
173
174
|
|
|
174
|
-
### Contributing
|
|
175
|
-
|
|
175
|
+
### Contributing
|
|
176
|
+
|
|
177
|
+
See contributing [guidelines](https://github.com/bbc/sqs-consumer/blob/main/.github/CONTRIBUTING.md).
|
package/dist/consumer.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
|
-
import
|
|
2
|
+
import { SQS } from 'aws-sdk';
|
|
3
3
|
import { EventEmitter } from 'events';
|
|
4
|
-
export
|
|
4
|
+
export type SQSMessage = SQS.Types.Message;
|
|
5
5
|
export interface ConsumerOptions {
|
|
6
6
|
queueUrl?: string;
|
|
7
7
|
attributeNames?: string[];
|
|
@@ -22,14 +22,14 @@ export interface ConsumerOptions {
|
|
|
22
22
|
handleMessageBatch?(messages: SQSMessage[]): Promise<void>;
|
|
23
23
|
}
|
|
24
24
|
interface Events {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
25
|
+
response_processed: [];
|
|
26
|
+
empty: [];
|
|
27
|
+
message_received: [SQSMessage];
|
|
28
|
+
message_processed: [SQSMessage];
|
|
29
|
+
error: [Error, void | SQSMessage | SQSMessage[]];
|
|
30
|
+
timeout_error: [Error, SQSMessage];
|
|
31
|
+
processing_error: [Error, SQSMessage];
|
|
32
|
+
stopped: [];
|
|
33
33
|
}
|
|
34
34
|
export declare class Consumer extends EventEmitter {
|
|
35
35
|
private queueUrl;
|
package/dist/consumer.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Consumer = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const aws_sdk_1 = require("aws-sdk");
|
|
5
|
+
const debug_1 = require("debug");
|
|
6
6
|
const events_1 = require("events");
|
|
7
7
|
const bind_1 = require("./bind");
|
|
8
8
|
const errors_1 = require("./errors");
|
|
9
|
-
const debug =
|
|
9
|
+
const debug = (0, debug_1.default)('sqs-consumer');
|
|
10
10
|
const requiredOptions = [
|
|
11
11
|
'queueUrl',
|
|
12
12
|
// only one of handleMessage / handleMessagesBatch is required
|
|
@@ -31,13 +31,16 @@ function assertOptions(options) {
|
|
|
31
31
|
if (options.batchSize > 10 || options.batchSize < 1) {
|
|
32
32
|
throw new Error('SQS batchSize option must be between 1 and 10.');
|
|
33
33
|
}
|
|
34
|
-
if (options.heartbeatInterval &&
|
|
34
|
+
if (options.heartbeatInterval &&
|
|
35
|
+
!(options.heartbeatInterval < options.visibilityTimeout)) {
|
|
35
36
|
throw new Error('heartbeatInterval must be less than visibilityTimeout.');
|
|
36
37
|
}
|
|
37
38
|
}
|
|
38
39
|
function isConnectionError(err) {
|
|
39
40
|
if (err instanceof errors_1.SQSError) {
|
|
40
|
-
return (err.statusCode === 403 ||
|
|
41
|
+
return (err.statusCode === 403 ||
|
|
42
|
+
err.code === 'CredentialsError' ||
|
|
43
|
+
err.code === 'UnknownEndpoint');
|
|
41
44
|
}
|
|
42
45
|
return false;
|
|
43
46
|
}
|
|
@@ -68,16 +71,20 @@ class Consumer extends events_1.EventEmitter {
|
|
|
68
71
|
this.stopped = true;
|
|
69
72
|
this.batchSize = options.batchSize || 1;
|
|
70
73
|
this.visibilityTimeout = options.visibilityTimeout;
|
|
71
|
-
this.terminateVisibilityTimeout =
|
|
74
|
+
this.terminateVisibilityTimeout =
|
|
75
|
+
options.terminateVisibilityTimeout || false;
|
|
72
76
|
this.heartbeatInterval = options.heartbeatInterval;
|
|
73
77
|
this.waitTimeSeconds = (_a = options.waitTimeSeconds) !== null && _a !== void 0 ? _a : 20;
|
|
74
|
-
this.authenticationErrorTimeout =
|
|
78
|
+
this.authenticationErrorTimeout =
|
|
79
|
+
(_b = options.authenticationErrorTimeout) !== null && _b !== void 0 ? _b : 10000;
|
|
75
80
|
this.pollingWaitTimeMs = (_c = options.pollingWaitTimeMs) !== null && _c !== void 0 ? _c : 0;
|
|
76
81
|
this.shouldDeleteMessages = (_d = options.shouldDeleteMessages) !== null && _d !== void 0 ? _d : true;
|
|
77
|
-
this.sqs =
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
82
|
+
this.sqs =
|
|
83
|
+
options.sqs ||
|
|
84
|
+
new aws_sdk_1.SQS({
|
|
85
|
+
region: options.region || process.env.AWS_REGION || 'eu-west-1'
|
|
86
|
+
});
|
|
87
|
+
(0, bind_1.autoBind)(this);
|
|
81
88
|
}
|
|
82
89
|
emit(event, ...args) {
|
|
83
90
|
return super.emit(event, ...args);
|
|
@@ -149,9 +156,7 @@ class Consumer extends events_1.EventEmitter {
|
|
|
149
156
|
}
|
|
150
157
|
async receiveMessage(params) {
|
|
151
158
|
try {
|
|
152
|
-
return await this.sqs
|
|
153
|
-
.receiveMessage(params)
|
|
154
|
-
.promise();
|
|
159
|
+
return await this.sqs.receiveMessage(params).promise();
|
|
155
160
|
}
|
|
156
161
|
catch (err) {
|
|
157
162
|
throw toSQSError(err, `SQS receive message failed: ${err.message}`);
|
|
@@ -168,9 +173,7 @@ class Consumer extends events_1.EventEmitter {
|
|
|
168
173
|
ReceiptHandle: message.ReceiptHandle
|
|
169
174
|
};
|
|
170
175
|
try {
|
|
171
|
-
await this.sqs
|
|
172
|
-
.deleteMessage(deleteParams)
|
|
173
|
-
.promise();
|
|
176
|
+
await this.sqs.deleteMessage(deleteParams).promise();
|
|
174
177
|
}
|
|
175
178
|
catch (err) {
|
|
176
179
|
throw toSQSError(err, `SQS delete message failed: ${err.message}`);
|
|
@@ -182,10 +185,7 @@ class Consumer extends events_1.EventEmitter {
|
|
|
182
185
|
try {
|
|
183
186
|
if (this.handleMessageTimeout) {
|
|
184
187
|
[timeout, pending] = createTimeout(this.handleMessageTimeout);
|
|
185
|
-
await Promise.race([
|
|
186
|
-
this.handleMessage(message),
|
|
187
|
-
pending
|
|
188
|
-
]);
|
|
188
|
+
await Promise.race([this.handleMessage(message), pending]);
|
|
189
189
|
}
|
|
190
190
|
else {
|
|
191
191
|
await this.handleMessage(message);
|
|
@@ -253,9 +253,11 @@ class Consumer extends events_1.EventEmitter {
|
|
|
253
253
|
currentPollingTimeout = this.authenticationErrorTimeout;
|
|
254
254
|
}
|
|
255
255
|
return;
|
|
256
|
-
})
|
|
256
|
+
})
|
|
257
|
+
.then(() => {
|
|
257
258
|
setTimeout(this.poll, currentPollingTimeout);
|
|
258
|
-
})
|
|
259
|
+
})
|
|
260
|
+
.catch((err) => {
|
|
259
261
|
this.emit('error', err);
|
|
260
262
|
});
|
|
261
263
|
}
|
|
@@ -300,9 +302,7 @@ class Consumer extends events_1.EventEmitter {
|
|
|
300
302
|
}))
|
|
301
303
|
};
|
|
302
304
|
try {
|
|
303
|
-
await this.sqs
|
|
304
|
-
.deleteMessageBatch(deleteParams)
|
|
305
|
-
.promise();
|
|
305
|
+
await this.sqs.deleteMessageBatch(deleteParams).promise();
|
|
306
306
|
}
|
|
307
307
|
catch (err) {
|
|
308
308
|
throw toSQSError(err, `SQS delete message failed: ${err.message}`);
|
|
@@ -327,9 +327,7 @@ class Consumer extends events_1.EventEmitter {
|
|
|
327
327
|
}))
|
|
328
328
|
};
|
|
329
329
|
try {
|
|
330
|
-
return await this.sqs
|
|
331
|
-
.changeMessageVisibilityBatch(params)
|
|
332
|
-
.promise();
|
|
330
|
+
return await this.sqs.changeMessageVisibilityBatch(params).promise();
|
|
333
331
|
}
|
|
334
332
|
catch (err) {
|
|
335
333
|
this.emit('error', toSQSError(err, `Error changing visibility timeout: ${err.message}`), messages);
|
package/dist/index.js
CHANGED