subdomains

This commit is contained in:
Chris Wanstrath 2025-09-23 21:16:52 -07:00
parent cfa8f2795e
commit bdd320eab1
2 changed files with 17 additions and 10 deletions

View File

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

View File

@ -9,19 +9,25 @@ type Connection = {
subdomain: string
ws: any
}
const connections: Record<string, Connection> = {}
const conns: Record<string, Connection> = {}
// returns the sneaker subdomain if successful
export async function connectSneaker(app: string): Promise<string> {
if (connections[app]) {
return connections[app].subdomain
export async function connectSneaker(app: string, subdomain = ""): Promise<string> {
if (conns[app]) {
return conns[app].subdomain
}
const ws = new WebSocket(`ws${SNEAKER_TLS ? "s" : ""}://${SNEAKER_URL}/tunnel?app=${app}`)
let url = `ws${SNEAKER_TLS ? "s" : ""}://${SNEAKER_URL}/tunnel?app=${app}`
if (subdomain) url += `&subdomain=${subdomain}`
const ws = new WebSocket(url)
let resolve: (v: string) => void
let promise = new Promise<string>(res => resolve = res)
ws.onclose = (e) => delete connections[app]
ws.onclose = e => {
delete conns[app]
setTimeout(() => connectSneaker(app, subdomain), 1000) // simple retry
}
ws.onerror = e => console.error("sneaker error", e)
@ -29,7 +35,8 @@ export async function connectSneaker(app: string): Promise<string> {
const msg = JSON.parse(event.data.toString())
if (msg.subdomain) {
connections[app] = { subdomain: "", ws }
conns[app] = { subdomain: msg.subdomain, ws }
subdomain = msg.subdomain
resolve(msg.subdomain)
return
}