UNPKG

@vermaysha/routeros

Version:

NodeJS / Bun RouterOS API

138 lines (137 loc) 3.86 kB
import { EventEmitter } from 'node:events'; import { IRosOptions } from './IRosOptions'; /** * Connector class responsible for communicating with * the routeros via api, sending and receiving buffers. * * The main focus of this class is to be able to * construct and destruct dinamically by the RouterOSAPI class * when needed, so the authentication parameters don't * need to be changed every time we need to reconnect. */ export declare class Connector extends EventEmitter { /** * The host or address of where to connect to */ host: string; /** * The port of the API */ port: number; /** * The timeout in seconds of the connection */ timeout: number; /** * The socket of the connection */ private socket?; /** * The transmitter object to write commands */ private transmitter?; /** * The receiver object to read commands */ private receiver?; /** * Connected status */ private connected; /** * Connecting status */ private connecting; /** * Closing status */ private closing; /** * TLS data */ private tls; /** * Creates a new Connector object with the given options * * @param {IRosOptions} options - connection options * @param {string} options.host - The host to connect to * @param {number} [options.port=8728] - The port of the API * @param {number} [options.timeout=10] - The timeout of the connection * @param {TLSSocketOptions} [options.tls=null] - TLS Options to use, if any */ constructor(options: IRosOptions); /** * Establishes a connection to the routerboard. If the connection is already * established or a connection is in progress, the function does nothing. * * @returns {this} - The current Connector instance */ connect(): this; /** * Writes the provided command parameters to the connector, appending a newline * after each command and at the end of the list. * * @param {string[]} data - The command parameters to send to the routerboard. * @returns {this} - The current Connector instance */ write(data: string[]): Connector; /** * Register a tag to receive data * * @param tag - The tag to register * @param callback - The callback function to handle the received packet */ read(tag: string, callback: (packet: string[]) => void): void; /** * Unregister a tag, so it no longer waits for data * @param tag - The tag to unregister */ stopRead(tag: string): void; /** * Start closing the connection * Ensures the connection is properly closed */ close(): void; /** * Destroy the socket, preventing any further data exchange. * This method also removes all event listeners. */ destroy(): void; /** * Socket connection event listener. * After the connection is stablished, * ask the transmitter to run any * command stored over the pool * * @returns {(this: Connector) => void} */ private onConnect; /** * Socket end event listener. * Emits a "close" event and destroys the socket, * ensuring that the connection is properly terminated. */ private onEnd; /** * Socket error event listener. * Emmits the error while trying to connect and * destroys the socket. * * @returns {function} */ private onError; /** * Socket timeout event listener * Emmits timeout error and destroys the socket * * @returns {function} */ private onTimeout; /** * Socket data event listener * Receives the data and sends it to processing * * @returns {function} */ private onData; }