import { AckIdGenerator, type AckListener, AckResponseCache, type Connection, decodeAck, decodeProc, encodeAck, encodeProc, getMessageType, READY_MESSAGE, type Impl, type MaybePromise, MessageType, PONG_MESSAGE, Proc, encodeBroadcast, } 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; } interface HandshakeAckListener { ws: Bun.ServerWebSocket; } class ServerSocketHandler implements Bun.WebSocketHandler { _server: ChatterServer; constructor(server: ChatterServer) { this._server = server; } open = async (ws: Bun.ServerWebSocket) => { const { sessionId } = ws.data; const current = this._server._connections.get(sessionId); if (current) { // Point current connection to new socket. await current._reconnect(ws); ws.sendBinary(READY_MESSAGE); } else { // Otherwise init new. let connection: ServerConnection; const opts = this._server._options; if (opts.getConnectionData) { const data = await opts.getConnectionData(ws); connection = new ServerConnection(this._server, ws, data); } else { const data = {} as ConnectionData; connection = new ServerConnection(this._server, ws, data); } this._server._connections.set(sessionId, connection); if (opts.onOpen) { await opts.onOpen(connection); } // Let the client know we're ready. ws.sendBinary(READY_MESSAGE); } }; close = async ( ws: Bun.ServerWebSocket, code: number, reason: string ) => { // Remove client const opts = this._server._options; const connection = this._server._connections.get(ws.data.sessionId); if (!connection) return; connection._disconnect(); // Unsubscribe from all topics. // connection._topics.forEach((topic) => connection.unsubscribe(topic)); // Already disconnected, so we can stop receiving now. // this._server._connections.delete(ws); if (opts.onClose) { await opts.onClose(connection, code, reason); } }; // ping = async (ws: Bun.ServerWebSocket, data: Buffer) => { // const connection = this._server._connections.get(ws); // console.log("ping received", connection); // }; // pong = async (ws: Bun.ServerWebSocket, data: Buffer) => { // const connection = this._server._connections.get(ws); // console.log("pong received", connection); // }; drain = async (ws: Bun.ServerWebSocket) => {}; message = async ( ws: Bun.ServerWebSocket, data: string | Buffer ) => { if (typeof data === "string") { throw new Error( `Expected websocket message to be a Buffer. Got: ${data}` ); } const connection = this._server._connections.get(ws.data.sessionId); if (connection == null) { throw new Error(`No connection found for this socket.`); } const message = new Uint8Array(data); const type = getMessageType(message); // Process incoming message switch (type) { case MessageType.Ping: return this._handlePing(connection); case MessageType.Proc: return this._handleProc(connection, message); case MessageType.Ack: return this._handleAck(connection, message); } }; async _handlePing( connection: ServerConnection ): Promise { connection._ws.sendBinary(PONG_MESSAGE); } async _handleProc( connection: ServerConnection, message: Uint8Array ): Promise { const ws = connection._ws; const decoded = decodeProc(message); const impl = this._server._procs.get(decoded.name); if (!impl) { // Respond with an error. ws.sendBinary( encodeAck( decoded.ackId, false, `No implementation for proc '${decoded.name}'` ) ); return; } try { const parsedInput = await impl.proc.parseInput(decoded.input); const output = await impl.handler(parsedInput, connection); const parsedOutput = await impl.proc.parseOutput(output); // Cache message in case of connection failure. const message = encodeAck(decoded.ackId, true, parsedOutput); connection._ackResponses.set(impl.proc, decoded.ackId, message); ws.sendBinary(message); // TODO: Use return value of sendBinary to decide if we need to cache it? } catch (error) { const message = encodeAck(decoded.ackId, false, error); connection._ackResponses.set(impl.proc, decoded.ackId, message); ws.sendBinary(message); } } async _handleAck( connection: ServerConnection, message: Uint8Array ): Promise { const decoded = decodeAck(message); const listener = connection._ackListeners.get(decoded.ackId); if (!listener) { // Received unexpected acknowledgement. return; } if (decoded.success) { try { const parsedOutput = await listener.proc.parseOutput(decoded.output); listener.resolve(parsedOutput); } catch (error) { if (error instanceof Error) { listener.reject(error as Error); } else { listener.reject(new Error(`Unknown error: ${error}`)); } } } else { if ((decoded.output as any) instanceof Error) { listener.reject(decoded.output as any as Error); } else { listener.reject(new Error(decoded.output)); } } connection._ackListeners.delete(decoded.ackId); } } export function createServer( options?: ServerOptions ): ChatterServer { return new ChatterServer(options); } class ChatterServer { readonly _options: ServerOptions; readonly _connections = new Map>(); readonly _procs = new Map< string, Impl> >(); _topicMap = new Map>>(); constructor(options?: ServerOptions) { this._options = options ?? {}; this.websocket = new ServerSocketHandler(this); } 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`. */ async upgrade( req: Request, server: Bun.Server, meta?: Record ): Promise { const params = new URLSearchParams(req.url.split("?")[1]); const clientId = params.get("cid"); const sessionId = params.get("sid"); if (!clientId) { return Response.json( { message: "Missing `cid` search param." }, { status: 400 } ); } if (!sessionId) { return Response.json( { message: "Missing `sid` search param." }, { status: 400 } ); } const upgraded = server.upgrade(req, { data: { req, clientId, sessionId, meta: meta ?? {} } satisfies SocketData, }); if (!upgraded) { return Response.json( { message: "Websocket upgrade failed." }, { status: 400 } ); } } /** * Implements a `Proc` so it can be called from the other end of the connection. */ on(proc: Proc, handler: ServerProcHandler) { if (this._procs.has(proc.name)) { console.warn( `Procedure '${proc.name}' has already been added and will be overwritten!` ); } this._procs.set(proc.name, { proc, handler }); } /** * Calls a procedure on all clients subscribed to `topic`. */ async broadcast( topic: string, proc: Proc, input?: null, options?: BroadcastOptions ): Promise; async broadcast( topic: string, proc: Proc, input: I, options?: BroadcastOptions ): Promise; /** * Calls a procedure on all clients subscribed to any topic in `topics`. */ async broadcast( topics: Iterable, proc: Proc, input?: null, options?: BroadcastOptions ): Promise; async broadcast( topics: Iterable, proc: Proc, input: I, options?: BroadcastOptions ): Promise; async broadcast( topic: string | Iterable, proc: Proc, input?: I, options?: BroadcastOptions ): Promise { const parsedInput = await proc.parseInput(input); const source = this._connections.values().next().value; if (!source) return; // Collect all connections subscribed to any of the given topics. const connections = new Set>(); const topics = new Set(typeof topic === "string" ? [topic] : topic); for (const t of topics) { const set = this._topicMap.get(t); if (set) { for (const connection of set) { connections.add(connection); } } } if (connections.size === 0) { return; } // IDEA: Give server an adapter to broadcast to multiple app instances. // Remove excluded connections. if (options?.exclude) { for (const connection of options.exclude) { connections.delete(connection); } } const encoded = encodeBroadcast(proc, parsedInput); // Send message to all sockets. for (const connection of connections) { if (!connection.isConnected) { connection._offlineMessageQueue.push(encoded); } else { const result = connection._ws.sendBinary(encoded); if (result < 0) { // Result of -1 indicates a connection failure. connection._offlineMessageQueue.push(encoded); } } } } in(topic: string | Iterable) { return new TopicScope(this, topic); } /** * Returns the first connection for which `where` returns a truthy value. */ find(where: (connection: ServerConnection) => any) { for (const conn of this._connections.values()) { if (where(conn)) { return conn; } } } /** * Returns all connections for which `where` returns a truthy value. */ filter(where: (connection: ServerConnection) => any) { const matches: ServerConnection[] = []; for (const conn of this._connections.values()) { if (where(conn)) { matches.push(conn); } } return matches; } } class TopicScope { #server: ChatterServer; #topics: string[]; constructor( server: ChatterServer, topic: string | Iterable ) { this.#server = server; if (typeof topic === "string") { this.#topics = [topic]; } else { this.#topics = Array.from(topic); } } /** * Calls a procedure on all clients subscribed to `topic`. */ async broadcast(proc: Proc, input?: null): Promise; async broadcast(proc: Proc, input: I): Promise; /** * Calls a procedure on all clients subscribed to any topic in `topics`. */ async broadcast(proc: Proc, input?: null): Promise; async broadcast(proc: Proc, input: I): Promise; async broadcast(proc: Proc, input?: I): Promise { return this.#server.broadcast(this.#topics, proc, input); } // todo: subscribe / unsubscribe (to make all sockets subscribed to a topic sub/unsub from other topics) } export interface ServerConnectionBroadcastOptions extends BroadcastOptions { includeSelf?: boolean; } enum ConnectionState { Disconnected, Connected, } /** * Represents one client from the server's perspective. */ export class ServerConnection implements Connection { _server: ChatterServer; _ws: Bun.ServerWebSocket; _topics = new Set(); _ackListeners = new Map(); /** * Cache acks to send to the client while state is Disconnected. */ _ackResponses = new AckResponseCache(); /** * Cache messages to be sent while the connection is offline. */ _offlineMessageQueue: Uint8Array[] = []; _ids = new AckIdGenerator(); _sessionId: string; _destroyTimer: any; data: Data; state = ConnectionState.Connected; get isConnected() { return ( this._ws.readyState === 1 && this.state === ConnectionState.Connected ); } constructor( server: ChatterServer, ws: Bun.ServerWebSocket, data: Data ) { this._server = server; this._ws = ws; this._sessionId = ws.data.sessionId; this.data = data; } /** * Calls a procedure on the client and returns its response. */ async call(proc: Proc): Promise; async call(proc: Proc, input: I): Promise; async call(proc: Proc, input?: I): Promise { // Parse input data const parsedInput = await proc.parseInput(input); let timer: any; const waitTime = proc.timeout; return new Promise(async (resolve, reject) => { const ackId = this._ids.next(); const listener: AckListener = { proc, timestamp: Date.now(), resolve, reject, }; this._ackListeners.set(ackId, listener); // Configure timeout timer = setTimeout(() => { listener.reject( new Error(`Procedure call timed out after ${waitTime}ms`) ); this._ackListeners.delete(ackId); }, waitTime); // Send over _ws, await acknowledgement. const message = encodeProc(proc, ackId, parsedInput); if (!this.isConnected) { this._offlineMessageQueue.push(message); } else { const result = this._ws.sendBinary(message); if (result < 0) { // Result of -1 means there was a connection error. Queue for send on reconnect. this._offlineMessageQueue.push(message); } } }).finally(() => { clearTimeout(timer); }); } /** * 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; broadcast( topic: string | Iterable, proc: Proc, input?: I, options?: ServerConnectionBroadcastOptions ): Promise { return this._server.broadcast(topic, proc, input, { ...options, exclude: options?.includeSelf === true ? undefined : [this], }); } /** * Subscribes this connection to `topic`. */ subscribe(topic: string) { this._topics.add(topic); const set = this._server._topicMap.get(topic); if (!set) { this._server._topicMap.set(topic, new Set([this])); } else { set.add(this); } } /** * Unsubscribes this connection from `topic`. */ unsubscribe(topic: string) { this._topics.delete(topic); this._server._topicMap.get(topic)?.delete(this); } isSubscribed(topic: string): boolean { return this._topics.has(topic); } /** * Called when the connection needs to be cleaned up and removed and can no longer reconnect. */ _destroy() { for (const topic of this._topics) { this._server._topicMap.get(topic)?.delete(this); } this._topics.clear(); this._ws.close(); this._ackListeners.clear(); this._offlineMessageQueue.length = 0; } /** * Called by the socket handler when the socket closes. */ _disconnect() { this.state = ConnectionState.Disconnected; // Clean up this connection if not reconnected within 10 seconds. if (!this._destroyTimer) { this._destroyTimer = setTimeout(() => this._destroy(), 10000); } } /** * Called by the socket handler when a socket with the same sessionId is established. * Associates this connection with the new websocket object. */ async _reconnect(ws: Bun.ServerWebSocket) { if (this._destroyTimer) { clearTimeout(this._destroyTimer); this._destroyTimer = undefined; } this.state = ConnectionState.Connected; this._ws = ws; // Send unsent acks (client will ignore them if they're not needed) for (const [ackId, message] of this._ackResponses.entries()) { ws.sendBinary(message); } // Sent unsent messages for (const message of this._offlineMessageQueue) { ws.sendBinary(message); } this._offlineMessageQueue.length = 0; } }