MCPcopy Create free account
hub / github.com/CopilotKit/aimock / handleUpgradeRequest

Function handleUpgradeRequest

src/server.ts:2844–2932  ·  view source on GitHub ↗
(
    req: http.IncomingMessage,
    socket: import("node:net").Socket,
    head: Buffer,
  )

Source from the content-addressed store, hash-verified

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 });

Callers 1

createServerFunction · 0.85

Calls 14

normalizeCompatPathFunction · 0.85
upgradeToWebSocketFunction · 0.85
getTestIdFunction · 0.85
handleWebSocketResponsesFunction · 0.85
handleWebSocketRealtimeFunction · 0.85
handleUpgradeMethod · 0.80
writeMethod · 0.80
errorMethod · 0.80
addMethod · 0.80
onMethod · 0.80
destroyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…