@fibercom/routeros-api
Version:
Robust MikroTik RouterOS API client for Node.js and TypeScript
64 lines (63 loc) • 1.83 kB
TypeScript
import { Socket } from "net";
export interface ISentence {
sentence: string;
hadMore: boolean;
}
/**
* Represents a callback associated with a tag, invoked when data for the tag is received.
*/
export interface IReadCallback {
name: string;
callback: (data: string[]) => void;
}
/**
* Responsible for decoding and routing raw RouterOS API socket data
* to the appropriate tagged command listeners.
*/
export declare class Receiver {
private socket;
private tags;
private dataLength;
private sentencePipe;
private processingSentencePipe;
private currentLine;
private currentReply;
private currentTag;
private currentPacket;
private lengthDescriptorSegment;
constructor(socket: Socket);
/**
* Register a callback for a given tag. Each command gets its own tag.
*/
read(tag: string, callback: (packet: string[]) => void): void;
/**
* Unregister the callback for a tag once command completes (!done).
*/
stop(tag: string): void;
/**
* Handle incoming socket data, decode lines based on RouterOS framing protocol.
* Convert win1252-encoded text to UTF-8.
*/
processRawData(data: Buffer): void;
/**
* Process each complete sentence by dispatching to the appropriate tag.
*/
private processSentence;
/**
* Dispatch current packet to the registered tag callback.
*/
private sendTagData;
/**
* Reset tag state so next packet is not mixed.
*/
private cleanUp;
/**
* Decode RouterOS variable-length prefix for sentence lengths.
*
* Supports multi-byte formats depending on the highest bit set.
*
* @param {Buffer} data
* @returns {[number, number]} - [lengthDescriptorSize, sentenceLength]
*/
private decodeLength;
}