-
-
Notifications
You must be signed in to change notification settings - Fork 795
/
LambdaFunction.js
185 lines (150 loc) · 4.85 KB
/
LambdaFunction.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
'use strict'
const { resolve } = require('path')
const HandlerRunner = require('./handler-runner/index.js')
const LambdaContext = require('./LambdaContext.js')
const serverlessLog = require('./serverlessLog.js')
const {
DEFAULT_LAMBDA_MEMORY_SIZE,
DEFAULT_LAMBDA_RUNTIME,
DEFAULT_LAMBDA_TIMEOUT,
supportedRuntimes,
} = require('./config/index.js')
const { createUniqueId, splitHandlerPathAndName } = require('./utils/index.js')
const { now } = Date
module.exports = class LambdaFunction {
constructor(functionName, functionObj, provider, config, options) {
// TEMP options.location, for compatibility with serverless-webpack:
// https://github.com/dherault/serverless-offline/issues/787
// TODO FIXME look into better way to work with serverless-webpack
const servicePath = resolve(config.servicePath, options.location)
const { /* servicePath, */ serverlessPath } = config
const { name, handler } = functionObj
const [handlerPath, handlerName] = splitHandlerPathAndName(handler)
const memorySize =
functionObj.memorySize ||
provider.memorySize ||
DEFAULT_LAMBDA_MEMORY_SIZE
const runtime =
functionObj.runtime || provider.runtime || DEFAULT_LAMBDA_RUNTIME
const timeout =
(functionObj.timeout || provider.timeout || DEFAULT_LAMBDA_TIMEOUT) * 1000
// TEMP
const funOptions = {
functionName,
handlerName,
handlerPath: resolve(servicePath, handlerPath),
runtime,
serverlessPath,
servicePath,
}
this._awsRequestId = null
this._environment = {
...provider.environment,
...functionObj.environment,
}
this._executionTimeEnded = null
this._executionTimeStarted = null
this._executionTimeout = null
this._functionName = functionName
this._handler = handler
this._handlerRunner = new HandlerRunner(funOptions, options, this._stage)
this._lambdaName = name
this._memorySize = memorySize
this._region = provider.region
this._runtime = runtime
this._serverlessPath = serverlessPath
this._servicePath = servicePath
this._stage = provider.stage
this._timeout = timeout
this._verifySupportedRuntime()
}
_startExecutionTimer() {
this._executionTimeStarted = now()
this._executionTimeout = this._executionTimeStarted + this._timeout * 1000
}
_stopExecutionTimer() {
this._executionTimeEnded = now()
}
_verifySupportedRuntime() {
// print message but keep working (don't error out or exit process)
if (!supportedRuntimes.has(this._runtime)) {
// this.printBlankLine(); // TODO
console.log('')
serverlessLog(
`Warning: found unsupported runtime '${this._runtime}' for function '${this._functionName}'`,
)
}
}
_getProcessEnv() {
return {
...process.env,
_HANDLER: this._handler,
AWS_REGION: this._region, // TODO what is this good for?
...this._environment,
}
}
addEvent(event) {
this._event = event
}
getExecutionTimeInMillis() {
return this._executionTimeEnded - this._executionTimeStarted
}
getAwsRequestId() {
return this._awsRequestId
}
async runHandler() {
this._awsRequestId = createUniqueId()
const lambdaContext = new LambdaContext({
awsRequestId: this._awsRequestId,
getRemainingTimeInMillis: () => {
const time = this._executionTimeout - now()
// just return 0 for now if we are beyond alotted time (timeout)
return time > 0 ? time : 0
},
lambdaName: this._lambdaName,
memorySize: this._memorySize,
})
let callback
const callbackCalled = new Promise((resolve, reject) => {
callback = (err, data) => {
if (err) {
reject(err)
}
resolve(data)
}
lambdaContext.once('contextCalled', callback)
})
const context = lambdaContext.create()
this._startExecutionTimer()
let result
process.env = this._getProcessEnv()
// execute (run) handler
try {
result = this._handlerRunner.run(this._event, context, callback)
} catch (err) {
// this only executes when we have an exception caused by synchronous code
// TODO logging
console.log(err)
throw new Error(`Uncaught error in '${this._functionName}' handler.`)
}
// // not a Promise, which is not supported by aws
// if (result == null || typeof result.then !== 'function') {
// throw new Error(`Synchronous function execution is not supported.`);
// }
const callbacks = [callbackCalled]
// Promise was returned
if (result != null && typeof result.then === 'function') {
callbacks.push(result)
}
let callbackResult
try {
callbackResult = await Promise.race(callbacks)
} catch (err) {
// TODO logging
console.log(err)
throw err
}
this._stopExecutionTimer()
return callbackResult
}
}