UNPKG

@nestjs/microservices

Version:

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

74 lines (73 loc) • 4.36 kB
import { KafkaContext } from '../ctx-host/index.js'; import { KafkaStatus } from '../events/index.js'; import { BrokersFunction, Consumer, EachMessagePayload, Kafka, Message, Producer, RecordMetadata } from '../external/kafka.interface.js'; import { KafkaParser } from '../helpers/index.js'; import { KafkaOptions, MessageHandler, OutgoingResponse, ReadPacket, TransportId } from '../interfaces/index.js'; import { Server } from './server.js'; import { Logger } from '@nestjs/common'; /** * @publicApi */ export declare class ServerKafka extends Server<never, KafkaStatus> { protected readonly options: Required<KafkaOptions>['options']; readonly propagatesEventHandlerErrors = true; transportId: TransportId; protected logger: Logger; protected client: Kafka | null; protected consumer: Consumer | null; protected producer: Producer | null; protected parser: KafkaParser | null; protected brokers: string[] | BrokersFunction; protected clientId: string; protected groupId: string; constructor(options: Required<KafkaOptions>['options']); addHandler(pattern: any, callback: MessageHandler, isEventHandler?: boolean, extras?: Record<string, any>): void; listen(callback: (err?: unknown, ...optionalParams: unknown[]) => void): Promise<void>; close(): Promise<void>; start(callback: () => void): Promise<void>; protected registerConsumerEventListeners(): void; protected registerProducerEventListeners(): void; createClient<T = any>(): Promise<T>; bindEvents(consumer: Consumer): Promise<void>; getHandlerByPattern(pattern: string): MessageHandler | null; private isPatternMatch; getMessageHandler(): (payload: EachMessagePayload) => Promise<any>; getPublisher(replyTopic: string, replyPartition: string, correlationId: string, context: KafkaContext): (data: any) => Promise<RecordMetadata[]>; /** * Handles a message received by the consumer. * * A message with the `kafka_correlationId` and `kafka_replyTopic` headers, * which `ClientKafka#send()` adds, is a request: what its handler responds * with, an error included, is published to the reply topic. Any other * message is handled as an event (see `handleEvent`), also when its * handler is a `@MessagePattern()` one, so that such handlers can consume * topics written by producers that do not add these headers. */ handleMessage(payload: EachMessagePayload): Promise<any>; unwrap<T>(): T; on<EventKey extends string | number | symbol = string | number | symbol, EventCallback = any>(event: EventKey, callback: EventCallback): void; private combineStreamsAndThrowIfRetriable; sendMessage(message: OutgoingResponse, replyTopic: string, replyPartition: string | undefined | null, correlationId: string, context: KafkaContext): Promise<RecordMetadata[]>; assignIsDisposedHeader(outgoingResponse: OutgoingResponse, outgoingMessage: Message): void; assignErrorHeader(outgoingResponse: OutgoingResponse, outgoingMessage: Message): void; assignCorrelationIdHeader(correlationId: string, outgoingMessage: Message): void; assignReplyPartition(replyPartition: string | null | undefined, outgoingMessage: Message): void; /** * Handles a message as an event: one for an `@EventPattern()` handler, or * one without the reply headers (see `handleMessage`). * * The returned promise settles once the stream the handler returns * completes, and kafkajs only then moves on to the next message of the * partition, so a handler whose stream does not complete holds the * partition. A handler that fails (as when a guard or a pipe rejects the * message) rejects into kafkajs, which does not commit the message and * delivers it again: it retries it, and once its retries are exhausted it * restarts the consumer from the last committed offset. Handlers of topics * that other producers write to should complete their streams, and should * not fail on messages they cannot process unless these are to be * delivered again. */ handleEvent(pattern: string, packet: ReadPacket, context: KafkaContext): Promise<any>; protected initializeSerializer(options: KafkaOptions['options']): void; protected initializeDeserializer(options: KafkaOptions['options']): void; }