UNPKG

@vermaysha/routeros

Version:

NodeJS / Bun RouterOS API

181 lines (180 loc) 6.39 kB
import { EventEmitter } from 'node:events'; import { Channel } from './Channel'; export type RStreamCallback<T> = (err: Error | null, packet?: T, stream?: RStream) => void; /** * Stream class is responsible for handling * continuous data from some parts of the * routeros, like /ip/address/listen or * /tool/torch which keeps sending data endlessly. * It is also possible to pause/resume/stop generated * streams. */ export declare class RStream extends EventEmitter { private channel; private params; /** * Callback function to receive data * from the routerboard */ private callback?; /** * The function that will send empty data * unless debounced by real data from the command */ private debounceSendingEmptyData; /** Flag for turning on empty data debouncing */ private shouldDebounceEmptyData; /** * If is streaming flag */ private streaming; /** * If is pausing flag */ private pausing; /** * If is paused flag */ private paused; /** * If is stopping flag */ private stopping; /** * If is stopped flag */ private stopped; /** * If got a trap error */ private trapped; /** * Save the current section of the packet, if has any */ private currentSection; /** * Forcely stop the stream */ private forcelyStop; /** * Store the current section in a single * array before sending when another section comes */ private currentSectionPacket; /** * Waiting timeout before sending received section packets */ private sectionPacketSendingTimeout; /** * Creates a new stream to be sent to the routerboard * * @param channel - The channel which will be used to send the command * @param params - The command parameters to be sent * @param callback - The callback that will receive the data from the routerboard */ constructor(channel: Channel, params: string[]); /** * Function to receive the callback which * will receive data, if not provided over the * constructor or changed later after the streaming * have started. * * @param {function} callback */ data<T>(callback: RStreamCallback<T>): void; /** * Resumes the stream if it is not currently streaming. * If the stream is stopped or stopping, a rejection error * with "STREAMCLOSD" is returned. Otherwise, it resets the * pausing state, starts the stream, and sets the streaming * flag to true. * * @returns {Promise<void>} - A promise that resolves when the stream * is successfully resumed or rejects if the stream is closed. */ resume(): Promise<void>; /** * Pauses the stream if it is not currently paused or stopping. * If the stream is stopped or stopping, a rejection error * with "STREAMCLOSD" is returned. Otherwise, it sets the * pausing flag to true, stops the stream, and sets the paused * flag to true. * * @returns {Promise<void>} - A promise that resolves when the stream * is successfully paused or rejects if the stream is closed. */ pause(): Promise<void>; /** * Stops the stream if it is not currently stopped or stopping. * If the stream is stopped or stopping, a rejection error * with "STREAMCLOSD" is returned. Otherwise, it sets the * stopping flag to true and sends a /cancel command to the * channel. If the pausing flag is set to true, the stream is * paused and the stopped flag is set to false. * * @param {boolean} [pausing=false] - If true, the stream is paused instead of stopped. * @returns {Promise<void>} - A promise that resolves when the stream * is successfully stopped or rejects if the stream is closed. */ stop(pausing?: boolean): Promise<void>; /** * Close the stream. Calls `stop()` internally * * @returns {Promise<void>} - A promise that resolves when the stream * is successfully closed or rejects if the stream is closed. */ close(): Promise<void>; /** * Start the stream if it is not currently streaming. * If the stream is stopped or stopping, a rejection error * with "STREAMCLOSD" is returned. Otherwise, it resets the * stopping state, starts the stream, and sets the streaming * flag to true. * * @returns {void} */ start(): void; /** * Prepares the debounce mechanism for sending empty data packets * at a determined interval. This is used to ensure that the stream * remains active by invoking the onStream method with an empty * packet when no data is being received. * * It checks for an `=interval=` parameter within the stream's * parameters to determine the debounce interval. If the parameter * is found, the interval is set based on its value, otherwise, * a default interval of 2000 milliseconds is used. The interval * is adjusted by adding 300 milliseconds to it. * * This method sets the `shouldDebounceEmptyData` flag to true * and initializes the `debounceSendingEmptyData` function * using the calculated interval. */ prepareDebounceEmptyData(): void; /** * Called when the stream emits data. If the packet is a section packet, * it collects all packets with the same section name and sends them * to the callback after a 300ms delay. If the packet is not a section * packet, it is sent to the callback immediately. * @param {any} packet * @memberof RStream * @private */ private onStream; /** * Handles the trap event, which is emitted when the stream is interrupted * or when an error occurs. If the trap is due to an interruption, it sets * the `streaming` flag to false. If the trap is due to an error, it sets * the `stopped` and `trapped` flags to true, calls the callback with the * error if it is defined, and emits an "error" event with the error. * @private * @param {any} data */ private onTrap; /** * Handles the "done" event. If the stream is stopped and the channel is * defined, it closes the channel with the force flag set to true. * @private */ private onDone; }