2021-12-15 12:25:52 +01:00
|
|
|
const env = require("../environment")
|
|
|
|
const CouchDB = require("../db")
|
|
|
|
const { init } = require("@budibase/auth")
|
2021-12-16 17:58:15 +01:00
|
|
|
const redis = require("@budibase/auth/redis")
|
|
|
|
const { SEPARATOR } = require("@budibase/auth/db")
|
2021-12-16 23:43:14 +01:00
|
|
|
const { processStringSync } = require("@budibase/string-templates")
|
2021-12-16 17:58:15 +01:00
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
const VARIABLE_TTL_SECONDS = 3600
|
2021-12-16 17:58:15 +01:00
|
|
|
let client
|
|
|
|
|
2021-12-21 19:48:54 +01:00
|
|
|
const IS_TRIPLE_BRACE = new RegExp(/^{{3}.*}{3}$/)
|
|
|
|
const IS_HANDLEBARS = new RegExp(/^{{2}.*}{2}$/)
|
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
async function getClient() {
|
2021-12-16 20:33:47 +01:00
|
|
|
if (!client) {
|
|
|
|
client = await new redis.Client(redis.utils.Databases.QUERY_VARS).init()
|
|
|
|
}
|
2021-12-16 23:43:14 +01:00
|
|
|
return client
|
2021-12-16 17:58:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
process.on("exit", async () => {
|
|
|
|
if (client) await client.finish()
|
|
|
|
})
|
2021-12-15 12:25:52 +01:00
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
exports.threadSetup = () => {
|
2021-12-15 13:26:29 +01:00
|
|
|
// don't run this if not threading
|
|
|
|
if (env.isTest() || env.DISABLE_THREADING) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-15 12:25:52 +01:00
|
|
|
// when thread starts, make sure it is recorded
|
|
|
|
env.setInThread()
|
|
|
|
init(CouchDB)
|
|
|
|
}
|
2021-12-16 17:58:15 +01:00
|
|
|
|
|
|
|
function makeVariableKey(queryId, variable) {
|
|
|
|
return `${queryId}${SEPARATOR}${variable}`
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.checkCacheForDynamicVariable = async (queryId, variable) => {
|
2021-12-16 23:43:14 +01:00
|
|
|
const cache = await getClient()
|
|
|
|
return cache.get(makeVariableKey(queryId, variable))
|
|
|
|
}
|
|
|
|
|
2021-12-17 18:56:28 +01:00
|
|
|
exports.invalidateDynamicVariables = async cachedVars => {
|
|
|
|
let promises = []
|
|
|
|
for (let variable of cachedVars) {
|
|
|
|
promises.push(
|
|
|
|
client.delete(makeVariableKey(variable.queryId, variable.name))
|
|
|
|
)
|
|
|
|
}
|
|
|
|
await Promise.all(promises)
|
|
|
|
}
|
|
|
|
|
2021-12-16 23:43:14 +01:00
|
|
|
exports.storeDynamicVariable = async (queryId, variable, value) => {
|
|
|
|
const cache = await getClient()
|
|
|
|
await cache.store(
|
|
|
|
makeVariableKey(queryId, variable),
|
|
|
|
value,
|
|
|
|
VARIABLE_TTL_SECONDS
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.formatResponse = resp => {
|
|
|
|
if (typeof resp === "string") {
|
|
|
|
try {
|
|
|
|
resp = JSON.parse(resp)
|
|
|
|
} catch (err) {
|
|
|
|
resp = { response: resp }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return resp
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.hasExtraData = response => {
|
|
|
|
return (
|
|
|
|
typeof response === "object" &&
|
|
|
|
!Array.isArray(response) &&
|
|
|
|
response.data != null &&
|
|
|
|
response.info != null
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.enrichQueryFields = (fields, parameters = {}) => {
|
|
|
|
const enrichedQuery = {}
|
|
|
|
|
|
|
|
// enrich the fields with dynamic parameters
|
|
|
|
for (let key of Object.keys(fields)) {
|
|
|
|
if (fields[key] == null) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (typeof fields[key] === "object") {
|
|
|
|
// enrich nested fields object
|
|
|
|
enrichedQuery[key] = this.enrichQueryFields(fields[key], parameters)
|
|
|
|
} else if (typeof fields[key] === "string") {
|
|
|
|
// enrich string value as normal
|
2021-12-21 19:48:54 +01:00
|
|
|
let value = fields[key]
|
|
|
|
// add triple brace to avoid escaping e.g. '=' in cookie header
|
|
|
|
if (IS_HANDLEBARS.test(value) && !IS_TRIPLE_BRACE.test(value)) {
|
|
|
|
value = `{${value}}`
|
|
|
|
}
|
|
|
|
enrichedQuery[key] = processStringSync(value, parameters, {
|
2021-12-16 23:43:14 +01:00
|
|
|
noHelpers: true,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
enrichedQuery[key] = fields[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
enrichedQuery.json ||
|
|
|
|
enrichedQuery.customData ||
|
|
|
|
enrichedQuery.requestBody
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
enrichedQuery.json = JSON.parse(
|
|
|
|
enrichedQuery.json ||
|
|
|
|
enrichedQuery.customData ||
|
|
|
|
enrichedQuery.requestBody
|
|
|
|
)
|
|
|
|
} catch (err) {
|
|
|
|
// no json found, ignore
|
|
|
|
}
|
|
|
|
delete enrichedQuery.customData
|
|
|
|
}
|
|
|
|
|
|
|
|
return enrichedQuery
|
2021-12-16 17:58:15 +01:00
|
|
|
}
|