import { EventEmitter } from "node:events"; import type { BWSEvents, IClientWSOptions } from "./Types"; import { StateMachine } from "snowtransfer"; /** * Helper Class for simplifying the websocket connection to Discord. * @since 0.1.4 */ declare class BetterWs extends EventEmitter { address: string; options: Omit & { encoding?: IClientWSOptions["encoding"] | "other"; }; /** The encoding to send/receive messages to/from the server with. */ encoding: "etf" | "json" | "other"; /** If the messages sent/received are compressed with zlib. */ compress: boolean; /** How much to delay repeated calls to `connect()`. */ connectThrottle: number; /** Tracks the state this WebSocket is in and what operations are allowed. */ sm: StateMachine; /** The raw net.Socket retrieved from upgrading the connection or null if not upgraded/closed. */ private _socket; /** Code received from frame op 8 */ private _lastCloseCode; /** Reason received from frame op 8 */ private _lastCloseReason; /** A promise that resolves when the connection is fully closed or null if not closing the connection if any. */ private _closePromise; private _closeResolver; /** A zlib Inflate instance if messages sent/received are going to be compressed. Auto created on connect. */ private _zlib; /** Stores when the last connection attempt was made, in order to limit reconnection loops. */ private lastConnectAttempt; /** The in-flight upgrade request if currently connecting. Kept so a cancelled connect can abort it. */ private _req; /** * Creates a new lightweight WebSocket. * @param address The http(s):// or ws(s):// URL corresponding to the server to connect to. * @param options Options specific to this WebSocket. */ constructor(address: string, options: Omit & { encoding?: IClientWSOptions["encoding"] | "other"; }); /** * Connects to the server. Does nothing if not fully disconnected. * @since 0.5.0 */ connect(): void; /** * Encodes and writes a message to the server immediately. * @since 0.1.4 * @param data What to send to the server. */ sendMessage(data: any): void; /** * Disconnects from the server. * When connected or half-closed, asks the server for a clean close and resolves once the connection is fully closed. * When waiting to connect or connecting, cancels the connection attempt. Does nothing if already disconnected. * @since 0.1.4 * @param code The close code. * @param reason The reason for closing if any. * @returns A Promise that resolves when the connection is fully closed. */ close(code: number, reason?: string): Promise; /** * Emit an error if anyone listens for them, otherwise degrade to a debug event. * "error" with zero listeners throws by EventEmitter contract, which must never happen inside wire-handling code paths. * @since 0.19.0 */ private _emitError; /** * Method to raw write messages to the server. * @since 0.4.1 * @param packet Buffer containing the message to send. * @param opcode WebSocket spec op code. */ private _write; /** * Handler for when there is data in the socket's Buffer to read. * @since 0.3.1 */ private _onReadable; /** * Transforms/reads raw messages from the server and emits the appropriate event. * @since 0.4.1 * @param opcode WebSocket spec op code. * @param message Buffer of data to transform/read. */ private _processFrame; } export = BetterWs;