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.
184 lines (183 loc) • 5.14 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Encoder = void 0;
class Encoder {
buffer;
offset = 0;
constructor(initialCapacity = 512) {
this.buffer = Buffer.allocUnsafe(initialCapacity);
}
ensure(extra) {
const need = this.offset + extra;
if (need <= this.buffer.length)
return;
let cap = this.buffer.length;
while (cap < need)
cap <<= 1;
const n = Buffer.allocUnsafe(cap);
this.buffer.copy(n, 0, 0, this.offset);
this.buffer = n;
}
getBufferLength() {
return this.offset;
}
write(src) {
this.ensure(src.length);
src.copy(this.buffer, this.offset);
this.offset += src.length;
return this;
}
writeEncoder(other) {
this.write(other.buffer.subarray(0, other.offset));
return this;
}
writeInt8(value) {
this.ensure(1);
this.buffer.writeInt8(value, this.offset);
this.offset += 1;
return this;
}
writeInt16(value) {
this.ensure(2);
this.buffer.writeInt16BE(value, this.offset);
this.offset += 2;
return this;
}
writeInt32(value) {
this.ensure(4);
this.buffer.writeInt32BE(value, this.offset);
this.offset += 4;
return this;
}
writeUInt32(value) {
this.ensure(4);
this.buffer.writeUInt32BE(value, this.offset);
this.offset += 4;
return this;
}
writeInt64(value) {
this.ensure(8);
this.buffer.writeBigInt64BE(value, this.offset);
this.offset += 8;
return this;
}
writeUVarInt(value) {
this.ensure(5);
while (value & 0xffffff80) {
this.buffer[this.offset++] = (value & 0x7f) | 0x80;
value >>>= 7;
}
this.buffer[this.offset++] = value & 0x7f;
return this;
}
writeVarInt(value) {
return this.writeUVarInt((value << 1) ^ (value >> 31));
}
writeUVarLong(value) {
this.ensure(10);
while (value >= 0x80n) {
this.buffer[this.offset++] = Number((value & 0x7fn) | 0x80n);
value >>= 7n;
}
this.buffer[this.offset++] = Number(value);
return this;
}
writeVarLong(value) {
return this.writeUVarLong((value << 1n) ^ (value >> 63n));
}
writeString(value) {
if (value === null)
return this.writeInt16(-1);
const buffer = Buffer.from(value, 'utf-8');
this.writeInt16(buffer.length);
this.write(buffer);
return this;
}
writeCompactString(value) {
if (value === null)
return this.writeUVarInt(0);
const b = Buffer.from(value, 'utf-8');
this.writeUVarInt(b.length + 1);
this.write(b);
return this;
}
writeVarIntString(value) {
if (value === null)
return this.writeVarInt(-1);
const b = Buffer.from(value, 'utf-8');
this.writeVarInt(b.length);
this.write(b);
return this;
}
writeUUID(value) {
if (value === null) {
this.ensure(16);
this.buffer.fill(0, this.offset, this.offset + 16);
this.offset += 16;
return this;
}
this.write(Buffer.from(value, 'hex'));
return this;
}
writeBoolean(value) {
return this.writeInt8(value ? 1 : 0);
}
writeArray(arr, callback) {
if (arr === null)
return this.writeInt32(-1);
this.writeInt32(arr.length);
for (const it of arr)
callback(this, it);
return this;
}
writeCompactArray(arr, callback) {
if (arr === null)
return this.writeUVarInt(0);
this.writeUVarInt(arr.length + 1);
for (const it of arr)
callback(this, it);
return this;
}
writeVarIntArray(arr, callback) {
this.writeVarInt(arr.length);
for (const it of arr)
callback(this, it);
return this;
}
writeBytes(value) {
if (value === null) {
this.writeInt32(-1);
return this;
}
this.writeInt32(value.length);
this.write(value);
return this;
}
writeCompactBytes(value) {
if (value === null) {
this.writeUVarInt(0);
return this;
}
this.writeUVarInt(value.length + 1);
this.write(value);
return this;
}
writeTagBuffer(tags) {
if (!tags)
return this.writeUVarInt(0);
const entries = Object.entries(tags)
.map(([k, v]) => [Number(k), v])
.sort(([a], [b]) => a - b);
this.writeUVarInt(entries.length);
for (const [tagId, tagBuffer] of entries) {
this.writeUVarInt(tagId);
this.writeUVarInt(tagBuffer.length);
this.write(tagBuffer);
}
return this;
}
value() {
return this.buffer.subarray(0, this.offset);
}
}
exports.Encoder = Encoder;