@comicrelief/lambda-wrapper 1.10.1 → 1.10.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.
- package/README.md +2 -0
- package/dist/Config/Dependencies.js +0 -7
- package/dist/DependencyInjection/DependencyAware.class.js +2 -9
- package/dist/DependencyInjection/DependencyInjection.class.js +8 -24
- package/dist/Model/CloudEvent.model.js +12 -30
- package/dist/Model/Model.model.js +1 -7
- package/dist/Model/Response.model.js +10 -25
- package/dist/Model/SQS/MarketingPreference.model.js +52 -110
- package/dist/Model/SQS/Message.model.js +7 -19
- package/dist/Model/Status.model.js +5 -16
- package/dist/Service/BaseConfig.service.js +20 -41
- package/dist/Service/HTTP.service.js +3 -16
- package/dist/Service/Logger.service.js +17 -54
- package/dist/Service/Request.service.js +26 -73
- package/dist/Service/SQS.service.js +17 -70
- package/dist/Service/Timer.service.js +2 -11
- package/dist/Wrapper/LambdaTermination.js +0 -5
- package/dist/Wrapper/LambdaWrapper.js +15 -36
- package/dist/Wrapper/PromisifiedDelay.js +2 -6
- package/dist/index.js +0 -19
- package/package.json +17 -17
|
@@ -4,27 +4,16 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = exports.REQUEST_TYPES = exports.HTTP_METHODS_WITH_PAYLOADS = exports.HTTP_METHODS_WITHOUT_PAYLOADS = exports.ERROR_TYPES = void 0;
|
|
7
|
-
|
|
8
7
|
var _querystring = _interopRequireDefault(require("querystring"));
|
|
9
|
-
|
|
10
8
|
var _useragent = _interopRequireDefault(require("useragent"));
|
|
11
|
-
|
|
12
9
|
var _validate = _interopRequireDefault(require("validate.js/validate"));
|
|
13
|
-
|
|
14
10
|
var _xml2js = _interopRequireDefault(require("xml2js"));
|
|
15
|
-
|
|
16
11
|
var _Dependencies = require("../Config/Dependencies");
|
|
17
|
-
|
|
18
12
|
var _DependencyAware = _interopRequireDefault(require("../DependencyInjection/DependencyAware.class"));
|
|
19
|
-
|
|
20
13
|
var _Response = _interopRequireDefault(require("../Model/Response.model"));
|
|
21
|
-
|
|
22
14
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
23
|
-
|
|
24
15
|
/* eslint-disable class-methods-use-this */
|
|
25
|
-
|
|
26
|
-
/* eslint-disable sonarjs/no-duplicate-string */
|
|
27
|
-
const REQUEST_TYPES = {
|
|
16
|
+
/* eslint-disable sonarjs/no-duplicate-string */const REQUEST_TYPES = {
|
|
28
17
|
DELETE: 'DELETE',
|
|
29
18
|
GET: 'GET',
|
|
30
19
|
HEAD: 'HEAD',
|
|
@@ -36,18 +25,18 @@ const REQUEST_TYPES = {
|
|
|
36
25
|
exports.REQUEST_TYPES = REQUEST_TYPES;
|
|
37
26
|
const HTTP_METHODS_WITHOUT_PAYLOADS = [REQUEST_TYPES.DELETE, REQUEST_TYPES.GET, REQUEST_TYPES.HEAD, REQUEST_TYPES.OPTIONS];
|
|
38
27
|
exports.HTTP_METHODS_WITHOUT_PAYLOADS = HTTP_METHODS_WITHOUT_PAYLOADS;
|
|
39
|
-
const HTTP_METHODS_WITH_PAYLOADS = [REQUEST_TYPES.PATCH, REQUEST_TYPES.POST, REQUEST_TYPES.PUT];
|
|
28
|
+
const HTTP_METHODS_WITH_PAYLOADS = [REQUEST_TYPES.PATCH, REQUEST_TYPES.POST, REQUEST_TYPES.PUT];
|
|
40
29
|
|
|
30
|
+
// Define action specific error types
|
|
41
31
|
exports.HTTP_METHODS_WITH_PAYLOADS = HTTP_METHODS_WITH_PAYLOADS;
|
|
42
32
|
const ERROR_TYPES = {
|
|
43
33
|
VALIDATION_ERROR: new _Response.default({}, 400, 'required fields are missing')
|
|
44
34
|
};
|
|
35
|
+
|
|
45
36
|
/**
|
|
46
37
|
* RequestService class
|
|
47
38
|
*/
|
|
48
|
-
|
|
49
39
|
exports.ERROR_TYPES = ERROR_TYPES;
|
|
50
|
-
|
|
51
40
|
class RequestService extends _DependencyAware.default {
|
|
52
41
|
/**
|
|
53
42
|
* Get a parameter from the request.
|
|
@@ -58,24 +47,23 @@ class RequestService extends _DependencyAware.default {
|
|
|
58
47
|
*/
|
|
59
48
|
get(parameter, ifNull = null, requestType = null) {
|
|
60
49
|
const queryParameters = this.getAll(requestType);
|
|
61
|
-
|
|
62
50
|
if (queryParameters === null) {
|
|
63
51
|
return ifNull;
|
|
64
52
|
}
|
|
65
|
-
|
|
66
53
|
return typeof queryParameters[parameter] !== 'undefined' ? queryParameters[parameter] : ifNull;
|
|
67
54
|
}
|
|
55
|
+
|
|
68
56
|
/**
|
|
69
57
|
* Get all HTTP headers included in the request.
|
|
70
58
|
*
|
|
71
59
|
* @returns {object} An object with a key for each header.
|
|
72
60
|
*/
|
|
73
|
-
|
|
74
|
-
|
|
75
61
|
getAllHeaders() {
|
|
76
|
-
return {
|
|
62
|
+
return {
|
|
63
|
+
...this.getContainer().getEvent().headers
|
|
77
64
|
};
|
|
78
65
|
}
|
|
66
|
+
|
|
79
67
|
/**
|
|
80
68
|
* Get an HTTP header from the request.
|
|
81
69
|
*
|
|
@@ -86,64 +74,55 @@ class RequestService extends _DependencyAware.default {
|
|
|
86
74
|
* (default: empty string)
|
|
87
75
|
* @returns {string}
|
|
88
76
|
*/
|
|
89
|
-
|
|
90
|
-
|
|
91
77
|
getHeader(name, whenMissing = '') {
|
|
92
78
|
const headers = this.getAllHeaders();
|
|
93
|
-
|
|
94
79
|
if (!headers) {
|
|
95
80
|
return whenMissing;
|
|
96
81
|
}
|
|
97
|
-
|
|
98
82
|
const lowerName = name.toLowerCase();
|
|
99
83
|
const key = Object.keys(headers).find(k => k.toLowerCase() === lowerName);
|
|
100
84
|
return key && headers[key] || whenMissing;
|
|
101
85
|
}
|
|
86
|
+
|
|
102
87
|
/**
|
|
103
88
|
* Get authorization token
|
|
104
89
|
*
|
|
105
90
|
* @returns {*}
|
|
106
91
|
*/
|
|
107
|
-
|
|
108
|
-
|
|
109
92
|
getAuthorizationToken() {
|
|
110
93
|
const authorization = this.getHeader('Authorization');
|
|
111
|
-
|
|
112
94
|
if (!authorization) {
|
|
113
95
|
return null;
|
|
114
96
|
}
|
|
115
|
-
|
|
116
97
|
const tokenParts = authorization.split(' ');
|
|
117
98
|
const tokenValue = tokenParts[1];
|
|
118
|
-
|
|
119
99
|
if (!(tokenParts[0].toLowerCase() === 'bearer' && tokenValue)) {
|
|
120
100
|
return null;
|
|
121
101
|
}
|
|
122
|
-
|
|
123
102
|
return tokenValue;
|
|
124
103
|
}
|
|
104
|
+
|
|
125
105
|
/**
|
|
126
106
|
* Get a path parameter
|
|
127
107
|
*
|
|
128
108
|
* @param parameter
|
|
129
109
|
* @param ifNull mixed
|
|
130
110
|
*/
|
|
131
|
-
|
|
132
|
-
|
|
133
111
|
getPathParameter(parameter = null, ifNull = {}) {
|
|
134
|
-
const event = this.getContainer().getEvent();
|
|
112
|
+
const event = this.getContainer().getEvent();
|
|
135
113
|
|
|
114
|
+
// If no parameter has been requested, return all path parameters
|
|
136
115
|
if (parameter === null && typeof event.pathParameters === 'object') {
|
|
137
116
|
return event.pathParameters;
|
|
138
|
-
}
|
|
139
|
-
|
|
117
|
+
}
|
|
140
118
|
|
|
119
|
+
// If a specifc parameter has been requested, return the parameter if it exists
|
|
141
120
|
if (typeof parameter === 'string' && typeof event.pathParameters === 'object' && event.pathParameters !== null && typeof event.pathParameters[parameter] !== 'undefined') {
|
|
142
121
|
return event.pathParameters[parameter];
|
|
143
122
|
}
|
|
144
|
-
|
|
145
123
|
return ifNull;
|
|
146
124
|
}
|
|
125
|
+
|
|
147
126
|
/**
|
|
148
127
|
* Get all request parameters
|
|
149
128
|
*
|
|
@@ -151,30 +130,25 @@ class RequestService extends _DependencyAware.default {
|
|
|
151
130
|
* @returns {{}}
|
|
152
131
|
*/
|
|
153
132
|
// eslint-disable-next-line sonarjs/cognitive-complexity
|
|
154
|
-
|
|
155
|
-
|
|
156
133
|
getAll(requestType = null) {
|
|
157
134
|
const event = this.getContainer().getEvent();
|
|
158
|
-
|
|
159
135
|
if (HTTP_METHODS_WITHOUT_PAYLOADS.includes(event.httpMethod) || HTTP_METHODS_WITHOUT_PAYLOADS.includes(requestType)) {
|
|
160
136
|
// get simple parameters
|
|
161
|
-
const params = {
|
|
162
|
-
|
|
163
|
-
|
|
137
|
+
const params = {
|
|
138
|
+
...event.queryStringParameters
|
|
139
|
+
};
|
|
140
|
+
// add array parameters as arrays
|
|
164
141
|
Object.keys(params).filter(key => key.endsWith('[]')).forEach(key => {
|
|
165
142
|
params[key] = event.multiValueQueryStringParameters[key];
|
|
166
143
|
});
|
|
167
144
|
return params;
|
|
168
145
|
}
|
|
169
|
-
|
|
170
146
|
if (HTTP_METHODS_WITH_PAYLOADS.includes(event.httpMethod) || HTTP_METHODS_WITH_PAYLOADS.includes(requestType)) {
|
|
171
147
|
const contentType = this.getHeader('Content-Type');
|
|
172
148
|
let queryParameters = {};
|
|
173
|
-
|
|
174
149
|
if (contentType.includes('application/x-www-form-urlencoded')) {
|
|
175
150
|
queryParameters = _querystring.default.parse(event.body);
|
|
176
151
|
}
|
|
177
|
-
|
|
178
152
|
if (contentType.includes('application/json')) {
|
|
179
153
|
try {
|
|
180
154
|
queryParameters = JSON.parse(event.body);
|
|
@@ -182,55 +156,44 @@ class RequestService extends _DependencyAware.default {
|
|
|
182
156
|
queryParameters = {};
|
|
183
157
|
}
|
|
184
158
|
}
|
|
185
|
-
|
|
186
159
|
if (contentType.includes('text/xml')) {
|
|
187
160
|
_xml2js.default.parseString(event.body, (error, result) => {
|
|
188
161
|
queryParameters = error ? {} : result;
|
|
189
162
|
});
|
|
190
163
|
}
|
|
191
|
-
|
|
192
164
|
if (contentType.includes('multipart/form-data')) {
|
|
193
165
|
queryParameters = this.parseForm(true);
|
|
194
166
|
}
|
|
195
|
-
|
|
196
167
|
return typeof queryParameters !== 'undefined' ? queryParameters : {};
|
|
197
168
|
}
|
|
198
|
-
|
|
199
169
|
return null;
|
|
200
170
|
}
|
|
171
|
+
|
|
201
172
|
/**
|
|
202
173
|
* Fetch the request IP address
|
|
203
174
|
*
|
|
204
175
|
* @returns {*}
|
|
205
176
|
*/
|
|
206
|
-
|
|
207
|
-
|
|
208
177
|
getIp() {
|
|
209
178
|
const event = this.getContainer().getEvent();
|
|
210
|
-
|
|
211
179
|
if (typeof event.requestContext !== 'undefined' && typeof event.requestContext.identity !== 'undefined' && typeof event.requestContext.identity.sourceIp !== 'undefined') {
|
|
212
180
|
return event.requestContext.identity.sourceIp;
|
|
213
181
|
}
|
|
214
|
-
|
|
215
182
|
return null;
|
|
216
183
|
}
|
|
184
|
+
|
|
217
185
|
/**
|
|
218
186
|
* Get user agent
|
|
219
187
|
*
|
|
220
188
|
* @returns {*}
|
|
221
189
|
*/
|
|
222
|
-
|
|
223
|
-
|
|
224
190
|
getUserBrowserAndDevice() {
|
|
225
191
|
const userAgent = this.getHeader('user-agent', null);
|
|
226
|
-
|
|
227
192
|
if (userAgent === null) {
|
|
228
193
|
return null;
|
|
229
194
|
}
|
|
230
|
-
|
|
231
195
|
try {
|
|
232
196
|
const agent = _useragent.default.parse(userAgent);
|
|
233
|
-
|
|
234
197
|
const os = agent.os.toJSON();
|
|
235
198
|
return {
|
|
236
199
|
'browser-type': agent.family,
|
|
@@ -244,19 +207,17 @@ class RequestService extends _DependencyAware.default {
|
|
|
244
207
|
return null;
|
|
245
208
|
}
|
|
246
209
|
}
|
|
210
|
+
|
|
247
211
|
/**
|
|
248
212
|
* Test a request against validation constraints
|
|
249
213
|
*
|
|
250
214
|
* @param constraints
|
|
251
215
|
* @returns {Promise<any>}
|
|
252
216
|
*/
|
|
253
|
-
|
|
254
|
-
|
|
255
217
|
validateAgainstConstraints(constraints) {
|
|
256
218
|
const Logger = this.getContainer().get(_Dependencies.DEFINITIONS.LOGGER);
|
|
257
219
|
return new Promise((resolve, reject) => {
|
|
258
220
|
const validation = (0, _validate.default)(this.getAll(), constraints);
|
|
259
|
-
|
|
260
221
|
if (typeof validation === 'undefined') {
|
|
261
222
|
resolve();
|
|
262
223
|
} else {
|
|
@@ -267,14 +228,13 @@ class RequestService extends _DependencyAware.default {
|
|
|
267
228
|
}
|
|
268
229
|
});
|
|
269
230
|
}
|
|
231
|
+
|
|
270
232
|
/**
|
|
271
233
|
* Fetch the request multipart form
|
|
272
234
|
*
|
|
273
235
|
* @param useBuffer
|
|
274
236
|
* @returns {*}
|
|
275
237
|
*/
|
|
276
|
-
|
|
277
|
-
|
|
278
238
|
parseForm(useBuffer) {
|
|
279
239
|
const event = this.getContainer().getEvent();
|
|
280
240
|
const boundary = this.getBoundary(event);
|
|
@@ -294,23 +254,21 @@ class RequestService extends _DependencyAware.default {
|
|
|
294
254
|
});
|
|
295
255
|
return result;
|
|
296
256
|
}
|
|
257
|
+
|
|
297
258
|
/**
|
|
298
259
|
* Fetch the request AWS event Records
|
|
299
260
|
*
|
|
300
261
|
* @returns {*}
|
|
301
262
|
*/
|
|
302
|
-
|
|
303
|
-
|
|
304
263
|
getAWSRecords() {
|
|
305
264
|
const event = this.getContainer().getEvent();
|
|
306
265
|
const eventRecord = event.Records && event.Records[0];
|
|
307
|
-
|
|
308
266
|
if (typeof event.Records !== 'undefined' && typeof event.Records[0] !== 'undefined' && typeof eventRecord.eventSource !== 'undefined') {
|
|
309
267
|
return eventRecord;
|
|
310
268
|
}
|
|
311
|
-
|
|
312
269
|
return null;
|
|
313
270
|
}
|
|
271
|
+
|
|
314
272
|
/**
|
|
315
273
|
* Gets a value independently from
|
|
316
274
|
* the case of the key
|
|
@@ -318,24 +276,19 @@ class RequestService extends _DependencyAware.default {
|
|
|
318
276
|
* @param object
|
|
319
277
|
* @param key
|
|
320
278
|
*/
|
|
321
|
-
|
|
322
|
-
|
|
323
279
|
getValueIgnoringKeyCase(object, key) {
|
|
324
280
|
const foundKey = Object.keys(object).find(currentKey => currentKey.toLocaleLowerCase() === key.toLowerCase());
|
|
325
281
|
return object[foundKey];
|
|
326
282
|
}
|
|
283
|
+
|
|
327
284
|
/**
|
|
328
285
|
* Returns the content type
|
|
329
286
|
* assoiated with the request
|
|
330
287
|
*
|
|
331
288
|
* @param event
|
|
332
289
|
*/
|
|
333
|
-
|
|
334
|
-
|
|
335
290
|
getBoundary(event) {
|
|
336
291
|
return this.getValueIgnoringKeyCase(event.headers, 'Content-Type').split('=')[1];
|
|
337
292
|
}
|
|
338
|
-
|
|
339
293
|
}
|
|
340
|
-
|
|
341
294
|
exports.default = RequestService;
|
|
@@ -4,31 +4,18 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = exports.SQS_PUBLISH_FAILURE_MODES = exports.SQS_OFFLINE_MODES = void 0;
|
|
7
|
-
|
|
8
7
|
var _alai = _interopRequireDefault(require("alai"));
|
|
9
|
-
|
|
10
8
|
var _each = _interopRequireDefault(require("async/each"));
|
|
11
|
-
|
|
12
9
|
var _awsSdk = _interopRequireDefault(require("aws-sdk"));
|
|
13
|
-
|
|
14
10
|
var _uuid = require("uuid");
|
|
15
|
-
|
|
16
11
|
var _Dependencies = require("../Config/Dependencies");
|
|
17
|
-
|
|
18
12
|
var _DependencyAware = _interopRequireDefault(require("../DependencyInjection/DependencyAware.class"));
|
|
19
|
-
|
|
20
13
|
var _DependencyInjection = _interopRequireDefault(require("../DependencyInjection/DependencyInjection.class"));
|
|
21
|
-
|
|
22
14
|
var _Message = _interopRequireDefault(require("../Model/SQS/Message.model"));
|
|
23
|
-
|
|
24
15
|
var _Status = _interopRequireWildcard(require("../Model/Status.model"));
|
|
25
|
-
|
|
26
16
|
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
27
|
-
|
|
28
17
|
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
29
|
-
|
|
30
18
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
31
|
-
|
|
32
19
|
/**
|
|
33
20
|
* Allowed values for `process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE`.
|
|
34
21
|
*/
|
|
@@ -39,24 +26,22 @@ const SQS_OFFLINE_MODES = {
|
|
|
39
26
|
* the default.
|
|
40
27
|
*/
|
|
41
28
|
DIRECT: 'direct',
|
|
42
|
-
|
|
43
29
|
/**
|
|
44
30
|
* When running offline, send messages to an offline SQS service defined by
|
|
45
31
|
* `process.env.LAMBDA_WRAPPER_OFFLINE_SQS_HOST`.
|
|
46
32
|
*/
|
|
47
33
|
LOCAL: 'local',
|
|
48
|
-
|
|
49
34
|
/**
|
|
50
35
|
* When running offline, send messages to AWS as normal.
|
|
51
36
|
*/
|
|
52
37
|
AWS: 'aws'
|
|
53
38
|
};
|
|
39
|
+
|
|
54
40
|
/**
|
|
55
41
|
* Defines the preferred behaviour
|
|
56
42
|
* for SQSService.prototype.publish
|
|
57
43
|
* should AWS SQS fail.
|
|
58
44
|
*/
|
|
59
|
-
|
|
60
45
|
exports.SQS_OFFLINE_MODES = SQS_OFFLINE_MODES;
|
|
61
46
|
const SQS_PUBLISH_FAILURE_MODES = {
|
|
62
47
|
/**
|
|
@@ -66,19 +51,17 @@ const SQS_PUBLISH_FAILURE_MODES = {
|
|
|
66
51
|
* and for LambdaWrapper 1.8.2 and above
|
|
67
52
|
*/
|
|
68
53
|
CATCH: 'catch',
|
|
69
|
-
|
|
70
54
|
/**
|
|
71
55
|
* Throws the exception so that the caller
|
|
72
56
|
* can handle it directly.
|
|
73
57
|
*/
|
|
74
58
|
THROW: 'throw'
|
|
75
59
|
};
|
|
60
|
+
|
|
76
61
|
/**
|
|
77
62
|
* SQSService class
|
|
78
63
|
*/
|
|
79
|
-
|
|
80
64
|
exports.SQS_PUBLISH_FAILURE_MODES = SQS_PUBLISH_FAILURE_MODES;
|
|
81
|
-
|
|
82
65
|
class SQSService extends _DependencyAware.default {
|
|
83
66
|
/**
|
|
84
67
|
* SQSService constructor
|
|
@@ -101,12 +84,11 @@ class SQSService extends _DependencyAware.default {
|
|
|
101
84
|
this.queues = {};
|
|
102
85
|
this.$lambda = null;
|
|
103
86
|
this.$sqs = null;
|
|
104
|
-
|
|
105
87
|
if (container.isOffline && !Object.values(SQS_OFFLINE_MODES).includes(offlineMode)) {
|
|
106
88
|
throw new Error(`Invalid LAMBDA_WRAPPER_OFFLINE_SQS_MODE: ${offlineMode}\n` + `Please use one of: ${Object.values(SQS_OFFLINE_MODES).join(', ')}`);
|
|
107
|
-
}
|
|
108
|
-
|
|
89
|
+
}
|
|
109
90
|
|
|
91
|
+
// Add the queues from configuration
|
|
110
92
|
if (queues !== null && Object.keys(queues).length > 0) {
|
|
111
93
|
Object.keys(queues).forEach(queueDefinition => {
|
|
112
94
|
if (container.isOffline && offlineMode === SQS_OFFLINE_MODES.LOCAL) {
|
|
@@ -119,11 +101,10 @@ class SQSService extends _DependencyAware.default {
|
|
|
119
101
|
});
|
|
120
102
|
}
|
|
121
103
|
}
|
|
104
|
+
|
|
122
105
|
/**
|
|
123
106
|
* Returns an SQS client instance
|
|
124
107
|
*/
|
|
125
|
-
|
|
126
|
-
|
|
127
108
|
get sqs() {
|
|
128
109
|
if (!this.$sqs) {
|
|
129
110
|
this.$sqs = new _awsSdk.default.SQS({
|
|
@@ -134,45 +115,41 @@ class SQSService extends _DependencyAware.default {
|
|
|
134
115
|
timeout: 8 * 1000
|
|
135
116
|
},
|
|
136
117
|
maxRetries: 3 // default is 3, we can change that
|
|
137
|
-
|
|
138
118
|
});
|
|
139
119
|
}
|
|
140
120
|
|
|
141
121
|
return this.$sqs;
|
|
142
122
|
}
|
|
123
|
+
|
|
143
124
|
/**
|
|
144
125
|
* Returns a Lambda client instance
|
|
145
126
|
*/
|
|
146
|
-
|
|
147
|
-
|
|
148
127
|
get lambda() {
|
|
149
128
|
if (!this.$lambda) {
|
|
150
129
|
const endpoint = process.env.SERVICE_LAMBDA_URL;
|
|
151
|
-
|
|
152
130
|
if (!endpoint) {
|
|
153
131
|
throw new Error('process.env.SERVICE_LAMBDA_URL must be defined.');
|
|
154
|
-
}
|
|
155
|
-
|
|
132
|
+
}
|
|
156
133
|
|
|
134
|
+
// move to subprocess
|
|
157
135
|
this.$lambda = new _awsSdk.default.Lambda({
|
|
158
136
|
region: process.env.AWS_REGION,
|
|
159
137
|
endpoint
|
|
160
138
|
});
|
|
161
139
|
}
|
|
162
|
-
|
|
163
140
|
return this.$lambda;
|
|
164
141
|
}
|
|
142
|
+
|
|
165
143
|
/**
|
|
166
144
|
* Returns the mode to use for offline SQS.
|
|
167
145
|
*
|
|
168
146
|
* This is configured by `process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE`. The
|
|
169
147
|
* default is `SQS_OFFLINE_MODES.LAMBDA`.
|
|
170
148
|
*/
|
|
171
|
-
|
|
172
|
-
|
|
173
149
|
static get offlineMode() {
|
|
174
150
|
return process.env.LAMBDA_WRAPPER_OFFLINE_SQS_MODE || SQS_OFFLINE_MODES.DIRECT;
|
|
175
151
|
}
|
|
152
|
+
|
|
176
153
|
/**
|
|
177
154
|
* Batch delete messages
|
|
178
155
|
*
|
|
@@ -180,8 +157,6 @@ class SQSService extends _DependencyAware.default {
|
|
|
180
157
|
* @param messageModels [SQSMessageModel]
|
|
181
158
|
* @returns {Promise<any>}
|
|
182
159
|
*/
|
|
183
|
-
|
|
184
|
-
|
|
185
160
|
batchDelete(queue, messageModels) {
|
|
186
161
|
const container = this.getContainer();
|
|
187
162
|
const queueUrl = this.queues[queue];
|
|
@@ -190,8 +165,8 @@ class SQSService extends _DependencyAware.default {
|
|
|
190
165
|
const timerId = `sqs-batch-delete-${(0, _uuid.v4)()} - Queue: '${queueUrl}'`;
|
|
191
166
|
return new Promise(resolve => {
|
|
192
167
|
const messagesForDeletion = [];
|
|
193
|
-
Timer.start(timerId);
|
|
194
|
-
|
|
168
|
+
Timer.start(timerId);
|
|
169
|
+
// assuming openFiles is an array of file names
|
|
195
170
|
(0, _each.default)(messageModels, (messageModel, callback) => {
|
|
196
171
|
if (messageModel instanceof _Message.default && messageModel.isForDeletion() === true) {
|
|
197
172
|
messagesForDeletion.push({
|
|
@@ -199,36 +174,31 @@ class SQSService extends _DependencyAware.default {
|
|
|
199
174
|
ReceiptHandle: messageModel.getReceiptHandle()
|
|
200
175
|
});
|
|
201
176
|
}
|
|
202
|
-
|
|
203
177
|
callback();
|
|
204
178
|
}, loopError => {
|
|
205
179
|
if (loopError) {
|
|
206
180
|
Logger.error(loopError);
|
|
207
181
|
resolve();
|
|
208
182
|
}
|
|
209
|
-
|
|
210
183
|
this.sqs.deleteMessageBatch({
|
|
211
184
|
Entries: messagesForDeletion,
|
|
212
185
|
QueueUrl: queueUrl
|
|
213
186
|
}, error => {
|
|
214
187
|
Timer.stop(timerId);
|
|
215
|
-
|
|
216
188
|
if (error) {
|
|
217
189
|
Logger.error(error);
|
|
218
190
|
}
|
|
219
|
-
|
|
220
191
|
resolve();
|
|
221
192
|
});
|
|
222
193
|
});
|
|
223
194
|
});
|
|
224
195
|
}
|
|
196
|
+
|
|
225
197
|
/**
|
|
226
198
|
* Check SQS status
|
|
227
199
|
*
|
|
228
200
|
* @returns {Promise<any>}
|
|
229
201
|
*/
|
|
230
|
-
|
|
231
|
-
|
|
232
202
|
checkStatus() {
|
|
233
203
|
const container = this.getContainer();
|
|
234
204
|
const Logger = container.get(_Dependencies.DEFINITIONS.LOGGER);
|
|
@@ -239,28 +209,24 @@ class SQSService extends _DependencyAware.default {
|
|
|
239
209
|
this.sqs.listQueues({}, (error, data) => {
|
|
240
210
|
Timer.stop(timerId);
|
|
241
211
|
const statusModel = new _Status.default('SQS', _Status.STATUS_TYPES.OK);
|
|
242
|
-
|
|
243
212
|
if (error) {
|
|
244
213
|
Logger.error(error);
|
|
245
214
|
statusModel.setStatus(_Status.STATUS_TYPES.APPLICATION_FAILURE);
|
|
246
215
|
}
|
|
247
|
-
|
|
248
216
|
if (typeof data.QueueUrls === 'undefined' || data.QueueUrls.length === 0) {
|
|
249
217
|
statusModel.setStatus(_Status.STATUS_TYPES.APPLICATION_FAILURE);
|
|
250
218
|
}
|
|
251
|
-
|
|
252
219
|
resolve(statusModel);
|
|
253
220
|
});
|
|
254
221
|
});
|
|
255
222
|
}
|
|
223
|
+
|
|
256
224
|
/**
|
|
257
225
|
* Get number of messages in a queue
|
|
258
226
|
*
|
|
259
227
|
* @param queue
|
|
260
228
|
* @returns {Promise<any>}
|
|
261
229
|
*/
|
|
262
|
-
|
|
263
|
-
|
|
264
230
|
getMessageCount(queue) {
|
|
265
231
|
const container = this.getContainer();
|
|
266
232
|
const queueUrl = this.queues[queue];
|
|
@@ -274,16 +240,15 @@ class SQSService extends _DependencyAware.default {
|
|
|
274
240
|
QueueUrl: queueUrl
|
|
275
241
|
}, (error, data) => {
|
|
276
242
|
Timer.stop(timerId);
|
|
277
|
-
|
|
278
243
|
if (error) {
|
|
279
244
|
Logger.error(error);
|
|
280
245
|
resolve(0);
|
|
281
246
|
}
|
|
282
|
-
|
|
283
247
|
resolve(Number.parseInt(data.Attributes.ApproximateNumberOfMessages, 10));
|
|
284
248
|
});
|
|
285
249
|
});
|
|
286
250
|
}
|
|
251
|
+
|
|
287
252
|
/**
|
|
288
253
|
* Publish to message queue
|
|
289
254
|
*
|
|
@@ -299,13 +264,10 @@ class SQSService extends _DependencyAware.default {
|
|
|
299
264
|
* - `throw`: errors will be thrown, causing promise to reject.
|
|
300
265
|
* @returns {Promise<any>}
|
|
301
266
|
*/
|
|
302
|
-
|
|
303
|
-
|
|
304
267
|
async publish(queue, messageObject, messageGroupId = null, failureMode = SQS_PUBLISH_FAILURE_MODES.CATCH) {
|
|
305
268
|
if (!Object.values(SQS_PUBLISH_FAILURE_MODES).includes(failureMode)) {
|
|
306
269
|
throw new Error(`Invalid value for 'failureMode': ${failureMode}`);
|
|
307
270
|
}
|
|
308
|
-
|
|
309
271
|
const container = this.getContainer();
|
|
310
272
|
const queueUrl = this.queues[queue];
|
|
311
273
|
const Timer = container.get(_Dependencies.DEFINITIONS.TIMER);
|
|
@@ -315,12 +277,10 @@ class SQSService extends _DependencyAware.default {
|
|
|
315
277
|
MessageBody: JSON.stringify(messageObject),
|
|
316
278
|
QueueUrl: queueUrl
|
|
317
279
|
};
|
|
318
|
-
|
|
319
280
|
if (queueUrl.includes('.fifo')) {
|
|
320
281
|
messageParameters.MessageDeduplicationId = (0, _uuid.v4)();
|
|
321
282
|
messageParameters.MessageGroupId = messageGroupId !== null ? messageGroupId : (0, _uuid.v4)();
|
|
322
283
|
}
|
|
323
|
-
|
|
324
284
|
try {
|
|
325
285
|
if (container.isOffline && this.constructor.offlineMode === SQS_OFFLINE_MODES.DIRECT) {
|
|
326
286
|
await this.publishOffline(queue, messageParameters);
|
|
@@ -332,12 +292,11 @@ class SQSService extends _DependencyAware.default {
|
|
|
332
292
|
container.get(_Dependencies.DEFINITIONS.LOGGER).error(error);
|
|
333
293
|
return null;
|
|
334
294
|
}
|
|
335
|
-
|
|
336
295
|
throw error;
|
|
337
296
|
}
|
|
338
|
-
|
|
339
297
|
return queue;
|
|
340
298
|
}
|
|
299
|
+
|
|
341
300
|
/**
|
|
342
301
|
* Sends a message to a queue consumer running in serverless-offline.
|
|
343
302
|
*
|
|
@@ -348,22 +307,16 @@ class SQSService extends _DependencyAware.default {
|
|
|
348
307
|
* @param queue
|
|
349
308
|
* @param messageParameters
|
|
350
309
|
*/
|
|
351
|
-
|
|
352
|
-
|
|
353
310
|
async publishOffline(queue, messageParameters) {
|
|
354
311
|
const container = this.getContainer();
|
|
355
|
-
|
|
356
312
|
if (!container.isOffline) {
|
|
357
313
|
throw new Error('Can only publishOffline while running serverless offline.');
|
|
358
314
|
}
|
|
359
|
-
|
|
360
315
|
const consumers = container.getConfiguration('QUEUE_CONSUMERS') || {};
|
|
361
316
|
const FunctionName = consumers[queue];
|
|
362
|
-
|
|
363
317
|
if (!FunctionName) {
|
|
364
318
|
throw new Error(`Queue consumer for queue ${queue} was not found. Please configure your application's QUEUE_CONSUMERS.`);
|
|
365
319
|
}
|
|
366
|
-
|
|
367
320
|
const InvocationType = 'RequestResponse';
|
|
368
321
|
const Payload = JSON.stringify({
|
|
369
322
|
Records: [{
|
|
@@ -377,6 +330,7 @@ class SQSService extends _DependencyAware.default {
|
|
|
377
330
|
};
|
|
378
331
|
await this.lambda.invoke(parameters).promise();
|
|
379
332
|
}
|
|
333
|
+
|
|
380
334
|
/**
|
|
381
335
|
* Receive from message queue
|
|
382
336
|
*
|
|
@@ -384,8 +338,6 @@ class SQSService extends _DependencyAware.default {
|
|
|
384
338
|
* @param timeout number
|
|
385
339
|
* @returns {Promise<any>}
|
|
386
340
|
*/
|
|
387
|
-
|
|
388
|
-
|
|
389
341
|
receive(queue, timeout = 15) {
|
|
390
342
|
const container = this.getContainer();
|
|
391
343
|
const queueUrl = this.queues[queue];
|
|
@@ -400,21 +352,16 @@ class SQSService extends _DependencyAware.default {
|
|
|
400
352
|
MaxNumberOfMessages: 10
|
|
401
353
|
}, (error, data) => {
|
|
402
354
|
Timer.stop(timerId);
|
|
403
|
-
|
|
404
355
|
if (error) {
|
|
405
356
|
Logger.error(error);
|
|
406
357
|
return reject(error);
|
|
407
358
|
}
|
|
408
|
-
|
|
409
359
|
if (typeof data.Messages === 'undefined') {
|
|
410
360
|
return resolve([]);
|
|
411
361
|
}
|
|
412
|
-
|
|
413
362
|
return resolve(data.Messages.map(message => new _Message.default(message)));
|
|
414
363
|
});
|
|
415
364
|
});
|
|
416
365
|
}
|
|
417
|
-
|
|
418
366
|
}
|
|
419
|
-
|
|
420
367
|
exports.default = SQSService;
|