Compare commits
No commits in common. "3ac9a06a9f5542ee07f0d3957d8609c0c93d0dd3" and "c19dc02090832cae85a428abf14f70929cca2f35" have entirely different histories.
3ac9a06a9f
...
c19dc02090
|
|
@ -117,7 +117,6 @@ export function connect(options: TunnelOptions): Tunnel {
|
|||
method: req.method,
|
||||
headers: req.headers,
|
||||
body: hasBody ? req.body : undefined,
|
||||
redirect: "manual",
|
||||
})
|
||||
|
||||
const contentType = response.headers.get("content-type")
|
||||
|
|
|
|||
|
|
@ -25,11 +25,9 @@ type Success = {
|
|||
|
||||
export const GIT_SHA = process.env.RENDER_GIT_COMMIT ?? (await Bun.$`git rev-parse HEAD`.text()).trim()
|
||||
|
||||
const REQUEST_TIMEOUT = 30_000
|
||||
|
||||
type Connection = { app: string, ws: any }
|
||||
let connections: Record<string, Connection> = {}
|
||||
const pending = new Map<string, { resolve: (res: Response) => void, subdomain: string }>
|
||||
const pending = new Map<string, (res: any) => void>
|
||||
|
||||
const app = new Hono
|
||||
|
||||
|
|
@ -67,21 +65,15 @@ app.get("/tunnel", c => {
|
|||
console.log(`connection opened: ${name} -> ${app}`)
|
||||
send(ws, { subdomain: name })
|
||||
},
|
||||
onClose: (_event, _ws) => {
|
||||
onClose: (_event, ws) => {
|
||||
console.log("connection closed:", name)
|
||||
delete connections[name]
|
||||
for (const [id, entry] of pending) {
|
||||
if (entry.subdomain === name) {
|
||||
entry.resolve({ id, status: 502, headers: {}, body: "Tunnel disconnected" })
|
||||
pending.delete(id)
|
||||
}
|
||||
}
|
||||
},
|
||||
async onMessage(event, _ws) {
|
||||
const msg = JSON.parse(event.data.toString())
|
||||
const entry = pending.get(msg.id)
|
||||
if (entry) {
|
||||
entry.resolve(msg)
|
||||
const resolve = pending.get(msg.id)
|
||||
if (resolve) {
|
||||
resolve(msg)
|
||||
pending.delete(msg.id)
|
||||
}
|
||||
},
|
||||
|
|
@ -103,24 +95,11 @@ app.all("*", async c => {
|
|||
|
||||
const id = randomID()
|
||||
const headers = Object.fromEntries(c.req.raw.headers)
|
||||
delete headers['host']
|
||||
delete headers['connection']
|
||||
delete headers['keep-alive']
|
||||
delete headers['transfer-encoding']
|
||||
delete headers['content-length']
|
||||
const body = await c.req.text()
|
||||
const app = connection.app
|
||||
|
||||
const result = await new Promise<Response>((resolve, reject) => {
|
||||
const timer = setTimeout(() => {
|
||||
pending.delete(id)
|
||||
reject(new Error("Tunnel request timed out"))
|
||||
}, REQUEST_TIMEOUT)
|
||||
|
||||
pending.set(id, {
|
||||
resolve: (res) => { clearTimeout(timer); resolve(res) },
|
||||
subdomain,
|
||||
})
|
||||
const result = await new Promise<Response>(resolve => {
|
||||
pending.set(id, resolve)
|
||||
send(connection.ws, {
|
||||
id,
|
||||
app,
|
||||
|
|
@ -129,7 +108,7 @@ app.all("*", async c => {
|
|||
headers,
|
||||
body
|
||||
})
|
||||
}).catch((): Response => ({ id, status: 504, headers: {}, body: "Gateway Timeout" }))
|
||||
})
|
||||
|
||||
if (result.isBinary) {
|
||||
const buffer = Buffer.from(result.body, 'base64')
|
||||
|
|
@ -146,7 +125,7 @@ app.all("*", async c => {
|
|||
})
|
||||
|
||||
function send(connection: any, msg: Request | Success) {
|
||||
console.log("sending", 'id' in msg ? `${msg.id} ${msg.method} ${msg.path}` : `connected: ${msg.subdomain}`)
|
||||
console.log("sending", msg)
|
||||
connection.send(JSON.stringify(msg))
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user