forked from erikerikson/serverless-artillery
-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.js
96 lines (85 loc) · 3.47 KB
/
handler.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
/* eslint-disable no-underscore-dangle */
const fs = require('fs')
const { safeLoad } = require('js-yaml')
const merge = require('lodash.merge')
const omit = require('lodash.omit') // eslint-disable-line import/no-unresolved
const task = require('./artillery-task.js')
const platform = require('./platform-settings.js')
const path = require('path')
const promisify = require('util-promisify') // eslint-disable-line import/no-unresolved
const mergeFileField = '>>'
const readFileAsync = promisify(fs.readFile)
const lambdaHandler = {
handleUnhandledRejection: (ex) => {
console.log('###############################################################')
console.log('## !! Unhandled promise rejection !! ##')
console.log('## This probably results from an unforseen circumstance in ##')
console.log('## a plugin. Please report the following stack trace at: ##')
console.log('## https://github.com/Nordstrom/serverless-artillery/issues ##')
console.log('###############################################################')
console.log(ex.stack)
console.log('###############################################################')
throw ex
},
getMergeFilePath: (
mergeFileInput,
resolve = path.resolve,
dirname = __dirname
) => {
const reject = message => Promise.reject(new Error(message))
if (!mergeFileInput || typeof mergeFileInput !== 'string') {
return reject(`'${typeof mergeFileInput}' is not a valid path.`)
}
const absolutePath = resolve(mergeFileInput)
if (!absolutePath.startsWith(dirname)) {
return reject(`Merge file ${absolutePath} is not a local file path.`)
}
return Promise.resolve(absolutePath)
},
readMergeFile: (
mergeFilePath,
readFile = readFileAsync,
error = console.error,
getMergeFilePath = lambdaHandler.getMergeFilePath
) =>
getMergeFilePath(mergeFilePath)
.then(readFile)
.then(safeLoad)
.catch((ex) => {
error('Failed to read merge file.', mergeFilePath, ex.stack)
throw ex
}),
mergeIf: (script, readMergeFile = lambdaHandler.readMergeFile) =>
(mergeFileField in script
? readMergeFile(script[mergeFileField])
.then(inputData => merge({}, inputData, omit(script, [mergeFileField])))
: Promise.resolve(script)),
createHandler: (taskToExecute, platformSettings) =>
(event, context, callback) => {
try {
const script = event
script._funcAws = {
functionName: context.functionName,
}
const settings = platformSettings.getSettings(script)
lambdaHandler.mergeIf(script)
.then(mergedScript => taskToExecute.executeTask(mergedScript, settings))
.then((result) => {
if (process.env.SA_DEBUG) console.log(`LambdaResult ${JSON.stringify(result)}`)
callback(null, result)
})
.catch((ex) => {
console.log(ex.stack)
if (process.env.SA_DEBUG) console.log(`LambdaResult ${JSON.stringify(ex)}`)
callback(null, `Error executing task: ${ex.message}`)
})
} catch (ex) {
console.log(ex.stack)
if (process.env.SA_DEBUG) console.log(`LambdaResult ${JSON.stringify(ex)}`)
callback(null, `Error validating event: ${ex.message}`)
}
},
}
process.on('unhandledRejection', lambdaHandler.handleUnhandledRejection)
module.exports.handler = lambdaHandler.createHandler(task, platform)
module.exports.lambdaHandler = lambdaHandler