@nestjs/microservices
Version:
Microservices for NestJS: message and event handlers over Kafka, RabbitMQ, NATS, Redis, MQTT, gRPC or TCP.
116 lines (115 loc) • 5.43 kB
TypeScript
import { Observable } from 'rxjs';
import { KafkaStatus } from '../events/index.js';
import { BrokersFunction, Consumer, ConsumerGroupJoinEvent, EachMessagePayload, Kafka, Producer, TopicPartitionOffsetAndMetadata } from '../external/kafka.interface.js';
import { KafkaParser } from '../helpers/index.js';
import { ClientKafkaProxy, KafkaOptions, OutgoingEvent, PacketMetadata, ReadPacket, WritePacket } from '../interfaces/index.js';
import { ClientProxy } from './client-proxy.js';
import { Logger } from '@nestjs/common';
/**
* The consumer and producer created by a single `connect()` attempt, kept so a
* failed attempt can disconnect its own instances even after a newer attempt
* has replaced them on the client.
*/
interface KafkaConnectionAttempt {
consumer?: Consumer;
producer?: Producer;
}
/**
* @publicApi
*/
export declare class ClientKafka extends ClientProxy<never, KafkaStatus> implements ClientKafkaProxy {
protected readonly options: Required<KafkaOptions>['options'];
protected logger: Logger;
protected client: Kafka | null;
protected parser: KafkaParser | null;
protected initialized: Promise<void> | null;
/**
* The attempt allowed to keep writing the client's state, or null once
* `close()` has taken the client over. Every step of a connection attempt
* checks it after an await, so an attempt that outlives `close()` stops
* instead of reading the fields `close()` has already cleared.
*/
protected connectionAttempt: KafkaConnectionAttempt | null;
protected responsePatterns: string[];
protected consumerAssignments: {
[key: string]: number;
};
protected readonly pendingRequestPartitions: Map<string, {
replyTopic: string;
replyPartition: number;
}>;
protected brokers: string[] | BrokersFunction;
protected clientId: string;
protected groupId: string;
protected producerOnlyMode: boolean;
protected _consumer: Consumer | null;
protected _producer: Producer | null;
get consumer(): Consumer;
get producer(): Producer;
constructor(options: Required<KafkaOptions>['options']);
subscribeToResponseOf(pattern: unknown): void;
close(): Promise<void>;
handleClose(): void;
connect(): Promise<Producer>;
/**
* Tears down whatever a failed connection attempt managed to create, so a
* consumer that joined its group before the producer failed does not stay
* behind when the next attempt creates a new one. The client's state is only
* reset if neither `close()` nor a newer attempt has taken it over in the
* meantime (e.g., `close()` followed by `connect()` while the failed attempt
* was still pending).
*/
private discardPartialConnection;
/**
* Stops an attempt once `close()` or a newer `connect()` has taken the
* client over. Without it an attempt that outlives `close()` keeps going:
* it reads `client`, `_consumer` or `_producer` after the await during which
* `close()` cleared them, and rejects with a `TypeError` long after
* `close()` returned.
*/
private assertConnectionIsActive;
private initializeClientAndConnections;
bindTopics(): Promise<void>;
createClient<T = any>(): Promise<T>;
createResponseCallback(): (payload: EachMessagePayload) => any;
getConsumerAssignments(): {
[key: string]: number;
};
emitBatch<TResult = any, TInput = any>(pattern: any, data: {
messages: TInput[];
}): Observable<TResult>;
commitOffsets(topicPartitions: TopicPartitionOffsetAndMetadata[]): Promise<void>;
unwrap<T>(): T;
on<EventKey extends string | number | symbol = string | number | symbol, EventCallback = any>(event: EventKey, callback: EventCallback): void;
protected registerConsumerEventListeners(): void;
protected registerProducerEventListeners(): void;
protected dispatchBatchEvent<TInput = any>(packets: ReadPacket<{
messages: TInput[];
}>): Promise<any>;
protected dispatchEvent(packet: OutgoingEvent): Promise<any>;
protected getReplyTopicPartition(topic: string): string;
protected publish(partialPacket: ReadPacket, callback: (packet: WritePacket) => any): () => void;
protected getResponsePatternName(pattern: string): string;
/**
* Adds the metadata a dispatch hook attached to a packet to the headers of
* a serialized message, under every name the message does not set itself.
* Returns a copy, as the message may be the caller's own record.
*/
protected mergeMetadata<T extends {
headers?: Record<string, any>;
}>(message: T, metadata: PacketMetadata | undefined): T;
/**
* Returns a copy of an event's message without the headers that make
* ServerKafka take a message with a correlation id for a request, and
* publish its response to the topic they name. The headers of an event can
* come from the data it forwards (an object with a "value" is sent as the
* message it describes), so they cannot ask for a reply.
*/
protected withoutReplyHeaders<T extends {
headers?: Record<string, any>;
}>(message: T): T;
protected setConsumerAssignments(data: ConsumerGroupJoinEvent): void;
protected initializeSerializer(options: KafkaOptions['options']): void;
protected initializeDeserializer(options: KafkaOptions['options']): void;
}
export {};