sqs-consumer 7.0.3 → 7.1.0-canary.2

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.
@@ -0,0 +1,17 @@
1
+ body:
2
+ - type: textarea
3
+ attributes:
4
+ label: Background
5
+ description: Why do you think this feature is needed? Are there current alternatives?
6
+ validations:
7
+ required: true
8
+ - type: textarea
9
+ attributes:
10
+ label: Objectives
11
+ description: What should this feature request aim to address?
12
+ value: |
13
+ 1.
14
+ 2.
15
+ 3.
16
+ validations:
17
+ required: true
@@ -0,0 +1,13 @@
1
+ body:
2
+ - type: textarea
3
+ attributes:
4
+ label: Summary
5
+ description: What do you need help with?
6
+ validations:
7
+ required: true
8
+ - type: input
9
+ attributes:
10
+ label: Example
11
+ description: A link to a minimal reproduction is helpful for debugging!
12
+ validations:
13
+ required: false
package/README.md CHANGED
@@ -12,14 +12,14 @@ Build SQS-based applications without the boilerplate. Just define an async funct
12
12
  To install this package, simply enter the following command into your terminal (or the variant of whatever package manager you are using):
13
13
 
14
14
  ```bash
15
- npm install --save-dev sqs-consumer
15
+ npm install sqs-consumer
16
16
  ```
17
17
 
18
18
  > **Note**
19
19
  > This library assumes you are using [AWS SDK v3](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-sqs/index.html). If you are using v2, please install v5.8.0:
20
20
  >
21
21
  > ```bash
22
- > npm install sqs-consumer@5.8.0 --save-dev
22
+ > npm install sqs-consumer@5.8.0
23
23
  > ```
24
24
 
25
25
  ### Node version
@@ -127,6 +127,12 @@ By default, the value of `abort` is set to `false` which means pre existing requ
127
127
 
128
128
  Returns the current polling state of the consumer: `true` if it is actively polling, `false` if it is not.
129
129
 
130
+ ### `consumer.updateOption(option, value)`
131
+
132
+ Updates the provided option with the provided value.
133
+
134
+ You can [find out more about this here](https://bbc.github.io/sqs-consumer/classes/Consumer.html#updateOption).
135
+
130
136
  ### Events
131
137
 
132
138
  Each consumer is an [`EventEmitter`](https://nodejs.org/api/events.html) and [emits these events](https://bbc.github.io/sqs-consumer/interfaces/Events.html).
@@ -1,4 +1,4 @@
1
- import { ConsumerOptions, TypedEventEmitter, StopOptions } from './types';
1
+ import { ConsumerOptions, TypedEventEmitter, StopOptions, UpdatableOptions } from './types';
2
2
  /**
3
3
  * [Usage](https://bbc.github.io/sqs-consumer/index.html#usage)
4
4
  */
@@ -37,6 +37,17 @@ export declare class Consumer extends TypedEventEmitter {
37
37
  * Returns the current polling state of the consumer: `true` if it is actively polling, `false` if it is not.
38
38
  */
39
39
  get isRunning(): boolean;
40
+ /**
41
+ * Updates visibilityTimeout to the provided value.
42
+ * @param value The value to set visibilityTimeout to
43
+ */
44
+ private updateVisibilityTimeout;
45
+ /**
46
+ * Updates the provided option to the provided value.
47
+ * @param option The option that you want to update
48
+ * @param value The value to set the option to
49
+ */
50
+ updateOption(option: UpdatableOptions, value: ConsumerOptions[UpdatableOptions]): void;
40
51
  /**
41
52
  * Emit one of the consumer's error events depending on the error received.
42
53
  * @param err The error object to forward on
package/dist/consumer.js CHANGED
@@ -61,6 +61,7 @@ class Consumer extends types_1.TypedEventEmitter {
61
61
  if (this.stopped) {
62
62
  debug('Starting consumer');
63
63
  this.stopped = false;
64
+ this.emit('started');
64
65
  this.poll();
65
66
  }
66
67
  }
@@ -91,6 +92,36 @@ class Consumer extends types_1.TypedEventEmitter {
91
92
  get isRunning() {
92
93
  return !this.stopped;
93
94
  }
95
+ /**
96
+ * Updates visibilityTimeout to the provided value.
97
+ * @param value The value to set visibilityTimeout to
98
+ */
99
+ updateVisibilityTimeout(value) {
100
+ if (typeof value !== 'number') {
101
+ throw new Error('visibilityTimeout must be a number');
102
+ }
103
+ if (typeof value !== 'number' ||
104
+ (this.heartbeatInterval && value <= this.heartbeatInterval)) {
105
+ throw new Error('heartbeatInterval must be less than visibilityTimeout.');
106
+ }
107
+ debug(`Updating the visibilityTimeout option to the value ${value}`);
108
+ this.visibilityTimeout = value;
109
+ this.emit('option_updated', 'visibilityTimeout', value);
110
+ }
111
+ /**
112
+ * Updates the provided option to the provided value.
113
+ * @param option The option that you want to update
114
+ * @param value The value to set the option to
115
+ */
116
+ updateOption(option, value) {
117
+ switch (option) {
118
+ case 'visibilityTimeout':
119
+ this.updateVisibilityTimeout(value);
120
+ break;
121
+ default:
122
+ throw new Error(`The update ${option} cannot be updated`);
123
+ }
124
+ }
94
125
  /**
95
126
  * Emit one of the consumer's error events depending on the error received.
96
127
  * @param err The error object to forward on
package/dist/types.d.ts CHANGED
@@ -105,6 +105,7 @@ export interface ConsumerOptions {
105
105
  */
106
106
  handleMessageBatch?(messages: Message[]): Promise<Message[] | void>;
107
107
  }
108
+ export type UpdatableOptions = 'visibilityTimeout';
108
109
  export interface StopOptions {
109
110
  /**
110
111
  * Default to `false`, if you want the stop action to also abort requests to SQS
@@ -149,10 +150,18 @@ export interface Events {
149
150
  * Fired when requests to SQS were aborted.
150
151
  */
151
152
  aborted: [];
153
+ /**
154
+ * Fired when the consumer starts its work..
155
+ */
156
+ started: [];
152
157
  /**
153
158
  * Fired when the consumer finally stops its work.
154
159
  */
155
160
  stopped: [];
161
+ /**
162
+ * Fired when an option is updated
163
+ */
164
+ option_updated: [UpdatableOptions, ConsumerOptions[UpdatableOptions]];
156
165
  }
157
166
  export declare class TypedEventEmitter extends EventEmitter {
158
167
  /**
package/dist/types.js CHANGED
@@ -17,7 +17,7 @@ class TypedEventEmitter extends events_1.EventEmitter {
17
17
  * @param listener A function to trigger when the event is emitted
18
18
  */
19
19
  once(event, listener) {
20
- return super.on(event, listener);
20
+ return super.once(event, listener);
21
21
  }
22
22
  /**
23
23
  * Emits an event with the provided arguments
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sqs-consumer",
3
- "version": "7.0.3",
3
+ "version": "7.1.0-canary.2",
4
4
  "description": "Build SQS-based Node applications without the boilerplate",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
package/src/consumer.ts CHANGED
@@ -17,7 +17,12 @@ import {
17
17
  } from '@aws-sdk/client-sqs';
18
18
  import Debug from 'debug';
19
19
 
20
- import { ConsumerOptions, TypedEventEmitter, StopOptions } from './types';
20
+ import {
21
+ ConsumerOptions,
22
+ TypedEventEmitter,
23
+ StopOptions,
24
+ UpdatableOptions
25
+ } from './types';
21
26
  import { autoBind } from './bind';
22
27
  import {
23
28
  SQSError,
@@ -93,6 +98,7 @@ export class Consumer extends TypedEventEmitter {
93
98
  if (this.stopped) {
94
99
  debug('Starting consumer');
95
100
  this.stopped = false;
101
+ this.emit('started');
96
102
  this.poll();
97
103
  }
98
104
  }
@@ -132,6 +138,47 @@ export class Consumer extends TypedEventEmitter {
132
138
  return !this.stopped;
133
139
  }
134
140
 
141
+ /**
142
+ * Updates visibilityTimeout to the provided value.
143
+ * @param value The value to set visibilityTimeout to
144
+ */
145
+ private updateVisibilityTimeout(value: ConsumerOptions['visibilityTimeout']) {
146
+ if (typeof value !== 'number') {
147
+ throw new Error('visibilityTimeout must be a number');
148
+ }
149
+
150
+ if (
151
+ typeof value !== 'number' ||
152
+ (this.heartbeatInterval && value <= this.heartbeatInterval)
153
+ ) {
154
+ throw new Error('heartbeatInterval must be less than visibilityTimeout.');
155
+ }
156
+
157
+ debug(`Updating the visibilityTimeout option to the value ${value}`);
158
+
159
+ this.visibilityTimeout = value;
160
+
161
+ this.emit('option_updated', 'visibilityTimeout', value);
162
+ }
163
+
164
+ /**
165
+ * Updates the provided option to the provided value.
166
+ * @param option The option that you want to update
167
+ * @param value The value to set the option to
168
+ */
169
+ public updateOption(
170
+ option: UpdatableOptions,
171
+ value: ConsumerOptions[UpdatableOptions]
172
+ ) {
173
+ switch (option) {
174
+ case 'visibilityTimeout':
175
+ this.updateVisibilityTimeout(value);
176
+ break;
177
+ default:
178
+ throw new Error(`The update ${option} cannot be updated`);
179
+ }
180
+ }
181
+
135
182
  /**
136
183
  * Emit one of the consumer's error events depending on the error received.
137
184
  * @param err The error object to forward on
package/src/types.ts CHANGED
@@ -106,6 +106,8 @@ export interface ConsumerOptions {
106
106
  handleMessageBatch?(messages: Message[]): Promise<Message[] | void>;
107
107
  }
108
108
 
109
+ export type UpdatableOptions = 'visibilityTimeout';
110
+
109
111
  export interface StopOptions {
110
112
  /**
111
113
  * Default to `false`, if you want the stop action to also abort requests to SQS
@@ -151,10 +153,18 @@ export interface Events {
151
153
  * Fired when requests to SQS were aborted.
152
154
  */
153
155
  aborted: [];
156
+ /**
157
+ * Fired when the consumer starts its work..
158
+ */
159
+ started: [];
154
160
  /**
155
161
  * Fired when the consumer finally stops its work.
156
162
  */
157
163
  stopped: [];
164
+ /**
165
+ * Fired when an option is updated
166
+ */
167
+ option_updated: [UpdatableOptions, ConsumerOptions[UpdatableOptions]];
158
168
  }
159
169
 
160
170
  export class TypedEventEmitter extends EventEmitter {
@@ -178,7 +188,7 @@ export class TypedEventEmitter extends EventEmitter {
178
188
  event: E,
179
189
  listener: (...args: Events[E]) => void
180
190
  ): this {
181
- return super.on(event, listener);
191
+ return super.once(event, listener);
182
192
  }
183
193
  /**
184
194
  * Emits an event with the provided arguments