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.

// Source: https://github.com/my-claude-utils/clsh | License: MIT | Copyright 2026 my-claude-utils
// Provided via justclaw.it.com; keep this notice with the code.
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: Serv

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

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

Get a free API key