Compare commits

..

3 Commits

Author SHA1 Message Date
Chris Wanstrath
df700ffe91 fix GIT_SHA to work in both browser+server 2025-10-08 13:18:57 -07:00
Chris Wanstrath
e35aef35cd support async components 2025-10-08 13:15:52 -07:00
Chris Wanstrath
d5e0dbd2db subdomain sites get priority 2025-10-08 13:15:39 -07:00
3 changed files with 55 additions and 59 deletions

View File

@ -12,4 +12,6 @@ export const DEFAULT_PROJECT = "root"
export const NOSE_ROOT_BIN = join(NOSE_DIR, DEFAULT_PROJECT, "bin") export const NOSE_ROOT_BIN = join(NOSE_DIR, DEFAULT_PROJECT, "bin")
export const NOSE_STARTED = Date.now() export const NOSE_STARTED = Date.now()
export const GIT_SHA = (await $`git rev-parse --short HEAD`.text()).trim()
export const GIT_SHA = (await $`git rev-parse --short HEAD`.text()).trim()
; (globalThis as any).GIT_SHA = GIT_SHA

View File

@ -3,7 +3,6 @@
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 color from "kleur" import color from "kleur"
import type { Message } from "./shared/types" import type { Message } from "./shared/types"
@ -34,13 +33,7 @@ import { initCommands } from "./commands"
const app = new Hono() const app = new Hono()
app.use("*", prettyJSON()) // console logging
app.use('/*', serveStatic({ root: './public' }))
app.use('/img/*', serveStatic({ root: './public' }))
app.use('/vendor/*', serveStatic({ root: './public' }))
app.use('/css/*', serveStatic({ root: './src' }))
app.use("*", async (c, next) => { app.use("*", async (c, next) => {
const start = Date.now() const start = Date.now()
await next() await next()
@ -49,6 +42,31 @@ app.use("*", async (c, next) => {
console.log(fn(`${c.res.status} ${c.req.method} ${c.req.url} (${end - start}ms)`)) console.log(fn(`${c.res.status} ${c.req.method} ${c.req.url} (${end - start}ms)`))
}) })
// subdomain route needs to go first
app.use("*", async (c, next) => {
const url = new URL(c.req.url)
const localhost = url.hostname.endsWith("localhost")
const domains = url.hostname.split(".")
let subdomain = ""
if (domains.length > (localhost ? 1 : 2))
subdomain = domains[0]!
if (subdomain) {
const app = serveApp(c, subdomain)
return await app
}
return next()
})
// static files
app.use('/*', serveStatic({ root: './public' }))
app.use('/img/*', serveStatic({ root: './public' }))
app.use('/vendor/*', serveStatic({ root: './public' }))
app.use('/css/*', serveStatic({ root: './src' }))
// blue screen of death error page if NOSE_DIR isn't found
app.use("*", async (c, next) => { app.use("*", async (c, next) => {
const error = fatal ? fatal : !isDir(NOSE_DIR) ? `NOSE_DIR doesn't exist: ${NOSE_DIR}` : undefined const error = fatal ? fatal : !isDir(NOSE_DIR) ? `NOSE_DIR doesn't exist: ${NOSE_DIR}` : undefined
@ -59,25 +77,23 @@ app.use("*", async (c, next) => {
} }
}) })
app.on("GET", ["/js/:path{.+}", "/shared/:path{.+}"], async c => { // only transpile browser JS on the fly in dev mode
let path = "./src/" + c.req.path.replace("..", ".") if (process.env.NODE_ENV !== "production")
app.on("GET", ["/js/:path{.+}", "/shared/:path{.+}"], async c => {
let path = "./src/" + c.req.path.replace("..", ".")
// path must end in .js or .ts // path must end in .js or .ts
if (!path.endsWith(".js") && !path.endsWith(".ts")) path += ".ts" if (!path.endsWith(".js") && !path.endsWith(".ts")) path += ".ts"
const ts = path.replace(".js", ".ts") const ts = path.replace(".js", ".ts")
if (isFile(ts)) { if (isFile(ts)) {
return new Response(await transpile(ts), { headers: { "Content-Type": "text/javascript" } }) return new Response(await transpile(ts), { headers: { "Content-Type": "text/javascript" } })
} else if (isFile(path)) { } else if (isFile(path)) {
return new Response(Bun.file(path), { headers: { "Content-Type": "text/javascript" } }) return new Response(Bun.file(path), { headers: { "Content-Type": "text/javascript" } })
} else { } else {
return c.text("File not found", 404) return c.text("File not found", 404)
} }
}) })
//
// app routes
//
// inject browser-nav.js into NOSE apps when loaded in a NOSE iframe // inject browser-nav.js into NOSE apps when loaded in a NOSE iframe
app.use("*", async (c, next) => { app.use("*", async (c, next) => {
@ -112,23 +128,7 @@ app.use("*", async (c, next) => {
}) })
}) })
app.use("*", async (c, next) => { // source code of commands - used to load and eval() browser commands
const url = new URL(c.req.url)
const localhost = url.hostname.endsWith("localhost")
const domains = url.hostname.split(".")
let subdomain = ""
if (domains.length > (localhost ? 1 : 2))
subdomain = domains[0]!
if (subdomain) {
const app = serveApp(c, subdomain)
return await app
}
return next()
})
app.get("/source/:name", async c => { app.get("/source/:name", async c => {
const name = c.req.param("name") const name = c.req.param("name")
const sessionId = c.req.query("session") || "0" const sessionId = c.req.query("session") || "0"
@ -142,6 +142,7 @@ app.get("/source/:name", async c => {
}) })
}) })
// call a command's GET or POST function, if one exists
app.on(["GET", "POST"], ["/cmd/:name"], async c => { app.on(["GET", "POST"], ["/cmd/:name"], async c => {
const sessionId = c.req.header("X-Session") || "0" const sessionId = c.req.header("X-Session") || "0"
const cmd = c.req.param("name") const cmd = c.req.param("name")
@ -158,12 +159,10 @@ app.on(["GET", "POST"], ["/cmd/:name"], async c => {
} }
}) })
// the terminal
app.get("/", c => c.html(<Layout><Terminal /></Layout>)) app.get("/", c => c.html(<Layout><Terminal /></Layout>))
// // the main websocket
// websocket
//
app.get("/ws", c => { app.get("/ws", c => {
const _sessionId = c.req.query("session") const _sessionId = c.req.query("session")
const url = new URL(c.req.url) const url = new URL(c.req.url)
@ -229,16 +228,17 @@ if (process.env.BUN_HOT) {
} }
// //
// production mode // server start
// //
if (process.env.NODE_ENV === "production") { if (process.env.NODE_ENV === "production") {
initDNS() initDNS()
} }
// await initNoseDir()
// server start initCommands()
// initWebapps()
initSneakers()
console.log(color.cyan(NOSE_ICON)) console.log(color.cyan(NOSE_ICON))
console.log(color.blue(" NOSE_BIN:"), color.yellow(tilde(NOSE_BIN))) console.log(color.blue(" NOSE_BIN:"), color.yellow(tilde(NOSE_BIN)))
@ -246,11 +246,6 @@ console.log(color.blue(" NOSE_DATA:"), color.yellow(tilde(NOSE_DATA)))
console.log(color.blue(" NOSE_DIR:"), color.yellow(tilde(NOSE_DIR))) console.log(color.blue(" NOSE_DIR:"), color.yellow(tilde(NOSE_DIR)))
console.log(color.blue("NOSE_ROOT_BIN:"), color.yellow(tilde(NOSE_ROOT_BIN))) console.log(color.blue("NOSE_ROOT_BIN:"), color.yellow(tilde(NOSE_ROOT_BIN)))
await initNoseDir()
initCommands()
initWebapps()
initSneakers()
export default { export default {
port: process.env.PORT || 3000, port: process.env.PORT || 3000,
hostname: "0.0.0.0", hostname: "0.0.0.0",

View File

@ -3,7 +3,6 @@
import type { Child } from "hono/jsx" import type { Child } from "hono/jsx"
import { type Context, Hono } from "hono" import { type Context, Hono } from "hono"
import { renderToString } from "hono/jsx/dom/server"
import { join } from "path" import { join } from "path"
import { readdirSync, watch } from "fs" import { readdirSync, watch } from "fs"
import { sendAll } from "./websocket" import { sendAll } from "./websocket"
@ -79,13 +78,13 @@ async function loadApp(path: string): Promise<App | undefined> {
return mod.default as App return mod.default as App
} }
export function toResponse(source: string | Child | Response): Response { export async function toResponse(source: string | Child | Response): Promise<Response> {
if (source instanceof Response) if (source instanceof Response)
return source return source
else if (typeof source === "string") else if (typeof source === "string")
return new Response(source) return new Response(source)
else else
return new Response(renderToString(source), { return new Response(await source?.toString(), {
headers: { headers: {
"Content-Type": "text/html; charset=utf-8" "Content-Type": "text/html; charset=utf-8"
} }