kafka-ts
Version:
**KafkaTS** is a Apache Kafka client library for Node.js. It provides both a low-level API for communicating directly with the Apache Kafka cluster and high-level APIs for publishing and subscribing to Kafka topics.
48 lines (47 loc) • 1.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Broker = void 0;
const api_1 = require("./api");
const connection_1 = require("./connection");
class Broker {
options;
connection;
sendRequest;
constructor(options) {
this.options = options;
this.connection = new connection_1.Connection({
clientId: this.options.clientId,
connection: this.options.options,
ssl: this.options.ssl,
requestTimeout: this.options.requestTimeout,
});
this.sendRequest = this.connection.sendRequest.bind(this.connection);
}
async connect() {
if (!this.connection.isConnected()) {
await this.connection.connect();
await this.fetchApiVersions();
await this.saslHandshake();
await this.saslAuthenticate();
}
return this;
}
async disconnect() {
await this.connection.disconnect();
}
async fetchApiVersions() {
const { versions } = await this.sendRequest(api_1.API.API_VERSIONS, {});
const versionsByApiKey = Object.fromEntries(versions.map(({ apiKey, minVersion, maxVersion }) => [apiKey, { minVersion, maxVersion }]));
this.connection.setVersions(versionsByApiKey);
}
async saslHandshake() {
if (!this.options.sasl) {
return;
}
await this.sendRequest(api_1.API.SASL_HANDSHAKE, { mechanism: this.options.sasl.mechanism });
}
async saslAuthenticate() {
await this.options.sasl?.authenticate({ sendRequest: this.sendRequest });
}
}
exports.Broker = Broker;