idea-aws 3.9.1 → 3.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/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/src/attachments.js +2 -9
- package/dist/src/cognito.js +2 -7
- package/dist/src/comprehend.js +1 -5
- package/dist/src/dynamoDB.js +24 -27
- package/dist/src/genericController.d.ts +29 -1
- package/dist/src/genericController.js +31 -5
- package/dist/src/logger.d.ts +19 -0
- package/dist/src/logger.js +53 -0
- package/dist/src/resourceController.d.ts +42 -9
- package/dist/src/resourceController.js +201 -141
- package/dist/src/s3.js +8 -10
- package/dist/src/secretsManager.js +3 -14
- package/dist/src/ses.js +8 -15
- package/dist/src/sns.js +2 -8
- package/dist/src/streamController.js +3 -2
- package/dist/src/translate.js +1 -5
- package/index.ts +2 -0
- package/package.json +8 -7
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ResourceController = void 0;
|
|
4
|
-
|
|
3
|
+
exports.RCError = exports.ResourceController = void 0;
|
|
4
|
+
require("source-map-support/register");
|
|
5
5
|
const fs_1 = require("fs");
|
|
6
6
|
const aws_sdk_1 = require("aws-sdk");
|
|
7
7
|
const idea_toolbox_1 = require("idea-toolbox");
|
|
8
|
+
const logger_1 = require("./logger");
|
|
8
9
|
const genericController_1 = require("./genericController");
|
|
9
10
|
/**
|
|
10
11
|
* An abstract class to inherit to manage API requests (AWS API Gateway) in an AWS Lambda function.
|
|
@@ -12,82 +13,82 @@ const genericController_1 = require("./genericController");
|
|
|
12
13
|
class ResourceController extends genericController_1.GenericController {
|
|
13
14
|
constructor(event, callback, options = {}) {
|
|
14
15
|
super(event, callback, options);
|
|
16
|
+
this.logger = new logger_1.Logger();
|
|
15
17
|
this.templateMatcher = /{{\s?([^{}\s]*)\s?}}/g;
|
|
16
18
|
///
|
|
17
19
|
/// REQUEST HANDLERS
|
|
18
20
|
///
|
|
19
|
-
this.handleRequest = () => {
|
|
20
|
-
|
|
21
|
-
.
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
21
|
+
this.handleRequest = async () => {
|
|
22
|
+
try {
|
|
23
|
+
await this.checkAuthBeforeRequest();
|
|
24
|
+
try {
|
|
25
|
+
let response;
|
|
26
|
+
if (this.resourceId) {
|
|
27
|
+
switch (this.httpMethod) {
|
|
28
|
+
// resource/{resourceId}
|
|
29
|
+
case 'GET':
|
|
30
|
+
response = await this.getResource();
|
|
31
|
+
break;
|
|
32
|
+
case 'POST':
|
|
33
|
+
response = await this.postResource();
|
|
34
|
+
break;
|
|
35
|
+
case 'PUT':
|
|
36
|
+
response = await this.putResource();
|
|
37
|
+
break;
|
|
38
|
+
case 'DELETE':
|
|
39
|
+
response = await this.deleteResource();
|
|
40
|
+
break;
|
|
41
|
+
case 'PATCH':
|
|
42
|
+
response = await this.patchResource();
|
|
43
|
+
break;
|
|
44
|
+
case 'HEAD':
|
|
45
|
+
response = await this.headResource();
|
|
46
|
+
break;
|
|
47
|
+
default:
|
|
48
|
+
this.done(new RCError('Unsupported method'));
|
|
49
|
+
}
|
|
45
50
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
51
|
+
else {
|
|
52
|
+
switch (this.httpMethod) {
|
|
53
|
+
// resource
|
|
54
|
+
case 'GET':
|
|
55
|
+
response = await this.getResources();
|
|
56
|
+
break;
|
|
57
|
+
case 'POST':
|
|
58
|
+
response = await this.postResources();
|
|
59
|
+
break;
|
|
60
|
+
case 'PUT':
|
|
61
|
+
response = await this.putResources();
|
|
62
|
+
break;
|
|
63
|
+
case 'DELETE':
|
|
64
|
+
response = await this.deleteResources();
|
|
65
|
+
break;
|
|
66
|
+
case 'PATCH':
|
|
67
|
+
response = await this.patchResources();
|
|
68
|
+
break;
|
|
69
|
+
case 'HEAD':
|
|
70
|
+
response = await this.headResources();
|
|
71
|
+
break;
|
|
72
|
+
default:
|
|
73
|
+
this.done(new RCError('Unsupported method'));
|
|
74
|
+
}
|
|
68
75
|
}
|
|
69
|
-
|
|
70
|
-
this.done(new Error('Unsupported method'));
|
|
71
|
-
else {
|
|
72
|
-
request.then((res) => this.done(null, res)).catch((err) => this.done(err));
|
|
76
|
+
this.done(null, response);
|
|
73
77
|
}
|
|
74
|
-
|
|
75
|
-
|
|
78
|
+
catch (err) {
|
|
79
|
+
this.done(this.controlHandlerError(err, 'HANDLER-ERROR', 'Operation failed'));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch (err) {
|
|
83
|
+
this.done(this.controlHandlerError(err, 'AUTH-CHECK-ERROR', 'Forbidden'));
|
|
84
|
+
}
|
|
76
85
|
};
|
|
77
|
-
this.
|
|
78
|
-
this.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
this.resource = (event.resource || '').replace('+', ''); // {proxy+} -> {proxy}
|
|
84
|
-
this.path = event.path || '';
|
|
85
|
-
this.resourceId =
|
|
86
|
-
event.pathParameters && event.pathParameters[options.resourceId || 'proxy']
|
|
87
|
-
? decodeURIComponent(event.pathParameters[options.resourceId || 'proxy'])
|
|
88
|
-
: '';
|
|
89
|
-
this.queryParams = event.queryStringParameters || {};
|
|
90
|
-
this.body = (event.body ? JSON.parse(event.body) : {}) || {};
|
|
86
|
+
this.event = event;
|
|
87
|
+
this.callback = callback;
|
|
88
|
+
if (event.version === '2.0')
|
|
89
|
+
this.initFromEventV2(event, options);
|
|
90
|
+
else
|
|
91
|
+
this.initFromEventV1(event, options);
|
|
91
92
|
this.logRequestsWithKey = options.logRequestsWithKey;
|
|
92
93
|
// acquire some info about the client, if available
|
|
93
94
|
let version = '?', platform = '?';
|
|
@@ -101,15 +102,55 @@ class ResourceController extends genericController_1.GenericController {
|
|
|
101
102
|
}
|
|
102
103
|
// print the initial log
|
|
103
104
|
const info = { principalId: this.principalId, queryParams: this.queryParams, body: this.body, version, platform };
|
|
104
|
-
|
|
105
|
+
this.logger.info(`START: ${this.httpMethod} ${this.path}`, info);
|
|
106
|
+
}
|
|
107
|
+
initFromEventV2(event, options) {
|
|
108
|
+
this.authorization = event.headers.authorization;
|
|
109
|
+
const contextFromAuthorizer = event.requestContext?.authorizer?.lambda || {};
|
|
110
|
+
this.principalId = contextFromAuthorizer.principalId;
|
|
111
|
+
this.stage = event.requestContext.stage;
|
|
112
|
+
this.httpMethod = event.requestContext.http.method;
|
|
113
|
+
this.resource = event.routeKey.replace('+', ''); // {proxy+} -> {proxy}
|
|
114
|
+
this.path = event.rawPath;
|
|
115
|
+
this.pathParameters = {};
|
|
116
|
+
for (const param in event.pathParameters)
|
|
117
|
+
this.pathParameters[param] = event.pathParameters[param] ? decodeURIComponent(event.pathParameters[param]) : null;
|
|
118
|
+
this.resourceId = this.pathParameters[options.resourceId || 'proxy'];
|
|
119
|
+
this.queryParams = event.queryStringParameters || {};
|
|
120
|
+
this.body = (event.body ? JSON.parse(event.body) : {}) || {};
|
|
121
|
+
}
|
|
122
|
+
initFromEventV1(event, options) {
|
|
123
|
+
this.authorization = event.headers.Authorization;
|
|
124
|
+
this.claims = event.requestContext.authorizer?.claims || {};
|
|
125
|
+
this.principalId = this.claims.sub;
|
|
126
|
+
this.cognitoUser = this.principalId ? new idea_toolbox_1.CognitoUser(this.claims) : null;
|
|
127
|
+
this.stage = event.requestContext.stage;
|
|
128
|
+
this.httpMethod = event.httpMethod;
|
|
129
|
+
this.resource = event.resource.replace('+', ''); // {proxy+} -> {proxy}
|
|
130
|
+
this.path = event.path;
|
|
131
|
+
this.pathParameters = {};
|
|
132
|
+
for (const param in event.pathParameters)
|
|
133
|
+
this.pathParameters[param] = event.pathParameters[param] ? decodeURIComponent(event.pathParameters[param]) : null;
|
|
134
|
+
this.resourceId = this.pathParameters[options.resourceId || 'proxy'];
|
|
135
|
+
this.queryParams = event.queryStringParameters || {};
|
|
136
|
+
this.body = (event.body ? JSON.parse(event.body) : {}) || {};
|
|
105
137
|
}
|
|
106
|
-
|
|
107
|
-
|
|
138
|
+
controlHandlerError(err = {}, context, replaceWithErrorMessage) {
|
|
139
|
+
if (err instanceof RCError)
|
|
140
|
+
return new Error(err.message);
|
|
141
|
+
this.logger.error(context, err);
|
|
142
|
+
return new Error(replaceWithErrorMessage);
|
|
143
|
+
}
|
|
144
|
+
done(err, res, statusCode = this.returnStatusCode || (err ? 400 : 200)) {
|
|
145
|
+
if (err)
|
|
146
|
+
this.logger.info('END-FAILED', { statusCode, error: err.message || err.errorMessage });
|
|
147
|
+
else
|
|
148
|
+
this.logger.info('END-SUCCESS', { statusCode });
|
|
108
149
|
// if configured, store the log of the request
|
|
109
150
|
if (this.logRequestsWithKey)
|
|
110
151
|
this.storeLog(!err);
|
|
111
152
|
this.callback(null, {
|
|
112
|
-
statusCode: statusCode
|
|
153
|
+
statusCode: String(statusCode),
|
|
113
154
|
body: err ? JSON.stringify({ message: err.message }) : JSON.stringify(res || {}),
|
|
114
155
|
headers: { 'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*' }
|
|
115
156
|
});
|
|
@@ -117,80 +158,80 @@ class ResourceController extends genericController_1.GenericController {
|
|
|
117
158
|
/**
|
|
118
159
|
* To @override
|
|
119
160
|
*/
|
|
120
|
-
checkAuthBeforeRequest() {
|
|
121
|
-
return
|
|
161
|
+
async checkAuthBeforeRequest() {
|
|
162
|
+
return;
|
|
122
163
|
}
|
|
123
164
|
/**
|
|
124
165
|
* To @override
|
|
125
166
|
*/
|
|
126
|
-
getResource() {
|
|
127
|
-
|
|
167
|
+
async getResource() {
|
|
168
|
+
throw new RCError('Unsupported method');
|
|
128
169
|
}
|
|
129
170
|
/**
|
|
130
171
|
* To @override
|
|
131
172
|
*/
|
|
132
|
-
postResource() {
|
|
133
|
-
|
|
173
|
+
async postResource() {
|
|
174
|
+
throw new RCError('Unsupported method');
|
|
134
175
|
}
|
|
135
176
|
/**
|
|
136
177
|
* To @override
|
|
137
178
|
*/
|
|
138
|
-
putResource() {
|
|
139
|
-
|
|
179
|
+
async putResource() {
|
|
180
|
+
throw new RCError('Unsupported method');
|
|
140
181
|
}
|
|
141
182
|
/**
|
|
142
183
|
* To @override
|
|
143
184
|
*/
|
|
144
|
-
deleteResource() {
|
|
145
|
-
|
|
185
|
+
async deleteResource() {
|
|
186
|
+
throw new RCError('Unsupported method');
|
|
146
187
|
}
|
|
147
188
|
/**
|
|
148
189
|
* To @override
|
|
149
190
|
*/
|
|
150
|
-
headResource() {
|
|
151
|
-
|
|
191
|
+
async headResource() {
|
|
192
|
+
throw new RCError('Unsupported method');
|
|
152
193
|
}
|
|
153
194
|
/**
|
|
154
195
|
* To @override
|
|
155
196
|
*/
|
|
156
|
-
getResources() {
|
|
157
|
-
|
|
197
|
+
async getResources() {
|
|
198
|
+
throw new RCError('Unsupported method');
|
|
158
199
|
}
|
|
159
200
|
/**
|
|
160
201
|
* To @override
|
|
161
202
|
*/
|
|
162
|
-
postResources() {
|
|
163
|
-
|
|
203
|
+
async postResources() {
|
|
204
|
+
throw new RCError('Unsupported method');
|
|
164
205
|
}
|
|
165
206
|
/**
|
|
166
207
|
* To @override
|
|
167
208
|
*/
|
|
168
|
-
putResources() {
|
|
169
|
-
|
|
209
|
+
async putResources() {
|
|
210
|
+
throw new RCError('Unsupported method');
|
|
170
211
|
}
|
|
171
212
|
/**
|
|
172
213
|
* To @override
|
|
173
214
|
*/
|
|
174
|
-
patchResource() {
|
|
175
|
-
|
|
215
|
+
async patchResource() {
|
|
216
|
+
throw new RCError('Unsupported method');
|
|
176
217
|
}
|
|
177
218
|
/**
|
|
178
219
|
* To @override
|
|
179
220
|
*/
|
|
180
|
-
patchResources() {
|
|
181
|
-
|
|
221
|
+
async patchResources() {
|
|
222
|
+
throw new RCError('Unsupported method');
|
|
182
223
|
}
|
|
183
224
|
/**
|
|
184
225
|
* To @override
|
|
185
226
|
*/
|
|
186
|
-
deleteResources() {
|
|
187
|
-
|
|
227
|
+
async deleteResources() {
|
|
228
|
+
throw new RCError('Unsupported method');
|
|
188
229
|
}
|
|
189
230
|
/**
|
|
190
231
|
* To @override
|
|
191
232
|
*/
|
|
192
|
-
headResources() {
|
|
193
|
-
|
|
233
|
+
async headResources() {
|
|
234
|
+
throw new RCError('Unsupported method');
|
|
194
235
|
}
|
|
195
236
|
///
|
|
196
237
|
/// HELPERS
|
|
@@ -242,47 +283,56 @@ class ResourceController extends genericController_1.GenericController {
|
|
|
242
283
|
* @return the body of the response
|
|
243
284
|
* @deprecated don't run a Lambda from another Lambda (bad practice)
|
|
244
285
|
*/
|
|
245
|
-
invokeInternalAPIRequest(params) {
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
}
|
|
286
|
+
async invokeInternalAPIRequest(params) {
|
|
287
|
+
if (params.lambda)
|
|
288
|
+
return await this.invokeInternalAPIRequestWithLambda(params);
|
|
289
|
+
if (params.eventBridge)
|
|
290
|
+
return await this.invokeInternalAPIRequestWithEventBridge(params);
|
|
291
|
+
throw new Error('Either "lambda" or "eventBus" parameters must be set.');
|
|
292
|
+
}
|
|
293
|
+
async invokeInternalAPIRequestWithLambda(params) {
|
|
294
|
+
const lambdaInvokeParams = {
|
|
295
|
+
FunctionName: params.lambda,
|
|
296
|
+
InvocationType: 'RequestResponse',
|
|
297
|
+
Payload: this.mapEventForInternalApiRequest(params),
|
|
298
|
+
Qualifier: params.stage || this.stage
|
|
299
|
+
};
|
|
300
|
+
const res = await new aws_sdk_1.Lambda().invoke(lambdaInvokeParams).promise();
|
|
301
|
+
const payload = JSON.parse(res.Payload);
|
|
302
|
+
const body = JSON.parse(payload.body);
|
|
303
|
+
if (Number(payload.statusCode) !== 200)
|
|
304
|
+
throw new Error(body.message);
|
|
305
|
+
return body;
|
|
306
|
+
}
|
|
307
|
+
async invokeInternalAPIRequestWithEventBridge(params) {
|
|
308
|
+
const request = {
|
|
309
|
+
EventBusName: params.eventBridge.bus,
|
|
310
|
+
Source: this.constructor.name,
|
|
311
|
+
DetailType: params.eventBridge.target,
|
|
312
|
+
Detail: this.mapEventForInternalApiRequest(params)
|
|
313
|
+
};
|
|
314
|
+
return await new aws_sdk_1.EventBridge().putEvents({ Entries: [request] }).promise();
|
|
315
|
+
}
|
|
316
|
+
mapEventForInternalApiRequest(params) {
|
|
317
|
+
const event = JSON.parse(JSON.stringify(this.event));
|
|
318
|
+
// change only the event attributes we need; e.g. the authorization is unchanged
|
|
319
|
+
if (!event.requestContext)
|
|
320
|
+
event.requestContext = {};
|
|
321
|
+
event.requestContext.stage = params.stage || this.stage;
|
|
322
|
+
if (!event.requestContext.http)
|
|
323
|
+
event.requestContext.http;
|
|
324
|
+
event.requestContext.http.method = event.httpMethod = params.httpMethod;
|
|
325
|
+
event.routeKey = event.resource = params.resource;
|
|
326
|
+
event.pathParameters = params.pathParams || {};
|
|
327
|
+
event.queryStringParameters = params.queryParams || {};
|
|
328
|
+
event.body = JSON.stringify(params.body || {});
|
|
329
|
+
event.rawPath = event.path = params.resource;
|
|
330
|
+
for (const p in event.pathParameters)
|
|
331
|
+
if (event.pathParameters[p])
|
|
332
|
+
event.rawPath = event.path = event.path.replace(`{${p}}`, event.pathParameters[p]);
|
|
333
|
+
// set a flag to make the invoked to recognise that is an internal request
|
|
334
|
+
event.internalAPIRequest = true;
|
|
335
|
+
return JSON.stringify(event);
|
|
286
336
|
}
|
|
287
337
|
/**
|
|
288
338
|
* Whether the current request comes from an internal API request, i.e. it was invoked by another controller.
|
|
@@ -365,3 +415,13 @@ class ResourceController extends genericController_1.GenericController {
|
|
|
365
415
|
}
|
|
366
416
|
}
|
|
367
417
|
exports.ResourceController = ResourceController;
|
|
418
|
+
/**
|
|
419
|
+
* Explicitly define a specific type of error to use in the RC's handler, to distinguish it from the normal errors.
|
|
420
|
+
*/
|
|
421
|
+
class RCError extends Error {
|
|
422
|
+
constructor(message) {
|
|
423
|
+
super(message);
|
|
424
|
+
Object.setPrototypeOf(this, RCError.prototype);
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
exports.RCError = RCError;
|
package/dist/src/s3.js
CHANGED
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.GetObjectTypes = exports.S3 = void 0;
|
|
4
4
|
const aws_sdk_1 = require("aws-sdk");
|
|
5
5
|
const idea_toolbox_1 = require("idea-toolbox");
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
const logger_1 = require("./logger");
|
|
7
|
+
const logger = new logger_1.Logger();
|
|
8
8
|
/**
|
|
9
9
|
* A wrapper for AWS Simple Storage Service.
|
|
10
10
|
*/
|
|
@@ -17,9 +17,7 @@ class S3 {
|
|
|
17
17
|
this.DEFAULT_DOWNLOAD_BUCKET = 'idea-downloads';
|
|
18
18
|
this.DEFAULT_DOWNLOAD_BUCKET_SEC_TO_EXP = 180;
|
|
19
19
|
this.DEFAULT_UPLOAD_BUCKET_SEC_TO_EXP = 300;
|
|
20
|
-
|
|
21
|
-
ideaWarmStart_s3 = new aws_sdk_1.S3({ apiVersion: '2006-03-01', signatureVersion: 'v4' });
|
|
22
|
-
this.s3 = ideaWarmStart_s3;
|
|
20
|
+
this.s3 = new aws_sdk_1.S3({ apiVersion: '2006-03-01', signatureVersion: 'v4' });
|
|
23
21
|
}
|
|
24
22
|
/**
|
|
25
23
|
* Create a download link of a piece of data (through S3).
|
|
@@ -67,14 +65,14 @@ class S3 {
|
|
|
67
65
|
* Make a copy of an object of the bucket.
|
|
68
66
|
*/
|
|
69
67
|
async copyObject(options) {
|
|
70
|
-
|
|
68
|
+
logger.debug(`S3 copy object: ${options.key}`);
|
|
71
69
|
await this.s3.copyObject({ CopySource: options.copySource, Bucket: options.bucket, Key: options.key }).promise();
|
|
72
70
|
}
|
|
73
71
|
/**
|
|
74
72
|
* Get an object from a S3 bucket.
|
|
75
73
|
*/
|
|
76
74
|
async getObject(options) {
|
|
77
|
-
|
|
75
|
+
logger.debug(`S3 get object: ${options.type}`);
|
|
78
76
|
const result = await this.s3.getObject({ Bucket: options.bucket, Key: options.key }).promise();
|
|
79
77
|
switch (options.type) {
|
|
80
78
|
case GetObjectTypes.JSON:
|
|
@@ -96,21 +94,21 @@ class S3 {
|
|
|
96
94
|
params.ACL = options.acl;
|
|
97
95
|
if (options.metadata)
|
|
98
96
|
params.Metadata = options.metadata;
|
|
99
|
-
|
|
97
|
+
logger.debug(`S3 put object: ${options.key}`);
|
|
100
98
|
return await this.s3.putObject(params).promise();
|
|
101
99
|
}
|
|
102
100
|
/**
|
|
103
101
|
* Delete an object from an S3 bucket.
|
|
104
102
|
*/
|
|
105
103
|
async deleteObject(options) {
|
|
106
|
-
|
|
104
|
+
logger.debug(`S3 delete object: ${options.key}`);
|
|
107
105
|
return await this.s3.deleteObject({ Bucket: options.bucket, Key: options.key }).promise();
|
|
108
106
|
}
|
|
109
107
|
/**
|
|
110
108
|
* List the objects of an S3 bucket.
|
|
111
109
|
*/
|
|
112
110
|
async listObjects(options) {
|
|
113
|
-
|
|
111
|
+
logger.debug(`S3 list object: ${options.prefix}`);
|
|
114
112
|
return await this.s3.listObjects({ Bucket: options.bucket, Prefix: options.prefix }).promise();
|
|
115
113
|
}
|
|
116
114
|
/**
|
|
@@ -2,30 +2,19 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.SecretsManager = void 0;
|
|
4
4
|
const aws_sdk_1 = require("aws-sdk");
|
|
5
|
-
const idea_toolbox_1 = require("idea-toolbox");
|
|
6
|
-
// declare libs as global vars to be reused in warm starts by the Lambda function
|
|
7
|
-
let ideaWarmStart_secretsManager = null;
|
|
8
5
|
/**
|
|
9
6
|
* A wrapper for AWS Secrets manager.
|
|
10
7
|
*/
|
|
11
8
|
class SecretsManager {
|
|
12
9
|
constructor() {
|
|
13
|
-
|
|
14
|
-
ideaWarmStart_secretsManager = new aws_sdk_1.SecretsManager();
|
|
15
|
-
this.sm = ideaWarmStart_secretsManager;
|
|
10
|
+
this.sm = new aws_sdk_1.SecretsManager();
|
|
16
11
|
}
|
|
17
12
|
/**
|
|
18
13
|
* Get a secret string from the Secret Manager by its id.
|
|
19
14
|
*/
|
|
20
15
|
async getStringById(secretId) {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
return result.SecretString;
|
|
24
|
-
}
|
|
25
|
-
catch (err) {
|
|
26
|
-
(0, idea_toolbox_1.logger)('SECRET NOT FOUND', err, secretId);
|
|
27
|
-
throw new Error('Not found');
|
|
28
|
-
}
|
|
16
|
+
const result = await this.sm.getSecretValue({ SecretId: secretId }).promise();
|
|
17
|
+
return result.SecretString;
|
|
29
18
|
}
|
|
30
19
|
}
|
|
31
20
|
exports.SecretsManager = SecretsManager;
|
package/dist/src/ses.js
CHANGED
|
@@ -3,10 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SES = void 0;
|
|
4
4
|
const aws_sdk_1 = require("aws-sdk");
|
|
5
5
|
const nodemailer_1 = require("nodemailer");
|
|
6
|
-
const idea_toolbox_1 = require("idea-toolbox");
|
|
7
6
|
const dynamoDB_1 = require("./dynamoDB");
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
const logger_1 = require("./logger");
|
|
8
|
+
const logger = new logger_1.Logger();
|
|
10
9
|
/**
|
|
11
10
|
* A wrapper for AWS Simple Email Service.
|
|
12
11
|
*/
|
|
@@ -24,10 +23,8 @@ class SES {
|
|
|
24
23
|
Source: sesParams.sourceName ? `${sesParams.sourceName} <${sesParams.source}>` : sesParams.source,
|
|
25
24
|
SourceArn: sesParams.sourceArn
|
|
26
25
|
};
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
ideaWarmStart_ses = new aws_sdk_1.SES({ region: sesParams.region });
|
|
30
|
-
return await ideaWarmStart_ses.sendTemplatedEmail(request).promise();
|
|
26
|
+
logger.debug('SES send templated email');
|
|
27
|
+
return await new aws_sdk_1.SES({ region: sesParams.region }).sendTemplatedEmail(request).promise();
|
|
31
28
|
}
|
|
32
29
|
/**
|
|
33
30
|
* Send an email through AWS Simple Email Service.
|
|
@@ -61,10 +58,8 @@ class SES {
|
|
|
61
58
|
Source: sesParams.sourceName ? `${sesParams.sourceName} <${sesParams.source}>` : sesParams.source,
|
|
62
59
|
SourceArn: sesParams.sourceArn
|
|
63
60
|
};
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
ideaWarmStart_ses = new aws_sdk_1.SES({ region: sesParams.region });
|
|
67
|
-
return await ideaWarmStart_ses.sendEmail(request).promise();
|
|
61
|
+
logger.debug('SES send email');
|
|
62
|
+
return await new aws_sdk_1.SES({ region: sesParams.region }).sendEmail(request).promise();
|
|
68
63
|
}
|
|
69
64
|
async sendEmailWithNodemailer(emailData, sesParams) {
|
|
70
65
|
const mailOptions = {};
|
|
@@ -82,10 +77,8 @@ class SES {
|
|
|
82
77
|
if (emailData.text)
|
|
83
78
|
mailOptions.text = emailData.text;
|
|
84
79
|
mailOptions.attachments = emailData.attachments;
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
ideaWarmStart_ses = new aws_sdk_1.SES({ region: sesParams.region });
|
|
88
|
-
return await (0, nodemailer_1.createTransport)({ SES: ideaWarmStart_ses }).sendMail(mailOptions);
|
|
80
|
+
logger.debug('SES send email (Nodemailer)');
|
|
81
|
+
return await (0, nodemailer_1.createTransport)({ SES: new aws_sdk_1.SES({ region: sesParams.region }) }).sendMail(mailOptions);
|
|
89
82
|
}
|
|
90
83
|
prepareEmailDestination(emailData) {
|
|
91
84
|
return {
|
package/dist/src/sns.js
CHANGED
|
@@ -3,8 +3,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SNS = void 0;
|
|
4
4
|
const aws_sdk_1 = require("aws-sdk");
|
|
5
5
|
const idea_toolbox_1 = require("idea-toolbox");
|
|
6
|
-
// declare libs as global vars to be reused in warm starts by the Lambda function
|
|
7
|
-
let ideaWarmStart_sns = null;
|
|
8
6
|
/**
|
|
9
7
|
* A wrapper for AWS Simple Notification Service.
|
|
10
8
|
*/
|
|
@@ -29,9 +27,7 @@ class SNS {
|
|
|
29
27
|
throw new Error('Unsupported platform');
|
|
30
28
|
}
|
|
31
29
|
(0, idea_toolbox_1.logger)('SNS ADD PLATFORM ENDPOINT');
|
|
32
|
-
|
|
33
|
-
ideaWarmStart_sns = new aws_sdk_1.SNS({ apiVersion: '2010-03-31', region: snsParams.region });
|
|
34
|
-
const result = await ideaWarmStart_sns
|
|
30
|
+
const result = await new aws_sdk_1.SNS({ apiVersion: '2010-03-31', region: snsParams.region })
|
|
35
31
|
.createPlatformEndpoint({ PlatformApplicationArn: platformARN, Token: token })
|
|
36
32
|
.promise();
|
|
37
33
|
return result.EndpointArn;
|
|
@@ -60,9 +56,7 @@ class SNS {
|
|
|
60
56
|
throw new Error('Unsupported platform');
|
|
61
57
|
}
|
|
62
58
|
(0, idea_toolbox_1.logger)('SNS PUBLISH IN TOPIC');
|
|
63
|
-
|
|
64
|
-
ideaWarmStart_sns = new aws_sdk_1.SNS({ apiVersion: '2010-03-31', region: snsParams.region });
|
|
65
|
-
return await ideaWarmStart_sns
|
|
59
|
+
return await new aws_sdk_1.SNS({ apiVersion: '2010-03-31', region: snsParams.region })
|
|
66
60
|
.publish({ MessageStructure: 'json', Message: JSON.stringify(structuredMessage), TargetArn: snsParams.endpoint })
|
|
67
61
|
.promise();
|
|
68
62
|
}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.StreamController = void 0;
|
|
4
|
-
const idea_toolbox_1 = require("idea-toolbox");
|
|
5
4
|
const genericController_1 = require("./genericController");
|
|
5
|
+
const logger_1 = require("./logger");
|
|
6
|
+
const logger = new logger_1.Logger();
|
|
6
7
|
/**
|
|
7
8
|
* An abstract class to inherit to manage AWS DDB streams in an AWS Lambda function.
|
|
8
9
|
*/
|
|
@@ -10,7 +11,7 @@ class StreamController extends genericController_1.GenericController {
|
|
|
10
11
|
constructor(event, callback, options = {}) {
|
|
11
12
|
super(event, callback, options);
|
|
12
13
|
this.records = event.Records || [];
|
|
13
|
-
|
|
14
|
+
logger.info(`START STREAM: ${this.records.length || 0} records`);
|
|
14
15
|
}
|
|
15
16
|
}
|
|
16
17
|
exports.StreamController = StreamController;
|