2020-05-07 11:53:34 +02:00
|
|
|
const send = require("koa-send")
|
2020-05-06 11:33:30 +02:00
|
|
|
const { resolve, join } = require("path")
|
2020-05-11 16:42:42 +02:00
|
|
|
const {
|
|
|
|
budibaseAppsDir,
|
|
|
|
budibaseTempDir,
|
|
|
|
} = require("../../utilities/budibaseDir")
|
2020-05-18 07:40:29 +02:00
|
|
|
const env = require("../../environment")
|
2020-04-14 16:14:57 +02:00
|
|
|
|
|
|
|
exports.serveBuilder = async function(ctx) {
|
2020-05-14 22:18:36 +02:00
|
|
|
let builderPath = resolve(__dirname, "../../../builder")
|
2020-05-18 07:40:29 +02:00
|
|
|
ctx.cookies.set("builder:token", env.ADMIN_SECRET)
|
2020-05-06 13:17:15 +02:00
|
|
|
await send(ctx, ctx.file, { root: ctx.devPath || builderPath })
|
2020-04-14 16:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.serveApp = async function(ctx) {
|
2020-05-03 12:33:20 +02:00
|
|
|
// default to homedir
|
|
|
|
const appPath = resolve(
|
2020-05-11 16:42:42 +02:00
|
|
|
budibaseAppsDir(),
|
2020-05-03 12:33:20 +02:00
|
|
|
ctx.params.appId,
|
|
|
|
"public",
|
|
|
|
ctx.isAuthenticated ? "main" : "unauthenticated"
|
2020-05-07 11:53:34 +02:00
|
|
|
)
|
2020-06-12 21:42:55 +02:00
|
|
|
// only set the appId cookie for /appId .. we COULD check for valid appIds
|
|
|
|
// but would like to avoid that DB hit
|
|
|
|
if (looksLikeAppId(ctx.params.appId)) {
|
|
|
|
ctx.cookies.set("budibase:appid", ctx.params.appId, {
|
|
|
|
path: "/",
|
|
|
|
httpOnly: false,
|
|
|
|
expires: new Date(2099, 1, 1),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
await send(ctx, ctx.file || "index.html", { root: ctx.devPath || appPath })
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.serveAppAsset = async function(ctx) {
|
|
|
|
// default to homedir
|
|
|
|
const appPath = resolve(
|
|
|
|
budibaseAppsDir(),
|
|
|
|
ctx.appId,
|
|
|
|
"public",
|
|
|
|
ctx.isAuthenticated ? "main" : "unauthenticated"
|
|
|
|
)
|
2020-05-03 12:33:20 +02:00
|
|
|
|
2020-05-06 13:17:15 +02:00
|
|
|
await send(ctx, ctx.file, { root: ctx.devPath || appPath })
|
2020-04-14 16:14:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.serveComponentLibrary = async function(ctx) {
|
2020-05-06 13:17:15 +02:00
|
|
|
// default to homedir
|
|
|
|
let componentLibraryPath = resolve(
|
2020-05-11 16:42:42 +02:00
|
|
|
budibaseAppsDir(),
|
2020-06-12 21:42:55 +02:00
|
|
|
ctx.appId,
|
2020-05-07 11:53:34 +02:00
|
|
|
"node_modules",
|
2020-05-02 16:29:10 +02:00
|
|
|
decodeURI(ctx.query.library),
|
|
|
|
"dist"
|
2020-05-07 11:53:34 +02:00
|
|
|
)
|
2020-05-02 16:29:10 +02:00
|
|
|
|
2020-05-06 13:17:15 +02:00
|
|
|
if (ctx.isDev) {
|
|
|
|
componentLibraryPath = join(
|
2020-05-11 16:42:42 +02:00
|
|
|
budibaseTempDir(),
|
2020-05-06 11:33:30 +02:00
|
|
|
decodeURI(ctx.query.library),
|
|
|
|
"dist"
|
2020-05-07 11:53:34 +02:00
|
|
|
)
|
2020-05-06 11:33:30 +02:00
|
|
|
}
|
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
await send(ctx, "/index.js", { root: componentLibraryPath })
|
2020-05-06 11:33:30 +02:00
|
|
|
}
|
2020-06-12 21:42:55 +02:00
|
|
|
|
|
|
|
const looksLikeAppId = appId => {
|
|
|
|
const allowedChars = "0123456789abcdef".split("")
|
|
|
|
return (
|
|
|
|
appId.length === 32 && !appId.split("").some(c => allowedChars.includes(c))
|
|
|
|
)
|
|
|
|
}
|