|
| 1 | +#!/usr/bin/env npx tsx |
| 2 | +/** |
| 3 | + * Simple HTTP server to serve the host and sandbox html files with appropriate |
| 4 | + * Content Security Policy (CSP) headers. |
| 5 | + */ |
| 6 | + |
| 7 | +import express from "express"; |
| 8 | +import cors from "cors"; |
| 9 | +import { fileURLToPath } from "url"; |
| 10 | +import { dirname, join } from "path"; |
| 11 | + |
| 12 | +const __filename = fileURLToPath(import.meta.url); |
| 13 | +const __dirname = dirname(__filename); |
| 14 | + |
| 15 | +const PORT = parseInt(process.env.PORT || "8080", 10); |
| 16 | +const DIRECTORY = join(__dirname, "dist"); |
| 17 | + |
| 18 | +const app = express(); |
| 19 | + |
| 20 | +// CORS middleware for all routes |
| 21 | +app.use(cors()); |
| 22 | + |
| 23 | +// Custom middleware for sandbox.html and root |
| 24 | +app.use((req, res, next) => { |
| 25 | + if (req.path === "/sandbox.html" || req.path === "/") { |
| 26 | + // Permissive CSP to allow external resources (images, styles, scripts) |
| 27 | + const csp = [ |
| 28 | + "default-src 'self'", |
| 29 | + "img-src * data: blob: 'unsafe-inline'", |
| 30 | + "style-src * blob: data: 'unsafe-inline'", |
| 31 | + "script-src * blob: data: 'unsafe-inline' 'unsafe-eval'", |
| 32 | + "connect-src *", |
| 33 | + "font-src * blob: data:", |
| 34 | + "media-src * blob: data:", |
| 35 | + "frame-src * blob: data:", |
| 36 | + ].join("; "); |
| 37 | + res.setHeader("Content-Security-Policy", csp); |
| 38 | + |
| 39 | + // Disable caching to ensure fresh content on every request |
| 40 | + res.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); |
| 41 | + res.setHeader("Pragma", "no-cache"); |
| 42 | + res.setHeader("Expires", "0"); |
| 43 | + } |
| 44 | + next(); |
| 45 | +}); |
| 46 | + |
| 47 | +// Serve static files from dist directory |
| 48 | +app.use(express.static(DIRECTORY)); |
| 49 | + |
| 50 | +// Redirect root to example-host.html |
| 51 | +app.get("/", (_req, res) => { |
| 52 | + res.redirect("/example-host-react.html"); |
| 53 | +}); |
| 54 | + |
| 55 | +app.listen(PORT, () => { |
| 56 | + console.log(`Server running on: http://localhost:${PORT}`); |
| 57 | + console.log("Press Ctrl+C to stop the server\n"); |
| 58 | +}); |
0 commit comments