Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
23 lines (22 loc) · 1.55 KB
/
Copy pathserver.js
File metadata and controls
23 lines (22 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { createServer } from 'node:http';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const projectRoot = path.dirname(fileURLToPath(import.meta.url));
const root = path.join(projectRoot, 'public');
const types = { '.html': 'text/html; charset=utf-8', '.js': 'text/javascript; charset=utf-8', '.css': 'text/css; charset=utf-8' };
const server = createServer(async (req, res) => {
let pathname;
try { pathname = decodeURIComponent(new URL(req.url, 'http://localhost').pathname); }
catch { res.writeHead(400).end('Bad request'); return; }
const file = pathname === '/' ? 'index.html' : pathname.slice(1);
const resolved = pathname === '/src/analyzer.js' ? path.join(projectRoot, 'src', 'analyzer.js') : path.resolve(root, file);
if (pathname !== '/src/analyzer.js' && !resolved.startsWith(root + path.sep) && resolved !== root) { res.writeHead(403).end('Forbidden'); return; }
try {
const body = await readFile(resolved);
res.writeHead(200, { 'Content-Type': types[path.extname(resolved)] || 'application/octet-stream', 'Cache-Control': 'no-store', 'X-Content-Type-Options': 'nosniff', 'Content-Security-Policy': "default-src 'self'; script-src 'self'; style-src 'self'; img-src 'self' data:; connect-src 'none'; form-action 'none'; object-src 'none'; base-uri 'none'" });
res.end(body);
} catch { res.writeHead(404).end('Not found'); }
});
const port = Number(process.env.PORT || 4173);
server.listen(port, '127.0.0.1', () => console.log(`ClaimCheck running at http://127.0.0.1:${port}`));