-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (64 loc) · 2.15 KB
/
index.js
File metadata and controls
82 lines (64 loc) · 2.15 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
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 aerospaceAction from "./services/aerospace.js";
import missiveAction from "./services/missive.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 url = new URL(req.url, "http://localhost");
const urlSegments = url.pathname.split("/").slice(1);
const [realm, kind, action, userWidgetIndex] = urlSegments;
const params = new URLSearchParams(url.search);
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);
}
if (realm === "aerospace") {
aerospaceAction(res, wss.clients, kind, action, params);
}
if (realm === "missive") {
missiveAction(req, res, wss.clients, kind);
}
res.end();
});
server.listen(config.ports.http, "127.0.0.1");
server.on("listening", () => {
console.info(
`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 });
});