-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
69 lines (53 loc) · 1.73 KB
/
index.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
import http from "http";
import { WebSocketServer } from "ws";
import { exec } from "child_process";
import config from "./config.js";
import widgetAction from "./services/widget.js";
import yabaiAction from "./services/yabai.js";
import skhdAction from "./services/skhd.js";
import * as DATA from "./data.js";
process.title = "simple-bar-server";
const wssServer = http.createServer();
wssServer.listen(config.ports.ws, "127.0.0.1");
const wss = new WebSocketServer({ server: wssServer });
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
const [realm, kind, action, userWidgetIndex] = req.url.split("/").slice(1);
if (!realm) {
res.statusCode = 400;
res.end(`Missing realm name (${DATA.REALMS.join(", ")}).`);
return;
}
if (!DATA.REALMS.includes(realm)) {
res.statusCode = 400;
res.end(`Unknown realm "${realm}".`);
return;
}
if (realm === "widget") {
widgetAction(res, wss.clients, kind, action, userWidgetIndex);
}
if (realm === "yabai") {
yabaiAction(res, wss.clients, kind, action);
}
if (realm === "skhd") {
skhdAction(res, wss.clients, kind, action);
}
res.end();
});
server.listen(config.ports.http, "127.0.0.1");
server.on("listening", () => {
console.log(
`simple-bar-server running at http://localhost:${config.ports.http}`
);
exec(`osascript -e 'tell application id "tracesOf.Uebersicht" to refresh'`);
});
wss.on("connection", (ws, req) => {
const url = new URL(req.url, "http://localhost");
const target = url.searchParams.get("target");
const userWidgetIndex = url.searchParams.get("userWidgetIndex");
if (!target) {
ws.close();
return;
}
Object.assign(ws, { target, userWidgetIndex });
});