2020-10-28 21:35:06 +01:00
|
|
|
const env = require("../environment")
|
2020-11-03 14:45:49 +01:00
|
|
|
const { DocumentTypes, SEPARATOR } = require("../db/utils")
|
2020-12-01 14:39:34 +01:00
|
|
|
const fs = require("fs")
|
2020-12-09 11:52:18 +01:00
|
|
|
const { cloneDeep } = require("lodash/fp")
|
2020-11-03 14:45:49 +01:00
|
|
|
|
|
|
|
const APP_PREFIX = DocumentTypes.APP + SEPARATOR
|
2020-10-28 21:35:06 +01:00
|
|
|
|
2020-12-09 11:52:18 +01:00
|
|
|
/**
|
|
|
|
* A map of how we convert various properties in rows to each other based on the row type.
|
|
|
|
*/
|
|
|
|
const TYPE_TRANSFORM_MAP = {
|
|
|
|
link: {
|
|
|
|
"": [],
|
|
|
|
[null]: [],
|
|
|
|
[undefined]: undefined,
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
"": "",
|
|
|
|
[null]: "",
|
|
|
|
[undefined]: undefined,
|
|
|
|
},
|
|
|
|
string: {
|
|
|
|
"": "",
|
|
|
|
[null]: "",
|
|
|
|
[undefined]: undefined,
|
|
|
|
},
|
|
|
|
longform: {
|
|
|
|
"": "",
|
|
|
|
[null]: "",
|
|
|
|
[undefined]: undefined,
|
|
|
|
},
|
|
|
|
number: {
|
|
|
|
"": null,
|
|
|
|
[null]: null,
|
|
|
|
[undefined]: undefined,
|
|
|
|
parse: n => parseFloat(n),
|
|
|
|
},
|
|
|
|
datetime: {
|
|
|
|
"": null,
|
|
|
|
[undefined]: undefined,
|
|
|
|
[null]: null,
|
|
|
|
},
|
|
|
|
attachment: {
|
|
|
|
"": [],
|
|
|
|
[null]: [],
|
|
|
|
[undefined]: undefined,
|
|
|
|
},
|
|
|
|
boolean: {
|
|
|
|
"": null,
|
|
|
|
[null]: null,
|
|
|
|
[undefined]: undefined,
|
|
|
|
true: true,
|
|
|
|
false: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-11-09 15:38:29 +01:00
|
|
|
function confirmAppId(possibleAppId) {
|
|
|
|
return possibleAppId && possibleAppId.startsWith(APP_PREFIX)
|
|
|
|
? possibleAppId
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
|
2020-10-19 16:33:26 +02:00
|
|
|
exports.wait = ms => new Promise(resolve => setTimeout(resolve, ms))
|
2020-10-26 18:49:33 +01:00
|
|
|
|
|
|
|
exports.isDev = () => {
|
|
|
|
return (
|
2020-10-29 11:45:02 +01:00
|
|
|
!env.CLOUD &&
|
2020-10-28 21:35:06 +01:00
|
|
|
env.NODE_ENV !== "production" &&
|
|
|
|
env.NODE_ENV !== "jest" &&
|
|
|
|
env.NODE_ENV !== "cypress"
|
2020-10-26 18:49:33 +01:00
|
|
|
)
|
|
|
|
}
|
2020-11-02 16:46:08 +01:00
|
|
|
|
2020-11-02 21:14:10 +01:00
|
|
|
/**
|
|
|
|
* Given a request tries to find the appId, which can be located in various places
|
|
|
|
* @param {object} ctx The main request body to look through.
|
|
|
|
* @returns {string|undefined} If an appId was found it will be returned.
|
|
|
|
*/
|
2020-11-02 16:46:08 +01:00
|
|
|
exports.getAppId = ctx => {
|
2020-11-09 15:38:29 +01:00
|
|
|
let appId = confirmAppId(ctx.headers["x-budibase-app-id"])
|
2020-11-03 14:45:49 +01:00
|
|
|
if (!appId) {
|
2020-11-09 15:38:29 +01:00
|
|
|
appId = confirmAppId(env.CLOUD ? ctx.subdomains[1] : ctx.params.appId)
|
2020-11-03 14:45:49 +01:00
|
|
|
}
|
2020-11-02 21:14:10 +01:00
|
|
|
// look in body if can't find it in subdomain
|
|
|
|
if (!appId && ctx.request.body && ctx.request.body.appId) {
|
2020-11-09 15:38:29 +01:00
|
|
|
appId = confirmAppId(ctx.request.body.appId)
|
2020-11-02 21:14:10 +01:00
|
|
|
}
|
2020-11-03 14:45:49 +01:00
|
|
|
let appPath =
|
|
|
|
ctx.request.headers.referrer ||
|
|
|
|
ctx.path.split("/").filter(subPath => subPath.startsWith(APP_PREFIX))
|
|
|
|
if (!appId && appPath.length !== 0) {
|
2020-11-09 15:38:29 +01:00
|
|
|
appId = confirmAppId(appPath[0])
|
2020-11-02 21:14:10 +01:00
|
|
|
}
|
|
|
|
return appId
|
|
|
|
}
|
2020-11-02 16:46:08 +01:00
|
|
|
|
2020-11-02 21:14:10 +01:00
|
|
|
/**
|
|
|
|
* Get the name of the cookie which is to be updated/retrieved
|
2020-11-03 14:45:49 +01:00
|
|
|
* @param {string|undefined|null} name OPTIONAL can specify the specific app if previewing etc
|
2020-11-02 21:14:10 +01:00
|
|
|
* @returns {string} The name of the token trying to find
|
|
|
|
*/
|
2020-11-03 14:45:49 +01:00
|
|
|
exports.getCookieName = (name = "builder") => {
|
2020-11-02 23:46:31 +01:00
|
|
|
let environment = env.CLOUD ? "cloud" : "local"
|
2020-11-03 14:45:49 +01:00
|
|
|
return `budibase:${name}:${environment}`
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a cookie for the request, has a hardcoded expiry.
|
|
|
|
* @param {object} ctx The request which is to be manipulated.
|
|
|
|
* @param {string} name The name of the cookie to set.
|
|
|
|
* @param {string|object} value The value of cookie which will be set.
|
|
|
|
*/
|
|
|
|
exports.setCookie = (ctx, name, value) => {
|
|
|
|
const expires = new Date()
|
|
|
|
expires.setDate(expires.getDate() + 1)
|
|
|
|
|
|
|
|
ctx.cookies.set(exports.getCookieName(name), value, {
|
|
|
|
expires,
|
|
|
|
path: "/",
|
|
|
|
httpOnly: false,
|
|
|
|
overwrite: true,
|
|
|
|
})
|
2020-11-02 16:46:08 +01:00
|
|
|
}
|
2020-11-19 21:16:37 +01:00
|
|
|
|
|
|
|
exports.isClient = ctx => {
|
|
|
|
return ctx.headers["x-budibase-type"] === "client"
|
|
|
|
}
|
2020-12-01 14:39:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively walk a directory tree and execute a callback on all files.
|
|
|
|
* @param {String} dirPath - Directory to traverse
|
|
|
|
* @param {Function} callback - callback to execute on files
|
|
|
|
*/
|
|
|
|
exports.walkDir = (dirPath, callback) => {
|
|
|
|
for (let filename of fs.readdirSync(dirPath)) {
|
|
|
|
const filePath = `${dirPath}/${filename}`
|
|
|
|
const stat = fs.lstatSync(filePath)
|
|
|
|
|
|
|
|
if (stat.isFile()) {
|
|
|
|
callback(filePath)
|
|
|
|
} else {
|
|
|
|
exports.walkDir(filePath, callback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-12-09 11:52:18 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This will coerce the values in a row to the correct types based on the type transform map and the
|
|
|
|
* table schema.
|
|
|
|
* @param {object} row The row which is to be coerced to correct values based on schema, this input
|
|
|
|
* row will not be updated.
|
|
|
|
* @param {object} table The table that has been retrieved from DB, this must contain the expected
|
|
|
|
* schema for the rows.
|
|
|
|
* @returns {object} The updated row will be returned with all values coerced.
|
|
|
|
*/
|
|
|
|
exports.coerceRowValues = (row, table) => {
|
|
|
|
const clonedRow = cloneDeep(row)
|
|
|
|
for (let [key, value] of Object.entries(clonedRow)) {
|
|
|
|
const field = table.schema[key]
|
|
|
|
if (!field) continue
|
|
|
|
|
|
|
|
// eslint-disable-next-line no-prototype-builtins
|
|
|
|
if (TYPE_TRANSFORM_MAP[field.type].hasOwnProperty(value)) {
|
|
|
|
clonedRow[key] = TYPE_TRANSFORM_MAP[field.type][value]
|
|
|
|
} else if (TYPE_TRANSFORM_MAP[field.type].parse) {
|
|
|
|
clonedRow[key] = TYPE_TRANSFORM_MAP[field.type].parse(value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return clonedRow
|
|
|
|
}
|
2020-12-14 16:56:33 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the correct link to the logo URL depending on if running in Cloud or if running in self hosting.
|
|
|
|
* @returns {string} A URL which links to the correct default logo for new apps.
|
|
|
|
*/
|
|
|
|
exports.getLogoUrl = () => {
|
|
|
|
const BB_LOGO_URL =
|
|
|
|
"https://d33wubrfki0l68.cloudfront.net/aac32159d7207b5085e74a7ef67afbb7027786c5/2b1fd/img/logo/bb-emblem.svg"
|
|
|
|
if (env.SELF_HOSTED) {
|
|
|
|
return env.LOGO_URL || BB_LOGO_URL
|
|
|
|
}
|
|
|
|
return BB_LOGO_URL
|
|
|
|
}
|