share command

This commit is contained in:
Chris Wanstrath 2025-09-23 21:03:10 -07:00
parent 5797ff9ebc
commit cfa8f2795e
2 changed files with 77 additions and 34 deletions

14
app/nose/bin/share.ts Normal file
View File

@ -0,0 +1,14 @@
import { apps } from "app/src/webapp"
import { connectSneaker } from "app/src/sneaker"
export default async function (app: string) {
if (!app) {
return `usage: share <app>`
}
if (!apps().includes(app)) {
return { error: `${app} not found` }
}
return await connectSneaker(app)
}

View File

@ -1,38 +1,67 @@
import app from "./server" import nose from "./server"
// const SNEAKER_URL = "sneaker-ep2i.onrender.com"
// const SNEAKER_TLS = true
const SNEAKER_URL = "localhost:3100" const SNEAKER_URL = "localhost:3100"
const SNEAKER_TLS = false
const ws = new WebSocket(`ws://${SNEAKER_URL}/tunnel`) type Connection = {
subdomain: string
ws.onerror = e => console.log("sneaker error", e) ws: any
}
ws.onmessage = async event => { const connections: Record<string, Connection> = {}
const msg = JSON.parse(event.data.toString())
try { // returns the sneaker subdomain if successful
const req = new Request("http://localhost" + msg.path, { export async function connectSneaker(app: string): Promise<string> {
method: msg.method, if (connections[app]) {
headers: msg.headers, return connections[app].subdomain
body: msg.body || undefined, }
})
const ws = new WebSocket(`ws${SNEAKER_TLS ? "s" : ""}://${SNEAKER_URL}/tunnel?app=${app}`)
const res = await app.fetch(req) let resolve: (v: string) => void
let promise = new Promise<string>(res => resolve = res)
const body = await res.text()
const headers: Record<string, string> = {} ws.onclose = (e) => delete connections[app]
res.headers.forEach((v, k) => (headers[k] = v))
ws.onerror = e => console.error("sneaker error", e)
ws.send(JSON.stringify({
id: msg.id, ws.onmessage = async event => {
status: res.status, const msg = JSON.parse(event.data.toString())
headers,
body, if (msg.subdomain) {
})) connections[app] = { subdomain: "", ws }
} catch (err: any) { resolve(msg.subdomain)
ws.send(JSON.stringify({ return
id: msg.id, }
status: 500,
headers: { "content-type": "text/plain" }, try {
body: "error: " + err.message, const req = new Request(`http://${msg.app}.localhost` + msg.path, {
})) method: msg.method,
} headers: msg.headers,
body: msg.body || undefined,
})
const res = await nose.fetch(req)
const body = await res.text()
const headers: Record<string, string> = {}
res.headers.forEach((v, k) => (headers[k] = v))
ws.send(JSON.stringify({
id: msg.id,
status: res.status,
headers,
body,
}))
} catch (err: any) {
ws.send(JSON.stringify({
id: msg.id,
status: 500,
headers: { "content-type": "text/plain" },
body: "error: " + err.message,
}))
}
}
return promise
} }