-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
113 lines (97 loc) · 3.53 KB
/
Copy pathserver.js
File metadata and controls
113 lines (97 loc) · 3.53 KB
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
const http = require("http");
const fs = require("fs/promises");
const path = require("path");
const PORT = Number(process.env.PORT || 4173);
const HOST = process.env.HOST || "127.0.0.1";
const ROOT = __dirname;
const PUBLIC_DIR = path.join(ROOT, "public");
const LAYOUT_FILE = "uiAccessoriesLayout.json";
const LAYOUT_PATH = path.join(ROOT, LAYOUT_FILE);
const MIME_TYPES = {
".html": "text/html; charset=utf-8",
".css": "text/css; charset=utf-8",
".js": "application/javascript; charset=utf-8",
".json": "application/json; charset=utf-8",
".svg": "image/svg+xml"
};
function send(res, status, body, contentType = "text/plain; charset=utf-8") {
res.writeHead(status, { "Content-Type": contentType });
res.end(body);
}
async function readBody(req) {
const chunks = [];
for await (const chunk of req) {
chunks.push(chunk);
}
return Buffer.concat(chunks).toString("utf8");
}
async function serveStatic(req, res) {
const url = new URL(req.url, `http://${req.headers.host}`);
const pathname = url.pathname === "/" ? "/index.html" : url.pathname;
const decodedPath = decodeURIComponent(pathname);
const filePath = path.normalize(path.join(PUBLIC_DIR, decodedPath));
if (!filePath.startsWith(PUBLIC_DIR)) {
send(res, 403, "Forbidden");
return;
}
try {
const file = await fs.readFile(filePath);
send(res, 200, file, MIME_TYPES[path.extname(filePath)] || "application/octet-stream");
} catch (error) {
if (error.code === "ENOENT") {
send(res, 404, "Not found");
return;
}
send(res, 500, "Server error");
}
}
async function handleApi(req, res) {
if (req.url === "/api/layout" && req.method === "GET") {
try {
const layout = await fs.readFile(LAYOUT_PATH, "utf8");
send(res, 200, layout, "application/json; charset=utf-8");
} catch {
send(res, 500, JSON.stringify({ error: `Could not read ${LAYOUT_FILE}` }), "application/json; charset=utf-8");
}
return;
}
if (req.url === "/api/layout" && req.method === "POST") {
try {
const body = await readBody(req);
const parsed = JSON.parse(body);
const users = Object.keys(parsed);
if (users.length === 0 || !users.every((user) => Array.isArray(parsed[user]))) {
send(res, 400, JSON.stringify({ error: "Invalid layout format" }), "application/json; charset=utf-8");
return;
}
const stamp = new Date().toISOString().replace(/[:.]/g, "-");
await fs.copyFile(LAYOUT_PATH, path.join(ROOT, `uiAccessoriesLayout.backup-${stamp}.json`));
await fs.writeFile(LAYOUT_PATH, `${JSON.stringify(parsed, null, 4)}\n`, "utf8");
send(res, 200, JSON.stringify({ ok: true }), "application/json; charset=utf-8");
} catch (error) {
const status = error instanceof SyntaxError ? 400 : 500;
send(res, status, JSON.stringify({ error: `Could not save ${LAYOUT_FILE}` }), "application/json; charset=utf-8");
}
return;
}
send(res, 404, JSON.stringify({ error: "Not found" }), "application/json; charset=utf-8");
}
const server = http.createServer((req, res) => {
if (req.url.startsWith("/api/")) {
handleApi(req, res);
return;
}
serveStatic(req, res);
});
server.on("error", (error) => {
if (error.code === "EADDRINUSE") {
console.error(`Port ${PORT} is already in use. Open http://${HOST}:${PORT} or start with another port:`);
console.error("PORT=4174 npm start");
process.exit(1);
}
console.error(error);
process.exit(1);
});
server.listen(PORT, HOST, () => {
console.log(`Homebridge layout editor: http://${HOST}:${PORT}`);
});