dispatchMessage
This commit is contained in:
parent
a8b1dc91a3
commit
5f494ede79
24
src/dispatch.ts
Normal file
24
src/dispatch.ts
Normal 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}` })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,19 +1,18 @@
|
||||||
import { Hono } from "hono"
|
import { Hono } from "hono"
|
||||||
import { serveStatic, upgradeWebSocket, websocket } from "hono/bun"
|
import { serveStatic, upgradeWebSocket, websocket } from "hono/bun"
|
||||||
import { prettyJSON } from "hono/pretty-json"
|
import { prettyJSON } from "hono/pretty-json"
|
||||||
import { basename } from "path"
|
|
||||||
import color from "kleur"
|
import color from "kleur"
|
||||||
|
|
||||||
import type { Message } from "./shared/types"
|
import type { Message } from "./shared/types"
|
||||||
import { NOSE_ICON, NOSE_BIN, NOSE_WWW } from "./config"
|
import { NOSE_ICON, NOSE_BIN, NOSE_WWW } from "./config"
|
||||||
import { transpile, isFile, tilde } from "./utils"
|
import { transpile, isFile, tilde } from "./utils"
|
||||||
import { apps, serveApp, publishDNS } from "./webapp"
|
import { apps, serveApp, publishDNS } from "./webapp"
|
||||||
import { runCommand } from "./shell"
|
|
||||||
import { commands } from "./commands"
|
import { commands } from "./commands"
|
||||||
import { send, addWebsocket, removeWebsocket, closeWebsockets } from "./websocket"
|
import { send, addWebsocket, removeWebsocket, closeWebsockets } from "./websocket"
|
||||||
|
|
||||||
import { Layout } from "./components/layout"
|
import { Layout } from "./components/layout"
|
||||||
import { Terminal } from "./components/terminal"
|
import { Terminal } from "./components/terminal"
|
||||||
|
import { dispatchMessage } from "./dispatch"
|
||||||
|
|
||||||
//
|
//
|
||||||
// Hono setup
|
// 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
|
// app routes
|
||||||
//
|
//
|
||||||
|
|
@ -128,6 +86,35 @@ app.get("/apps", c => {
|
||||||
|
|
||||||
app.get("/", c => c.html(<Layout><Terminal /></Layout>))
|
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
|
// hot reload mode cleanup
|
||||||
//
|
//
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user