import { AckIdGenerator, type AckListener, AckResponseCache, type Connection, type Impl, type MaybePromise, Proc } from "./core"; /** * Takes the procedure's input data and a socket object. * The `connection` represents a link to the client. */ export type ServerProcHandler = (input: I, connection: ServerConnection) => MaybePromise; export interface BroadcastOptions { exclude?: ServerConnection[]; } interface SocketData { req: Request; clientId: string; sessionId: string; meta: Record; } /** * Options passed to the Server constructor. */ export interface ServerOptions { /** * Called just after the client connects. The return value is set as the `data` field on the connection object. */ getConnectionData?: (ws: Bun.ServerWebSocket) => Promise; /** * Called just after the connection is initialized, but before it starts receiving events. */ onOpen?: (connection: ServerConnection) => void | Promise; /** * Called just after the connection is closed. */ onClose?: (connection: ServerConnection, code: number, reason: string) => void | Promise; } declare class ServerSocketHandler implements Bun.WebSocketHandler { _server: ChatterServer; constructor(server: ChatterServer); open: (ws: Bun.ServerWebSocket) => Promise; close: (ws: Bun.ServerWebSocket, code: number, reason: string) => Promise; drain: (ws: Bun.ServerWebSocket) => Promise; message: (ws: Bun.ServerWebSocket, data: string | Buffer) => Promise; _handlePing(connection: ServerConnection): Promise; _handleProc(connection: ServerConnection, message: Uint8Array): Promise; _handleAck(connection: ServerConnection, message: Uint8Array): Promise; } export declare function createServer(options?: ServerOptions): ChatterServer; declare class ChatterServer { readonly _options: ServerOptions; readonly _connections: Map>; readonly _procs: Map>>; _topicMap: Map>>; constructor(options?: ServerOptions); readonly websocket: ServerSocketHandler; /** * @param req - The request object. * @param server - The Bun server object. * @param meta - An optional object with values that will be accessible in `getConnectionData`. */ upgrade(req: Request, server: Bun.Server, meta?: Record): Promise; /** * Implements a `Proc` so it can be called from the other end of the connection. */ on(proc: Proc, handler: ServerProcHandler): void; /** * Calls a procedure on all clients subscribed to `topic`. */ broadcast(topic: string, proc: Proc, input?: null, options?: BroadcastOptions): Promise; broadcast(topic: string, proc: Proc, input: I, options?: BroadcastOptions): Promise; /** * Calls a procedure on all clients subscribed to any topic in `topics`. */ broadcast(topics: Iterable, proc: Proc, input?: null, options?: BroadcastOptions): Promise; broadcast(topics: Iterable, proc: Proc, input: I, options?: BroadcastOptions): Promise; in(topic: string | Iterable): TopicScope; /** * Returns the first connection for which `where` returns a truthy value. */ find(where: (connection: ServerConnection) => any): ServerConnection | undefined; /** * Returns all connections for which `where` returns a truthy value. */ filter(where: (connection: ServerConnection) => any): ServerConnection[]; } declare class TopicScope { #private; constructor(server: ChatterServer, topic: string | Iterable); /** * Calls a procedure on all clients subscribed to `topic`. */ broadcast(proc: Proc, input?: null): Promise; broadcast(proc: Proc, input: I): Promise; /** * Calls a procedure on all clients subscribed to any topic in `topics`. */ broadcast(proc: Proc, input?: null): Promise; broadcast(proc: Proc, input: I): Promise; } export interface ServerConnectionBroadcastOptions extends BroadcastOptions { includeSelf?: boolean; } declare enum ConnectionState { Disconnected = 0, Connected = 1 } /** * Represents one client from the server's perspective. */ export declare class ServerConnection implements Connection { _server: ChatterServer; _ws: Bun.ServerWebSocket; _topics: Set; _ackListeners: Map>; /** * Cache acks to send to the client while state is Disconnected. */ _ackResponses: AckResponseCache; /** * Cache messages to be sent while the connection is offline. */ _offlineMessageQueue: Uint8Array[]; _ids: AckIdGenerator; _sessionId: string; _destroyTimer: any; data: Data; state: ConnectionState; get isConnected(): boolean; constructor(server: ChatterServer, ws: Bun.ServerWebSocket, data: Data); /** * Calls a procedure on the client and returns its response. */ call(proc: Proc): Promise; call(proc: Proc, input: I): Promise; /** * Calls a procedure on all clients subscribed to `topic`. * Excludes this client unless `options.includeSelf` is true. */ broadcast(topic: string, proc: Proc, input?: null, options?: ServerConnectionBroadcastOptions): Promise; broadcast(topic: string, proc: Proc, input: I, options?: ServerConnectionBroadcastOptions): Promise; /** * Calls a procedure on all clients subscribed to any topic in `topics`. * Excludes this client unless `options.includeSelf` is true. */ broadcast(topics: Iterable, proc: Proc, input?: null, options?: ServerConnectionBroadcastOptions): Promise; broadcast(topics: Iterable, proc: Proc, input: I, options?: ServerConnectionBroadcastOptions): Promise; /** * Subscribes this connection to `topic`. */ subscribe(topic: string): void; /** * Unsubscribes this connection from `topic`. */ unsubscribe(topic: string): void; isSubscribed(topic: string): boolean; /** * Called when the connection needs to be cleaned up and removed and can no longer reconnect. */ _destroy(): void; /** * Called by the socket handler when the socket closes. */ _disconnect(): void; /** * Called by the socket handler when a socket with the same sessionId is established. * Associates this connection with the new websocket object. */ _reconnect(ws: Bun.ServerWebSocket): Promise; } export {};