From 16cc15655594dd2e893257574ef3398bcbd8f013 Mon Sep 17 00:00:00 2001 From: TymanWasTaken <32660892+TymanWasTaken@users.noreply.github.com> Date: Sun, 1 May 2022 09:37:37 -0600 Subject: [PATCH] Improve dev server (#11) --- .eslintrc.json | 5 +- package.json | 4 +- pnpm-lock.yaml | 22 ++++++++ serve.mjs | 109 ++++++++++++++++++++++++++++++++++--- src/utils/debug/DebugWS.ts | 23 +++++--- tsconfig.json | 5 +- 6 files changed, 151 insertions(+), 17 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index abadcc2..2c5b102 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -23,7 +23,10 @@ "rules": { "indent": [ "error", - 4 + 4, + { + "SwitchCase": 1 + } ], "linebreak-style": [ "error", diff --git a/package.json b/package.json index d518397..1ec93ae 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ed8e8a7..48436cf 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,6 +9,7 @@ specifiers: '@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 @@ -17,6 +18,7 @@ specifiers: rollup-plugin-esbuild: ^4.9.1 rollup-plugin-hermes: ^0.11.1 typescript: ^4.6.3 + ws: ^8.5.0 dependencies: react-devtools-core: 4.24.4 @@ -30,6 +32,7 @@ devDependencies: '@types/react-native': 0.67.4 '@typescript-eslint/eslint-plugin': 5.19.0_f34adc8488d2e4f014fe61432d70cbf2 '@typescript-eslint/parser': 5.19.0_eslint@8.13.0+typescript@4.6.3 + chalk: 5.0.1 esbuild: 0.14.36 eslint: 8.13.0 eslint-plugin-react: 7.29.4_eslint@8.13.0 @@ -37,6 +40,7 @@ devDependencies: rollup-plugin-esbuild: 4.9.1_esbuild@0.14.36+rollup@2.70.2 rollup-plugin-hermes: 0.11.1 typescript: 4.6.3 + ws: 8.5.0 packages: @@ -708,6 +712,11 @@ packages: supports-color: 7.2.0 dev: true + /chalk/5.0.1: + resolution: {integrity: sha512-Fo07WOYGqMfCWHOzSXOt2CxDbC6skS/jO9ynEcmpANMoPrD+W1r1K6Vx7iNm+AQmETU1Xr2t+n8nzkV9t6xh3w==} + engines: {node: ^12.17.0 || ^14.13 || >=16.0.0} + dev: true + /color-convert/1.9.3: resolution: {integrity: sha512-QfAUtd+vFdAtFQcC8CCyYt1fYWxSqAiK2cSD6zDB8N3cpsEBAvRxp9zOGg6G/SHHJYAT88/az/IuDGALsNVbGg==} dependencies: @@ -2066,6 +2075,19 @@ packages: optional: true dev: false + /ws/8.5.0: + resolution: {integrity: sha512-BWX0SWVgLPzYwF8lTzEy1egjhS4S4OEAHfsO8o65WOVsrnSRGaSiUaa9e0ggGlkMTtBlmOpEXiie9RUcBO86qg==} + engines: {node: '>=10.0.0'} + peerDependencies: + bufferutil: ^4.0.1 + utf-8-validate: ^5.0.2 + peerDependenciesMeta: + bufferutil: + optional: true + utf-8-validate: + optional: true + dev: true + /yallist/4.0.0: resolution: {integrity: sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==} dev: true diff --git a/serve.mjs b/serve.mjs index 7156a22..b2ebc22 100644 --- a/serve.mjs +++ b/serve.mjs @@ -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) => { @@ -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(); +}); diff --git a/src/utils/debug/DebugWS.ts b/src/utils/debug/DebugWS.ts index 1a7fc84..a2d697f 100644 --- a/src/utils/debug/DebugWS.ts +++ b/src/utils/debug/DebugWS.ts @@ -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"); @@ -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; + } } diff --git a/tsconfig.json b/tsconfig.json index b0fcf53..47dd9c3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,7 +8,10 @@ "module": "ESNext", "declaration": true, "outDir": "build", - "lib": ["ESNext"] + "lib": [ + "ESNext", + "DOM" + ] }, "include": [ "src/**/*"