forked from Be-FaaS/BeFaaS-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenwhisk.js
42 lines (35 loc) · 929 Bytes
/
openwhisk.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
const _ = require('lodash')
const openwhiskHandler = require('expressjs-openwhisk')
function handlerSingelton (appFactory) {
let app = null
return () => {
if (!app) app = appFactory()
return app.openwhiskHandler
}
}
function extractEnv (args) {
return _.mapKeys(
_.pickBy(args, (v, k) => k.startsWith('__env_')),
(v, k) => k.replace(/^__env_/, '')
)
}
function stripPrivateArgs (args) {
return _.pickBy(args, (v, k) => !k.startsWith('__'))
}
module.exports = appFactory => {
const getHandler = handlerSingelton(appFactory)
return args => {
process.env = {
...process.env,
...extractEnv(args)
}
if (
!args.__ow_body &&
['post', 'put', 'patch'].includes(args.__ow_method)
) {
args.__ow_body = stripPrivateArgs(args)
args.__ow_headers = _.omit(args.__ow_headers, ['content-type'])
}
return openwhiskHandler(getHandler())(args)
}
}