UNPKG

@nestjs/microservices

Version:

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

83 lines (82 loc) • 3.94 kB
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 { TcpContext } from '../ctx-host/tcp.context.js'; import { TcpEvents, TcpStatus } from '../events/tcp.events.js'; import { TcpSocket } from '../helpers/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 maxSendBufferSize?: number; protected readonly drainTimeout?: 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>>; 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>; 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; }