Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
Improve dev server (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
TymanWasTaken authored May 1, 2022
1 parent d302733 commit 16cc156
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
"rules": {
"indent": [
"error",
4
4,
{
"SwitchCase": 1
}
],
"linebreak-style": [
"error",
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@
"@types/react-native": "^0.67.4",
"@typescript-eslint/eslint-plugin": "^5.19.0",
"@typescript-eslint/parser": "^5.19.0",
"chalk": "^5.0.1",
"esbuild": "^0.14.36",
"eslint": "^8.13.0",
"eslint-plugin-react": "^7.29.4",
"rollup": "^2.70.2",
"rollup-plugin-esbuild": "^4.9.1",
"rollup-plugin-hermes": "^0.11.1",
"typescript": "^4.6.3"
"typescript": "^4.6.3",
"ws": "^8.5.0"
},
"dependencies": {
"react-devtools-core": "^4.24.4"
Expand Down
22 changes: 22 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

109 changes: 102 additions & 7 deletions serve.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,40 @@ import { createServer } from "http";
import { readFile } from "fs";
import { spawn } from "child_process";
import { platform } from "process";
import readline from "readline";
import { WebSocketServer } from "ws";
import chalk from "chalk";

// http-server exists but it is so bloated 😩

let connected = false;
const logUtils = {
incoming: (message) => {
logUtils.info(chalk.greenBright("<-- ") + message);
},
info: (message) => {
logUtils.clearLine();
console.info(message);
if (connected) process.stdout.write(chalk.cyanBright("--> "));
},
success: (message) => {
logUtils.clearLine();
console.info(chalk.greenBright(message));
},
error: (message) => {
logUtils.clearLine();
console.error(chalk.redBright(message));
},
clearLine: () => {
process.stdout.clearLine();
process.stdout.cursorTo(0);
}
};

const whitelist = ["/Aliucord.js", "/Aliucord.js.map", "/Aliucord.js.bundle"];

createServer((req, res) => {
console.info("-> Received Request for", req.url);
const server = createServer((req, res) => {
logUtils.incoming("Received Request for " + req.url);
if (!whitelist.includes(req.url)) res.writeHead(404).end();
else {
readFile(`dist${req.url}`, { encoding: "utf-8" }, (err, data) => {
Expand All @@ -23,9 +50,77 @@ createServer((req, res) => {
});
}
})
.once("listening", () => console.info("Listening on Port 3000"))
.on("error", console.error)
.listen(3000);
.once("listening", () => logUtils.success("Listening on Port 3000"))
.on("error", (e) => logUtils.error(e.stack));

const wss = new WebSocketServer({ server });

let pnpmCommand;

wss.on("connection", async (ws) => {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
ws.on("message", data => {
const parsed = JSON.parse(data.toString());
switch (parsed.level) {
case 0:
logUtils.info(`${chalk.bold("T:")} ` + parsed.message);
break;
case 1:
logUtils.info(`${chalk.greenBright("I:")} ` + parsed.message);
break;
case 2:
logUtils.info(`${chalk.yellow("W:")} ` + parsed.message);
break;
case 3:
logUtils.info(`${chalk.redBright("E:")} ` + parsed.message);
break;
}
});
ws.on("close", () => {
connected = false;
rl.close();
logUtils.error("Websocket connection closed, waiting for reconnection");
});
logUtils.incoming("Discord client connected to websocket");

connected = true;
while (connected) {
await new Promise(r => {
rl.question(chalk.cyanBright("--> "), (cmd) => {
if (!connected) return;
else if (["exit", "quit"].includes(cmd)) {
ws.close();
pnpmCommand.kill("SIGINT");
process.exit();
} else if (cmd == "clear") {
console.clear();
process.stdout.write(chalk.cyanBright("--> "));
} else if (/^\s*$/.test(cmd)) {
r();
} else {
ws.send(cmd);
r();
}
});
});
}
});

server.listen(3000);

spawn("adb", ["reverse", "tcp:3000", "tcp:3000"], { stdio: "ignore" }).on("exit", (code) => {
if (code !== 0) logUtils.error(`Port forwarding port 3000 with adb exited with code ${code}, aliucord may not load`);
else logUtils.success("Successfully forwarded port 3000 to phone with adb");
});

pnpmCommand = spawn(platform === "win32" ? "pnpm.cmd" : "pnpm", ["dev"], { stdio: "ignore" }).on("spawn", () => {
logUtils.success("HTTP and websocket server started, waiting for connection to app...");
});

spawn("adb", ["reverse", "tcp:3000", "tcp:3000"], { stdio: "inherit" });
spawn(platform === "win32" ? "pnpm.cmd" : "pnpm", ["dev"], { stdio: "inherit" });
process.on("SIGINT", () => {
pnpmCommand.kill("SIGKILL");
process.exit();
});
23 changes: 16 additions & 7 deletions src/utils/debug/DebugWS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import { Logger } from "../Logger";
import { makeAsyncEval } from "../misc";
import { before } from "../patcher";

let ws: WebSocket;
let ws: WebSocket | null;
let patched: boolean;
export function startDebugWs() {
if (ws) return;
ws = new WebSocket("ws://localhost:9090");
ws = new WebSocket("ws://localhost:3000");

const logger = new Logger("DebugWS");
logger.info("Connecting to debug ws");
Expand All @@ -24,10 +25,18 @@ export function startDebugWs() {
logger.error(e as Error | string);
}
});

before(globalThis, "nativeLoggingHook", (_, message: string, level: number) => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ level, message }));
}
ws.addEventListener("close", () => {
logger.info("Disconnected from debug websocket, reconnecting in 3 seconds");
ws = null;
setTimeout(startDebugWs, 3000);
});

if (!patched) {
before(globalThis, "nativeLoggingHook", (_, message: string, level: number) => {
if (ws?.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify({ level, message }));
}
});
patched = true;
}
}
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
"module": "ESNext",
"declaration": true,
"outDir": "build",
"lib": ["ESNext"]
"lib": [
"ESNext",
"DOM"
]
},
"include": [
"src/**/*"
Expand Down

0 comments on commit 16cc156

Please sign in to comment.