sqs-consumer 7.5.0 → 8.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.
@@ -5,6 +5,11 @@ on:
5
5
  pull_request_target:
6
6
  types: [opened,closed,synchronize]
7
7
 
8
+ permissions:
9
+ contents: write
10
+ issues: write
11
+ pull-requests: write
12
+
8
13
  jobs:
9
14
  CLAAssistant:
10
15
  runs-on: ubuntu-latest
package/README.md CHANGED
@@ -50,10 +50,14 @@ app.start();
50
50
  ```
51
51
 
52
52
  - The queue is polled continuously for messages using [long polling](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-long-polling.html).
53
- - Messages are deleted from the queue once the handler function has completed successfully.
54
53
  - 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](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/SQSDeadLetterQueue.html) can be used to move messages that cannot be processed to a dead letter queue.
55
- - 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).
56
- - By default, messages that are sent to the `handleMessage` and `handleMessageBatch` functions will be considered as processed if they return without an error. To acknowledge individual messages, please return the message that you want to acknowledge if you are using `handleMessage` or the messages for `handleMessageBatch`. It's also important to await any processing that you are doing to ensure that messages are processed one at a time.
54
+ - 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 here](https://bbc.github.io/sqs-consumer/interfaces/ConsumerOptions.html#batchSize).
55
+ - It's also important to await any processing that you are doing to ensure that messages are processed one at a time.
56
+ - By default, messages that are sent to the `handleMessage` and `handleMessageBatch` functions will be considered as processed if they return without an error.
57
+ - To acknowledge individual messages, please return the message that you want to acknowledge if you are using `handleMessage` or the messages for `handleMessageBatch`.
58
+ - To note, returning an empty object or an empty array will be considered an acknowledgement of no message(s) and will result in no messages being deleted. If you would like to change this behaviour, please use the `alwaysAcknowledge` option [detailed here](https://bbc.github.io/sqs-consumer/interfaces/ConsumerOptions.html).
59
+ - By default, if an object or an array is not returned, all messages will be acknowledged.
60
+ - Messages are deleted from the queue once the handler function has completed successfully (the above items should also be taken into account).
57
61
 
58
62
  ### Credentials
59
63
 
@@ -16,6 +16,7 @@ export declare class Consumer extends TypedEventEmitter {
16
16
  private attributeNames;
17
17
  private messageAttributeNames;
18
18
  private shouldDeleteMessages;
19
+ private alwaysAcknowledge;
19
20
  private batchSize;
20
21
  private visibilityTimeout;
21
22
  private terminateVisibilityTimeout;
package/dist/consumer.js CHANGED
@@ -12,7 +12,7 @@ const logger_1 = require("./logger");
12
12
  */
13
13
  class Consumer extends emitter_1.TypedEventEmitter {
14
14
  constructor(options) {
15
- var _a, _b, _c, _d;
15
+ var _a, _b, _c, _d, _e;
16
16
  super();
17
17
  this.pollingTimeoutId = undefined;
18
18
  this.stopped = true;
@@ -35,6 +35,7 @@ class Consumer extends emitter_1.TypedEventEmitter {
35
35
  (_b = options.authenticationErrorTimeout) !== null && _b !== void 0 ? _b : 10000;
36
36
  this.pollingWaitTimeMs = (_c = options.pollingWaitTimeMs) !== null && _c !== void 0 ? _c : 0;
37
37
  this.shouldDeleteMessages = (_d = options.shouldDeleteMessages) !== null && _d !== void 0 ? _d : true;
38
+ this.alwaysAcknowledge = (_e = options.alwaysAcknowledge) !== null && _e !== void 0 ? _e : false;
38
39
  this.sqs =
39
40
  options.sqs ||
40
41
  new client_sqs_1.SQSClient({
@@ -346,7 +347,9 @@ class Consumer extends emitter_1.TypedEventEmitter {
346
347
  else {
347
348
  result = await this.handleMessage(message);
348
349
  }
349
- return result instanceof Object ? result : message;
350
+ return !this.alwaysAcknowledge && result instanceof Object
351
+ ? result
352
+ : message;
350
353
  }
351
354
  catch (err) {
352
355
  if (err instanceof errors_1.TimeoutError) {
@@ -370,7 +373,9 @@ class Consumer extends emitter_1.TypedEventEmitter {
370
373
  async executeBatchHandler(messages) {
371
374
  try {
372
375
  const result = await this.handleMessageBatch(messages);
373
- return result instanceof Object ? result : messages;
376
+ return !this.alwaysAcknowledge && result instanceof Object
377
+ ? result
378
+ : messages;
374
379
  }
375
380
  catch (err) {
376
381
  if (err instanceof Error) {
package/dist/types.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { SQSClient, Message } from '@aws-sdk/client-sqs';
1
+ import { SQSClient, Message, QueueAttributeName } from '@aws-sdk/client-sqs';
2
2
  export interface ConsumerOptions {
3
3
  /**
4
4
  * The SQS queue URL.
@@ -9,7 +9,7 @@ export interface ConsumerOptions {
9
9
  * `['All', 'ApproximateFirstReceiveTimestamp', 'ApproximateReceiveCount']`).
10
10
  * @defaultvalue `[]`
11
11
  */
12
- attributeNames?: string[];
12
+ attributeNames?: QueueAttributeName[];
13
13
  /**
14
14
  * List of message attributes to retrieve (i.e. `['name', 'address']`).
15
15
  * @defaultvalue `[]`
@@ -83,6 +83,14 @@ export interface ConsumerOptions {
83
83
  * @defaultvalue `true`
84
84
  */
85
85
  shouldDeleteMessages?: boolean;
86
+ /**
87
+ * By default, the consumer will treat an empty object or array from either of the
88
+ * handlers as a acknowledgement of no messages and will not delete those messages as
89
+ * a result. Set this to `true` to always acknowledge all messages no matter the returned
90
+ * value.
91
+ * @defaultvalue `false`
92
+ */
93
+ alwaysAcknowledge?: boolean;
86
94
  /**
87
95
  * An `async` function (or function that returns a `Promise`) to be called whenever
88
96
  * a message is received.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sqs-consumer",
3
- "version": "7.5.0",
3
+ "version": "8.1.0",
4
4
  "description": "Build SQS-based Node applications without the boilerplate",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -55,17 +55,17 @@
55
55
  "p-event": "^4.2.0",
56
56
  "prettier": "^3.0.3",
57
57
  "sinon": "^17.0.0",
58
- "sqs-producer": "^3.4.0",
58
+ "sqs-producer": "^4.0.0",
59
59
  "ts-node": "^10.9.1",
60
60
  "typedoc": "^0.25.2",
61
61
  "typescript": "^5.2.2"
62
62
  },
63
63
  "dependencies": {
64
- "@aws-sdk/client-sqs": "^3.428.0",
64
+ "@aws-sdk/client-sqs": "^3.447.0",
65
65
  "debug": "^4.3.4"
66
66
  },
67
67
  "peerDependencies": {
68
- "@aws-sdk/client-sqs": "^3.428.0"
68
+ "@aws-sdk/client-sqs": "^3.447.0"
69
69
  },
70
70
  "mocha": {
71
71
  "spec": "test/tests/**/**/*.test.ts",
package/src/consumer.ts CHANGED
@@ -13,7 +13,8 @@ import {
13
13
  DeleteMessageBatchCommandInput,
14
14
  ReceiveMessageCommand,
15
15
  ReceiveMessageCommandInput,
16
- ReceiveMessageCommandOutput
16
+ ReceiveMessageCommandOutput,
17
+ QueueAttributeName
17
18
  } from '@aws-sdk/client-sqs';
18
19
 
19
20
  import { ConsumerOptions, StopOptions, UpdatableOptions } from './types';
@@ -43,9 +44,10 @@ export class Consumer extends TypedEventEmitter {
43
44
  private postReceiveMessageCallback?: () => Promise<void>;
44
45
  private sqs: SQSClient;
45
46
  private handleMessageTimeout: number;
46
- private attributeNames: string[];
47
+ private attributeNames: QueueAttributeName[];
47
48
  private messageAttributeNames: string[];
48
49
  private shouldDeleteMessages: boolean;
50
+ private alwaysAcknowledge: boolean;
49
51
  private batchSize: number;
50
52
  private visibilityTimeout: number;
51
53
  private terminateVisibilityTimeout: boolean;
@@ -76,6 +78,7 @@ export class Consumer extends TypedEventEmitter {
76
78
  options.authenticationErrorTimeout ?? 10000;
77
79
  this.pollingWaitTimeMs = options.pollingWaitTimeMs ?? 0;
78
80
  this.shouldDeleteMessages = options.shouldDeleteMessages ?? true;
81
+ this.alwaysAcknowledge = options.alwaysAcknowledge ?? false;
79
82
  this.sqs =
80
83
  options.sqs ||
81
84
  new SQSClient({
@@ -452,7 +455,9 @@ export class Consumer extends TypedEventEmitter {
452
455
  result = await this.handleMessage(message);
453
456
  }
454
457
 
455
- return result instanceof Object ? result : message;
458
+ return !this.alwaysAcknowledge && result instanceof Object
459
+ ? result
460
+ : message;
456
461
  } catch (err) {
457
462
  if (err instanceof TimeoutError) {
458
463
  throw toTimeoutError(
@@ -481,7 +486,9 @@ export class Consumer extends TypedEventEmitter {
481
486
  try {
482
487
  const result = await this.handleMessageBatch(messages);
483
488
 
484
- return result instanceof Object ? result : messages;
489
+ return !this.alwaysAcknowledge && result instanceof Object
490
+ ? result
491
+ : messages;
485
492
  } catch (err) {
486
493
  if (err instanceof Error) {
487
494
  throw toStandardError(
package/src/types.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { SQSClient, Message } from '@aws-sdk/client-sqs';
1
+ import { SQSClient, Message, QueueAttributeName } from '@aws-sdk/client-sqs';
2
2
 
3
3
  export interface ConsumerOptions {
4
4
  /**
@@ -10,7 +10,7 @@ export interface ConsumerOptions {
10
10
  * `['All', 'ApproximateFirstReceiveTimestamp', 'ApproximateReceiveCount']`).
11
11
  * @defaultvalue `[]`
12
12
  */
13
- attributeNames?: string[];
13
+ attributeNames?: QueueAttributeName[];
14
14
  /**
15
15
  * List of message attributes to retrieve (i.e. `['name', 'address']`).
16
16
  * @defaultvalue `[]`
@@ -84,6 +84,14 @@ export interface ConsumerOptions {
84
84
  * @defaultvalue `true`
85
85
  */
86
86
  shouldDeleteMessages?: boolean;
87
+ /**
88
+ * By default, the consumer will treat an empty object or array from either of the
89
+ * handlers as a acknowledgement of no messages and will not delete those messages as
90
+ * a result. Set this to `true` to always acknowledge all messages no matter the returned
91
+ * value.
92
+ * @defaultvalue `false`
93
+ */
94
+ alwaysAcknowledge?: boolean;
87
95
  /**
88
96
  * An `async` function (or function that returns a `Promise`) to be called whenever
89
97
  * a message is received.