forked from egoist/ai-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.ts
More file actions
26 lines (22 loc) · 849 Bytes
/
Copy pathserver.ts
File metadata and controls
26 lines (22 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { serve } from "@hono/node-server"
import type { Server, IncomingMessage } from "node:http"
import type { Socket } from "node:net"
import app from "./main"
import { handleUpgrade } from "./ws"
const port = Number(process.env.PORT || "4000")
const server = serve({
fetch: app.fetch,
port,
}) as Server
server.on("upgrade", (req: IncomingMessage, socket: Socket, head: Buffer) => {
// a sync throw (e.g. malformed request-target in new URL) would become
// an unhandled rejection and kill the process. req.url is omitted from the
// log since it can carry credentials in the query string.
handleUpgrade(req, socket, head).catch((error) => {
console.error(
`ws upgrade handler error: ${error instanceof Error ? error.message : String(error)}`,
)
socket.destroy()
})
})
console.log(`http://localhost:${port}`)