2020-10-29 09:35:06 +13:00
|
|
|
const env = require("../environment")
|
2020-11-04 02:45:49 +13:00
|
|
|
const { DocumentTypes, SEPARATOR } = require("../db/utils")
|
2021-01-15 06:01:31 +13:00
|
|
|
const CouchDB = require("../db")
|
2021-04-02 00:48:38 +13:00
|
|
|
const { OBJ_STORE_DIRECTORY } = require("../constants")
|
2020-11-04 02:45:49 +13:00
|
|
|
|
2021-04-02 00:48:38 +13:00
|
|
|
const BB_CDN = "https://cdn.app.budi.live/assets"
|
2020-11-04 02:45:49 +13:00
|
|
|
const APP_PREFIX = DocumentTypes.APP + SEPARATOR
|
2020-10-29 09:35:06 +13:00
|
|
|
|
2020-11-10 03:38:29 +13:00
|
|
|
function confirmAppId(possibleAppId) {
|
|
|
|
return possibleAppId && possibleAppId.startsWith(APP_PREFIX)
|
|
|
|
? possibleAppId
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
|
2020-10-20 03:33:26 +13:00
|
|
|
exports.wait = ms => new Promise(resolve => setTimeout(resolve, ms))
|
2020-10-27 06:49:33 +13:00
|
|
|
|
2021-03-24 04:37:11 +13:00
|
|
|
exports.isDev = env.isDev
|
2020-11-03 04:46:08 +13:00
|
|
|
|
2020-11-03 09:14:10 +13: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-03 04:46:08 +13:00
|
|
|
exports.getAppId = ctx => {
|
2021-03-25 07:21:23 +13:00
|
|
|
const options = [ctx.headers["x-budibase-app-id"], ctx.params.appId]
|
|
|
|
if (ctx.subdomains) {
|
|
|
|
options.push(ctx.subdomains[1])
|
2020-11-04 02:45:49 +13:00
|
|
|
}
|
2021-03-25 07:21:23 +13:00
|
|
|
let appId
|
|
|
|
for (let option of options) {
|
|
|
|
appId = confirmAppId(option)
|
|
|
|
if (appId) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 09:14:10 +13:00
|
|
|
// look in body if can't find it in subdomain
|
|
|
|
if (!appId && ctx.request.body && ctx.request.body.appId) {
|
2020-11-10 03:38:29 +13:00
|
|
|
appId = confirmAppId(ctx.request.body.appId)
|
2020-11-03 09:14:10 +13:00
|
|
|
}
|
2020-11-04 02:45:49 +13:00
|
|
|
let appPath =
|
|
|
|
ctx.request.headers.referrer ||
|
|
|
|
ctx.path.split("/").filter(subPath => subPath.startsWith(APP_PREFIX))
|
|
|
|
if (!appId && appPath.length !== 0) {
|
2020-11-10 03:38:29 +13:00
|
|
|
appId = confirmAppId(appPath[0])
|
2020-11-03 09:14:10 +13:00
|
|
|
}
|
|
|
|
return appId
|
|
|
|
}
|
2020-11-03 04:46:08 +13:00
|
|
|
|
2020-11-03 09:14:10 +13:00
|
|
|
/**
|
|
|
|
* Get the name of the cookie which is to be updated/retrieved
|
2020-11-04 02:45:49 +13:00
|
|
|
* @param {string|undefined|null} name OPTIONAL can specify the specific app if previewing etc
|
2020-11-03 09:14:10 +13:00
|
|
|
* @returns {string} The name of the token trying to find
|
|
|
|
*/
|
2020-11-04 02:45:49 +13:00
|
|
|
exports.getCookieName = (name = "builder") => {
|
2021-03-25 07:21:23 +13:00
|
|
|
let environment = env.isProd() ? "cloud" : "local"
|
2020-11-04 02:45:49 +13: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.
|
|
|
|
*/
|
2021-01-29 07:30:59 +13:00
|
|
|
exports.setCookie = (ctx, value, name = "builder") => {
|
2020-11-04 02:45:49 +13:00
|
|
|
const expires = new Date()
|
|
|
|
expires.setDate(expires.getDate() + 1)
|
|
|
|
|
2021-01-29 07:30:59 +13:00
|
|
|
const cookieName = exports.getCookieName(name)
|
|
|
|
if (!value) {
|
|
|
|
ctx.cookies.set(cookieName)
|
|
|
|
} else {
|
|
|
|
ctx.cookies.set(cookieName, value, {
|
|
|
|
expires,
|
|
|
|
path: "/",
|
|
|
|
httpOnly: false,
|
|
|
|
overwrite: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility function, simply calls setCookie with an empty string for value
|
|
|
|
*/
|
|
|
|
exports.clearCookie = (ctx, name) => {
|
|
|
|
exports.setCookie(ctx, "", name)
|
2020-11-03 04:46:08 +13:00
|
|
|
}
|
2020-11-20 09:16:37 +13:00
|
|
|
|
2021-04-02 02:11:58 +13:00
|
|
|
/**
|
|
|
|
* Checks if the API call being made (based on the provided ctx object) is from the client. If
|
|
|
|
* the call is not from a client app then it is from the builder.
|
|
|
|
* @param {object} ctx The koa context object to be tested.
|
|
|
|
* @return {boolean} returns true if the call is from the client lib (a built app rather than the builder).
|
|
|
|
*/
|
2020-11-20 09:16:37 +13:00
|
|
|
exports.isClient = ctx => {
|
|
|
|
return ctx.headers["x-budibase-type"] === "client"
|
|
|
|
}
|
2020-12-02 02:39:34 +13:00
|
|
|
|
2021-04-02 02:11:58 +13:00
|
|
|
/**
|
|
|
|
* Lots of different points in the app need to find the full list of apps, this will
|
|
|
|
* enumerate the entire CouchDB cluster and get the list of databases (every app).
|
|
|
|
* NOTE: this operation is fine in self hosting, but cannot be used when hosting many
|
|
|
|
* different users/companies apps as there is no security around it - all apps are returned.
|
|
|
|
* @return {Promise<object[]>} returns the app information document stored in each app database.
|
|
|
|
*/
|
2021-01-15 06:01:31 +13:00
|
|
|
exports.getAllApps = async () => {
|
|
|
|
let allDbs = await CouchDB.allDbs()
|
|
|
|
const appDbNames = allDbs.filter(dbName => dbName.startsWith(APP_PREFIX))
|
|
|
|
const appPromises = appDbNames.map(db => new CouchDB(db).get(db))
|
|
|
|
if (appPromises.length === 0) {
|
|
|
|
return []
|
|
|
|
} else {
|
|
|
|
const response = await Promise.allSettled(appPromises)
|
|
|
|
return response
|
|
|
|
.filter(result => result.status === "fulfilled")
|
|
|
|
.map(({ value }) => value)
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 12:42:50 +13:00
|
|
|
|
2021-04-02 02:11:58 +13:00
|
|
|
/**
|
|
|
|
* Makes sure that a URL has the correct number of slashes, while maintaining the
|
|
|
|
* http(s):// double slashes.
|
|
|
|
* @param {string} url The URL to test and remove any extra double slashes.
|
|
|
|
* @return {string} The updated url.
|
|
|
|
*/
|
2021-03-26 12:42:50 +13:00
|
|
|
exports.checkSlashesInUrl = url => {
|
|
|
|
return url.replace(/(https?:\/\/)|(\/)+/g, "$1$2")
|
|
|
|
}
|
2021-04-02 00:48:38 +13:00
|
|
|
|
2021-04-02 02:11:58 +13:00
|
|
|
/**
|
|
|
|
* Gets the address of the object store, depending on whether self hosted or in cloud.
|
|
|
|
* @return {string} The base URL of the object store (MinIO or S3).
|
|
|
|
*/
|
2021-04-02 00:48:38 +13:00
|
|
|
exports.objectStoreUrl = () => {
|
|
|
|
if (env.SELF_HOSTED) {
|
|
|
|
// can use a relative url for this as all goes through the proxy (this is hosted in minio)
|
|
|
|
return OBJ_STORE_DIRECTORY
|
|
|
|
} else {
|
|
|
|
return BB_CDN
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 02:11:58 +13:00
|
|
|
/**
|
|
|
|
* In production the client library is stored in the object store, however in development
|
|
|
|
* we use the symlinked version produced by lerna, located in node modules. We link to this
|
|
|
|
* via a specific endpoint (under /api/assets/client).
|
|
|
|
* @param {string} appId In production we need the appId to look up the correct bucket, as the
|
|
|
|
* version of the client lib may differ between apps.
|
|
|
|
* @return {string} The URL to be inserted into appPackage response or server rendered
|
|
|
|
* app index file.
|
|
|
|
*/
|
2021-04-02 00:48:38 +13:00
|
|
|
exports.clientLibraryPath = appId => {
|
|
|
|
if (env.isProd()) {
|
|
|
|
return `${exports.objectStoreUrl()}/${appId}/budibase-client.js`
|
|
|
|
} else {
|
2021-04-02 02:11:58 +13:00
|
|
|
return `/api/assets/client`
|
2021-04-02 00:48:38 +13:00
|
|
|
}
|
|
|
|
}
|