sqs-consumer 7.1.0-canary.2 → 7.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.
@@ -32,7 +32,7 @@ body:
32
32
  Please add a link to a minimal reproduction.
33
33
  Note:
34
34
  - Please keep your example as simple and reproduceable as possible, try leaving out dependencies that are not required for reproduction.
35
- - To create a shareable code example for web, you can use CodeSandbox (https://codesandbox.io/s/new) or Stackblitz (https://stackblitz.com/).
35
+ - To create a shareable code example for web, you can use Stackblitz (https://stackblitz.com/edit/sqs-consumer-starter).
36
36
  - Please make sure the example is complete and runnable - e.g. avoid localhost URLs.
37
37
  placeholder: |
38
38
  e.g. Code Sandbox, Stackblitz, Expo Snack or TypeScript playground
@@ -38,14 +38,9 @@ export declare class Consumer extends TypedEventEmitter {
38
38
  */
39
39
  get isRunning(): boolean;
40
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
41
+ * Validates and then updates the provided option to the provided value.
42
+ * @param option The option to validate and then update
43
+ * @param value The value to set the provided option to
49
44
  */
50
45
  updateOption(option: UpdatableOptions, value: ConsumerOptions[UpdatableOptions]): void;
51
46
  /**
package/dist/consumer.js CHANGED
@@ -93,34 +93,15 @@ class Consumer extends types_1.TypedEventEmitter {
93
93
  return !this.stopped;
94
94
  }
95
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
96
+ * Validates and then updates the provided option to the provided value.
97
+ * @param option The option to validate and then update
98
+ * @param value The value to set the provided option to
115
99
  */
116
100
  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
- }
101
+ (0, validation_1.validateOption)(option, value, this, true);
102
+ debug(`Updating the ${option} option to the value ${value}`);
103
+ this[option] = value;
104
+ this.emit('option_updated', option, value);
124
105
  }
125
106
  /**
126
107
  * Emit one of the consumer's error events depending on the error received.
package/dist/types.d.ts CHANGED
@@ -105,7 +105,7 @@ export interface ConsumerOptions {
105
105
  */
106
106
  handleMessageBatch?(messages: Message[]): Promise<Message[] | void>;
107
107
  }
108
- export type UpdatableOptions = 'visibilityTimeout';
108
+ export type UpdatableOptions = 'visibilityTimeout' | 'batchSize' | 'waitTimeSeconds';
109
109
  export interface StopOptions {
110
110
  /**
111
111
  * Default to `false`, if you want the stop action to also abort requests to SQS
@@ -1,5 +1,8 @@
1
1
  import { ReceiveMessageCommandOutput } from '@aws-sdk/client-sqs';
2
2
  import { ConsumerOptions } from './types';
3
+ declare function validateOption(option: string, value: any, allOptions: {
4
+ [key: string]: any;
5
+ }, strict?: boolean): void;
3
6
  /**
4
7
  * Ensure that the required options have been set.
5
8
  * @param options The options that have been set by the application.
@@ -10,4 +13,4 @@ declare function assertOptions(options: ConsumerOptions): void;
10
13
  * @param response The response from SQS.
11
14
  */
12
15
  declare function hasMessages(response: ReceiveMessageCommandOutput): boolean;
13
- export { hasMessages, assertOptions };
16
+ export { hasMessages, assertOptions, validateOption };
@@ -1,11 +1,43 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.assertOptions = exports.hasMessages = void 0;
3
+ exports.validateOption = exports.assertOptions = exports.hasMessages = void 0;
4
4
  const requiredOptions = [
5
5
  'queueUrl',
6
6
  // only one of handleMessage / handleMessagesBatch is required
7
7
  'handleMessage|handleMessageBatch'
8
8
  ];
9
+ function validateOption(option, value, allOptions, strict) {
10
+ switch (option) {
11
+ case 'batchSize':
12
+ if (value > 10 || value < 1) {
13
+ throw new Error('batchSize must be between 1 and 10.');
14
+ }
15
+ break;
16
+ case 'heartbeatInterval':
17
+ if (!allOptions.visibilityTimeout ||
18
+ value >= allOptions.visibilityTimeout) {
19
+ throw new Error('heartbeatInterval must be less than visibilityTimeout.');
20
+ }
21
+ break;
22
+ case 'visibilityTimeout':
23
+ if (allOptions.heartbeatInterval &&
24
+ value <= allOptions.heartbeatInterval) {
25
+ throw new Error('heartbeatInterval must be less than visibilityTimeout.');
26
+ }
27
+ break;
28
+ case 'waitTimeSeconds':
29
+ if (value < 1 || value > 20) {
30
+ throw new Error('waitTimeSeconds must be between 0 and 20.');
31
+ }
32
+ break;
33
+ default:
34
+ if (strict) {
35
+ throw new Error(`The update ${option} cannot be updated`);
36
+ }
37
+ break;
38
+ }
39
+ }
40
+ exports.validateOption = validateOption;
9
41
  /**
10
42
  * Ensure that the required options have been set.
11
43
  * @param options The options that have been set by the application.
@@ -17,12 +49,11 @@ function assertOptions(options) {
17
49
  throw new Error(`Missing SQS consumer option [ ${possibilities.join(' or ')} ].`);
18
50
  }
19
51
  });
20
- if (options.batchSize > 10 || options.batchSize < 1) {
21
- throw new Error('SQS batchSize option must be between 1 and 10.');
52
+ if (options.batchSize) {
53
+ validateOption('batchSize', options.batchSize, options);
22
54
  }
23
- if (options.heartbeatInterval &&
24
- !(options.heartbeatInterval < options.visibilityTimeout)) {
25
- throw new Error('heartbeatInterval must be less than visibilityTimeout.');
55
+ if (options.heartbeatInterval) {
56
+ validateOption('heartbeatInterval', options.heartbeatInterval, options);
26
57
  }
27
58
  }
28
59
  exports.assertOptions = assertOptions;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sqs-consumer",
3
- "version": "7.1.0-canary.2",
3
+ "version": "7.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",
package/src/consumer.ts CHANGED
@@ -30,7 +30,7 @@ import {
30
30
  toSQSError,
31
31
  isConnectionError
32
32
  } from './errors';
33
- import { assertOptions, hasMessages } from './validation';
33
+ import { validateOption, assertOptions, hasMessages } from './validation';
34
34
  import { abortController } from './controllers';
35
35
 
36
36
  const debug = Debug('sqs-consumer');
@@ -139,44 +139,21 @@ export class Consumer extends TypedEventEmitter {
139
139
  }
140
140
 
141
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
142
+ * Validates and then updates the provided option to the provided value.
143
+ * @param option The option to validate and then update
144
+ * @param value The value to set the provided option to
168
145
  */
169
146
  public updateOption(
170
147
  option: UpdatableOptions,
171
148
  value: ConsumerOptions[UpdatableOptions]
172
149
  ) {
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
- }
150
+ validateOption(option, value, this, true);
151
+
152
+ debug(`Updating the ${option} option to the value ${value}`);
153
+
154
+ this[option] = value;
155
+
156
+ this.emit('option_updated', option, value);
180
157
  }
181
158
 
182
159
  /**
package/src/types.ts CHANGED
@@ -106,7 +106,10 @@ export interface ConsumerOptions {
106
106
  handleMessageBatch?(messages: Message[]): Promise<Message[] | void>;
107
107
  }
108
108
 
109
- export type UpdatableOptions = 'visibilityTimeout';
109
+ export type UpdatableOptions =
110
+ | 'visibilityTimeout'
111
+ | 'batchSize'
112
+ | 'waitTimeSeconds';
110
113
 
111
114
  export interface StopOptions {
112
115
  /**
package/src/validation.ts CHANGED
@@ -8,6 +8,51 @@ const requiredOptions = [
8
8
  'handleMessage|handleMessageBatch'
9
9
  ];
10
10
 
11
+ function validateOption(
12
+ option: string,
13
+ value: any,
14
+ allOptions: { [key: string]: any },
15
+ strict?: boolean
16
+ ): void {
17
+ switch (option) {
18
+ case 'batchSize':
19
+ if (value > 10 || value < 1) {
20
+ throw new Error('batchSize must be between 1 and 10.');
21
+ }
22
+ break;
23
+ case 'heartbeatInterval':
24
+ if (
25
+ !allOptions.visibilityTimeout ||
26
+ value >= allOptions.visibilityTimeout
27
+ ) {
28
+ throw new Error(
29
+ 'heartbeatInterval must be less than visibilityTimeout.'
30
+ );
31
+ }
32
+ break;
33
+ case 'visibilityTimeout':
34
+ if (
35
+ allOptions.heartbeatInterval &&
36
+ value <= allOptions.heartbeatInterval
37
+ ) {
38
+ throw new Error(
39
+ 'heartbeatInterval must be less than visibilityTimeout.'
40
+ );
41
+ }
42
+ break;
43
+ case 'waitTimeSeconds':
44
+ if (value < 1 || value > 20) {
45
+ throw new Error('waitTimeSeconds must be between 0 and 20.');
46
+ }
47
+ break;
48
+ default:
49
+ if (strict) {
50
+ throw new Error(`The update ${option} cannot be updated`);
51
+ }
52
+ break;
53
+ }
54
+ }
55
+
11
56
  /**
12
57
  * Ensure that the required options have been set.
13
58
  * @param options The options that have been set by the application.
@@ -22,15 +67,11 @@ function assertOptions(options: ConsumerOptions): void {
22
67
  }
23
68
  });
24
69
 
25
- if (options.batchSize > 10 || options.batchSize < 1) {
26
- throw new Error('SQS batchSize option must be between 1 and 10.');
70
+ if (options.batchSize) {
71
+ validateOption('batchSize', options.batchSize, options);
27
72
  }
28
-
29
- if (
30
- options.heartbeatInterval &&
31
- !(options.heartbeatInterval < options.visibilityTimeout)
32
- ) {
33
- throw new Error('heartbeatInterval must be less than visibilityTimeout.');
73
+ if (options.heartbeatInterval) {
74
+ validateOption('heartbeatInterval', options.heartbeatInterval, options);
34
75
  }
35
76
  }
36
77
 
@@ -42,4 +83,4 @@ function hasMessages(response: ReceiveMessageCommandOutput): boolean {
42
83
  return response.Messages && response.Messages.length > 0;
43
84
  }
44
85
 
45
- export { hasMessages, assertOptions };
86
+ export { hasMessages, assertOptions, validateOption };