1 | import { SQSClient, Message, QueueAttributeName } from "@aws-sdk/client-sqs";
|
2 | /**
|
3 | * The options for the consumer.
|
4 | */
|
5 | export interface ConsumerOptions {
|
6 | /**
|
7 | * The SQS queue URL.
|
8 | */
|
9 | queueUrl: string;
|
10 | /**
|
11 | * List of queue attributes to retrieve, see [AWS docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-sqs/Variable/QueueAttributeName/).
|
12 | * @defaultvalue `[]`
|
13 | */
|
14 | attributeNames?: QueueAttributeName[];
|
15 | /**
|
16 | * List of message attributes to retrieve (i.e. `['name', 'address']`).
|
17 | * @defaultvalue `[]`
|
18 | */
|
19 | messageAttributeNames?: string[];
|
20 | /** @hidden */
|
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 | */
|
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 | */
|
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 | */
|
40 | waitTimeSeconds?: number;
|
41 | /**
|
42 | * The duration (in milliseconds) to wait before retrying after an authentication error.
|
43 | * @defaultvalue `10000`
|
44 | */
|
45 | authenticationErrorTimeout?: number;
|
46 | /**
|
47 | * The duration (in milliseconds) to wait before repolling the queue.
|
48 | * @defaultvalue `0`
|
49 | */
|
50 | pollingWaitTimeMs?: number;
|
51 | /**
|
52 | * If you want the stop action to wait for the final poll to complete and in-flight messages
|
53 | * to be processed before emitting 'stopped' set this to the max amount of time to wait.
|
54 | * @defaultvalue `0`
|
55 | */
|
56 | pollingCompleteWaitTimeMs?: number;
|
57 | /**
|
58 | * If true, sets the message visibility timeout to 0 after a `processing_error`.
|
59 | * @defaultvalue `false`
|
60 | */
|
61 | terminateVisibilityTimeout?: boolean;
|
62 | /**
|
63 | * The interval (in seconds) between requests to extend the message visibility timeout.
|
64 | *
|
65 | * On each heartbeat the visibility is extended by adding `visibilityTimeout` to
|
66 | * the number of seconds since the start of the handler function.
|
67 | *
|
68 | * This value must less than `visibilityTimeout`.
|
69 | */
|
70 | heartbeatInterval?: number;
|
71 | /**
|
72 | * An optional [SQS Client](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-sqs/classes/sqsclient.html)
|
73 | * object to use if you need to configure the client manually.
|
74 | */
|
75 | sqs?: SQSClient;
|
76 | /**
|
77 | * The AWS region.
|
78 | * @defaultValue process.env.AWS_REGION || `eu-west-1`
|
79 | */
|
80 | region?: string;
|
81 | /**
|
82 | * Set this value to false to ignore the `queueUrl` and use the
|
83 | * client's resolved endpoint, which may be a custom endpoint.
|
84 | * @defaultValue `true`
|
85 | */
|
86 | useQueueUrlAsEndpoint?: boolean;
|
87 | /**
|
88 | * Time in ms to wait for `handleMessage` to process a message before timing out.
|
89 | *
|
90 | * Emits `timeout_error` on timeout. By default, if `handleMessage` times out,
|
91 | * the unprocessed message returns to the end of the queue.
|
92 | */
|
93 | handleMessageTimeout?: number;
|
94 | /**
|
95 | * Default to `true`, if you don't want the package to delete messages from sqs
|
96 | * set this to `false`.
|
97 | * @defaultvalue `true`
|
98 | */
|
99 | shouldDeleteMessages?: boolean;
|
100 | /**
|
101 | * By default, the consumer will treat an empty object or array from either of the
|
102 | * handlers as a acknowledgement of no messages and will not delete those messages as
|
103 | * a result. Set this to `true` to always acknowledge all messages no matter the returned
|
104 | * value.
|
105 | * @defaultvalue `false`
|
106 | */
|
107 | alwaysAcknowledge?: boolean;
|
108 | /**
|
109 | * An `async` function (or function that returns a `Promise`) to be called whenever
|
110 | * a message is received.
|
111 | *
|
112 | * In the case that you need to acknowledge the message, return an object containing
|
113 | * the MessageId that you'd like to acknowledge.
|
114 | */
|
115 | handleMessage?(message: Message): Promise<Message | void>;
|
116 | /**
|
117 | * An `async` function (or function that returns a `Promise`) to be called whenever
|
118 | * a batch of messages is received. Similar to `handleMessage` but will receive the
|
119 | * list of messages, not each message individually.
|
120 | *
|
121 | * **If both are set, `handleMessageBatch` overrides `handleMessage`**.
|
122 | *
|
123 | * In the case that you need to ack only some of the messages, return an array with
|
124 | * the successful messages only.
|
125 | */
|
126 | handleMessageBatch?(messages: Message[]): Promise<Message[] | void>;
|
127 | /**
|
128 | * An `async` function (or function that returns a `Promise`) to be called right
|
129 | * before the SQS Client sends a receive message command.
|
130 | *
|
131 | * This function is usefull if SQS Client module exports have been modified, for
|
132 | * example to add middlewares.
|
133 | */
|
134 | preReceiveMessageCallback?(): Promise<void>;
|
135 | /**
|
136 | * An `async` function (or function that returns a `Promise`) to be called right
|
137 | * after the SQS Client sends a receive message command.
|
138 | *
|
139 | * This function is usefull if SQS Client module exports have been modified, for
|
140 | * example to add middlewares.
|
141 | */
|
142 | postReceiveMessageCallback?(): Promise<void>;
|
143 | /**
|
144 | * Set this to `true` if you want to receive additional information about the error
|
145 | * that occurred from AWS, such as the response and metadata.
|
146 | */
|
147 | extendedAWSErrors?: boolean;
|
148 | }
|
149 | /**
|
150 | * A subset of the ConsumerOptions that can be updated at runtime.
|
151 | */
|
152 | export type UpdatableOptions = "visibilityTimeout" | "batchSize" | "waitTimeSeconds" | "pollingWaitTimeMs";
|
153 | /**
|
154 | * The options for the stop method.
|
155 | */
|
156 | export interface StopOptions {
|
157 | /**
|
158 | * Default to `false`, if you want the stop action to also abort requests to SQS
|
159 | * set this to `true`.
|
160 | * @defaultvalue `false`
|
161 | */
|
162 | abort?: boolean;
|
163 | }
|
164 | /**
|
165 | * These are the events that the consumer emits.
|
166 | */
|
167 | export interface Events {
|
168 | /**
|
169 | * Fired after one batch of items (up to `batchSize`) has been successfully processed.
|
170 | */
|
171 | response_processed: [];
|
172 | /**
|
173 | * Fired when the queue is empty (All messages have been consumed).
|
174 | */
|
175 | empty: [];
|
176 | /**
|
177 | * Fired when a message is received.
|
178 | */
|
179 | message_received: [Message];
|
180 | /**
|
181 | * Fired when a message is successfully processed and removed from the queue.
|
182 | */
|
183 | message_processed: [Message];
|
184 | /**
|
185 | * Fired when an error occurs interacting with the queue.
|
186 | *
|
187 | * If the error correlates to a message, that message is included in Params
|
188 | */
|
189 | error: [Error, void | Message | Message[]];
|
190 | /**
|
191 | * Fired when `handleMessageTimeout` is supplied as an option and if
|
192 | * `handleMessage` times out.
|
193 | */
|
194 | timeout_error: [Error, Message];
|
195 | /**
|
196 | * Fired when an error occurs processing the message.
|
197 | */
|
198 | processing_error: [Error, Message];
|
199 | /**
|
200 | * Fired when requests to SQS were aborted.
|
201 | */
|
202 | aborted: [];
|
203 | /**
|
204 | * Fired when the consumer starts its work..
|
205 | */
|
206 | started: [];
|
207 | /**
|
208 | * Fired when the consumer finally stops its work.
|
209 | */
|
210 | stopped: [];
|
211 | /**
|
212 | * Fired when an option is updated
|
213 | */
|
214 | option_updated: [UpdatableOptions, ConsumerOptions[UpdatableOptions]];
|
215 | /**
|
216 | * Fired when the Consumer is waiting for polling to complete before stopping.
|
217 | */
|
218 | waiting_for_polling_to_complete: [];
|
219 | /**
|
220 | * Fired when the Consumer has waited for polling to complete and is stopping due to a timeout.
|
221 | */
|
222 | waiting_for_polling_to_complete_timeout_exceeded: [];
|
223 | }
|
224 | /**
|
225 | * The error object that is emitted with error events from AWS.
|
226 | */
|
227 | export type AWSError = {
|
228 | /**
|
229 | * Name, eg. ConditionalCheckFailedException
|
230 | */
|
231 | readonly name: string;
|
232 | /**
|
233 | * Human-readable error response message
|
234 | */
|
235 | message: string;
|
236 | /**
|
237 | * Non-standard stacktrace
|
238 | */
|
239 | stack?: string;
|
240 | /**
|
241 | * Whether the client or server are at fault.
|
242 | */
|
243 | readonly $fault: "client" | "server";
|
244 | /**
|
245 | * Represents an HTTP message as received in reply to a request
|
246 | */
|
247 | readonly $response?: {
|
248 | /**
|
249 | * The status code of the HTTP response.
|
250 | */
|
251 | statusCode?: number;
|
252 | /**
|
253 | * The headers of the HTTP message.
|
254 | */
|
255 | headers: Record<string, string>;
|
256 | /**
|
257 | * The body of the HTTP message.
|
258 | * Can be: ArrayBuffer | ArrayBufferView | string | Uint8Array | Readable | ReadableStream
|
259 | */
|
260 | body?: any;
|
261 | };
|
262 | /**
|
263 | * The service that encountered the exception.
|
264 | */
|
265 | readonly $service?: string;
|
266 | /**
|
267 | * Indicates that an error MAY be retried by the client.
|
268 | */
|
269 | readonly $retryable?: {
|
270 | /**
|
271 | * Indicates that the error is a retryable throttling error.
|
272 | */
|
273 | readonly throttling?: boolean;
|
274 | };
|
275 | readonly $metadata: {
|
276 | /**
|
277 | * The status code of the last HTTP response received for this operation.
|
278 | */
|
279 | readonly httpStatusCode?: number;
|
280 | /**
|
281 | * A unique identifier for the last request sent for this operation. Often
|
282 | * requested by AWS service teams to aid in debugging.
|
283 | */
|
284 | readonly requestId?: string;
|
285 | /**
|
286 | * A secondary identifier for the last request sent. Used for debugging.
|
287 | */
|
288 | readonly extendedRequestId?: string;
|
289 | /**
|
290 | * A tertiary identifier for the last request sent. Used for debugging.
|
291 | */
|
292 | readonly cfId?: string;
|
293 | /**
|
294 | * The number of times this operation was attempted.
|
295 | */
|
296 | readonly attempts?: number;
|
297 | /**
|
298 | * The total amount of time (in milliseconds) that was spent waiting between
|
299 | * retry attempts.
|
300 | */
|
301 | readonly totalRetryDelay?: number;
|
302 | };
|
303 | };
|