forked from Be-FaaS/BeFaaS-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
call.js
62 lines (55 loc) · 1.91 KB
/
call.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
const _ = require('lodash')
const fetch = require('node-fetch')
const helper = require('./helper')
const experiment = helper.loadExperiment()
const endpoints = {
aws: process.env.AWS_LAMBDA_ENDPOINT,
google: process.env.GOOGLE_CLOUDFUNCTION_ENDPOINT,
azure: process.env.AZURE_FUNCTIONS_ENDPOINT,
tinyfaas: process.env.TINYFAAS_ENDPOINT,
openfaas: process.env.OPENFAAS_ENDPOINT,
openwhisk: process.env.OPENWHISK_ENDPOINT
}
const publisherEndpoints = {
aws: process.env.PUBLISHER_AWS_ENDPOINT,
google: process.env.PUBLISHER_GOOGLE_ENDPOINT,
azure: process.env.PUBLISHER_AZURE_ENDPOINT,
tinyfaas: process.env.PUBLISHER_TINYFAAS_ENDPOINT
}
module.exports = async (fn, contextId, xPair, payload) => {
if (!_.isObject(payload)) throw new Error('payload is not an object')
var provider = ""
if (fn === "publisher") {
var functionName = payload.fun
provider = _.get(experiment, `program.functions.${functionName}.provider`)
// console.log("Will call publisher, provider is " + provider)
if (!publisherEndpoints[provider]) throw new Error('unknown publisher provider')
} else {
provider = _.get(experiment, `program.functions.${fn}.provider`)
// console.log("Will call " + fn + ", provider is " + provider)
if (!endpoints[provider]) throw new Error('unknown provider')
}
if (fn === "publisher") {
const res = await fetch(`${publisherEndpoints[provider]}/call`, {
method: 'post',
body: JSON.stringify(payload || {}),
headers: {
'Content-Type': 'application/json',
'X-Context': contextId,
'X-Pair': xPair
}
})
return res.json()
} else {
const res = await fetch(`${endpoints[provider]}/${fn}/call`, {
method: 'post',
body: JSON.stringify(payload || {}),
headers: {
'Content-Type': 'application/json',
'X-Context': contextId,
'X-Pair': xPair
}
})
return res.json()
}
}