-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
145 lines (124 loc) · 3.93 KB
/
index.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
'use strict';
const AWS = require('aws-sdk');
// Wrapper class for AWS Lambda
class Wrapped {
constructor(mod, opts) {
const options = opts || {};
this.lambdaModule = mod;
const handler = options.handler || 'handler';
if (mod[handler]) {
this.handler = mod[handler];
} else {
if (!this.lambdaModule.lambdaFunction) {
throw new Error("Invalid input. Expected either lambda module (with exposed handler) " +
"or AWS Lambda configuration (with provided 'lambdaFunction' name)");
}
}
}
runHandler(event, customContext, cb) {
return new Promise((resolve, reject) => {
const promiseCallback = (error, response) => {
if(error) {
return reject(error);
}
return resolve(response);
};
const callback = cb || promiseCallback;
const defaultContext = {
succeed: success => callback(null, success),
fail: error => callback(error, null),
done: (error, success) => callback(error, success)
};
const lambdaContext = Object.assign({}, defaultContext, customContext);
try {
if (this.handler) {
const handlerResult = this.handler(event, lambdaContext, callback);
// Check if the result itself is a promise
if (handlerResult && handlerResult.then) {
handlerResult.then(function(data) {
// Avoid Maximum call stack size exceeded exceptions
return setImmediate(function() {
callback(null, data);
});
}).catch(function(err) {
// Avoid Maximum call stack size exceeded exceptions
return setImmediate(function() {
callback(err);
});
});
}
} else {
if (this.lambdaModule.region) {
AWS.config.update({
region: this.lambdaModule.region
});
}
const lambda = new AWS.Lambda();
const params = {
FunctionName: this.lambdaModule.lambdaFunction,
InvocationType: 'RequestResponse',
LogType: 'None',
Payload: JSON.stringify(event)
};
lambda.invoke(params, (err, data) => {
if (err) {
return callback(err);
}
if (data.FunctionError) {
return callback(Object.assign(new Error(JSON.parse(data.Payload).errorMessage), data));
}
return callback(null, JSON.parse(data.Payload));
});
}
} catch (ex) {
return callback(ex);
}
});
}
run(event, context, callback) {
let callbackFunction = callback;
let contextObject = context;
if (typeof context === 'function') {
// backwards compability
callbackFunction = context;
contextObject = {};
}
return this.runHandler(event, contextObject, callbackFunction);
}
}
// Wrapper factory
const wrap = (mod, options) => new Wrapped(mod, options);
// Static variables (for backwards compatibility)
let latest;
// Public interface for the module
module.exports = exports = {
// reusable wrap method
wrap,
// static init/run interface for backwards compatibility
init: (mod, options) => {
latest = wrap(mod, options);
},
run: (event, context, callback) => new Promise((resolve, reject) => {
let callbackFunction = callback;
let contextObject = context;
if (typeof context === 'function') {
// backwards compability
callbackFunction = context;
contextObject = {};
}
if (typeof latest === typeof undefined) {
const error = 'Module not initialized';
reject(error);
return callbackFunction(error, null);
}
return latest.run(event, contextObject, (err, data) => {
if (callbackFunction) {
return callbackFunction(err, data);
}
if (err) {
return reject(err);
}
return resolve(data);
});
})
};