This repository has been archived by the owner on Mar 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
serve.js
92 lines (77 loc) · 2.25 KB
/
serve.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
const https = require("https");
const fs = require("fs");
const path = require("path");
const promisify = require("util").promisify;
module.exports = serve;
function serve(flags = {}) {
const options = {
key: fs.readFileSync(path.join(__dirname, "support", "self-signed.key")),
cert: fs.readFileSync(path.join(__dirname, "support", "self-signed.pem"))
};
return new Promise(resolve => {
const handler = createHandler(flags);
const server = https.createServer(options, handler);
const port = flags.port || 3131;
server.listen(port, err => {
if (err) reject(err);
if (!flags.quiet) console.info(`running on port ${port}`);
resolve(server);
});
});
}
function createHandler(flags) {
return (req, res) => {
if (req.url === "/") {
res.setHeader("Content-Type", "text/plain; charset=utf-8");
res.write("joof is on da roof!");
res.end();
return;
}
handleUrl(flags, req.url)
.then(
body => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Content-Type", "text/javascript; charset=utf-8");
res.write(body);
res.end();
},
err => {
res.end();
throw err;
}
)
.catch(err => {
res.end();
throw err;
});
};
}
async function handleUrl(flags, url) {
const globalJsPath = path.join(flags.joofDir, "global.js");
const jsPath = path.join(flags.joofDir, url);
const cssPath = jsPath.replace(/\.js$/, ".css");
let jsContent = (await readFile(globalJsPath)) || "";
jsContent += ";\n"; // make sure that global.js ends with a semicolon
jsContent += (await readFile(jsPath)) || "";
const cssContent = await readFile(cssPath);
if (cssContent) {
jsContent += injectStyles(cssContent);
}
return jsContent;
}
const readFilePromise = promisify(fs.readFile);
async function readFile(path) {
try {
const content = await readFilePromise(path);
return content;
} catch (err) {
if (err.code !== "ENOENT") {
throw err;
}
return undefined;
}
}
function injectStyles(css) {
return `
function _joofInject() {var d=document;var e = d.createElement('style');e.innerHTML=\`\n${css}\`;d.body.appendChild(e);};_joofInject()`;
}