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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
import { readFileSync } from "node:fs";
import * as Fs from "node:fs/promises";
import * as Http from "node:http";
import * as Path from "node:path";
import * as Process from "node:process";
const cwd = Process.cwd();
const root = Path.join(cwd, "build/dev/javascript");
const toml = readFileSync(Path.join(cwd, "gleam.toml"), "utf-8");
const name = toml.match(/name *= *"(.+)"/)[1];
let html;
const server = Http.createServer((req, res) => {
res.setHeader(
"Cache-Control",
"no-store, no-cache, must-revalidate, private"
);
res.setHeader("Pragma", "no-cache");
switch (true) {
case req.url === "/": {
res.setHeader("Content-Type", "text/html");
res.statusCode = 200;
res.end(html);
break;
}
case req.url.endsWith(".js"):
case req.url.endsWith(".mjs"): {
Fs.readFile(Path.join(root, req.url), "utf-8")
.then((src) => {
res.setHeader("Content-Type", "application/javascript");
res.statusCode = 200;
res.end(src);
})
.catch((_err) => {
res.statusCode = 404;
res.end();
});
break;
}
case req.url.endsWith(".css"): {
Fs.readFile(Path.join(root, req.url), "utf-8")
.then((src) => {
res.setHeader("Content-Type", "text/css");
res.statusCode = 200;
res.end(src);
})
.catch((_err) => {
res.statusCode = 404;
res.end();
});
break;
}
default: {
Fs.readFile(Path.join(root, req.url), "utf-8")
.then((src) => {
res.setHeader("Content-Type", "text/plain");
res.statusCode = 200;
res.end(src);
})
.catch((_err) => {
res.statusCode = 404;
res.end();
});
}
}
});
export const serve = (
{ host, port, include_styles },
on_start,
on_port_taken
) => {
let is_first_try = true;
html = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Lustre preview server</title>
${
include_styles
? `<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/lustre-labs/ui/priv/styles.css">`
: ""
}
<script type="module">
import { main } from "./${name}/${name}.mjs"
document.addEventListener("DOMContentLoaded", () => {
main();
});
</script>
</head>
<body>
<div data-lustre-app></div>
</body>
</html>`;
server
.on("error", (error) => {
if (error.code === "EADDRINUSE") {
if (is_first_try) {
on_port_taken(port);
is_first_try = false;
}
server.listen(++port, host);
}
})
.listen(port, host, () => {
on_start(port);
});
};
|