@comicrelief/lambda-wrapper 1.10.2 → 2.0.0-beta.10
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/README.md +196 -50
- package/dist/core/DependencyAwareClass.d.ts +16 -0
- package/dist/core/DependencyAwareClass.d.ts.map +1 -0
- package/dist/core/DependencyAwareClass.js +20 -0
- package/dist/core/DependencyAwareClass.js.map +1 -0
- package/dist/core/DependencyInjection.d.ts +62 -0
- package/dist/core/DependencyInjection.d.ts.map +1 -0
- package/dist/core/DependencyInjection.js +105 -0
- package/dist/core/DependencyInjection.js.map +1 -0
- package/dist/core/LambdaWrapper.d.ts +73 -0
- package/dist/core/LambdaWrapper.d.ts.map +1 -0
- package/dist/core/LambdaWrapper.js +170 -0
- package/dist/core/LambdaWrapper.js.map +1 -0
- package/dist/core/config.d.ts +20 -0
- package/dist/core/config.d.ts.map +1 -0
- package/dist/core/config.js +21 -0
- package/dist/core/config.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +57 -143
- package/dist/index.js.map +1 -0
- package/dist/models/ResponseModel.d.ts +73 -0
- package/dist/models/ResponseModel.d.ts.map +1 -0
- package/dist/models/ResponseModel.js +100 -0
- package/dist/models/ResponseModel.js.map +1 -0
- package/dist/models/SQSMessageModel.d.ts +46 -0
- package/dist/models/SQSMessageModel.d.ts.map +1 -0
- package/dist/models/SQSMessageModel.js +65 -0
- package/dist/models/SQSMessageModel.js.map +1 -0
- package/dist/services/BaseConfigService.d.ts +101 -0
- package/dist/services/BaseConfigService.d.ts.map +1 -0
- package/dist/services/BaseConfigService.js +180 -0
- package/dist/services/BaseConfigService.js.map +1 -0
- package/dist/services/HTTPService.d.ts +29 -0
- package/dist/services/HTTPService.d.ts.map +1 -0
- package/dist/services/HTTPService.js +55 -0
- package/dist/services/HTTPService.js.map +1 -0
- package/dist/services/LoggerService.d.ts +102 -0
- package/dist/services/LoggerService.d.ts.map +1 -0
- package/dist/services/LoggerService.js +249 -0
- package/dist/services/LoggerService.js.map +1 -0
- package/dist/services/RequestService.d.ts +119 -0
- package/dist/services/RequestService.d.ts.map +1 -0
- package/dist/services/RequestService.js +293 -0
- package/dist/services/RequestService.js.map +1 -0
- package/dist/services/SQSService.d.ts +263 -0
- package/dist/services/SQSService.d.ts.map +1 -0
- package/dist/services/SQSService.js +411 -0
- package/dist/services/SQSService.js.map +1 -0
- package/dist/services/TimerService.d.ts +22 -0
- package/dist/services/TimerService.d.ts.map +1 -0
- package/dist/services/TimerService.js +40 -0
- package/dist/services/TimerService.js.map +1 -0
- package/dist/types/Status.d.ts +6 -0
- package/dist/types/Status.d.ts.map +1 -0
- package/dist/types/Status.js +3 -0
- package/dist/types/Status.js.map +1 -0
- package/dist/utils/LambdaTermination.d.ts +14 -0
- package/dist/utils/LambdaTermination.d.ts.map +1 -0
- package/dist/utils/LambdaTermination.js +23 -0
- package/dist/utils/LambdaTermination.js.map +1 -0
- package/dist/utils/PromisifiedDelay.d.ts +17 -0
- package/dist/utils/PromisifiedDelay.d.ts.map +1 -0
- package/dist/utils/PromisifiedDelay.js +48 -0
- package/dist/utils/PromisifiedDelay.js.map +1 -0
- package/package.json +36 -26
- package/.eslintrc.yml +0 -16
- package/.github/dependabot.yml +0 -11
- package/.github/workflows/main.yml +0 -74
- package/babel.config.js +0 -13
- package/dist/Config/Dependencies.js +0 -33
- package/dist/DependencyInjection/DependencyAware.class.js +0 -40
- package/dist/DependencyInjection/DependencyInjection.class.js +0 -115
- package/dist/Model/CloudEvent.model.js +0 -138
- package/dist/Model/Model.model.js +0 -39
- package/dist/Model/Response.model.js +0 -135
- package/dist/Model/SQS/MarketingPreference.constraints.json +0 -28
- package/dist/Model/SQS/MarketingPreference.model.js +0 -583
- package/dist/Model/SQS/Message.model.js +0 -92
- package/dist/Model/Status.model.js +0 -71
- package/dist/Service/BaseConfig.service.js +0 -204
- package/dist/Service/HTTP.service.js +0 -57
- package/dist/Service/Logger.service.js +0 -256
- package/dist/Service/Request.service.js +0 -294
- package/dist/Service/SQS.service.js +0 -367
- package/dist/Service/Timer.service.js +0 -46
- package/dist/Wrapper/LambdaTermination.js +0 -34
- package/dist/Wrapper/LambdaWrapper.js +0 -143
- package/dist/Wrapper/PromisifiedDelay.js +0 -55
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.SQS_PUBLISH_FAILURE_MODES = exports.SQS_OFFLINE_MODES = void 0;
|
|
7
|
+
const alai_1 = __importDefault(require("alai"));
|
|
8
|
+
const async_1 = require("async");
|
|
9
|
+
const aws_sdk_1 = __importDefault(require("aws-sdk"));
|
|
10
|
+
const uuid_1 = require("uuid");
|
|
11
|
+
const DependencyAwareClass_1 = __importDefault(require("../core/DependencyAwareClass"));
|
|
12
|
+
const SQSMessageModel_1 = __importDefault(require("../models/SQSMessageModel"));
|
|
13
|
+
const LoggerService_1 = __importDefault(require("./LoggerService"));
|
|
14
|
+
const TimerService_1 = __importDefault(require("./TimerService"));
|
|
15
|
+
/**
|
|
16
|
+
* Allowed values for `process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE`.
|
|
17
|
+
*/
|
|
18
|
+
exports.SQS_OFFLINE_MODES = {
|
|
19
|
+
/**
|
|
20
|
+
* When running offline, messages will trigger the consumer function directly
|
|
21
|
+
* via a Lambda endpoint, set using `process.env.SERVICE_LAMBDA_URL`. This is
|
|
22
|
+
* the default.
|
|
23
|
+
*/
|
|
24
|
+
DIRECT: 'direct',
|
|
25
|
+
/**
|
|
26
|
+
* When running offline, send messages to an offline SQS service defined by
|
|
27
|
+
* `process.env.LAMBDA_WRAPPER_OFFLINE_SQS_HOST`.
|
|
28
|
+
*/
|
|
29
|
+
LOCAL: 'local',
|
|
30
|
+
/**
|
|
31
|
+
* When running offline, send messages to AWS as normal.
|
|
32
|
+
*/
|
|
33
|
+
AWS: 'aws',
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Defines the preferred behaviour for `SQSService.prototype.send` in case the
|
|
37
|
+
* AWS SQS call fails.
|
|
38
|
+
*/
|
|
39
|
+
exports.SQS_PUBLISH_FAILURE_MODES = {
|
|
40
|
+
/**
|
|
41
|
+
* Catch the exception and logs it.
|
|
42
|
+
*
|
|
43
|
+
* This is the default behaviour for Lambda Wrapper v1.8.0 and below and for
|
|
44
|
+
* Lambda Wrapper v1.8.2 and above.
|
|
45
|
+
*/
|
|
46
|
+
CATCH: 'catch',
|
|
47
|
+
/**
|
|
48
|
+
* Throw the exception so that the caller can handle it directly.
|
|
49
|
+
*/
|
|
50
|
+
THROW: 'throw',
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Helper service for working with SQS.
|
|
54
|
+
*
|
|
55
|
+
* Config for this service goes in the `sqs` key of your Lambda Wrapper config.
|
|
56
|
+
* The `queues` key maps short friendly names to the full SQS queue name.
|
|
57
|
+
* Usually we define queue names in our `serverless.yml` and provide them to
|
|
58
|
+
* the application via environment variables.
|
|
59
|
+
*
|
|
60
|
+
* ```ts
|
|
61
|
+
* const lambdaWrapper = lw.configure({
|
|
62
|
+
* sqs: {
|
|
63
|
+
* queues: {
|
|
64
|
+
* // add an entry for each queue mapping to its AWS name
|
|
65
|
+
* submissions: process.env.SQS_QUEUE_SUBMISSIONS as string,
|
|
66
|
+
* },
|
|
67
|
+
* },
|
|
68
|
+
* });
|
|
69
|
+
* ```
|
|
70
|
+
*
|
|
71
|
+
* You can then send messages to a queue within your Lambda handler using the
|
|
72
|
+
* `publish` method.
|
|
73
|
+
*
|
|
74
|
+
* ```ts
|
|
75
|
+
* export default lambdaWrapper.wrap(async (di) => {
|
|
76
|
+
* const sqs = di.get(SQSService);
|
|
77
|
+
* const message = { data: 'Hello SQS!' };
|
|
78
|
+
* await sqs.publish('submissions', message);
|
|
79
|
+
* });
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* When using TypeScript, queue names are inferred from your Lambda Wrapper
|
|
83
|
+
* config so that IntelliSense can provide hints and TypeScript will tell you
|
|
84
|
+
* at compile-time if you try to publish to an undefined queue.
|
|
85
|
+
*
|
|
86
|
+
* ```ts
|
|
87
|
+
* // ok
|
|
88
|
+
* await sqs.publish('submissions', message);
|
|
89
|
+
*
|
|
90
|
+
* // error: Argument of type '"submission"' is not assignable to parameter of
|
|
91
|
+
* // type '"submissions"'.
|
|
92
|
+
* await sqs.publish('submission', message);
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* Note that if you're passing the queue name in as a variable, you'll need to
|
|
96
|
+
* ensure the variable type is specific enough and not simply `string`. If you
|
|
97
|
+
* have a list of queue names you will need to declare it `as const`. Otherwise,
|
|
98
|
+
* use string literal types, or the `QueueName` generic type which extracts the
|
|
99
|
+
* type of all queue names from your Lambda Wrapper config.
|
|
100
|
+
*
|
|
101
|
+
* ```ts
|
|
102
|
+
* const myQueues = ['queue1', 'queue2'];
|
|
103
|
+
* for (const queue of myQueues) {
|
|
104
|
+
* // won't compile because `queue` is of type `string`
|
|
105
|
+
* await sqs.publish(queue, message);
|
|
106
|
+
* }
|
|
107
|
+
*
|
|
108
|
+
* const myQueues = ['queue1', 'queue2'] as const;
|
|
109
|
+
* for (const queue of myQueues) {
|
|
110
|
+
* // ok now because `queue` is of type `"queue1" | "queue2"`
|
|
111
|
+
* await sqs.publish(queue, message);
|
|
112
|
+
* }
|
|
113
|
+
*
|
|
114
|
+
* // you can also simply use string literal types
|
|
115
|
+
* let queue: "queue1" | "queue2";
|
|
116
|
+
*
|
|
117
|
+
* // or accept any queue defined in the config using `QueueName`
|
|
118
|
+
* let queue: QueueName<typeof lambdaWrapper.config>;
|
|
119
|
+
* ```
|
|
120
|
+
*
|
|
121
|
+
* This is all pretty cool, but the current implementation has a caveat: the
|
|
122
|
+
* `WithSQSServiceConfig` type has to be a little vague about `sqs.queues` in
|
|
123
|
+
* order to get TypeScript to infer its keys. The following config will not
|
|
124
|
+
* raise any errors itself, but is invalid and will make the `QueueName` type
|
|
125
|
+
* `never`.
|
|
126
|
+
*
|
|
127
|
+
* ```ts
|
|
128
|
+
* lambdaWrapper.configure<WithSQSServiceConfig>({
|
|
129
|
+
* sqs: {
|
|
130
|
+
* queues: {
|
|
131
|
+
* good: 'good-queue',
|
|
132
|
+
* bad: 0, // oops, not a string, but no errors here!
|
|
133
|
+
* },
|
|
134
|
+
* },
|
|
135
|
+
* });
|
|
136
|
+
*
|
|
137
|
+
* // even though this is queue has valid config, the invalid one breaks it:
|
|
138
|
+
* // Argument of type 'string' is not assignable to parameter of type 'never'.
|
|
139
|
+
* await sqs.publish('good', message);
|
|
140
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* If you start getting _not assignable to parameter of type 'never'_ errors on
|
|
143
|
+
* all your `SQSService` method calls, double-check that your config is correct.
|
|
144
|
+
* Be particularly careful with environment variables – by default they have
|
|
145
|
+
* type `string | undefined`. In the first example at the top of this page, a
|
|
146
|
+
* type assertion was used to coerce this to `string`.
|
|
147
|
+
*/
|
|
148
|
+
class SQSService extends DependencyAwareClass_1.default {
|
|
149
|
+
constructor(di) {
|
|
150
|
+
super(di);
|
|
151
|
+
const config = this.di.config.sqs;
|
|
152
|
+
this.queues = config?.queues || {};
|
|
153
|
+
this.queueConsumers = config?.queueConsumers || {};
|
|
154
|
+
const { LAMBDA_WRAPPER_OFFLINE_SQS_HOST: offlineHost = 'localhost', LAMBDA_WRAPPER_OFFLINE_SQS_PORT: offlinePort = '4576', LAMBDA_WRAPPER_OFFLINE_SQS_MODE: offlineMode = exports.SQS_OFFLINE_MODES.DIRECT, AWS_ACCOUNT_ID, REGION, } = process.env;
|
|
155
|
+
const accountId = (di.context.invokedFunctionArn && alai_1.default.parse(di.context))
|
|
156
|
+
|| AWS_ACCOUNT_ID;
|
|
157
|
+
if (di.isOffline && !Object.values(exports.SQS_OFFLINE_MODES).includes(offlineMode)) {
|
|
158
|
+
throw new Error(`Invalid LAMBDA_WRAPPER_OFFLINE_SQS_MODE: ${offlineMode}\n`
|
|
159
|
+
+ `Please use one of: ${Object.values(exports.SQS_OFFLINE_MODES).join(', ')}`);
|
|
160
|
+
}
|
|
161
|
+
const useLocalQueues = di.isOffline && offlineMode === exports.SQS_OFFLINE_MODES.LOCAL;
|
|
162
|
+
this.queueUrls = Object.fromEntries(Object.entries(this.queues).map((([key, queueName]) => [key, useLocalQueues
|
|
163
|
+
? `http://${offlineHost}:${offlinePort}/queue/${queueName}`
|
|
164
|
+
: `https://sqs.${REGION}.amazonaws.com/${accountId}/${queueName}`])));
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Returns an SQS client instance
|
|
168
|
+
*/
|
|
169
|
+
get sqs() {
|
|
170
|
+
if (!this.$sqs) {
|
|
171
|
+
this.$sqs = new aws_sdk_1.default.SQS({
|
|
172
|
+
region: process.env.REGION,
|
|
173
|
+
httpOptions: {
|
|
174
|
+
// longest publish on NOTV took 5 seconds
|
|
175
|
+
connectTimeout: 8 * 1000,
|
|
176
|
+
timeout: 8 * 1000,
|
|
177
|
+
},
|
|
178
|
+
maxRetries: 3, // default is 3, we can change that
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return this.$sqs;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Returns a Lambda client instance
|
|
185
|
+
*/
|
|
186
|
+
get lambda() {
|
|
187
|
+
if (!this.$lambda) {
|
|
188
|
+
const endpoint = process.env.SERVICE_LAMBDA_URL;
|
|
189
|
+
if (!endpoint) {
|
|
190
|
+
throw new Error('process.env.SERVICE_LAMBDA_URL must be defined.');
|
|
191
|
+
}
|
|
192
|
+
// move to subprocess
|
|
193
|
+
this.$lambda = new aws_sdk_1.default.Lambda({
|
|
194
|
+
region: process.env.AWS_REGION,
|
|
195
|
+
endpoint,
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
return this.$lambda;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Returns the mode to use for offline SQS.
|
|
202
|
+
*
|
|
203
|
+
* This is configured by `process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE`. The
|
|
204
|
+
* default is `SQS_OFFLINE_MODES.LAMBDA`.
|
|
205
|
+
*/
|
|
206
|
+
static get offlineMode() {
|
|
207
|
+
return process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE || exports.SQS_OFFLINE_MODES.DIRECT;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Batch delete messages.
|
|
211
|
+
*
|
|
212
|
+
* @param queue
|
|
213
|
+
* @param messageModels
|
|
214
|
+
*/
|
|
215
|
+
batchDelete(queue, messageModels) {
|
|
216
|
+
const queueUrl = this.queueUrls[queue];
|
|
217
|
+
const logger = this.di.get(LoggerService_1.default);
|
|
218
|
+
const timer = this.di.get(TimerService_1.default);
|
|
219
|
+
const timerId = `sqs-batch-delete-${(0, uuid_1.v4)()} - Queue: '${queueUrl}'`;
|
|
220
|
+
return new Promise((resolve) => {
|
|
221
|
+
const messagesForDeletion = [];
|
|
222
|
+
timer.start(timerId);
|
|
223
|
+
// assuming openFiles is an array of file names
|
|
224
|
+
(0, async_1.each)(messageModels, (messageModel, callback) => {
|
|
225
|
+
if (messageModel instanceof SQSMessageModel_1.default && messageModel.isForDeletion() === true) {
|
|
226
|
+
messagesForDeletion.push({
|
|
227
|
+
Id: messageModel.getMessageId(),
|
|
228
|
+
ReceiptHandle: messageModel.getReceiptHandle(),
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
callback();
|
|
232
|
+
}, (loopError) => {
|
|
233
|
+
if (loopError) {
|
|
234
|
+
logger.error(loopError);
|
|
235
|
+
resolve();
|
|
236
|
+
}
|
|
237
|
+
this.sqs.deleteMessageBatch({
|
|
238
|
+
Entries: messagesForDeletion,
|
|
239
|
+
QueueUrl: queueUrl,
|
|
240
|
+
}, (error) => {
|
|
241
|
+
timer.stop(timerId);
|
|
242
|
+
if (error) {
|
|
243
|
+
logger.error(error);
|
|
244
|
+
}
|
|
245
|
+
resolve();
|
|
246
|
+
});
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Check SQS status.
|
|
252
|
+
*/
|
|
253
|
+
checkStatus() {
|
|
254
|
+
const logger = this.di.get(LoggerService_1.default);
|
|
255
|
+
const timer = this.di.get(TimerService_1.default);
|
|
256
|
+
const timerId = `sqs-list-queues-${(0, uuid_1.v4)()}`;
|
|
257
|
+
return new Promise((resolve) => {
|
|
258
|
+
timer.start(timerId);
|
|
259
|
+
this.sqs.listQueues({}, (error, data) => {
|
|
260
|
+
timer.stop(timerId);
|
|
261
|
+
let status = 'OK';
|
|
262
|
+
if (error) {
|
|
263
|
+
logger.error(error);
|
|
264
|
+
status = 'APPLICATION_FAILURE';
|
|
265
|
+
}
|
|
266
|
+
if (typeof data.QueueUrls === 'undefined' || data.QueueUrls.length === 0) {
|
|
267
|
+
status = 'APPLICATION_FAILURE';
|
|
268
|
+
}
|
|
269
|
+
resolve({
|
|
270
|
+
service: 'SQS',
|
|
271
|
+
status,
|
|
272
|
+
});
|
|
273
|
+
});
|
|
274
|
+
});
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Get the approximate number of messages in a queue.
|
|
278
|
+
*
|
|
279
|
+
* @param queue
|
|
280
|
+
*/
|
|
281
|
+
getMessageCount(queue) {
|
|
282
|
+
const queueUrl = this.queueUrls[queue];
|
|
283
|
+
const logger = this.di.get(LoggerService_1.default);
|
|
284
|
+
const timer = this.di.get(TimerService_1.default);
|
|
285
|
+
const timerId = `sqs-get-queue-attributes-${(0, uuid_1.v4)()} - Queue: '${queueUrl}'`;
|
|
286
|
+
return new Promise((resolve) => {
|
|
287
|
+
timer.start(timerId);
|
|
288
|
+
this.sqs.getQueueAttributes({
|
|
289
|
+
AttributeNames: ['ApproximateNumberOfMessages'],
|
|
290
|
+
QueueUrl: queueUrl,
|
|
291
|
+
}, (error, data) => {
|
|
292
|
+
timer.stop(timerId);
|
|
293
|
+
if (error) {
|
|
294
|
+
logger.error(error);
|
|
295
|
+
resolve(0);
|
|
296
|
+
}
|
|
297
|
+
const messageCount = data.Attributes?.ApproximateNumberOfMessages || '0';
|
|
298
|
+
resolve(Number.parseInt(messageCount, 10));
|
|
299
|
+
});
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Publish to message queue.
|
|
304
|
+
*
|
|
305
|
+
* When running within serverless-offline, messages can be published to a
|
|
306
|
+
* local Lambda or SQS service instead of to AWS, depending on the offline
|
|
307
|
+
* mode specified by `process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE`.
|
|
308
|
+
*
|
|
309
|
+
* @param queue string
|
|
310
|
+
* @param messageObject object
|
|
311
|
+
* @param messageGroupId string
|
|
312
|
+
* @param failureMode Choose how failures are handled:
|
|
313
|
+
* - `catch`: errors will be caught and logged. This is the default.
|
|
314
|
+
* - `throw`: errors will be thrown, causing promise to reject.
|
|
315
|
+
*/
|
|
316
|
+
async publish(queue, messageObject, messageGroupId = null, failureMode = exports.SQS_PUBLISH_FAILURE_MODES.CATCH) {
|
|
317
|
+
if (!Object.values(exports.SQS_PUBLISH_FAILURE_MODES).includes(failureMode)) {
|
|
318
|
+
throw new Error(`Invalid value for 'failureMode': ${failureMode}`);
|
|
319
|
+
}
|
|
320
|
+
const queueUrl = this.queueUrls[queue];
|
|
321
|
+
const timer = this.di.get(TimerService_1.default);
|
|
322
|
+
const timerId = `sqs-send-message-${(0, uuid_1.v4)()} - Queue: '${queueUrl}'`;
|
|
323
|
+
timer.start(timerId);
|
|
324
|
+
const messageParameters = {
|
|
325
|
+
MessageBody: JSON.stringify(messageObject),
|
|
326
|
+
QueueUrl: queueUrl,
|
|
327
|
+
};
|
|
328
|
+
if (queueUrl.includes('.fifo')) {
|
|
329
|
+
messageParameters.MessageDeduplicationId = (0, uuid_1.v4)();
|
|
330
|
+
messageParameters.MessageGroupId = messageGroupId !== null ? messageGroupId : (0, uuid_1.v4)();
|
|
331
|
+
}
|
|
332
|
+
try {
|
|
333
|
+
if (this.di.isOffline && SQSService.offlineMode === exports.SQS_OFFLINE_MODES.DIRECT) {
|
|
334
|
+
await this.publishOffline(queue, messageParameters);
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
await this.sqs.sendMessage(messageParameters).promise();
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
catch (error) {
|
|
341
|
+
if (failureMode === exports.SQS_PUBLISH_FAILURE_MODES.CATCH) {
|
|
342
|
+
this.di.get(LoggerService_1.default).error(error);
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
throw error;
|
|
346
|
+
}
|
|
347
|
+
return queue;
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* Sends a message to a queue consumer running in serverless-offline.
|
|
351
|
+
*
|
|
352
|
+
* This method invokes the consumer function directly instead of sending the
|
|
353
|
+
* message to SQS, which requires a real or emulated SQS service not provided
|
|
354
|
+
* by serverless-offline. This works very well for local testing.
|
|
355
|
+
*
|
|
356
|
+
* @param queue
|
|
357
|
+
* @param messageParameters
|
|
358
|
+
*/
|
|
359
|
+
async publishOffline(queue, messageParameters) {
|
|
360
|
+
if (!this.di.isOffline) {
|
|
361
|
+
throw new Error('Can only publishOffline while running serverless offline.');
|
|
362
|
+
}
|
|
363
|
+
const FunctionName = this.queueConsumers[queue];
|
|
364
|
+
if (!FunctionName) {
|
|
365
|
+
throw new Error(`Queue consumer for queue ${queue} was not found. Please add it to `
|
|
366
|
+
+ 'the sqs.queueConsumers key in your Lambda Wrapper config.');
|
|
367
|
+
}
|
|
368
|
+
const InvocationType = 'RequestResponse';
|
|
369
|
+
const Payload = JSON.stringify({
|
|
370
|
+
Records: [
|
|
371
|
+
{
|
|
372
|
+
body: messageParameters.MessageBody,
|
|
373
|
+
},
|
|
374
|
+
],
|
|
375
|
+
});
|
|
376
|
+
const parameters = { FunctionName, InvocationType, Payload };
|
|
377
|
+
await this.lambda.invoke(parameters).promise();
|
|
378
|
+
}
|
|
379
|
+
/**
|
|
380
|
+
* Receive from message queue
|
|
381
|
+
*
|
|
382
|
+
* @param queue string
|
|
383
|
+
* @param timeout number
|
|
384
|
+
*/
|
|
385
|
+
receive(queue, timeout = 15) {
|
|
386
|
+
const queueUrl = this.queueUrls[queue];
|
|
387
|
+
const logger = this.di.get(LoggerService_1.default);
|
|
388
|
+
const timer = this.di.get(TimerService_1.default);
|
|
389
|
+
const timerId = `sqs-receive-message-${(0, uuid_1.v4)()} - Queue: '${queueUrl}'`;
|
|
390
|
+
return new Promise((resolve, reject) => {
|
|
391
|
+
timer.start(timerId);
|
|
392
|
+
this.sqs.receiveMessage({
|
|
393
|
+
QueueUrl: queueUrl,
|
|
394
|
+
VisibilityTimeout: timeout,
|
|
395
|
+
MaxNumberOfMessages: 10,
|
|
396
|
+
}, (error, data) => {
|
|
397
|
+
timer.stop(timerId);
|
|
398
|
+
if (error) {
|
|
399
|
+
logger.error(error);
|
|
400
|
+
return reject(error);
|
|
401
|
+
}
|
|
402
|
+
if (typeof data.Messages === 'undefined') {
|
|
403
|
+
return resolve([]);
|
|
404
|
+
}
|
|
405
|
+
return resolve(data.Messages.map((message) => new SQSMessageModel_1.default(message)));
|
|
406
|
+
});
|
|
407
|
+
});
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
exports.default = SQSService;
|
|
411
|
+
//# sourceMappingURL=SQSService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SQSService.js","sourceRoot":"","sources":["../../src/services/SQSService.ts"],"names":[],"mappings":";;;;;;AAAA,gDAAwB;AACxB,iCAA6B;AAC7B,sDAA0B;AAC1B,+BAAkC;AAElC,wFAAgE;AAGhE,gFAAwD;AAExD,oEAA4C;AAC5C,kEAA0C;AAsD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,EAAE,MAAM,EAAE,EAAE;YACrC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;YAErB,IAAI,CAAC,GAAG,CAAC,cAAc,CACrB;gBACE,QAAQ,EAAE,QAAQ;gBAClB,iBAAiB,EAAE,OAAO;gBAC1B,mBAAmB,EAAE,EAAE;aACxB,EACD,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;gBACd,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;gBAEpB,IAAI,KAAK,EAAE;oBACT,MAAM,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;oBACpB,OAAO,MAAM,CAAC,KAAK,CAAC,CAAC;iBACtB;gBAED,IAAI,OAAO,IAAI,CAAC,QAAQ,KAAK,WAAW,EAAE;oBACxC,OAAO,OAAO,CAAC,EAAE,CAAC,CAAC;iBACpB;gBAED,OAAO,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,IAAI,yBAAe,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;YAC/E,CAAC,CACF,CAAC;QACJ,CAAC,CAAC,CAAC;IACL,CAAC;CACF;AA1VD,6BA0VC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import DependencyAwareClass from '../core/DependencyAwareClass';
|
|
2
|
+
/**
|
|
3
|
+
* Timer helper that can be used to measure how long operations take.
|
|
4
|
+
*/
|
|
5
|
+
export default class TimerService extends DependencyAwareClass {
|
|
6
|
+
timers: Record<string, number>;
|
|
7
|
+
/**
|
|
8
|
+
* Start a timer.
|
|
9
|
+
*
|
|
10
|
+
* To stop the timer, call `stop()` with the same `identifier`.
|
|
11
|
+
*
|
|
12
|
+
* @param identifier
|
|
13
|
+
*/
|
|
14
|
+
start(identifier: string): void;
|
|
15
|
+
/**
|
|
16
|
+
* Stop a timer and log the elapsed time.
|
|
17
|
+
*
|
|
18
|
+
* @param identifier
|
|
19
|
+
*/
|
|
20
|
+
stop(identifier: string): void;
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=TimerService.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TimerService.d.ts","sourceRoot":"","sources":["../../src/services/TimerService.ts"],"names":[],"mappings":"AAAA,OAAO,oBAAoB,MAAM,8BAA8B,CAAC;AAGhE;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,YAAa,SAAQ,oBAAoB;IAC5D,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAM;IAEpC;;;;;;OAMG;IACH,KAAK,CAAC,UAAU,EAAE,MAAM;IAIxB;;;;OAIG;IACH,IAAI,CAAC,UAAU,EAAE,MAAM;CAOxB"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const DependencyAwareClass_1 = __importDefault(require("../core/DependencyAwareClass"));
|
|
7
|
+
const LoggerService_1 = __importDefault(require("./LoggerService"));
|
|
8
|
+
/**
|
|
9
|
+
* Timer helper that can be used to measure how long operations take.
|
|
10
|
+
*/
|
|
11
|
+
class TimerService extends DependencyAwareClass_1.default {
|
|
12
|
+
constructor() {
|
|
13
|
+
super(...arguments);
|
|
14
|
+
this.timers = {};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Start a timer.
|
|
18
|
+
*
|
|
19
|
+
* To stop the timer, call `stop()` with the same `identifier`.
|
|
20
|
+
*
|
|
21
|
+
* @param identifier
|
|
22
|
+
*/
|
|
23
|
+
start(identifier) {
|
|
24
|
+
this.timers[identifier] = Date.now();
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Stop a timer and log the elapsed time.
|
|
28
|
+
*
|
|
29
|
+
* @param identifier
|
|
30
|
+
*/
|
|
31
|
+
stop(identifier) {
|
|
32
|
+
if (identifier in this.timers) {
|
|
33
|
+
const logger = this.di.get(LoggerService_1.default);
|
|
34
|
+
const duration = Date.now() - this.timers[identifier];
|
|
35
|
+
logger.info(`Timing - ${identifier} took ${duration} ms to complete`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.default = TimerService;
|
|
40
|
+
//# sourceMappingURL=TimerService.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TimerService.js","sourceRoot":"","sources":["../../src/services/TimerService.ts"],"names":[],"mappings":";;;;;AAAA,wFAAgE;AAChE,oEAA4C;AAE5C;;GAEG;AACH,MAAqB,YAAa,SAAQ,8BAAoB;IAA9D;;QACE,WAAM,GAA2B,EAAE,CAAC;IAyBtC,CAAC;IAvBC;;;;;;OAMG;IACH,KAAK,CAAC,UAAkB;QACtB,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IACvC,CAAC;IAED;;;;OAIG;IACH,IAAI,CAAC,UAAkB;QACrB,IAAI,UAAU,IAAI,IAAI,CAAC,MAAM,EAAE;YAC7B,MAAM,MAAM,GAAG,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,uBAAa,CAAC,CAAC;YAC1C,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YACtD,MAAM,CAAC,IAAI,CAAC,YAAY,UAAU,SAAS,QAAQ,iBAAiB,CAAC,CAAC;SACvE;IACH,CAAC;CACF;AA1BD,+BA0BC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Status.d.ts","sourceRoot":"","sources":["../../src/types/Status.ts"],"names":[],"mappings":"AAAA,oBAAY,MAAM,GACd,IAAI,GACJ,oBAAoB,GACpB,qBAAqB,CAAC;AAE1B,oBAAY,aAAa,GAAG;IAC1B,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Status.js","sourceRoot":"","sources":["../../src/types/Status.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An error that triggers a Lambda termination.
|
|
3
|
+
*
|
|
4
|
+
* Offers developer details (that are logged), a code for the Lambda and a
|
|
5
|
+
* front-facing consumer message.
|
|
6
|
+
*/
|
|
7
|
+
export default class LambdaTermination extends Error {
|
|
8
|
+
readonly internal: object | string;
|
|
9
|
+
readonly code: number;
|
|
10
|
+
readonly body: object | string | null;
|
|
11
|
+
readonly details: string;
|
|
12
|
+
constructor(internal: object | string, code?: number, body?: object | string | null, details?: string);
|
|
13
|
+
}
|
|
14
|
+
//# sourceMappingURL=LambdaTermination.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LambdaTermination.d.ts","sourceRoot":"","sources":["../../src/utils/LambdaTermination.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,OAAO,iBAAkB,SAAQ,KAAK;IAEhD,QAAQ,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM;IAClC,QAAQ,CAAC,IAAI;IACb,QAAQ,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI;IACrC,QAAQ,CAAC,OAAO;gBAHP,QAAQ,EAAE,MAAM,GAAG,MAAM,EACzB,IAAI,SAAM,EACV,IAAI,GAAE,MAAM,GAAG,MAAM,GAAG,IAAW,EACnC,OAAO,SAAkB;CAUrC"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
/**
|
|
4
|
+
* An error that triggers a Lambda termination.
|
|
5
|
+
*
|
|
6
|
+
* Offers developer details (that are logged), a code for the Lambda and a
|
|
7
|
+
* front-facing consumer message.
|
|
8
|
+
*/
|
|
9
|
+
class LambdaTermination extends Error {
|
|
10
|
+
constructor(internal, code = 500, body = null, details = 'unknown error') {
|
|
11
|
+
const stringified = typeof internal === 'string'
|
|
12
|
+
? internal
|
|
13
|
+
: JSON.stringify(internal);
|
|
14
|
+
super(stringified);
|
|
15
|
+
this.internal = internal;
|
|
16
|
+
this.code = code;
|
|
17
|
+
this.body = body;
|
|
18
|
+
this.details = details;
|
|
19
|
+
this.body = body || 'unknown error';
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.default = LambdaTermination;
|
|
23
|
+
//# sourceMappingURL=LambdaTermination.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"LambdaTermination.js","sourceRoot":"","sources":["../../src/utils/LambdaTermination.ts"],"names":[],"mappings":";;AAAA;;;;;GAKG;AACH,MAAqB,iBAAkB,SAAQ,KAAK;IAClD,YACW,QAAyB,EACzB,OAAO,GAAG,EACV,OAA+B,IAAI,EACnC,UAAU,eAAe;QAElC,MAAM,WAAW,GAAG,OAAO,QAAQ,KAAK,QAAQ;YAC9C,CAAC,CAAC,QAAQ;YACV,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC;QAE7B,KAAK,CAAC,WAAW,CAAC,CAAC;QATV,aAAQ,GAAR,QAAQ,CAAiB;QACzB,SAAI,GAAJ,IAAI,CAAM;QACV,SAAI,GAAJ,IAAI,CAA+B;QACnC,YAAO,GAAP,OAAO,CAAkB;QAQlC,IAAI,CAAC,IAAI,GAAG,IAAI,IAAI,eAAe,CAAC;IACtC,CAAC;CACF;AAfD,oCAeC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* PromisifiedDelay class
|
|
3
|
+
*/
|
|
4
|
+
export default class PromisifiedDelay {
|
|
5
|
+
readonly delays: number[];
|
|
6
|
+
/**
|
|
7
|
+
* PromisifiedDelay constructor
|
|
8
|
+
*
|
|
9
|
+
* @param highLatency
|
|
10
|
+
*/
|
|
11
|
+
constructor(highLatency?: boolean);
|
|
12
|
+
/**
|
|
13
|
+
* Create a promisified delay
|
|
14
|
+
*/
|
|
15
|
+
get(): Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=PromisifiedDelay.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PromisifiedDelay.d.ts","sourceRoot":"","sources":["../../src/utils/PromisifiedDelay.ts"],"names":[],"mappings":"AAgBA;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,gBAAgB;IACnC,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,CAAM;IAE/B;;;;OAIG;gBACS,WAAW,UAAO;IAY9B;;OAEG;IACH,GAAG,IAAI,OAAO,CAAC,IAAI,CAAC;CAOrB"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const STANDARD_LATENCY_DELAYS = {
|
|
4
|
+
2000: 70,
|
|
5
|
+
3500: 15,
|
|
6
|
+
4000: 10,
|
|
7
|
+
5000: 5,
|
|
8
|
+
};
|
|
9
|
+
const HIGH_LATENCY_DELAYS = {
|
|
10
|
+
2000: 65,
|
|
11
|
+
3500: 15,
|
|
12
|
+
4000: 9,
|
|
13
|
+
5000: 5,
|
|
14
|
+
10000: 5,
|
|
15
|
+
20000: 1,
|
|
16
|
+
};
|
|
17
|
+
/**
|
|
18
|
+
* PromisifiedDelay class
|
|
19
|
+
*/
|
|
20
|
+
class PromisifiedDelay {
|
|
21
|
+
/**
|
|
22
|
+
* PromisifiedDelay constructor
|
|
23
|
+
*
|
|
24
|
+
* @param highLatency
|
|
25
|
+
*/
|
|
26
|
+
constructor(highLatency = true) {
|
|
27
|
+
this.delays = [];
|
|
28
|
+
const delayArray = highLatency === true ? HIGH_LATENCY_DELAYS : STANDARD_LATENCY_DELAYS;
|
|
29
|
+
Object.keys(delayArray).forEach((delayDuration) => {
|
|
30
|
+
const delayIterations = delayArray[delayDuration];
|
|
31
|
+
for (let i = 0; i < delayIterations; i += 1) {
|
|
32
|
+
this.delays.push(Number.parseInt(delayDuration, 10));
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Create a promisified delay
|
|
38
|
+
*/
|
|
39
|
+
get() {
|
|
40
|
+
return new Promise((resolve) => {
|
|
41
|
+
setTimeout(() => {
|
|
42
|
+
resolve();
|
|
43
|
+
}, this.delays[Math.floor(Math.random() * this.delays.length)]);
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
exports.default = PromisifiedDelay;
|
|
48
|
+
//# sourceMappingURL=PromisifiedDelay.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PromisifiedDelay.js","sourceRoot":"","sources":["../../src/utils/PromisifiedDelay.ts"],"names":[],"mappings":";;AAAA,MAAM,uBAAuB,GAAG;IAC9B,IAAI,EAAE,EAAE;IACR,IAAI,EAAE,EAAE;IACR,IAAI,EAAE,EAAE;IACR,IAAI,EAAE,CAAC;CACR,CAAC;AAEF,MAAM,mBAAmB,GAAG;IAC1B,IAAI,EAAE,EAAE;IACR,IAAI,EAAE,EAAE;IACR,IAAI,EAAE,CAAC;IACP,IAAI,EAAE,CAAC;IACP,KAAM,EAAE,CAAC;IACT,KAAM,EAAE,CAAC;CACV,CAAC;AAEF;;GAEG;AACH,MAAqB,gBAAgB;IAGnC;;;;OAIG;IACH,YAAY,WAAW,GAAG,IAAI;QAPrB,WAAM,GAAa,EAAE,CAAC;QAQ7B,MAAM,UAAU,GAAG,WAAW,KAAK,IAAI,CAAC,CAAC,CAAC,mBAAmB,CAAC,CAAC,CAAC,uBAAuB,CAAC;QAExF,MAAM,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC,OAAO,CAAC,CAAC,aAAa,EAA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