UNPKG

@nestjs/microservices

Version:

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

128 lines (127 loc) • 7 kB
import { Observable, ObservedValueOf, ReplaySubject, Subscription } from 'rxjs'; import { BaseRpcContext } from '../ctx-host/base-rpc.context.js'; import { Transport } from '../enums/index.js'; import { ConsumerDeserializer } from '../interfaces/deserializer.interface.js'; import { ClientOptions, MessageHandler, MicroserviceOptions, MsPattern, ReadPacket, WritePacket } from '../interfaces/index.js'; import { ConsumerSerializer } from '../interfaces/serializer.interface.js'; import { ITransportServer, type LoggerService } from '@nestjs/common'; /** * @publicApi */ export declare abstract class Server<EventsMap extends Record<string, Function> = Record<string, Function>, Status extends string = string> implements ITransportServer { /** * Unique transport identifier. */ transportId?: Transport | symbol; protected readonly messageHandlers: Map<string, MessageHandler<any, any, any>>; protected readonly logger: LoggerService; /** * Whether this transport hands an event handler failure to its client * library, which then reports it. The base `handleEvent` connects the * stream without subscribing, so nothing observes the failure and Nest logs * it instead. `ServerKafka` awaits the stream and sets this to `true`. */ readonly propagatesEventHandlerErrors: boolean; protected serializer: ConsumerSerializer; protected deserializer: ConsumerDeserializer; protected onProcessingStartHook: (transportId: Transport | symbol, context: BaseRpcContext, done: () => Promise<any>) => void; protected onProcessingEndHook: (transportId: Transport | symbol, context: BaseRpcContext) => void; protected _status$: ReplaySubject<Status>; /** * Returns an observable that emits status changes. */ get status(): Observable<Status>; /** * Registers an event listener for the given event. * @param event Event name * @param callback Callback to be executed when the event is emitted */ abstract on<EventKey extends keyof EventsMap = keyof EventsMap, EventCallback extends EventsMap[EventKey] = EventsMap[EventKey]>(event: EventKey, callback: EventCallback): any; /** * Returns an instance of the underlying server/broker instance, * or a group of servers if there are more than one. */ abstract unwrap<T>(): T; /** * Method called when server is being initialized. * @param callback Function to be called upon initialization */ abstract listen(callback: (...optionalParams: unknown[]) => any): any; /** * Method called when server is being terminated. */ abstract close(): any; /** * Sets the transport identifier. * @param transportId Unique transport identifier. */ setTransportId(transportId: Transport | symbol): void; /** * Sets a hook that will be called when processing starts. */ setOnProcessingStartHook(hook: (transportId: Transport | symbol, context: unknown, done: () => Promise<any>) => void): void; /** * Sets a hook that will be called when processing ends. */ setOnProcessingEndHook(hook: (transportId: Transport | symbol, context: unknown) => void): void; addHandler(pattern: any, callback: MessageHandler, isEventHandler?: boolean, extras?: Record<string, any>): void; getHandlers(): Map<string, MessageHandler>; getHandlerByPattern(pattern: string): MessageHandler | null; send(stream$: Observable<any>, respond: (data: WritePacket) => Promise<unknown> | void): Subscription; handleEvent(pattern: string, packet: ReadPacket, context: BaseRpcContext): Promise<any>; /** * Handles a request-response message. `produce` returns the response * stream, whose values are replied through `respond`, and the processing * end hook runs once that stream settles (or once `produce` rejects). */ protected handleRequest(context: BaseRpcContext, produce: () => Promise<Observable<any>>, respond: (data: WritePacket) => Promise<unknown> | void): void; /** * Runs `fn` between the processing start and end hooks. * * `fn` receives a runner that closes the span exactly once and attaches it * to the teardown of whatever stream it produces. If `fn` rejects before * that teardown can run, the span is closed here and the rejection travels * on to the transport's client library. * * `endHookContext` is for transports that report a different context to * the end hook than to the start hook (gRPC passes the request). */ protected runWithProcessingHooks(context: BaseRpcContext, fn: (runEndHook: () => void) => Promise<void>, endHookContext?: BaseRpcContext): void; /** * Returns a function that runs the processing end hook exactly once. * * An event handler can fail either by rejecting or by returning a stream * that errors, and `ServerKafka#handleEvent` awaits that stream, so both the * `finalize` teardown and the `catch` block can be reached for a single * event. The hook closes a span, so it must not run twice. */ protected createProcessingEndHookRunner(context: BaseRpcContext): () => void; transformToObservable<T>(resultOrDeferred: Observable<T> | Promise<T>): Observable<T>; transformToObservable<T>(resultOrDeferred: T): never extends Observable<ObservedValueOf<T>> ? Observable<T> : Observable<ObservedValueOf<T>>; getOptionsProp<Options extends MicroserviceOptions['options'], Attribute extends keyof Options>(obj: Options, prop: Attribute): Options[Attribute]; getOptionsProp<Options extends MicroserviceOptions['options'], Attribute extends keyof Options, DefaultValue extends Options[Attribute] = Options[Attribute]>(obj: Options, prop: Attribute, defaultValue: DefaultValue): Required<Options>[Attribute]; protected handleError(error: string): void; protected loadPackage<T = any>(name: string, ctx: string, loader?: () => T): T | Promise<T>; protected loadPackageSynchronously<T = any>(name: string, ctx: string, loader?: () => T): T; protected initializeSerializer(options: ClientOptions['options']): void; protected initializeDeserializer(options: ClientOptions['options']): void; /** * Transforms the server Pattern to valid type and returns a route for him. * * @param {string} pattern - server pattern * @returns string */ protected getRouteFromPattern(pattern: string): string; protected normalizePattern(pattern: MsPattern): string; /** * Returns the string representation of an incoming message pattern. * * Patterns are client-controlled: serializing a deeply nested one makes * `JSON.stringify` throw a `RangeError`, which must not escape the message * handler (as an unhandled promise rejection that terminates the process). * * @param {unknown} pattern - client pattern * @returns string */ protected getPatternAsString(pattern: unknown): string; }