import { Channel } from './channel'; import { ChannelManager } from './channel_manager'; export { Channel, ChannelManager }; export type { ChannelParams } from './channel'; export type { WsAuthResolver } from './channel_manager'; export { PresenceStore, type PresenceStoreInterface } from './presence'; export { createBroadcast, type Broadcast } from './broadcast'; export interface WsContext { ws: ServerWebSocket; data: T; message: string | Buffer; } export interface WsHandler { open?: (ws: ServerWebSocket) => void | Promise; message?: (ws: ServerWebSocket, message: string | Buffer) => void | Promise; close?: (ws: ServerWebSocket, code: number, reason: string) => void | Promise; drain?: (ws: ServerWebSocket) => void | Promise; upgrade?: (req: Request) => T | Promise; } export interface WsRoute { path: string; handler: WsHandler; } export interface ServerWebSocket { send(data: string | Buffer): void; close(code?: number, reason?: string): void; data: T; readyState: number; readonly remoteAddress: string; subscribe(topic: string): void; unsubscribe(topic: string): void; publish(topic: string, data: string | Buffer): void; isSubscribed(topic: string): boolean; cork(callback: () => void): void; } /** * WebSocket manager — collects WS routes and produces Bun.serve websocket config. * * @example * ```ts * import { WsManager } from '@tekir/core' * * const wsm = server.ws() * * wsm.route('/ws/chat', { * upgrade(req) { * const url = new URL(req.url) * return { room: url.searchParams.get('room') || 'general' } * }, * open(ws) { * ws.subscribe(ws.data.room) * ws.publish(ws.data.room, `User joined`) * }, * message(ws, msg) { * ws.publish(ws.data.room, String(msg)) * }, * close(ws) { * ws.publish(ws.data.room, `User left`) * }, * }) * ``` */ export declare class WsManager { private routes; private channelManager; route(path: string, handler: WsHandler): this; /** * Set the auth resolver for WebSocket channels. * Runs during upgrade — resolve user from token, cookie, or query param. * * @example * ```ts * // JWT from query string * wsm.channelAuth(async (req) => { * const token = new URL(req.url).searchParams.get('token') * return token ? await jwt.verify(token) : null * }) * * // Database token from header * wsm.channelAuth(async (req) => { * const token = req.headers.get('authorization')?.replace('Bearer ', '') * return token ? await db.queryOne('SELECT u.* FROM users u JOIN tokens t ON t.user_id = u.id WHERE t.token = ?', [token]) : null * }) * ``` */ channelAuth(resolver: (req: Request) => unknown | Promise): this; /** Register a channel class for the multiplexed /ws endpoint */ channel(name: string, ChannelClass: new () => Channel): this; /** Get the ChannelManager instance (if channels are registered) */ getChannelManager(): ChannelManager | null; hasRoutes(): boolean; build(): { upgradeHandler: (req: Request, server: any) => Promise; websocket: { open(ws: any): void; message(ws: any, message: any): void; close(ws: any, code: number, reason: string): void; drain(ws: any): void; }; }; }