import { Buffer } from 'buffer';
import { TcpSocket } from './tcp-socket.js';
export interface JsonSocketOptions {
    maxBufferSize?: number;
    incompleteMessageTimeout?: number;
    messageReceiveTimeout?: number;
    maxSendBufferSize?: number;
    drainTimeout?: number;
    pauseOnBackpressure?: boolean;
}
export declare class JsonSocket extends TcpSocket {
    private contentLength;
    private buffer;
    private readonly stringDecoder;
    private readonly delimiter;
    private readonly maxBufferSize;
    private readonly incompleteMessageTimeout;
    private readonly messageReceiveTimeout;
    private readonly maxSendBufferSize;
    private readonly drainTimeout;
    private readonly pauseOnBackpressure;
    private incompleteMessageTimer;
    private messageReceiveDeadline;
    private drainTimer;
    private isBackpressured;
    /**
     * Set while the owner of the socket asked for no further messages (see
     * "pauseMessages"). Reading goes on, so that a peer that leaves is noticed,
     * until more than "maxBufferSize" characters are waiting.
     */
    private isHoldingMessages;
    private isReadingPaused;
    private stalledAt;
    private holdTimer;
    private isProcessingBuffer;
    private writtenLength;
    /**
     * Set once the data received could not be framed. Whatever the peer sends
     * after that can no longer be split into packets reliably.
     */
    private hasFramingError;
    private readsSinceFlattened;
    constructor(socket: any, options?: JsonSocketOptions);
    protected handleSend(message: any, callback?: (err?: any) => void): void;
    /**
     * Suspending reads bounds a peer that keeps asking for more, but responses
     * to requests already in flight still queue up behind a peer that reads
     * nothing at all. This caps how much may be held for one connection, which
     * mirrors "maxBufferSize" on the receiving side.
     */
    private assertSendBufferWithinLimit;
    protected handleData(dataRaw: Buffer | string): void;
    /**
     * Appending to a string does not copy it: the engine keeps each appended
     * read as a piece of its own, at up to 32 bytes or so on top of its
     * characters, until the string is read. Nothing reads the buffer while the
     * rest of a packet is awaited, so a packet arriving a few bytes per read
     * would take many times the 1 or 2 bytes per character "maxBufferSize"
     * stands for. Once the reads may weigh as much as the characters, the
     * buffer is copied into a single string. Each copy is paid for by the reads
     * since the previous one, and reads of an ordinary size never cause one.
     */
    private flattenBufferIfFragmented;
    /**
     * The socket is ended after a framing error, but the peer may keep sending
     * until it closes. Parsing that from wherever the failed packet left off
     * could turn an arbitrary part of it into a message, so it is ignored.
     */
    private discardAfterFramingError;
    private processBuffer;
    private processBufferedPackets;
    /**
     * Whether the buffer starts with a packet that "maxBufferSize" allows: one
     * whose header has been read (its declared length was checked then), or
     * one whose header declares a length within the limit. Such a packet is
     * either complete, and taken off the buffer right away, or keeps the
     * buffer within the limit plus one read.
     */
    private startsWithPacketWithinLimit;
    /**
     * Emits no further messages until "resumeMessages" is called. The data
     * read meanwhile is kept, and reading is suspended only once more than
     * "maxBufferSize" characters are waiting. If none of the pending messages
     * settles for "drainTimeout" after that, the peer is dropped: with reading
     * suspended, it would not be noticed if it left.
     */
    pauseMessages(): void;
    resumeMessages(): void;
    /**
     * Whether buffered frames are kept rather than turned into messages, during
     * which the peer is not held to the timeouts of an incomplete packet.
     */
    private get isStalled();
    private stall;
    private unstall;
    /**
     * Suspends reading while the outgoing buffer is backed up, or while more
     * than "maxBufferSize" characters wait for messages to be resumed.
     */
    private updateReading;
    private armHoldTimer;
    private clearHoldTimer;
    private handleHoldTimeout;
    private applyBackpressure;
    private releaseBackpressure;
    /**
     * Arms a timer while reads are suspended, as only the peer, by reading,
     * can bring about the "drain" that resumes them. Whenever it fires, it is
     * armed again if some of the queued data has been written out since, so
     * it only drops a peer once nothing at all has been written out for a
     * full period - a slow but reading peer is given more time.
     */
    private armDrainTimer;
    private clearDrainTimer;
    private handleDrainTimeout;
    /**
     * How much of the data written so far has left the outgoing buffer. Unlike
     * "writableLength", it does not move when more responses are queued, only
     * when a pending write completes, which is as fine-grained as Node reports
     * progress. Sockets do not decode strings, so "writableLength" counts them
     * by length, as "writtenLength" does.
     */
    private getDrainedLength;
    /**
     * Arms a timer whenever a packet is only partially received, and disarms
     * it as soon as the buffer is drained. It fires on whichever comes first:
     * - the peer staying silent mid-packet for "incompleteMessageTimeout", as
     *   the timer is refreshed on every read,
     * - the packet not being complete "messageReceiveTimeout" after it began,
     *   which no amount of reads extends. Otherwise, a peer sending a byte now
     *   and then would pin a partial packet in memory indefinitely.
     */
    private refreshIncompleteMessageTimer;
    private clearIncompleteMessageTimer;
    private handleIncompleteMessageTimeout;
    private handleMessageReceiveTimeout;
    private discardIncompleteMessage;
    private handleMessage;
    private formatMessageData;
}
