@betit/orion-node-sdk
Version:
SDK for orion
127 lines • 4.47 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const kafkajs_1 = require("kafkajs");
/**
* Kafka transport.
*/
class KafkaTransport {
/**
* Create new Kafka transport.
*/
constructor(_options) {
this._options = _options;
this.handlers = {};
this._options = Object.assign({
url: process.env.KAFKA_HOST || 'localhost:9092',
timeout: process.env.KAFKA_SOCKET_TIMEOUT_MS || 1000,
groupId: process.env.KAFKA_GROUP_ID || 'default-node',
// not used at the moment
offset: process.env.KAFKA_OFFSET || 'earliest',
producerPartition: process.env.KAFKA_PRODUCER_PARTITION || 0,
topicPartition: process.env.KAFKA_TOPIC_PARTITION || 5,
replicationFactor: process.env.KAFKA_TOPIC_REPLICATION_FACTOR || 1,
}, _options);
this.client = new kafkajs_1.Kafka({ brokers: [this._options.url] });
this.producer = new kafkajs_1.Kafka({ brokers: [this._options.url] }).producer();
this.consumer = new kafkajs_1.Kafka({ brokers: [this._options.url] }).consumer({ groupId: this._options.groupId });
}
/**
* Transport listen.
*/
listen(callback) {
return __awaiter(this, void 0, void 0, function* () {
yield this.createTopics();
yield this.consumer.connect();
for (let topic in this.handlers) {
yield this.consumer.subscribe({ topic });
}
yield this.consumer.run({
eachMessage: ({ topic, message }) => __awaiter(this, void 0, void 0, function* () {
this.handlers[topic](message.value);
}),
});
yield this.producer.connect();
if (callback) {
callback();
}
});
}
/**
* Publish to a topic.
*/
publish(topic, message) {
topic = this.normalizeTopic(topic);
return this.producer
.send({
topic,
messages: [{
value: message,
partition: this._options.producerPartition,
}],
});
}
/**
* Subscribe to a topic.
*/
subscribe(topic, group, handle) {
topic = this.normalizeTopic(topic);
this.handlers[topic] = handle;
}
/**
* Transport handle.
*/
handle(route, group, callback) {
console.error('kafka rpc is not implemented');
}
request(route, payload, callback, timeout = 200) {
console.error('kafka rpc is not implemented');
}
/**
* Close connection.
*/
close(err) {
return __awaiter(this, void 0, void 0, function* () {
yield this.producer.disconnect();
yield this.consumer.disconnect();
if (this.closeHandler) {
this.closeHandler();
}
});
}
/**
* Connection closed handler
* @param {(...args: any[]) => void} callback
*/
onClose(callback) {
if (callback) {
this.closeHandler = callback;
}
}
normalizeTopic(topic) {
return topic.replace(':', '_');
}
createTopics() {
return __awaiter(this, void 0, void 0, function* () {
let topics = [];
for (let topic in this.handlers) {
topics.push({
topic,
numPartitions: this._options.topicPartition,
replicationFactor: this._options.replicationFactor,
});
}
return this.client.admin().createTopics({ topics });
});
}
}
exports.KafkaTransport = KafkaTransport;
//# sourceMappingURL=kafka.js.map