const fs = require("fs"); const util = require("util"); const vm = require("node:vm"); const path = require("path"); require('dotenv').config() const express = require("express"); const session = require("express-session"); const fileUpload = require("express-fileupload"); const SQLiteStore = require("better-sqlite3-session-store")(session); const { Eta } = require("eta"); const { match } = require("path-to-regexp"); const bcrypt = require("bcrypt"); const cheerio = require("cheerio"); const webPush = require("web-push"); const app = express(); const _expressWs = require("express-ws")(app); const bodyParser = require("body-parser"); const model = require("./db"); const PORT = 3000; const db = model.db; const wsRouter = express.Router() let viewpath = path.join(__dirname, "views"); let eta = new Eta({ views: viewpath, cache: false, autoEscape: true }); let routes = { GET: [], POST: [], PUT: [], DELETE: [] }; const wsRoutes = {} app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); app.use(express.static("public")); app.use(fileUpload()); app.use("/", wsRouter); app.use( session({ store: new SQLiteStore({ client: db, expired: { clear: true } }), secret: "your secret key", resave: false, saveUninitialized: true, cookie: { secure: false }, }), ); const vapidPublicKey = process.env.VAPID_PUBLIC_KEY const vapidPrivateKey = process.env.VAPID_PRIVATE_KEY // Configure web-push with your VAPID details webPush.setVapidDetails( "mailto:signups@sheepmail.net", // a mailto URL or URL vapidPublicKey, vapidPrivateKey, ); function bootstrapContext(db, structureId, routeId, initContext) { const allDbInstances = {}; function getDb(alias) { return allDbInstances[alias]; } // todo wrap template in data-bliss-edit-template thing since ws can't take us to the editor on a component basis?? maybe... // for now just designing with component approach const eta = model.getTemplater(structureId); const libs = { eta, db: getDb, push: webPush }; let dbs = model.getDbsForStructure(db, structureId); let context = vm.createContext({ ...initContext, require: function (str) { return libs[str]; }, module: { exports: null }, console: { log: function (...content) { const s = content.reduce((acc, curr) => { console.log(curr); return ( acc + (acc ? " " : "") + `${util.inspect(curr, { showHidden: false, depth: null, // `null` lets you see the full depth of the object colors: false, // Setting this to true uses ANSI color codes compact: false, })}` ); }, ""); model.createLog(db, structureId, routeId, s); }, }, vapidPublicKey: vapidPublicKey }); for (let appDb of dbs) { let dbInstance = model.getDbInstance(appDb.id); context.sql = dbInstance; vm.runInContext(appDb.library, context); allDbInstances[appDb.alias] = { library: context.module.exports, sql: dbInstance, }; context.module.exports = null; } return context; } function routeWithPrefix(route) { const prefix = route.route_prefix; return prefix ? path.join(prefix, route.path) : route.path; } function bootstrapWebsocketHandler(route) { // todo only expose app when running the handler, should not be available to the handler itself // need to move to runscript or whatever...or remove app because we can just call it using the handler? try { const context = bootstrapContext(db, route.structure_id, route.id, { app }); let handler = vm.runInContext(`${route.handler}\n\nhandler;`, context); if (!wsRoutes[routeWithPrefix(route)]) { // initialize ws router. future updates will only require updating // the wsRoutes dict, not create a whole new route wsRouter.ws(routeWithPrefix(route), (ws, req) => { ws.render = (template, context) => { context = context || {}; context.route = function (url) { return __urlPrefix ? path.join(__urlPrefix, url) : url; }; const eta = model.getTemplater(route.structure_id); const structure = model.getStructure(db, route.structure_id); ws.send( bootstrapTemplateWithHTMXetc( eta.render(template, context), `/workshop/${route.structure_id}/route/${route.id}`, `/workshop/${route.structure_id}/clone_modal/`, route.verb == "GET" ? embedHTML(req.originalUrl) : null, structure.head_injection, htmxRequest = true, ), ); }; return wsRoutes[routeWithPrefix(route)](ws, req) }); } wsRoutes[routeWithPrefix(route)] = (ws, req) => { try { return handler(ws, req) } catch (e) { const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`; model.createLog(db, route.structure_id, route.id, error, true); throw e; } } model.updateRoute(db, { ...route, error: null }); } catch (e) { const error = e.stack ? `${e}\n\n${e.stack}` : `${e}`; model.createLog(db, route.structure_id, route.id, error, true); model.updateRoute(db, { ...route, error: e.stack }); } } function buildRoutes() { let newRoutes = { GET: [], POST: [], PUT: [], DELETE: [], }; for (let route of model.getAllRoutes(db)) { const p = routeWithPrefix(route); if (route.verb == "WS") { bootstrapWebsocketHandler(route); } else { newRoutes[route.verb].push({ matcher: match(p, { decode: decodeURIComponent }), id: route.id, path: route.path, }); } } routes = newRoutes; } model.applyMigrations(); buildRoutes(); // __ __ _______ _______ ______ // | | | || || || _ | // | | | || _____|| ___|| | || // | |_| || |_____ | |___ | |_||_ // | ||_____ || ___|| __ | // | | _____| || |___ | | | | // |_______||_______||_______||___| |_| app.post("/register", async (req, res) => { try { const { username, password } = req.body; const hashedPassword = await bcrypt.hash(password, 10); // 10 is the saltRounds userId = model.createUser(db, username, hashedPassword); req.session.userId = userId; res.redirect("/workshop"); } catch (e) { return res.send(eta.render("auth/register", { error: e })); } }); app.post("/login", async (req, res) => { const { username, password } = req.body; const user = model.getUser(db, username); if (user && (await bcrypt.compare(password, user.password))) { req.session.userId = user.id; return res.redirect("/"); } return res.send( eta.render("auth/login", { error: "are you sure you entered that right?", }), ); }); app.get("/register", async (req, res) => { if (req.session.userId) { return res.redirect("/"); } return res.send(eta.render("auth/register", { error: null })); }); app.get("/login", async (req, res) => { if (req.session.userId) { return res.redirect("/"); } return res.send(eta.render("auth/login", { error: null })); }); app.all("/logout", async (req, res) => { return req.session.destroy(() => { res.redirect("/"); }); }); // _ _ _______ ______ ___ _ _______ __ __ _______ _______ // | | _ | || || _ | | | | || || | | || || | // | || || || _ || | || | |_| || _____|| |_| || _ || _ | // | || | | || |_||_ | _|| |_____ | || | | || |_| | // | || |_| || __ || |_ |_____ || || |_| || ___| // | _ || || | | || _ | _____| || _ || || | // |__| |__||_______||___| |_||___| |_||_______||__| |__||_______||___| // function bootstrapTemplateWithHTMXetc( htmlString, blissRoute, blissClone, blissCopy, headInjection, htmxRequest = false, ) { if ( htmlString.toLowerCase().startsWith("") || htmlString.toLowerCase().startsWith(""); head = $("head"); } head.attr("id", "head"); head.append(` ${headInjection || ""} `); if (blissRoute) { $("body").attr("data-bliss-route", blissRoute); $("body").attr("data-bliss-clone", blissClone); if (blissCopy) $("body").attr("data-bliss-copy", blissCopy); } htmlString = $.html(); } else if (htmxRequest && blissRoute) { const $ = cheerio.load(htmlString, null, false); const targets = []; // when we return an oob thing from htmx, we'll lose all the attrs when it swaps // so this adds those attrs to the children of the swap if possible for (let child of $.root().children()) { if (child.attribs && child.attribs["hx-swap-oob"]) { for (let childchild of child.children) { if (childchild.attribs) { targets.push(childchild); } } } else { targets.push(child); } } // Directly update attributes without wrapping in Cheerio for (let target of targets) { target.attribs["data-bliss-route"] = blissRoute; target.attribs["data-bliss-clone"] = blissClone; if (blissCopy) target.attribs["data-bliss-copy"] = blissCopy; } htmlString = $.html(); if (htmxRequest) { htmlString += `
${headInjection}`; } } return htmlString; } app.post("/workshop", (req, res) => { let structId = model.createStructure(db, req.body.name); return res.redirect("/workshop/" + structId); }); app.get("/workshop", (req, res) => { return res.send( bootstrapTemplateWithHTMXetc( eta.render("workshop/index", { structures: model.getStructures(db), }), ), ); }); function smartRedirect(req, res, redirectUrl) { if (req.headers["hx-request"]) { res.set("HX-Redirect", redirectUrl); res.send(); } else { res.redirect(redirectUrl); } } function sidebarStuff(db, structId) { return { structure: model.getStructure(db, structId), routes: model.getRoutes(db, structId), templates: model.getTemplates(db, structId), dbs: model.getDbsForStructure(db, structId), }; } app.get("/workshop/:structure_id", (req, res) => { return res.send( bootstrapTemplateWithHTMXetc( eta.render("workshop/editor", sidebarStuff(db, req.params.structure_id)), ), ); }); app.post("/workshop/:structure_id/clone", (req, res) => { let routePrefix = req.body.route_prefix; routePrefix = routePrefix[0] == "/" ? routePrefix : "/" + routePrefix; let newStructureId; try { newStructureId = model.cloneStructure( req.params.structure_id, req.body.name, 1, // req.session.userId, routePrefix, req.body.clone_dbs, ); buildRoutes(); } catch (e) { return res.send(e.stack); } return smartRedirect(req, res, `/workshop/${newStructureId}`); }); app.post("/workshop/:structure_id/db", (req, res) => { const dbId = model.createDb(db, req.params.structure_id, req.body.name); const redirectUrl = `/workshop/${req.params.structure_id}/db/${dbId}`; return smartRedirect(req, res, redirectUrl); }); app.post("/workshop/:structure_id/db/attach", (req, res) => { model.attachDb(db, req.params.structure_id, req.body.db_id, req.body.alias); const redirectUrl = `/workshop/${req.params.structure_id}/db/${req.body.db_id}`; return smartRedirect(req, res, redirectUrl); }); app.get("/workshop/:structure_id/db/:db_id", (req, res) => { const structdb = model.getDbForStructure( db, req.params.structure_id, req.params.db_id, ); if (!structdb) { return res.send("uh oh"); } return res.send( bootstrapTemplateWithHTMXetc( eta.render("workshop/db_garden", { db: structdb, ...sidebarStuff(db, req.params.structure_id), }), ), ); }); app.post("/workshop/:structure_id/route", (req, res) => { let p = req.body.path; let dummyHandler = "// put your handler code here\n\nfunction handler(req, res) {\n res.send('henlo world') \n}"; if (req.body.verb == "WS") { dummyHandler = `// put your websocket handler code here\n\nfunction handler(ws, req) {\n ws.on('message', function(msg) {\n ws.send(msg);\n })\n}`; } if (p[0] == "/") { p = p.substring(1); } if (p[p.length - 1] == "/") { p = p.substring(0, p.length - 1); } const route = model.createRoute( db, req.body.verb, p[0] == "/" ? p.substring(1) : "/" + p, req.params.structure_id, dummyHandler, ); // todo optimize by only adding new, don't just rebuild all buildRoutes(); return smartRedirect( req, res, `/workshop/${req.params.structure_id}/route/${route}`, ); }); app.put("/workshop/:structure_id/route/:route_id", (req, res) => { const route = model.getRoute(db, req.params.route_id); model.updateRoute(db, { ...route, ...req.body }); if (route.verb == "WS") { bootstrapWebsocketHandler({ ...route, ...req.body }) } return res.send("good"); }); app.put("/workshop/:structure_id/db/:db_id/library", (req, res) => { try { let dbId = req.params.db_id; let appDb = model.getDb(db, dbId); model.updateDb(db, { ...appDb, library: req.body.library }); appDb = model.getDb(db, dbId); let dbInstance = model.getDbInstance(dbId); let capturedOutput = []; let context = vm.createContext({ module: { exports: null }, sql: dbInstance, console: { log: (...args) => capturedOutput.push(args.join(" ")), }, }); let evaledCode = vm.runInContext(appDb.library, context); let stdout = capturedOutput .map((v) => { return util.inspect(v, { showHidden: false, depth: null, // `null` lets you see the full depth of the object colors: false, // Setting this to true uses ANSI color codes compact: false, }); }) .join("\n"); return res.send(`${stdout}\n\n> ${evaledCode}`.trim()); } catch (e) { return res.send(`${e}\n\n${e.stack}`); } }); app.post("/workshop/:structure_id/db/:db_id/repl", (req, res) => { try { let dbId = req.params.db_id; let appDb = model.getDb(db, dbId); let dbInstance = model.getDbInstance(dbId); let capturedOutput = []; let context = vm.createContext({ module: { exports: null }, sql: dbInstance, console: { log: (...args) => capturedOutput.push( args .map((v) => { return util.inspect(v, { showHidden: false, depth: null, // `null` lets you see the full depth of the object colors: false, // Setting this to true uses ANSI color codes compact: false, }); }) .join(" "), ), }, }); const libraryScript = new vm.Script(appDb.library); libraryScript.runInContext(context); context.library = context.module.exports; const replScript = new vm.Script(req.body.code); let evaledCode = JSON.stringify(replScript.runInContext(context)); let stdout = capturedOutput.join("\n"); return res.send(`${stdout}\n\n> ${evaledCode}`.trim()); } catch (e) { return res.send(`${e}\n\n${e.stack}`); } }); app.post("/workshop/:structure_id/template", (req, res) => { let name = req.body.name; const template = model.createTemplate( db, req.params.structure_id, name, "