sqs-consumer 6.1.0 → 6.2.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/dist/types.d.ts CHANGED
@@ -1,33 +1,166 @@
1
+ /// <reference types="node" />
1
2
  import { SQSClient, Message } from '@aws-sdk/client-sqs';
3
+ import { EventEmitter } from 'events';
2
4
  export interface ConsumerOptions {
5
+ /**
6
+ * The SQS queue URL.
7
+ */
3
8
  queueUrl: string;
9
+ /**
10
+ * List of queue attributes to retrieve (i.e.
11
+ * `['All', 'ApproximateFirstReceiveTimestamp', 'ApproximateReceiveCount']`).
12
+ * @defaultvalue `[]`
13
+ */
4
14
  attributeNames?: string[];
15
+ /**
16
+ * List of message attributes to retrieve (i.e. `['name', 'address']`).
17
+ * @defaultvalue `[]`
18
+ */
5
19
  messageAttributeNames?: string[];
20
+ /** @hidden */
6
21
  stopped?: boolean;
22
+ /**
23
+ * The number of messages to request from SQS when polling (default `1`).
24
+ *
25
+ * This cannot be higher than the
26
+ * [AWS limit of 10](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/quotas-messages.html).
27
+ * @defaultvalue `1`
28
+ */
7
29
  batchSize?: number;
30
+ /**
31
+ * The duration (in seconds) that the received messages are hidden from subsequent
32
+ * retrieve requests after being retrieved by a ReceiveMessage request.
33
+ */
8
34
  visibilityTimeout?: number;
35
+ /**
36
+ * The duration (in seconds) for which the call will wait for a message to arrive in
37
+ * the queue before returning.
38
+ * @defaultvalue `20`
39
+ */
9
40
  waitTimeSeconds?: number;
41
+ /**
42
+ * The duration (in milliseconds) to wait before retrying after an authentication error.
43
+ * @defaultvalue `10000`
44
+ */
10
45
  authenticationErrorTimeout?: number;
46
+ /**
47
+ * The duration (in milliseconds) to wait before repolling the queue.
48
+ * @defaultvalue `0`
49
+ */
11
50
  pollingWaitTimeMs?: number;
51
+ /**
52
+ * If true, sets the message visibility timeout to 0 after a `processing_error`.
53
+ * @defaultvalue `false`
54
+ */
12
55
  terminateVisibilityTimeout?: boolean;
56
+ /**
57
+ * The interval (in seconds) between requests to extend the message visibility timeout.
58
+ *
59
+ * On each heartbeat the visibility is extended by adding `visibilityTimeout` to
60
+ * the number of seconds since the start of the handler function.
61
+ *
62
+ * This value must less than `visibilityTimeout`.
63
+ */
13
64
  heartbeatInterval?: number;
65
+ /**
66
+ * An optional [SQS Client](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-sqs/classes/sqsclient.html)
67
+ * object to use if you need to configure the client manually.
68
+ */
14
69
  sqs?: SQSClient;
70
+ /**
71
+ * The AWS region.
72
+ * @defaultValue `eu-west-1`
73
+ */
15
74
  region?: string;
75
+ /**
76
+ * Time in ms to wait for `handleMessage` to process a message before timing out.
77
+ *
78
+ * Emits `timeout_error` on timeout. By default, if `handleMessage` times out,
79
+ * the unprocessed message returns to the end of the queue.
80
+ */
16
81
  handleMessageTimeout?: number;
82
+ /**
83
+ * Default to `true`, if you don't want the package to delete messages from sqs
84
+ * set this to `false`.
85
+ * @defaultvalue `true`
86
+ */
17
87
  shouldDeleteMessages?: boolean;
18
- handleMessage?(message: Message): Promise<void>;
88
+ /**
89
+ * An `async` function (or function that returns a `Promise`) to be called whenever
90
+ * a message is received.
91
+ *
92
+ * In the case that you need to acknowledge the message, return an object containing
93
+ * the MessageId that you'd like to acknowledge.
94
+ */
95
+ handleMessage?(message: Message): Promise<Message | void>;
96
+ /**
97
+ * An `async` function (or function that returns a `Promise`) to be called whenever
98
+ * a batch of messages is received. Similar to `handleMessage` but will receive the
99
+ * list of messages, not each message individually.
100
+ *
101
+ * **If both are set, `handleMessageBatch` overrides `handleMessage`**.
102
+ *
103
+ * In the case that you need to ack only some of the messages, return an array with
104
+ * the successful messages only.
105
+ */
19
106
  handleMessageBatch?(messages: Message[]): Promise<Message[] | void>;
20
107
  }
21
108
  export interface Events {
109
+ /**
110
+ * Fired after one batch of items (up to `batchSize`) has been successfully processed.
111
+ */
22
112
  response_processed: [];
113
+ /**
114
+ * Fired when the queue is empty (All messages have been consumed).
115
+ */
23
116
  empty: [];
117
+ /**
118
+ * Fired when a message is received.
119
+ */
24
120
  message_received: [Message];
121
+ /**
122
+ * Fired when a message is successfully processed and removed from the queue.
123
+ */
25
124
  message_processed: [Message];
125
+ /**
126
+ * Fired when an error occurs interacting with the queue.
127
+ *
128
+ * If the error correlates to a message, that message is included in Params
129
+ */
26
130
  error: [Error, void | Message | Message[]];
131
+ /**
132
+ * Fired when `handleMessageTimeout` is supplied as an option and if
133
+ * `handleMessage` times out.
134
+ */
27
135
  timeout_error: [Error, Message];
136
+ /**
137
+ * Fired when an error occurs processing the message.
138
+ */
28
139
  processing_error: [Error, Message];
140
+ /**
141
+ * Fired when the consumer finally stops its work.
142
+ */
29
143
  stopped: [];
30
144
  }
145
+ export declare class TypedEventEmitter extends EventEmitter {
146
+ /**
147
+ * Trigger a listener on all emitted events
148
+ * @param event The name of the event to listen to
149
+ * @param listener A function to trigger when the event is emitted
150
+ */
151
+ on<E extends keyof Events>(event: E, listener: (...args: Events[E]) => void): this;
152
+ /**
153
+ * Trigger a listener only once for an emitted event
154
+ * @param event The name of the event to listen to
155
+ * @param listener A function to trigger when the event is emitted
156
+ */
157
+ once<E extends keyof Events>(event: E, listener: (...args: Events[E]) => void): this;
158
+ /**
159
+ * Emits an event with the provided arguments
160
+ * @param event The name of the event to emit
161
+ */
162
+ emit<E extends keyof Events>(event: E, ...args: Events[E]): boolean;
163
+ }
31
164
  export type AWSError = {
32
165
  /**
33
166
  * Name, eg. ConditionalCheckFailedException
package/dist/types.js CHANGED
@@ -1,2 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TypedEventEmitter = void 0;
4
+ const events_1 = require("events");
5
+ class TypedEventEmitter extends events_1.EventEmitter {
6
+ /**
7
+ * Trigger a listener on all emitted events
8
+ * @param event The name of the event to listen to
9
+ * @param listener A function to trigger when the event is emitted
10
+ */
11
+ on(event, listener) {
12
+ return super.on(event, listener);
13
+ }
14
+ /**
15
+ * Trigger a listener only once for an emitted event
16
+ * @param event The name of the event to listen to
17
+ * @param listener A function to trigger when the event is emitted
18
+ */
19
+ once(event, listener) {
20
+ return super.on(event, listener);
21
+ }
22
+ /**
23
+ * Emits an event with the provided arguments
24
+ * @param event The name of the event to emit
25
+ */
26
+ emit(event, ...args) {
27
+ return super.emit(event, ...args);
28
+ }
29
+ }
30
+ exports.TypedEventEmitter = TypedEventEmitter;
@@ -0,0 +1,13 @@
1
+ import { ReceiveMessageCommandOutput } from '@aws-sdk/client-sqs';
2
+ import { ConsumerOptions } from './types';
3
+ /**
4
+ * Ensure that the required options have been set.
5
+ * @param options The options that have been set by the application.
6
+ */
7
+ declare function assertOptions(options: ConsumerOptions): void;
8
+ /**
9
+ * Determine if the response from SQS has messages in it.
10
+ * @param response The response from SQS.
11
+ */
12
+ declare function hasMessages(response: ReceiveMessageCommandOutput): boolean;
13
+ export { hasMessages, assertOptions };
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.assertOptions = exports.hasMessages = void 0;
4
+ const requiredOptions = [
5
+ 'queueUrl',
6
+ // only one of handleMessage / handleMessagesBatch is required
7
+ 'handleMessage|handleMessageBatch'
8
+ ];
9
+ /**
10
+ * Ensure that the required options have been set.
11
+ * @param options The options that have been set by the application.
12
+ */
13
+ function assertOptions(options) {
14
+ requiredOptions.forEach((option) => {
15
+ const possibilities = option.split('|');
16
+ if (!possibilities.find((p) => options[p])) {
17
+ throw new Error(`Missing SQS consumer option [ ${possibilities.join(' or ')} ].`);
18
+ }
19
+ });
20
+ if (options.batchSize > 10 || options.batchSize < 1) {
21
+ throw new Error('SQS batchSize option must be between 1 and 10.');
22
+ }
23
+ if (options.heartbeatInterval &&
24
+ !(options.heartbeatInterval < options.visibilityTimeout)) {
25
+ throw new Error('heartbeatInterval must be less than visibilityTimeout.');
26
+ }
27
+ }
28
+ exports.assertOptions = assertOptions;
29
+ /**
30
+ * Determine if the response from SQS has messages in it.
31
+ * @param response The response from SQS.
32
+ */
33
+ function hasMessages(response) {
34
+ return response.Messages && response.Messages.length > 0;
35
+ }
36
+ exports.hasMessages = hasMessages;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sqs-consumer",
3
- "version": "6.1.0",
3
+ "version": "6.2.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",
@@ -17,7 +17,8 @@
17
17
  "lint:fix": "eslint . --fix",
18
18
  "format": "prettier --loglevel warn --write \"**/*.{js,json,jsx,md,ts,tsx,html}\"",
19
19
  "format:check": "prettier --check \"**/*.{js,json,jsx,md,ts,tsx,html}\"",
20
- "posttest": "npm run lint && npm run format:check"
20
+ "posttest": "npm run lint && npm run format:check",
21
+ "generate-docs": "typedoc"
21
22
  },
22
23
  "repository": {
23
24
  "type": "git",
@@ -26,7 +27,7 @@
26
27
  "bugs": {
27
28
  "url": "https://github.com/BBC/sqs-consumer/issues"
28
29
  },
29
- "homepage": "https://github.com/BBC/sqs-consumer",
30
+ "homepage": "https://bbc.github.io/sqs-consumer/",
30
31
  "keywords": [
31
32
  "sqs",
32
33
  "queue",
@@ -37,26 +38,27 @@
37
38
  "@types/chai": "^4.3.4",
38
39
  "@types/debug": "^4.1.7",
39
40
  "@types/mocha": "^10.0.1",
40
- "@types/node": "^16.18.7",
41
+ "@types/node": "^18.11.18",
41
42
  "@types/sinon": "^10.0.13",
42
43
  "chai": "^4.3.7",
43
- "eslint": "^8.29.0",
44
+ "eslint": "^8.31.0",
44
45
  "eslint-config-iplayer-ts": "^4.1.0",
45
- "eslint-config-prettier": "^4.3.0",
46
- "mocha": "^10.1.0",
46
+ "eslint-config-prettier": "^8.5.0",
47
+ "mocha": "^10.2.0",
47
48
  "nyc": "^15.1.0",
48
49
  "p-event": "^4.2.0",
49
50
  "prettier": "^2.8.1",
50
- "sinon": "^15.0.0",
51
+ "sinon": "^15.0.1",
51
52
  "ts-node": "^10.9.1",
53
+ "typedoc": "^0.23.23",
52
54
  "typescript": "^4.9.4"
53
55
  },
54
56
  "dependencies": {
55
- "@aws-sdk/client-sqs": "^3.226.0",
57
+ "@aws-sdk/client-sqs": "^3.241.0",
56
58
  "debug": "^4.3.4"
57
59
  },
58
60
  "peerDependencies": {
59
- "@aws-sdk/client-sqs": "^3.226.0"
61
+ "@aws-sdk/client-sqs": "^3.241.0"
60
62
  },
61
63
  "mocha": {
62
64
  "spec": "test/**/**/*.test.ts",
@@ -78,9 +80,7 @@
78
80
  "eslintConfig": {
79
81
  "extends": [
80
82
  "iplayer-ts",
81
- "prettier",
82
- "prettier/react",
83
- "prettier/@typescript-eslint"
83
+ "prettier"
84
84
  ],
85
85
  "parserOptions": {
86
86
  "sourceType": "module"
package/src/bind.ts CHANGED
@@ -1,7 +1,16 @@
1
+ /**
2
+ * Determines if the property is a method
3
+ * @param propertyName the name of the property
4
+ * @param value the value of the property
5
+ */
1
6
  function isMethod(propertyName: string, value: any): boolean {
2
7
  return propertyName !== 'constructor' && typeof value === 'function';
3
8
  }
4
9
 
10
+ /**
11
+ * Auto binds the provided properties
12
+ * @param obj an object containing the available properties
13
+ */
5
14
  export function autoBind(obj: object): void {
6
15
  const propertyNames = Object.getOwnPropertyNames(obj.constructor.prototype);
7
16
  propertyNames.forEach((propertyName) => {