UNPKG

@nestjs/microservices

Version:

Nest - modern, fast, powerful node.js web framework (@microservices)

67 lines (66 loc) • 2.78 kB
import { Buffer } from 'buffer'; import { TcpSocket } from './tcp-socket.js'; export interface JsonSocketOptions { maxBufferSize?: number; incompleteMessageTimeout?: 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 maxSendBufferSize; private readonly drainTimeout; private readonly pauseOnBackpressure; private incompleteMessageTimer; private drainTimer; private isBackpressured; private writtenLength; 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; private processBuffer; 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 an idle timer whenever a packet is only partially received, and * disarms it as soon as the buffer is drained. The timer is refreshed on * every read, so it only fires when the peer stops sending mid-packet - * a slow but progressing transfer is never interrupted. */ private refreshIncompleteMessageTimer; private clearIncompleteMessageTimer; private handleIncompleteMessageTimeout; private handleMessage; private formatMessageData; }