/
LambdaHelper.js
207 lines (172 loc) · 5.21 KB
/
LambdaHelper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const aws = require('aws-sdk'),
log4js = require('log4js');
const assert = require('@barchart/common-js/lang/assert'),
Enum = require('@barchart/common-js/lang/Enum');
const LambdaEventParser = require('./LambdaEventParser'),
LambdaResponder = require('./LambdaResponder'),
LambdaSecretsManager = require('./LambdaSecretsManager'),
LambdaStage = require('./LambdaStage');
const FailureReason = require('@barchart/common-js/api/failures/FailureReason'),
FailureType = require('@barchart/common-js/api/failures/FailureType'),
LambdaFailureType = require('./LambdaFailureType');
const LambdaValidator = require('./LambdaValidator');
module.exports = (() => {
'use strict';
/**
* Basic utility for processing a Lambda Function.
*
* @public
*/
class LambdaHelper {
constructor() {
}
/**
* Configures and returns a log4js logger.
*
* @public
* @param {Object|String=} configuration - Configuration path (as string) or a configuration data (as object).
* @returns {Object}
*/
static getLogger(configuration) {
if (lambdaLogger === null) {
log4js.configure(configuration);
lambdaLogger = log4js.getLogger('LambdaHelper');
eventLogger = log4js.getLogger('LambdaHelper/Event');
const awsLogger = log4js.getLogger('aws-sdk');
const awsLogWrapper = { };
awsLogWrapper.log = (message) => {
if (awsLogger.isDebugEnabled()) {
awsLogger.debug(message);
}
};
aws.config.logger = awsLogWrapper;
}
return lambdaLogger;
}
/**
* Returns secret value from AWS Secrets Manager.
*
* @public
* @async
* @param {String} secretId
* @return {Promise<String>}
*/
static async getSecretValue(secretId) {
return LambdaSecretsManager.INSTANCE.getValue(secretId);
}
/**
* Builds and returns a new {@link LambdaEventParser}.
*
* @public
* @param {Object} event
* @returns {LambdaEventParser}
*/
static getEventParser(event) {
return new LambdaEventParser(event);
}
/**
* Builds and returns a new {@link LambdaValidator}.
*
* @public
* @returns {LambdaValidator}
*/
static getValidator() {
return new LambdaValidator();
}
/**
* Builds and returns a new {@link LambdaResponder}.
*
* @public
* @param {Function} callback
* @returns {LambdaResponder}
*/
static getResponder(callback) {
return new LambdaResponder(callback);
}
/**
* Builds and returns a new {@link LambdaStage}.
*
* @public
* @param {String} stage
* @returns {LambdaStage}
*/
static getStage(stage) {
assert.argumentIsRequired(stage, 'stage', String);
return Enum.fromCode(LambdaStage, stage);
}
/**
* Starts a promise chain for the Lambda function, invoking the suppressor, then
* the processor, and responding with the processor's result.
*
* @public
* @async
* @param {String} description - Human-readable description of the Lambda Function.
* @param {Object} event - The actual "event" object passed to the Lambda Function by the AWS framework.
* @param {Function} callback - The actual "callback" function passed to the Lambda Function by the AWS framework.
* @param {Callbacks.LambdaProcessorCallback} processor - The processor that is invoked to perform the work.
* @returns {Promise}
*/
static async process(description, event, callback, processor) {
const context = { };
return Promise.resolve(context)
.then((context) => {
assert.argumentIsRequired(description, 'description', String);
assert.argumentIsRequired(processor, 'processor', Function);
context.parser = LambdaHelper.getEventParser(event);
context.responder = LambdaHelper.getResponder(callback);
if (context.parser.plainText) {
context.responder.setPlainText();
}
if (eventLogger && eventLogger.isTraceEnabled()) {
eventLogger.trace(JSON.stringify(event, null, 2));
}
return context;
}).then((context) => {
const validator = LambdaHelper.getValidator();
return validator.validate(event)
.then((valid) => {
if (valid) {
return Promise.resolve(context);
} else {
return Promise.reject(FailureReason.from(LambdaFailureType.LAMBDA_INVOCATION_SUPPRESSED));
}
});
}).then((context) => {
return Promise.resolve()
.then(() => {
return processor(context.parser, context.responder);
}).then((response) => {
context.responder.send(response);
});
}).catch((e) => {
let reason;
if (e instanceof FailureReason) {
reason = e;
if (lambdaLogger) {
if (reason.getIsSevere()) {
lambdaLogger.error(reason.format());
} else {
lambdaLogger.warn(reason.format());
}
}
} else {
reason = new FailureReason({ endpoint: { description }});
reason = reason.addItem(FailureType.REQUEST_GENERAL_FAILURE);
if (lambdaLogger) {
lambdaLogger.error(e);
}
}
if (eventLogger && !eventLogger.isTraceEnabled()) {
eventLogger.warn(JSON.stringify(event, null, 2));
}
context.responder.sendError(reason, reason.getErrorCode());
});
}
toString() {
return 'LambdaHelper';
}
}
let lambdaLogger = null;
let eventLogger = null;
return LambdaHelper;
})();