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")
|
2021-01-14 18:01:31 +01:00
|
|
|
const CouchDB = require("../db")
|
2021-04-09 14:49:16 +02:00
|
|
|
const { OBJ_STORE_DIRECTORY, ObjectStoreBuckets } = require("../constants")
|
2020-11-03 14:45:49 +01:00
|
|
|
|
2021-04-01 13:48:38 +02:00
|
|
|
const BB_CDN = "https://cdn.app.budi.live/assets"
|
2020-11-03 14:45:49 +01:00
|
|
|
const APP_PREFIX = DocumentTypes.APP + SEPARATOR
|
2020-10-28 21:35:06 +01:00
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.wait = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
|
2020-10-26 18:49:33 +01:00
|
|
|
|
2021-03-23 16:37:11 +01:00
|
|
|
exports.isDev = env.isDev
|
2020-11-02 16:46:08 +01:00
|
|
|
|
2021-04-01 15:11:58 +02: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-14 18:01:31 +01:00
|
|
|
exports.getAllApps = async () => {
|
|
|
|
let allDbs = await CouchDB.allDbs()
|
2021-05-03 09:31:09 +02:00
|
|
|
const appDbNames = allDbs.filter((dbName) => dbName.startsWith(APP_PREFIX))
|
|
|
|
const appPromises = appDbNames.map((db) => new CouchDB(db).get(db))
|
2021-01-14 18:01:31 +01:00
|
|
|
if (appPromises.length === 0) {
|
|
|
|
return []
|
|
|
|
} else {
|
|
|
|
const response = await Promise.allSettled(appPromises)
|
|
|
|
return response
|
2021-05-03 09:31:09 +02:00
|
|
|
.filter((result) => result.status === "fulfilled")
|
2021-01-14 18:01:31 +01:00
|
|
|
.map(({ value }) => value)
|
|
|
|
}
|
|
|
|
}
|
2021-03-26 00:42:50 +01:00
|
|
|
|
2021-04-01 15:11:58 +02: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-05-03 09:31:09 +02:00
|
|
|
exports.checkSlashesInUrl = (url) => {
|
2021-03-26 00:42:50 +01:00
|
|
|
return url.replace(/(https?:\/\/)|(\/)+/g, "$1$2")
|
|
|
|
}
|
2021-04-01 13:48:38 +02:00
|
|
|
|
2021-04-01 15:11:58 +02: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-01 13:48:38 +02: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-01 15:11:58 +02: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-05-03 09:31:09 +02:00
|
|
|
exports.clientLibraryPath = (appId) => {
|
2021-04-01 13:48:38 +02:00
|
|
|
if (env.isProd()) {
|
|
|
|
return `${exports.objectStoreUrl()}/${appId}/budibase-client.js`
|
|
|
|
} else {
|
2021-04-01 15:11:58 +02:00
|
|
|
return `/api/assets/client`
|
2021-04-01 13:48:38 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-09 14:49:16 +02:00
|
|
|
|
2021-05-03 09:31:09 +02:00
|
|
|
exports.attachmentsRelativeURL = (attachmentKey) => {
|
2021-04-09 14:49:16 +02:00
|
|
|
return exports.checkSlashesInUrl(
|
|
|
|
`/${ObjectStoreBuckets.APPS}/${attachmentKey}`
|
|
|
|
)
|
|
|
|
}
|