mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-05-09 14:08:19 +00:00
refactor(websocket): move websocket logic to its own module
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import type { Server as HttpServer } from 'node:http';
|
||||
|
||||
import { WebSocketServer, type VerifyClientCallbackSync } from 'ws';
|
||||
|
||||
import { handleChatConnection } from '@/modules/websocket/services/chat-websocket.service.js';
|
||||
import { verifyWebSocketClient } from '@/modules/websocket/services/websocket-auth.service.js';
|
||||
import { handlePluginWsProxy } from '@/modules/websocket/services/plugin-websocket-proxy.service.js';
|
||||
import { handleShellConnection } from '@/modules/websocket/services/shell-websocket.service.js';
|
||||
import type { AuthenticatedWebSocketRequest } from '@/shared/types.js';
|
||||
|
||||
type WebSocketServerDependencies = {
|
||||
verifyClient: Parameters<typeof verifyWebSocketClient>[1];
|
||||
chat: Parameters<typeof handleChatConnection>[2];
|
||||
shell: Parameters<typeof handleShellConnection>[1];
|
||||
getPluginPort: Parameters<typeof handlePluginWsProxy>[2];
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates and wires the server-wide websocket gateway used for chat, shell, and
|
||||
* plugin proxy routes.
|
||||
*/
|
||||
export function createWebSocketServer(
|
||||
server: HttpServer,
|
||||
dependencies: WebSocketServerDependencies
|
||||
): WebSocketServer {
|
||||
const wss = new WebSocketServer({
|
||||
server,
|
||||
verifyClient: ((
|
||||
info: Parameters<VerifyClientCallbackSync<AuthenticatedWebSocketRequest>>[0]
|
||||
) => verifyWebSocketClient(info, dependencies.verifyClient)),
|
||||
});
|
||||
|
||||
wss.on('connection', (ws, request) => {
|
||||
const incomingRequest = request as AuthenticatedWebSocketRequest;
|
||||
const url = incomingRequest.url ?? '/';
|
||||
const pathname = new URL(url, 'http://localhost').pathname;
|
||||
|
||||
if (pathname === '/shell') {
|
||||
handleShellConnection(ws, dependencies.shell);
|
||||
return;
|
||||
}
|
||||
|
||||
if (pathname === '/ws') {
|
||||
handleChatConnection(ws, incomingRequest, dependencies.chat);
|
||||
return;
|
||||
}
|
||||
|
||||
if (pathname.startsWith('/plugin-ws/')) {
|
||||
handlePluginWsProxy(ws, pathname, dependencies.getPluginPort);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('[WARN] Unknown WebSocket path:', pathname);
|
||||
ws.close();
|
||||
});
|
||||
|
||||
return wss;
|
||||
}
|
||||
Reference in New Issue
Block a user