import ZmodemSession from './zsession'; import { SentryOpts } from './types'; /** * Class that parses an input stream for the beginning of a * ZMODEM session. We look for the tell-tale signs * of a ZMODEM transfer and allow the client to determine whether * it’s really ZMODEM or not. * * This is the “mother” class for zmodem.js * all other class instances are created, directly or indirectly, * by an instance of this class. * * This logic is not unlikely to need tweaking, and it can never * be fully bulletproof if it could be bulletproof it would be * simpler since there wouldn’t need to be the .confirm()/.deny() * step. * * One thing you could do to make things a bit simpler *is* just * to make that assumption for your users--i.e., to .confirm() * Detection objects automatically. That’ll be one less step * for the user, but an unaccustomed user might find that a bit * confusing. It’s also then possible to have a “false positive”: * a text stream that contains a ZMODEM initialization string but * isn’t, in fact, meant to start a ZMODEM session. * * Workflow: * - parse all input with .consume(). As long as nothing looks * like ZMODEM, all the traffic will go to to_terminal(). * * - when a “tell-tale” sequence of bytes arrives, we create a * Detection object and pass it to the “on_detect” handler. * * - Either .confirm() or .deny() with the Detection object. * This is the user’s chance to say, “yeah, I know those * bytes look like ZMODEM, but they’re not. So back off!” * * If you .confirm(), the Session object is returned, and * further input that goes to the Sentry’s .consume() will * go to the (now-active) Session object. * * - Sometimes additional traffic arrives that makes it apparent * that no ZMODEM session is intended to start in this case, * the Sentry marks the Detection as “stale” and calls the * `on_retract` handler. Any attempt from here to .confirm() * on the Detection object will prompt an exception. * * (This “retraction” behavior will only happen prior to * .confirm() or .deny() being called on the Detection object. * Beyond that point, either the Session has to deal with the * “garbage”, or it’s back to the terminal anyway. * * - Once the Session object is done, the Sentry will again send * all traffic to to_terminal(). */ declare class ZmodemSentry { _to_terminal: Function; _on_detect: Function; _on_retract: Function; _sender: Function; _cache: number[]; _zsession: ZmodemSession | null; _parsed_session: ZmodemSession | null; /** * Invoked directly. Creates a new Sentry that inspects all * traffic before it goes to the terminal. * * @param {Object} options - The Sentry parameters * * @param {Function} options.to_terminal - Handler that sends * traffic to the terminal object. Receives an iterable object * (e.g., an Array) that contains octet numbers. * * @param {Function} options.on_detect - Handler for new * detection events. Receives a new Detection object. * * @param {Function} options.on_retract - Handler for retraction * events. Receives no input. * * @param {Function} options.sender - Handler that sends traffic to * the peer. If, for example, your application uses WebSocket to talk * to the peer, use this to send data to the WebSocket instance. */ constructor(options: SentryOpts); _after_session_end: () => void; /** * “Consumes” a piece of input: * * - If there is no active or pending ZMODEM session, the text is * all output. (This is regardless of whether we’ve got a new * Detection.) * * - If there is no active ZMODEM session and the input **ends** with * a ZRINIT or ZRQINIT, then a new Detection object is created, * and it is passed to the “on_detect” function. * If there was another pending Detection object, it is retracted. * * - If there is no active ZMODEM session and the input does NOT end * with a ZRINIT or ZRQINIT, then any pending Detection object is * retracted. * * - If there is an active ZMODEM session, the input is passed to it. * Any non-ZMODEM data (i.e., “garbage”) parsed from the input * is sent to output. * If the ZMODEM session ends, any post-ZMODEM part of the input * is sent to output. * * @param {number[] | ArrayBuffer} input - Octets to parse as input. */ consume: (input: any) => void; /** * @return {Session|null} The sentry’s current Session object, or * null if there is none. */ get_confirmed_session: () => ZmodemSession | null; _send_abort: () => void; /** * Parse an input stream and decide how much of it goes to the * terminal or to a new Session object. * * This will accommodate input strings that are fragmented * across calls to this function e.g., if you send the first * two bytes at the end of one parse() call then send the rest * at the beginning of the next, parse() will recognize it as * the beginning of a ZMODEM session. * * In order to keep from blocking any actual useful data to the * terminal in real-time, this will send on the initial * ZRINIT/ZRQINIT bytes to the terminal. They’re meant to go to the * terminal anyway, so that should be fine. * * @private * * @param {Array|Uint8Array} arrayLike - The input bytes. * Each member should be a number between 0 and 255 (inclusive). * * @return {Array} A two-member list: * 0) the bytes that should be printed on the terminal * 1) the created Session object (if any) */ _parse: (arrayLike: number[] | Uint8Array) => null | ZmodemSession; } export default ZmodemSentry;