import { Message, ParseResult } from '../../constants'; import { DeepstreamPlugin, SocketConnectionEndpoint, SocketWrapper, ConnectionListener, DeepstreamServices, DeepstreamConfig, UnauthenticatedSocketWrapper } from '@deepstream/types'; export interface WebSocketServerConfig { outgoingBufferTimeout: number; maxBufferByteSize: number; headers: string[]; healthCheckPath: string; urlPath: string; [index: string]: any; } /** * This is the frontmost class of deepstream's message pipeline. It receives * connections and authentication requests, authenticates sockets and * forwards messages it receives from authenticated sockets. */ export default class BaseWebsocketConnectionEndpoint extends DeepstreamPlugin implements SocketConnectionEndpoint { private options; protected services: DeepstreamServices; protected dsOptions: DeepstreamConfig; description: string; private initialized; private flushTimeout; private authenticatedSocketWrappers; private scheduledSocketWrapperWrites; private logInvalidAuthData; private maxAuthAttempts; private unauthenticatedClientTimeout; private connectionListener; private clientVersions; constructor(options: WebSocketServerConfig, services: DeepstreamServices, dsOptions: DeepstreamConfig); whenReady(): Promise; createWebsocketServer(): void; closeWebsocketServer(): void; onSocketWrapperClosed(socketWrapper: UnauthenticatedSocketWrapper): void; setConnectionListener(connectionListener: ConnectionListener): void; getClientVersions(): { [index: string]: Set; }; /** * Called for every message that's received * from an authenticated socket * * This method will be overridden by an external class and is used instead * of an event emitter to improve the performance of the messaging pipeline */ onMessages(socketWrapper: SocketWrapper, messages: Message[]): void; /** * initialize and setup the http and WebSocket servers. */ init(): void; /** * Called from a socketWrapper. This method tells the connection endpoint * to flush the socket after a certain amount of time, used to low priority * messages */ scheduleFlush(socketWrapper: SocketWrapper): void; /** * Called when the flushTimeout occurs in order to send all pending socket acks */ private flushSockets; protected getOption(option: string): any; handleParseErrors(socketWrapper: SocketWrapper, parseResults: ParseResult[]): Message[]; /** * Receives a connected socket, wraps it in a SocketWrapper, sends a connection ack to the user * and subscribes to authentication messages. */ onConnection(socketWrapper: UnauthenticatedSocketWrapper): void; /** * Always challenges the client that connects. This will be opened up later to allow users * to put in their own challenge authentication. */ processConnectionMessage(socketWrapper: UnauthenticatedSocketWrapper, parsedMessages: Message[]): void; /** * Callback for the first message that's received from the socket. * This is expected to be an auth-message. This method makes sure that's * the case and - if so - forwards it to the permission handler for authentication */ private authenticateConnection; /** * Will be called for syntactically incorrect auth messages. Logs * the message, sends an error to the client and closes the socket */ private sendInvalidAuthMsg; /** * Callback for succesfully validated sockets. Removes * all authentication specific logic and registeres the * socket with the authenticated sockets */ private registerAuthenticatedSocket; /** * Append connection data to the socket wrapper */ private appendDataToSocketWrapper; /** * Callback for invalid credentials. Will notify the client * of the invalid auth attempt. If the number of invalid attempts * exceed the threshold specified in options.maxAuthAttempts * the client will be notified and the socket destroyed. */ private processInvalidAuth; /** * Callback for connections that have not authenticated succesfully within * the expected timeframe */ private processConnectionTimeout; /** * Callback for the results returned by the permission service */ private processAuthResult; /** * Notifies the (optional) onClientDisconnect method of the permission * that the specified client has disconnected */ onSocketClose(socketWrapper: UnauthenticatedSocketWrapper): void; /** * Closes the ws server connection. The ConnectionEndpoint * will emit a close event once succesfully shut down */ close(): Promise; }