import { Environment, ProcessedDatabucket, Route, RouteResponse, ServerEvents } from '@mockoon/commons'; import TypedEventEmitter from 'typed-emitter'; import { RawData, WebSocket, WebSocketServer } from 'ws'; import { ServerRequest } from '../requests'; export declare const SMALLEST_POSSIBLE_STREAMING_INTERVAL = 10; export type DelegatedTemplateParser = (content: string) => string; export type DelegatedBroadcastHandler = (responseNumber: number, enabledRouteResponse: RouteResponse) => void; export type ServerContext = { environment: Environment; processedDatabuckets: ProcessedDatabucket[]; globalVariables: Record; envVarPrefix: string; publicBaseUrl?: string; }; /** * Represents a WebSocket server instance with its route information */ export type WebSocketServerInstance = { instance: WebSocketServer; path: string; routeUuid: string; }; /** * Returns a safe streaming interval for one-to-one and broadcast websockets. * Having a small interval could make server potentially unusable. */ export declare const getSafeStreamingInterval: (givenInterval: number) => number; /** * Represents a single running streaming data for a route. * For all socket clients, there will be only one single instance. */ export declare class WsRunningInstance { private route; private serverContext; private serverRequest; private handler; private closeable; private isRunning; constructor(route: Route, serverContext: ServerContext, serverRequest: ServerRequest, handler: DelegatedBroadcastHandler); get running(): boolean; run(): void; close(): void; } /** * Context for all websocket broadcast end points. * This class holds all streaming data generators per route. * This guarantees that it creates one and only generator for a given route. */ export declare class BroadcastContext { private static context; private readonly routeDataGenerators; get runningInstances(): Set; static getInstance(): BroadcastContext; registerRoute(route: Route, serverContext: ServerContext, serverRequest: ServerRequest, nextResponseHandler: DelegatedBroadcastHandler): boolean; /** * This will close all running contexts. */ closeAll(): void; closeRoute(route: Route): void; } /** * Convert incoming websocket message to string representation. * * @param message */ export declare const messageToString: (message?: RawData) => string; /** * Returns true if the given socket client is still in open state. * * @param socket */ export declare const isWebSocketOpen: (socket: WebSocket) => boolean; /** * Serve the content of the file as a response. * Will use templating if specified to do so. * * @param socket * @param route * @param routeResponse * @param eventEmitter * @param filePath * @param templateParser */ export declare const serveFileContentInWs: (socket: WebSocket, route: Route, routeResponse: RouteResponse, eventEmitter: TypedEventEmitter, filePath: string, templateParser: DelegatedTemplateParser) => void;