const http = require('http');
const fs = require('fs');
const path = require('path');
function renderFileLink(p, name) {
return `
${name}`;
}
function isDirectory(p) {
try {
return fs.statSync(p).isDirectory();
} catch (e) {
return false;
}
}
const port = parseInt(process.argv[2], 10) || 3042;
const host = process.argv[3] || '127.0.0.1';
const proxy = http.createServer((req, res) => {
let p = path.join('.', req.url);
console.log(p);
if (p.startsWith('prism-assets')) {
p = path.join(__dirname, 'build', path.basename(p));
}
if (p.startsWith('prism-ruby')) {
p = path.join(__dirname, 'src', path.basename(p));
}
if (isDirectory(p)) {
const files = fs.readdirSync(p);
res.write(`
Prism Server
Prism Dev Server
${path.join(process.cwd(), p)}
${files.map(f => renderFileLink(req.url, f)).join("\n")}
`);
res.end();
return;
}
try {
if (p.endsWith('.wasm')) {
res.setHeader("Content-Type", "application/wasm")
}
if (p.endsWith('.js')) {
res.setHeader("Content-Type", "text/javascript")
}
if (p.endsWith('.rb') && req.headers.accept.includes("text/html")) {
res.write(`
`);
res.end();
return;
}
if (p.endsWith('rb')) {
res.setHeader('Content-Type', "application/ruby");
}
res.write(fs.readFileSync(p));
} catch (e) {
res.write(e.toString());
}
res.end();
});
console.log(`Listening on ${host}:${port}`);
proxy.listen(port, host);