From 5f494ede79e4624eb8e674a8e93ec27cff192b8c Mon Sep 17 00:00:00 2001 From: Chris Wanstrath Date: Sun, 21 Sep 2025 21:09:10 -0700 Subject: [PATCH] dispatchMessage --- src/dispatch.ts | 24 ++++++++++++++++ src/server.tsx | 73 ++++++++++++++++++++----------------------------- 2 files changed, 54 insertions(+), 43 deletions(-) create mode 100644 src/dispatch.ts diff --git a/src/dispatch.ts b/src/dispatch.ts new file mode 100644 index 0000000..fa74bfb --- /dev/null +++ b/src/dispatch.ts @@ -0,0 +1,24 @@ +//// +// Dispatch Messages + +import { basename } from "path" +import type { Message } from "./shared/types" +import { runCommand } from "./shell" +import { send } from "./websocket" +import { isFile } from "./utils" + +export async function dispatchMessage(ws: any, msg: Message) { + if (msg.type === "input") { + const result = await runCommand(msg.session || "", msg.id || "", msg.data as string) + send(ws, { id: msg.id, type: "output", data: result }) + + } else if (msg.type === "save-file") { + if (msg.id && typeof msg.data === "string" && isFile(msg.id)) { + await Bun.write(msg.id, msg.data) + send(ws, { type: "output", data: { status: "ok", output: `saved ${basename(msg.id)}` } }) + } + + } else { + send(ws, { type: "error", data: `unknown message: ${msg.type}` }) + } +} \ No newline at end of file diff --git a/src/server.tsx b/src/server.tsx index cb0c065..c86d3bb 100644 --- a/src/server.tsx +++ b/src/server.tsx @@ -1,19 +1,18 @@ import { Hono } from "hono" import { serveStatic, upgradeWebSocket, websocket } from "hono/bun" import { prettyJSON } from "hono/pretty-json" -import { basename } from "path" import color from "kleur" import type { Message } from "./shared/types" import { NOSE_ICON, NOSE_BIN, NOSE_WWW } from "./config" import { transpile, isFile, tilde } from "./utils" import { apps, serveApp, publishDNS } from "./webapp" -import { runCommand } from "./shell" import { commands } from "./commands" import { send, addWebsocket, removeWebsocket, closeWebsockets } from "./websocket" import { Layout } from "./components/layout" import { Terminal } from "./components/terminal" +import { dispatchMessage } from "./dispatch" // // Hono setup @@ -51,47 +50,6 @@ app.on("GET", ["/js/:path{.+}", "/shared/:path{.+}"], async c => { } }) -// -// websocket -// - -app.get("/ws", upgradeWebSocket(async c => { - return { - async onOpen(_e, ws) { - addWebsocket(ws) - send(ws, { type: "commands", data: await commands() }) - }, - async onMessage(event, ws) { - let data: Message | undefined - - try { - data = JSON.parse(event.data.toString()) - } catch (e) { - console.error("JSON parsing error", e) - send(ws, { type: "error", data: "json parsing error" }) - return - } - - if (!data) return - - if (data.type === "input") { - const result = await runCommand(data.session || "", data.id || "", data.data as string) - send(ws, { id: data.id, type: "output", data: result }) - } else if (data.type === "save-file") { - if (data.id && typeof data.data === "string" && isFile(data.id)) { - await Bun.write(data.id, data.data) - send(ws, { type: "output", data: { status: "ok", output: `saved ${basename(data.id)}` } }) - } - } else { - send(ws, { type: "error", data: `unknown message: ${data.type}` }) - } - }, - onClose: (event, ws) => { - removeWebsocket(ws) - }, - } -})) - // // app routes // @@ -128,6 +86,35 @@ app.get("/apps", c => { app.get("/", c => c.html()) +// +// websocket +// + +app.get("/ws", upgradeWebSocket(async c => { + return { + async onOpen(_e, ws) { + addWebsocket(ws) + send(ws, { type: "commands", data: await commands() }) + }, + async onMessage(event, ws) { + let data: Message | undefined + + try { + data = JSON.parse(event.data.toString()) + } catch (e) { + console.error("JSON parsing error", e) + send(ws, { type: "error", data: "json parsing error" }) + return + } + + if (!data) return + + await dispatchMessage(ws, data) + }, + onClose: (event, ws) => removeWebsocket(ws) + } +})) + // // hot reload mode cleanup //