dispatchMessage

This commit is contained in:
Chris Wanstrath 2025-09-21 21:09:10 -07:00
parent a8b1dc91a3
commit 5f494ede79
2 changed files with 54 additions and 43 deletions

24
src/dispatch.ts Normal file
View File

@ -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}` })
}
}

View File

@ -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(<Layout><Terminal /></Layout>))
//
// 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
//