(
req: http.IncomingMessage,
socket: import("node:net").Socket,
head: Buffer,
)
| 2842 | ); |
| 2843 | |
| 2844 | async function handleUpgradeRequest( |
| 2845 | req: http.IncomingMessage, |
| 2846 | socket: import("node:net").Socket, |
| 2847 | head: Buffer, |
| 2848 | ): Promise<void> { |
| 2849 | const parsedUrl = new URL(req.url ?? "/", `http://${req.headers.host ?? "localhost"}`); |
| 2850 | let pathname = parsedUrl.pathname; |
| 2851 | |
| 2852 | // Dispatch to mounted services before any path rewrites |
| 2853 | if (mounts) { |
| 2854 | for (const { path: mountPath, handler } of mounts) { |
| 2855 | if ( |
| 2856 | (pathname === mountPath || pathname.startsWith(mountPath + "/")) && |
| 2857 | handler.handleUpgrade |
| 2858 | ) { |
| 2859 | const subPath = pathname.slice(mountPath.length) || "/"; |
| 2860 | if (await handler.handleUpgrade(socket, head, subPath)) return; |
| 2861 | } |
| 2862 | } |
| 2863 | } |
| 2864 | |
| 2865 | // Normalize OpenAI-compatible paths (strip /openai/ prefix + rewrite arbitrary prefixes) |
| 2866 | // Skip Azure deployment paths — they have their own rewrite in the HTTP handler |
| 2867 | if (!pathname.match(AZURE_DEPLOYMENT_RE)) { |
| 2868 | pathname = normalizeCompatPath(pathname, logger); |
| 2869 | } |
| 2870 | |
| 2871 | if ( |
| 2872 | pathname !== RESPONSES_PATH && |
| 2873 | pathname !== REALTIME_PATH && |
| 2874 | pathname !== GEMINI_LIVE_PATH |
| 2875 | ) { |
| 2876 | socket.write("HTTP/1.1 404 Not Found\r\n\r\n"); |
| 2877 | socket.destroy(); |
| 2878 | return; |
| 2879 | } |
| 2880 | |
| 2881 | // Push any buffered data back before upgrading |
| 2882 | if (head.length > 0) { |
| 2883 | socket.unshift(head); |
| 2884 | } |
| 2885 | |
| 2886 | let ws: WebSocketConnection; |
| 2887 | try { |
| 2888 | ws = upgradeToWebSocket(req, socket); |
| 2889 | } catch (err: unknown) { |
| 2890 | const msg = err instanceof Error ? err.message : "WebSocket upgrade failed"; |
| 2891 | logger.error(`WebSocket upgrade error: ${msg}`); |
| 2892 | if (!socket.destroyed) socket.destroy(); |
| 2893 | return; |
| 2894 | } |
| 2895 | |
| 2896 | activeConnections.add(ws); |
| 2897 | |
| 2898 | ws.on("error", (err: Error) => { |
| 2899 | logger.error(`WebSocket error: ${err.message}`); |
| 2900 | activeConnections.delete(ws); |
| 2901 | }); |
no test coverage detected
searching dependent graphs…