setupWebSocketHandler (ws-handler.ts)

clsh · auth, websocket-server, agent-runtime, jwt-auth, realtime

Sets up authenticated WebSocket server, handling connections and messages for agent runtime.

import { WebSocketServer, WebSocket } from 'ws';
import type { IncomingMessage } from 'node:http';
import { verifyJWT } from './auth.js';
import { PTYManager, type PTYSession } from './pty-manager.js';
import type { ClientMessage, ServerMessage, ShellType } from './types.js';

/** WebSocket close codes. */
const WS_CLOSE_UNAUTHORIZED = 4001;
const WS_CLOSE_NORMAL = 1000;

/** Timeout for initial auth message (5 seconds). */
const AUTH_TIMEOUT_MS = 5_000;

/** Subscriptions map: which sessions each WebSocket client is subscribed to. */
type SubscriptionMap = Map<WebSocket, Set<string>>;

function send(ws: WebSocket, message: ServerMessage): void {
  if (ws.readyState === WebSocket.OPEN) {
    ws.send(JSON.stringify(message));
  }
}

function sendError(ws: WebSocket, message: string): void {

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search 40K more like it.

Get a free API key