@nestjs/microservices
Version:
Microservices for NestJS: message and event handlers over Kafka, RabbitMQ, NATS, Redis, MQTT, gRPC or TCP.
111 lines (110 loc) • 5.46 kB
TypeScript
import type { Type } from '@nestjs/common';
import * as net from 'net';
import { Server as NetSocket, Socket } from 'net';
import { Observable } from 'rxjs';
import { TlsOptions } from 'tls';
import { BaseRpcContext } from '../ctx-host/base-rpc.context.js';
import { TcpContext } from '../ctx-host/tcp.context.js';
import { TcpEvents, TcpStatus } from '../events/tcp.events.js';
import { TcpSocket } from '../helpers/index.js';
import { ReadPacket } from '../interfaces/index.js';
import { TcpOptions, TransportId } from '../interfaces/microservice-configuration.interface.js';
import { Server } from './server.js';
/**
* @publicApi
*/
export declare class ServerTCP extends Server<TcpEvents, TcpStatus> {
private readonly options;
transportId: TransportId;
protected server: NetSocket;
protected readonly port: number;
protected readonly host: string;
protected readonly socketClass: Type<TcpSocket>;
protected readonly maxBufferSize?: number;
protected readonly incompleteMessageTimeout?: number;
protected readonly messageReceiveTimeout?: number;
protected readonly maxSendBufferSize?: number;
protected readonly drainTimeout?: number;
protected readonly maxConcurrentMessages: number;
protected isManuallyTerminated: boolean;
protected retryAttemptsCount: number;
protected retryTimer?: ReturnType<typeof setTimeout>;
protected tlsOptions?: TlsOptions;
protected pendingEventListeners: Array<{
event: keyof TcpEvents;
callback: TcpEvents[keyof TcpEvents];
}>;
/**
* Sockets accepted by this server that are still open. "net.Server#close"
* only stops the server from accepting new connections, so these are tracked
* separately and torn down on "close" - otherwise the process outlives the
* shutdown and handlers keep running on already established connections.
*/
protected readonly openSockets: Set<net.Socket>;
/**
* Notifiers that emit once a socket bound by "bindHandler" can no longer be
* written to. They replay, so that a response stream that only starts after
* the peer has gone ends right away as well.
*/
protected readonly disconnectNotifiers: WeakMap<TcpSocket, Observable<void>>;
/**
* How many messages of each socket are being handled.
*/
protected readonly pendingMessages: WeakMap<TcpSocket, number>;
/**
* What settles the message of a context, until a processing end hook
* runner takes it over (see "createProcessingEndHookRunner").
*/
protected readonly pendingMessageSettlers: WeakMap<BaseRpcContext<unknown[]>, () => void>;
constructor(options: Required<TcpOptions>['options']);
listen(callback: (err?: unknown, ...optionalParams: unknown[]) => void): void;
close(): void;
bindHandler(socket: Socket): void;
handleMessage(socket: TcpSocket, rawMessage: unknown): Promise<any>;
protected dispatchMessage(socket: TcpSocket, packet: ReadPacket, pattern: string, tcpContext: TcpContext): void | Promise<any>;
/**
* Counts a message of "socket" as pending, and asks the socket for no
* further messages while "maxConcurrentMessages" of them are. How many a
* peer sends before any of them is done is up to the peer, and each takes
* up memory for as long as its handler, and the stream it returned, run.
* Returns what to call, once, when the message is no longer pending.
*/
protected trackPendingMessage(socket: TcpSocket): () => void;
/**
* Keeps a message pending from the moment its handler is about to run until
* the handler, and the stream it returned, have settled, which is also when
* the processing end hook runs.
*/
protected createProcessingEndHookRunner(context: BaseRpcContext): () => void;
handleClose(): undefined | number | NodeJS.Timer;
unwrap<T>(): T;
on<EventKey extends keyof TcpEvents = keyof TcpEvents, EventCallback extends TcpEvents[EventKey] = TcpEvents[EventKey]>(event: EventKey, callback: EventCallback): void;
protected init(): void;
protected registerListeningListener(socket: net.Server): void;
protected registerErrorListener(socket: net.Server): void;
protected registerCloseListener(socket: net.Server): void;
/**
* Keeps a reference to an accepted socket so that it can be destroyed when
* the server is closed, and drops it again once it closes on its own.
*/
protected trackOpenSocket(socket: Socket): void;
/**
* Destroys every socket still open. Called on shutdown so that "close" does
* not leave the process alive, and so that no further messages are dispatched
* to handlers over connections established before the shutdown.
*/
protected closeOpenSockets(): void;
/**
* Notes when the socket can no longer be written to: "TcpSocket" drops every
* message once the socket has closed or errored.
*/
protected trackDisconnect(socket: TcpSocket): void;
/**
* Turns a handler result into the response stream. As for HTTP, the stream
* ends once the peer disconnects only if the handler itself returned (or
* resolved to) an Observable: any other handler, and the interceptors
* around it, run to completion.
*/
protected endOnDisconnect(result: unknown, context: TcpContext, socket: TcpSocket): Observable<unknown>;
protected getSocketInstance(socket: Socket): TcpSocket;
}