Some further work to make sure all use of the budibaseAppsDir is removed in important places.

This commit is contained in:
mike12345567 2021-03-25 18:03:58 +00:00
parent f16fe2d8d2
commit 321d3cbd5d
5 changed files with 79 additions and 63 deletions

View File

@ -3,7 +3,8 @@ const yargs = require("yargs")
const fs = require("fs") const fs = require("fs")
const { join } = require("path") const { join } = require("path")
const CouchDB = require("../src/db") const CouchDB = require("../src/db")
const { budibaseAppsDir } = require("../src/utilities/budibaseDir") // load environment
const env = require("../src/environment")
// Script to export a chosen budibase app into a package // Script to export a chosen budibase app into a package
// Usage: ./scripts/exportAppTemplate.js export --name=Funky --appId=appId // Usage: ./scripts/exportAppTemplate.js export --name=Funky --appId=appId
@ -25,6 +26,9 @@ yargs
}, },
}, },
async args => { async args => {
if (!env.isDev()) {
throw "Only works in dev"
}
const name = args.name, const name = args.name,
appId = args.appId appId = args.appId
console.log("Exporting app..") console.log("Exporting app..")
@ -34,10 +38,11 @@ yargs
) )
return return
} }
const exportPath = join(budibaseAppsDir(), "templates", "app", name, "db") const exportPath = join(process.cwd(), name, "db")
fs.ensureDirSync(exportPath) fs.ensureDirSync(exportPath)
const writeStream = fs.createWriteStream(join(exportPath, "dump.text")) const writeStream = fs.createWriteStream(join(exportPath, "dump.text"))
// perform couch dump // perform couch dump
const instanceDb = new CouchDB(appId) const instanceDb = new CouchDB(appId)
await instanceDb.dump(writeStream, {}) await instanceDb.dump(writeStream, {})
console.log(`Template ${name} exported to ${exportPath}`) console.log(`Template ${name} exported to ${exportPath}`)

View File

@ -6,10 +6,7 @@ const fetch = require("node-fetch")
const uuid = require("uuid") const uuid = require("uuid")
const { prepareUpload } = require("../deploy/utils") const { prepareUpload } = require("../deploy/utils")
const { processString } = require("@budibase/string-templates") const { processString } = require("@budibase/string-templates")
const { const { budibaseTempDir } = require("../../../utilities/budibaseDir")
budibaseAppsDir,
budibaseTempDir,
} = require("../../../utilities/budibaseDir")
const { getDeployedApps } = require("../../../utilities/builder/hosting") const { getDeployedApps } = require("../../../utilities/builder/hosting")
const CouchDB = require("../../../db") const CouchDB = require("../../../db")
const setBuilderToken = require("../../../utilities/builder/setBuilderToken") const setBuilderToken = require("../../../utilities/builder/setBuilderToken")
@ -18,15 +15,36 @@ const env = require("../../../environment")
const { OBJ_STORE_DIRECTORY } = require("../../../constants") const { OBJ_STORE_DIRECTORY } = require("../../../constants")
const fileProcessor = require("../../../utilities/fileSystem/processor") const fileProcessor = require("../../../utilities/fileSystem/processor")
const BB_CDN = "https://cdn.app.budi.live/assets"
function objectStoreUrl() { function objectStoreUrl() {
if (env.SELF_HOSTED) { if (env.SELF_HOSTED) {
// can use a relative url for this as all goes through the proxy (this is hosted in minio) // can use a relative url for this as all goes through the proxy (this is hosted in minio)
return OBJ_STORE_DIRECTORY return OBJ_STORE_DIRECTORY
} else { } else {
return "https://cdn.app.budi.live/assets" return BB_CDN
} }
} }
function internalObjectStoreUrl() {
if (env.SELF_HOSTED) {
return (env.MINIO_URL + OBJ_STORE_DIRECTORY).replace(
/(https?:\/\/)|(\/)+/g,
"$1$2"
)
} else {
return BB_CDN
}
}
async function returnObjectStoreFile(ctx, path) {
const S3_URL = `${internalObjectStoreUrl()}/${path}`
const response = await fetch(S3_URL)
const body = await response.text()
ctx.set("Content-Type", response.headers.get("Content-Type"))
ctx.body = body
}
async function checkForSelfHostedURL(ctx) { async function checkForSelfHostedURL(ctx) {
// the "appId" component of the URL may actually be a specific self hosted URL // the "appId" component of the URL may actually be a specific self hosted URL
let possibleAppUrl = `/${encodeURI(ctx.params.appId).toLowerCase()}` let possibleAppUrl = `/${encodeURI(ctx.params.appId).toLowerCase()}`
@ -102,69 +120,47 @@ exports.serveApp = async function(ctx) {
} }
exports.serveAttachment = async function(ctx) { exports.serveAttachment = async function(ctx) {
const appId = ctx.user.appId await returnObjectStoreFile(
const attachmentsPath = resolve(budibaseAppsDir(), appId, "attachments") ctx,
join(ctx.user.appId, "attachments", ctx.file)
// Serve from object store )
if (env.isProd()) {
const S3_URL = join(objectStoreUrl(), appId, "attachments", ctx.file)
const response = await fetch(S3_URL)
const body = await response.text()
ctx.set("Content-Type", response.headers.get("Content-Type"))
ctx.body = body
return
}
await send(ctx, ctx.file, { root: attachmentsPath })
} }
exports.serveAppAsset = async function(ctx) { exports.serveAppAsset = async function(ctx) {
// default to homedir if (env.isDev() || env.isTest()) {
const appPath = resolve(budibaseAppsDir(), ctx.user.appId, "public") return send(ctx, ctx.file, { root: budibaseTempDir() })
}
await send(ctx, ctx.file, { root: ctx.devPath || appPath }) await returnObjectStoreFile(ctx, join(ctx.user.appId, "public", ctx.file))
} }
exports.serveComponentLibrary = async function(ctx) { exports.serveComponentLibrary = async function(ctx) {
const appId = ctx.query.appId || ctx.appId const appId = ctx.query.appId || ctx.appId
// default to homedir
let componentLibraryPath = resolve(
budibaseAppsDir(),
appId,
"node_modules",
decodeURI(ctx.query.library),
"package",
"dist"
)
if (env.isDev()) { if (env.isDev() || env.isTest()) {
componentLibraryPath = join( const componentLibraryPath = join(
budibaseTempDir(), budibaseTempDir(),
decodeURI(ctx.query.library), decodeURI(ctx.query.library),
"dist" "dist"
) )
} else { return send(ctx, "/awsDeploy.js", { root: componentLibraryPath })
let componentLib = "componentlibrary"
if (ctx.user.version) {
componentLib += `-${ctx.user.version}`
} else {
componentLib += `-${COMP_LIB_BASE_APP_VERSION}`
}
const S3_URL = encodeURI(
join(
objectStoreUrl(appId),
componentLib,
ctx.query.library,
"dist",
"index.js"
)
)
const response = await fetch(S3_URL)
const body = await response.text()
ctx.type = "application/javascript"
ctx.body = body
return
} }
let componentLib = "componentlibrary"
await send(ctx, "/awsDeploy.js", { root: componentLibraryPath }) if (ctx.user.version) {
componentLib += `-${ctx.user.version}`
} else {
componentLib += `-${COMP_LIB_BASE_APP_VERSION}`
}
const S3_URL = encodeURI(
join(
objectStoreUrl(appId),
componentLib,
ctx.query.library,
"dist",
"index.js"
)
)
const response = await fetch(S3_URL)
const body = await response.text()
ctx.type = "application/javascript"
ctx.body = body
} }

View File

@ -2,7 +2,6 @@ const Router = require("@koa/router")
const authenticated = require("../middleware/authenticated") const authenticated = require("../middleware/authenticated")
const compress = require("koa-compress") const compress = require("koa-compress")
const zlib = require("zlib") const zlib = require("zlib")
const { budibaseAppsDir } = require("../utilities/budibaseDir")
const { mainRoutes, authRoutes, staticRoutes } = require("./routes") const { mainRoutes, authRoutes, staticRoutes } = require("./routes")
const pkg = require("../../package.json") const pkg = require("../../package.json")
@ -24,7 +23,6 @@ router
) )
.use(async (ctx, next) => { .use(async (ctx, next) => {
ctx.config = { ctx.config = {
latestPackagesFolder: budibaseAppsDir(),
jwtSecret: env.JWT_SECRET, jwtSecret: env.JWT_SECRET,
useAppRootPath: true, useAppRootPath: true,
} }

View File

@ -8,6 +8,7 @@ const { ObjectStoreBuckets } = require("../../constants")
const { const {
upload, upload,
retrieve, retrieve,
retrieveToTmp,
streamUpload, streamUpload,
deleteFolder, deleteFolder,
downloadTarball, downloadTarball,
@ -38,6 +39,10 @@ exports.init = () => {
if (!fs.existsSync(tempDir)) { if (!fs.existsSync(tempDir)) {
fs.mkdirSync(tempDir) fs.mkdirSync(tempDir)
} }
const clientLibPath = join(budibaseTempDir(), "budibase-client.js")
if (env.isTest() && !fs.existsSync(clientLibPath)) {
fs.copyFileSync(require.resolve("@budibase/client"), clientLibPath)
}
} }
/** /**
@ -224,3 +229,4 @@ exports.cleanup = appIds => {
*/ */
exports.upload = upload exports.upload = upload
exports.retrieve = retrieve exports.retrieve = retrieve
exports.retrieveToTmp = retrieveToTmp

View File

@ -10,6 +10,7 @@ const fs = require("fs")
const { budibaseTempDir } = require("../budibaseDir") const { budibaseTempDir } = require("../budibaseDir")
const env = require("../../environment") const env = require("../../environment")
const { ObjectStoreBuckets } = require("../../constants") const { ObjectStoreBuckets } = require("../../constants")
const uuid = require("uuid/v4")
const streamPipeline = promisify(stream.pipeline) const streamPipeline = promisify(stream.pipeline)
@ -146,11 +147,11 @@ exports.streamUpload = async (bucket, filename, stream) => {
* retrieves the contents of a file from the object store, if it is a known content type it * retrieves the contents of a file from the object store, if it is a known content type it
* will be converted, otherwise it will be returned as a buffer stream. * will be converted, otherwise it will be returned as a buffer stream.
*/ */
exports.retrieve = async (bucket, filename) => { exports.retrieve = async (bucket, filepath) => {
const objectStore = exports.ObjectStore(bucket) const objectStore = exports.ObjectStore(bucket)
const params = { const params = {
Bucket: bucket, Bucket: bucket,
Key: sanitize(filename).replace(/\\/g, "/"), Key: sanitize(filepath).replace(/\\/g, "/"),
} }
const response = await objectStore.getObject(params).promise() const response = await objectStore.getObject(params).promise()
// currently these are all strings // currently these are all strings
@ -161,6 +162,16 @@ exports.retrieve = async (bucket, filename) => {
} }
} }
/**
* Same as retrieval function but puts to a temporary file.
*/
exports.retrieveToTmp = async (bucket, filepath) => {
const data = await exports.retrieve(bucket, filepath)
const outputPath = join(budibaseTempDir(), uuid())
fs.writeFileSync(outputPath, data)
return outputPath
}
exports.deleteFolder = async (bucket, folder) => { exports.deleteFolder = async (bucket, folder) => {
const client = exports.ObjectStore(bucket) const client = exports.ObjectStore(bucket)
const listParams = { const listParams = {