UNPKG

@vermaysha/routeros

Version:

NodeJS / Bun RouterOS API

95 lines (94 loc) 3.99 kB
import { EventEmitter } from "node:events"; import { Connector } from "./Connector"; import type { IRosGenericResponse } from "./IRosGenericResponse"; /** * Channel class is responsible for generating * ids for the channels and writing over * the ids generated, while listening for * their responses */ export declare class Channel extends EventEmitter { readonly connector: Connector; /** * Id of the channel */ readonly id: string; /** * Data received related to the channel */ private data; /** * If received a trap instead of a positive response */ private trapped; /** * If is streaming content */ private streaming; /** * Initializes a new Channel instance, generating a unique identifier * and setting up the connector for communicating with the routerboard. * Listens for unknown events to handle them appropriately. * * @param {Connector} connector - The connector instance to be used for communication. */ constructor(connector: Connector); /** * Writes the provided command parameters to the channel, appending a unique tag. * The function can handle both streaming and non-streaming scenarios and returns * a promise that resolves when the operation is complete. * * @param {string[]} params - The command parameters to send to the routerboard. * @param {boolean} [isStream=false] - Indicates if the channel is in streaming mode. * @param {boolean} [returnPromise=true] - If true, returns a promise that resolves * when the operation is done or rejects if a trap is encountered. * @returns {Promise<IRosGenericResponse[]>} - A promise that resolves with the response data * or rejects with an error message if a trap is received. */ write(params: string[], isStream?: boolean, returnPromise?: boolean): Promise<IRosGenericResponse[]>; /** * Closes the channel, optionally forcing closure and removing all listeners. * Emits a "close" event and stops reading for the current channel tag. * * @param {boolean} [force=false] - If true, all listeners are removed even if streaming. */ close(force?: boolean): void; /** * Initiates a read operation for the current channel's tag and writes * the provided parameters to the connector. The read operation sets up * a callback to process packets received for the channel, while the * write operation sends the parameters over the connection. * * @param {string[]} params - The parameters to be written to the connector. */ private readAndWrite; /** * Process a packet received from the connector, emitting "data" events if the packet is not a * stream packet and the channel is not streaming. If the packet is a stream packet, emits a * "stream" event. If the packet is a "!done" packet, emits a "done" event with the collected data * and closes the channel. If the packet is a "!trap" packet, sets the channel to a "trapped" * state and emits a "trap" event. If the packet is any other type of packet, emits an "unknown" * event and closes the channel. * * @private * @param {string[]} packet - The packet to be processed. */ private processPacket; /** * Takes a packet and parses it into a key-value object. * It works by splitting each line by "=" and using the first part as the key * and the second part as the value. It ignores empty lines and lines with no * "=" character. * * @private * @param {string[]} packet - The packet to be parsed. * @returns {Record<string, string>} - The parsed packet as a key-value object. */ private parsePacket; /** * Emits an error if the channel receives an unknown reply type. * @private * @param {string} reply - The reply type received from the routerboard. */ private onUnknown; }