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-06-18 17:59:31 +02:00
|
|
|
const setBuilderToken = require("../../utilities/builder/setBuilderToken")
|
|
|
|
const { ANON_LEVEL_ID } = require("../../utilities/accessLevels")
|
2020-06-19 17:59:46 +02:00
|
|
|
const jwt = require("jsonwebtoken")
|
2020-06-29 11:27:38 +02:00
|
|
|
const fetch = require("node-fetch")
|
|
|
|
|
|
|
|
const S3_URL_PREFIX = "https://s3-eu-west-1.amazonaws.com/apps.budibase.com"
|
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-06-19 17:59:46 +02:00
|
|
|
if (ctx.file === "index.html") {
|
|
|
|
setBuilderToken(ctx)
|
|
|
|
}
|
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-06-29 11:27:38 +02:00
|
|
|
const mainOrAuth = ctx.isAuthenticated ? "main" : "unauthenticated";
|
|
|
|
|
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",
|
2020-06-29 11:27:38 +02:00
|
|
|
mainOrAuth
|
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
|
2020-06-19 18:21:24 +02:00
|
|
|
const looksLikeAppId = /^[0-9a-f]{32}$/.test(ctx.params.appId)
|
|
|
|
if (looksLikeAppId && !ctx.isAuthenticated) {
|
2020-06-19 17:59:46 +02:00
|
|
|
const anonUser = {
|
2020-06-18 17:59:31 +02:00
|
|
|
userId: "ANON",
|
|
|
|
accessLevelId: ANON_LEVEL_ID,
|
|
|
|
appId: ctx.params.appId,
|
|
|
|
}
|
2020-06-19 17:59:46 +02:00
|
|
|
const anonToken = jwt.sign(anonUser, ctx.config.jwtSecret)
|
2020-06-18 17:59:31 +02:00
|
|
|
ctx.cookies.set("budibase:token", anonToken, {
|
2020-06-12 21:42:55 +02:00
|
|
|
path: "/",
|
|
|
|
httpOnly: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-29 11:27:38 +02:00
|
|
|
const { file = "index.html" } = ctx
|
|
|
|
|
|
|
|
if (ctx.isCloud) {
|
|
|
|
const requestUrl = `${S3_URL_PREFIX}/${ctx.params.appId}/public/${mainOrAuth}/${file}`
|
|
|
|
console.log('request url:' , requestUrl)
|
|
|
|
const response = await fetch(requestUrl)
|
|
|
|
const body = await response.text()
|
|
|
|
ctx.body = body
|
|
|
|
} else {
|
|
|
|
await send(ctx, file, { root: ctx.devPath || appPath })
|
|
|
|
}
|
2020-06-12 21:42:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.serveAppAsset = async function(ctx) {
|
|
|
|
// default to homedir
|
2020-06-29 11:27:38 +02:00
|
|
|
const mainOrAuth = ctx.isAuthenticated ? "main" : "unauthenticated";
|
|
|
|
|
2020-06-12 21:42:55 +02:00
|
|
|
const appPath = resolve(
|
|
|
|
budibaseAppsDir(),
|
2020-06-18 17:59:31 +02:00
|
|
|
ctx.user.appId,
|
2020-06-12 21:42:55 +02:00
|
|
|
"public",
|
2020-06-29 11:27:38 +02:00
|
|
|
mainOrAuth
|
2020-06-12 21:42:55 +02:00
|
|
|
)
|
2020-05-03 12:33:20 +02:00
|
|
|
|
2020-06-29 11:27:38 +02:00
|
|
|
if (ctx.isCloud) {
|
|
|
|
const requestUrl = `${S3_URL_PREFIX}/${appId}/public/${mainOrAuth}/${ctx.file || "index.html"}`
|
|
|
|
console.log('request url:' , requestUrl)
|
|
|
|
const response = await fetch(requestUrl)
|
|
|
|
const body = await response.text()
|
|
|
|
ctx.body = body
|
|
|
|
} else {
|
|
|
|
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-18 17:59:31 +02:00
|
|
|
ctx.user.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-06-29 11:27:38 +02:00
|
|
|
if (ctx.isCloud) {
|
|
|
|
const appId = ctx.user.appId
|
|
|
|
const requestUrl = encodeURI(`${S3_URL_PREFIX}/${appId}/node_modules/${ctx.query.library}/dist/index.js`)
|
|
|
|
console.log('request url components: ', requestUrl)
|
|
|
|
const response = await fetch(requestUrl)
|
|
|
|
const body = await response.text()
|
|
|
|
ctx.type = 'application/javascript'
|
|
|
|
ctx.body = body;
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-02 16:29:10 +02:00
|
|
|
await send(ctx, "/index.js", { root: componentLibraryPath })
|
2020-05-06 11:33:30 +02:00
|
|
|
}
|