@ethicdevs/react-global-state-hooks-debugger
Version:
A small websocket based debugger for use with the react-global-state-hooks library
111 lines (110 loc) • 4.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.cli = void 0;
const http_1 = require("http");
const util_1 = require("util");
const fs_1 = require("fs");
const path_1 = require("path");
const ws_1 = require("ws");
const uuid_1 = require("uuid");
const readFileAsync = (0, util_1.promisify)(fs_1.readFile);
const INDEX_FILE_PATH = (0, path_1.resolve)((0, path_1.join)(__dirname, "..", "public", "index.html"));
const DEBUGGER_UI_SCRIPT_PATH = (0, path_1.resolve)((0, path_1.join)(__dirname, "..", "public", "debugger-ui.js"));
async function cli() {
const wss = new ws_1.WebSocketServer({ port: 8080 }, () => {
console.log("[RGSH-Debugger] WSS server listening on ws://localhost:8080");
});
const clients = {};
wss.on("connection", function connection(ws) {
const client = ws;
const uid = (0, uuid_1.v4)();
client.uid = uid;
clients[client.uid] = client;
console.log(`[${uid}] client connected`);
ws.on("close", function close() {
clients[uid] = null; // so it get's garbage collected
delete clients[uid];
console.log(`[${uid}] client disconnected`);
});
ws.on("message", function message(data) {
try {
if (data.toString() === "attach") {
client.kind = "app";
console.log(`[${uid}] client kind set to: app`);
}
else if (data.toString() === "tail") {
client.kind = "debugger-ui";
console.log(`[${uid}] client kind set to: debugger-ui`);
}
else {
console.log(`[${uid}] client sent us logs, processing...`);
// Broadcast logs to debugger-ui's
const attachedDebuggers = Object.values(clients).filter((sockClient) => {
return !!(sockClient.kind === "debugger-ui" &&
sockClient.uid !== client.uid);
});
console.log(`[${uid}] will emit logs to ${attachedDebuggers.length} attached debuggers...`);
attachedDebuggers.forEach((sockClient) => {
if (sockClient && "send" in sockClient) {
sockClient.send(data.toString());
}
});
console.log(`[${uid}] emited logs to ${attachedDebuggers.length} attached debuggers`);
}
}
catch (err) {
console.warn(`[${uid}] could not send message:`, err);
// drop
}
});
});
const server = (0, http_1.createServer)(async (req, res) => {
const method = req.method;
const path = req.url;
if (method === "GET") {
switch (path) {
case "/": {
const indexFile = await readFileAsync(INDEX_FILE_PATH);
res.writeHead(200, "OK", { "Content-Type": "text/html" });
res.write(`<!--
| @author EthicDevs <ethicdevs.com>
| @name React Global State Hooks Debugger UI
| @license MIT
| @repository https://github.com/ethicdevs/react-global-state-hook-debugger
| @see https://github.com/ethicdevs/react-global-state-hook
-->
`);
res.write(indexFile);
res.end();
break;
}
case "/rgsh-debugger-ui.js": {
const debuggerUiScript = await readFileAsync(DEBUGGER_UI_SCRIPT_PATH);
res.writeHead(200, "OK", { "Content-Type": "text/javascript" });
res.write(`/**
* @author EthicDevs <ethicdevs.com>
* @name React Global State Hooks Debugger UI
* @license MIT
* @repository https://github.com/ethicdevs/react-global-state-hook-debugger
* @see https://github.com/ethicdevs/react-global-state-hook
*/
`);
res.write(debuggerUiScript);
res.end();
break;
}
default: {
res.end("Not found.");
break;
}
}
}
else {
res.end("");
}
});
server.listen(7979, "0.0.0.0", undefined, () => {
console.log("[RGSH-Debugger] HTTP server listening on http://localhost:7979");
});
}
exports.cli = cli;