kafka-ts 0.0.1-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierrc +7 -0
- package/LICENSE +24 -0
- package/README.md +88 -0
- package/certs/ca.crt +29 -0
- package/certs/ca.key +52 -0
- package/certs/ca.srl +1 -0
- package/certs/kafka.crt +29 -0
- package/certs/kafka.csr +26 -0
- package/certs/kafka.key +52 -0
- package/certs/kafka.keystore.jks +0 -0
- package/certs/kafka.truststore.jks +0 -0
- package/dist/api/api-versions.d.ts +9 -0
- package/dist/api/api-versions.js +24 -0
- package/dist/api/create-topics.d.ts +38 -0
- package/dist/api/create-topics.js +53 -0
- package/dist/api/delete-topics.d.ts +18 -0
- package/dist/api/delete-topics.js +33 -0
- package/dist/api/fetch.d.ts +77 -0
- package/dist/api/fetch.js +106 -0
- package/dist/api/find-coordinator.d.ts +21 -0
- package/dist/api/find-coordinator.js +39 -0
- package/dist/api/heartbeat.d.ts +11 -0
- package/dist/api/heartbeat.js +27 -0
- package/dist/api/index.d.ts +573 -0
- package/dist/api/index.js +164 -0
- package/dist/api/init-producer-id.d.ts +13 -0
- package/dist/api/init-producer-id.js +29 -0
- package/dist/api/join-group.d.ts +34 -0
- package/dist/api/join-group.js +51 -0
- package/dist/api/leave-group.d.ts +19 -0
- package/dist/api/leave-group.js +39 -0
- package/dist/api/list-offsets.d.ts +29 -0
- package/dist/api/list-offsets.js +48 -0
- package/dist/api/metadata.d.ts +40 -0
- package/dist/api/metadata.js +58 -0
- package/dist/api/offset-commit.d.ts +28 -0
- package/dist/api/offset-commit.js +48 -0
- package/dist/api/offset-fetch.d.ts +33 -0
- package/dist/api/offset-fetch.js +57 -0
- package/dist/api/produce.d.ts +53 -0
- package/dist/api/produce.js +129 -0
- package/dist/api/sasl-authenticate.d.ts +11 -0
- package/dist/api/sasl-authenticate.js +23 -0
- package/dist/api/sasl-handshake.d.ts +6 -0
- package/dist/api/sasl-handshake.js +19 -0
- package/dist/api/sync-group.d.ts +24 -0
- package/dist/api/sync-group.js +36 -0
- package/dist/broker.d.ts +29 -0
- package/dist/broker.js +60 -0
- package/dist/client.d.ts +23 -0
- package/dist/client.js +36 -0
- package/dist/cluster.d.ts +24 -0
- package/dist/cluster.js +72 -0
- package/dist/connection.d.ts +25 -0
- package/dist/connection.js +155 -0
- package/dist/consumer/consumer-group.d.ts +36 -0
- package/dist/consumer/consumer-group.js +182 -0
- package/dist/consumer/consumer-metadata.d.ts +7 -0
- package/dist/consumer/consumer-metadata.js +14 -0
- package/dist/consumer/consumer.d.ts +37 -0
- package/dist/consumer/consumer.js +178 -0
- package/dist/consumer/metadata.d.ts +24 -0
- package/dist/consumer/metadata.js +64 -0
- package/dist/consumer/offset-manager.d.ts +22 -0
- package/dist/consumer/offset-manager.js +56 -0
- package/dist/distributors/assignments-to-replicas.d.ts +17 -0
- package/dist/distributors/assignments-to-replicas.js +60 -0
- package/dist/distributors/assignments-to-replicas.test.d.ts +1 -0
- package/dist/distributors/assignments-to-replicas.test.js +40 -0
- package/dist/distributors/messages-to-topic-partition-leaders.d.ts +17 -0
- package/dist/distributors/messages-to-topic-partition-leaders.js +15 -0
- package/dist/distributors/messages-to-topic-partition-leaders.test.d.ts +1 -0
- package/dist/distributors/messages-to-topic-partition-leaders.test.js +30 -0
- package/dist/examples/src/replicator.js +34 -0
- package/dist/examples/src/utils/json.js +5 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +19 -0
- package/dist/metadata.d.ts +24 -0
- package/dist/metadata.js +89 -0
- package/dist/producer/producer.d.ts +19 -0
- package/dist/producer/producer.js +111 -0
- package/dist/request-handler.d.ts +16 -0
- package/dist/request-handler.js +67 -0
- package/dist/request-handler.test.d.ts +1 -0
- package/dist/request-handler.test.js +340 -0
- package/dist/src/api/api-versions.js +18 -0
- package/dist/src/api/create-topics.js +46 -0
- package/dist/src/api/delete-topics.js +26 -0
- package/dist/src/api/fetch.js +95 -0
- package/dist/src/api/find-coordinator.js +34 -0
- package/dist/src/api/heartbeat.js +22 -0
- package/dist/src/api/index.js +38 -0
- package/dist/src/api/init-producer-id.js +24 -0
- package/dist/src/api/join-group.js +48 -0
- package/dist/src/api/leave-group.js +30 -0
- package/dist/src/api/list-offsets.js +39 -0
- package/dist/src/api/metadata.js +47 -0
- package/dist/src/api/offset-commit.js +39 -0
- package/dist/src/api/offset-fetch.js +44 -0
- package/dist/src/api/produce.js +119 -0
- package/dist/src/api/sync-group.js +31 -0
- package/dist/src/broker.js +35 -0
- package/dist/src/connection.js +21 -0
- package/dist/src/consumer/consumer-group.js +131 -0
- package/dist/src/consumer/consumer.js +103 -0
- package/dist/src/consumer/metadata.js +52 -0
- package/dist/src/consumer/offset-manager.js +23 -0
- package/dist/src/index.js +19 -0
- package/dist/src/producer/producer.js +84 -0
- package/dist/src/request-handler.js +57 -0
- package/dist/src/request-handler.test.js +321 -0
- package/dist/src/types.js +2 -0
- package/dist/src/utils/api.js +5 -0
- package/dist/src/utils/decoder.js +161 -0
- package/dist/src/utils/encoder.js +137 -0
- package/dist/src/utils/error.js +10 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.js +2 -0
- package/dist/utils/api.d.ts +9 -0
- package/dist/utils/api.js +5 -0
- package/dist/utils/debug.d.ts +2 -0
- package/dist/utils/debug.js +11 -0
- package/dist/utils/decoder.d.ts +29 -0
- package/dist/utils/decoder.js +147 -0
- package/dist/utils/delay.d.ts +1 -0
- package/dist/utils/delay.js +5 -0
- package/dist/utils/encoder.d.ts +28 -0
- package/dist/utils/encoder.js +122 -0
- package/dist/utils/error.d.ts +11 -0
- package/dist/utils/error.js +27 -0
- package/dist/utils/memo.d.ts +1 -0
- package/dist/utils/memo.js +16 -0
- package/dist/utils/retrier.d.ts +10 -0
- package/dist/utils/retrier.js +22 -0
- package/dist/utils/tracer.d.ts +1 -0
- package/dist/utils/tracer.js +26 -0
- package/docker-compose.yml +104 -0
- package/examples/node_modules/.package-lock.json +22 -0
- package/examples/package-lock.json +30 -0
- package/examples/package.json +14 -0
- package/examples/src/client.ts +9 -0
- package/examples/src/consumer.ts +17 -0
- package/examples/src/create-topic.ts +37 -0
- package/examples/src/producer.ts +24 -0
- package/examples/src/replicator.ts +25 -0
- package/examples/src/utils/json.ts +1 -0
- package/examples/tsconfig.json +7 -0
- package/log4j.properties +95 -0
- package/package.json +17 -0
- package/scripts/generate-certs.sh +24 -0
- package/src/__snapshots__/request-handler.test.ts.snap +1687 -0
- package/src/api/api-versions.ts +21 -0
- package/src/api/create-topics.ts +78 -0
- package/src/api/delete-topics.ts +42 -0
- package/src/api/fetch.ts +143 -0
- package/src/api/find-coordinator.ts +39 -0
- package/src/api/heartbeat.ts +33 -0
- package/src/api/index.ts +164 -0
- package/src/api/init-producer-id.ts +35 -0
- package/src/api/join-group.ts +67 -0
- package/src/api/leave-group.ts +48 -0
- package/src/api/list-offsets.ts +65 -0
- package/src/api/metadata.ts +66 -0
- package/src/api/offset-commit.ts +67 -0
- package/src/api/offset-fetch.ts +74 -0
- package/src/api/produce.ts +173 -0
- package/src/api/sasl-authenticate.ts +21 -0
- package/src/api/sasl-handshake.ts +16 -0
- package/src/api/sync-group.ts +54 -0
- package/src/broker.ts +74 -0
- package/src/client.ts +47 -0
- package/src/cluster.ts +87 -0
- package/src/connection.ts +141 -0
- package/src/consumer/consumer-group.ts +209 -0
- package/src/consumer/consumer-metadata.ts +14 -0
- package/src/consumer/consumer.ts +229 -0
- package/src/consumer/offset-manager.ts +93 -0
- package/src/distributors/assignments-to-replicas.test.ts +43 -0
- package/src/distributors/assignments-to-replicas.ts +85 -0
- package/src/distributors/messages-to-topic-partition-leaders.test.ts +32 -0
- package/src/distributors/messages-to-topic-partition-leaders.ts +19 -0
- package/src/index.ts +3 -0
- package/src/metadata.ts +122 -0
- package/src/producer/producer.ts +132 -0
- package/src/request-handler.test.ts +366 -0
- package/src/types.ts +9 -0
- package/src/utils/api.ts +11 -0
- package/src/utils/debug.ts +9 -0
- package/src/utils/decoder.ts +168 -0
- package/src/utils/delay.ts +1 -0
- package/src/utils/encoder.ts +141 -0
- package/src/utils/error.ts +21 -0
- package/src/utils/memo.ts +12 -0
- package/src/utils/retrier.ts +39 -0
- package/src/utils/tracer.ts +28 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDecoder = void 0;
|
|
4
|
+
const createDecoder = ({ buffer, offset = 0 }) => {
|
|
5
|
+
const readInt8 = () => {
|
|
6
|
+
const value = buffer.readInt8(offset);
|
|
7
|
+
offset += 1;
|
|
8
|
+
return value;
|
|
9
|
+
};
|
|
10
|
+
const readInt16 = () => {
|
|
11
|
+
const value = buffer.readInt16BE(offset);
|
|
12
|
+
offset += 2;
|
|
13
|
+
return value;
|
|
14
|
+
};
|
|
15
|
+
const readInt32 = () => {
|
|
16
|
+
const value = buffer.readInt32BE(offset);
|
|
17
|
+
offset += 4;
|
|
18
|
+
return value;
|
|
19
|
+
};
|
|
20
|
+
const readUInt32 = () => {
|
|
21
|
+
const value = buffer.readUInt32BE(offset);
|
|
22
|
+
offset += 4;
|
|
23
|
+
return value;
|
|
24
|
+
};
|
|
25
|
+
const readInt64 = () => {
|
|
26
|
+
const value = buffer.readBigInt64BE(offset);
|
|
27
|
+
offset += 8;
|
|
28
|
+
return value;
|
|
29
|
+
};
|
|
30
|
+
const readString = () => {
|
|
31
|
+
const length = readInt16();
|
|
32
|
+
if (length === -1) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
const value = buffer.toString('utf-8', offset, offset + length);
|
|
36
|
+
offset += length;
|
|
37
|
+
return value;
|
|
38
|
+
};
|
|
39
|
+
const readCompactString = () => {
|
|
40
|
+
const length = readUVarInt() - 1;
|
|
41
|
+
if (length < 0) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
const value = buffer.toString('utf-8', offset, offset + length);
|
|
45
|
+
offset += length;
|
|
46
|
+
return value;
|
|
47
|
+
};
|
|
48
|
+
const readVarInt = () => {
|
|
49
|
+
let currentByte;
|
|
50
|
+
let result = 0;
|
|
51
|
+
let i = 0;
|
|
52
|
+
while (((currentByte = buffer[offset++]) & 0x80) !== 0) {
|
|
53
|
+
result |= (currentByte & 0x7f) << i;
|
|
54
|
+
i += 7;
|
|
55
|
+
if (i > 28) {
|
|
56
|
+
throw new Error("Invalid VarInt, must contain 5 bytes or less");
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
result |= currentByte << i;
|
|
60
|
+
return result;
|
|
61
|
+
};
|
|
62
|
+
const readUVarInt = () => {
|
|
63
|
+
const result = readVarInt();
|
|
64
|
+
return result >>> 0;
|
|
65
|
+
};
|
|
66
|
+
const readVarLong = () => {
|
|
67
|
+
let currentByte;
|
|
68
|
+
let result = BigInt(0);
|
|
69
|
+
let i = 0;
|
|
70
|
+
while (((currentByte = buffer[offset++]) & 0x80) !== 0) {
|
|
71
|
+
result |= BigInt(currentByte & 0x7f) << BigInt(i);
|
|
72
|
+
i += 7;
|
|
73
|
+
if (i > 63) {
|
|
74
|
+
throw new Error("Invalid VarLong, must contain 9 bytes or less");
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
result |= BigInt(currentByte) << BigInt(i);
|
|
78
|
+
return result;
|
|
79
|
+
};
|
|
80
|
+
const readUUID = () => {
|
|
81
|
+
const value = buffer.toString("hex", offset, offset + 16);
|
|
82
|
+
offset += 16;
|
|
83
|
+
return value;
|
|
84
|
+
};
|
|
85
|
+
const readBoolean = () => {
|
|
86
|
+
const value = buffer.readInt8(offset) === 1;
|
|
87
|
+
offset += 1;
|
|
88
|
+
return value;
|
|
89
|
+
};
|
|
90
|
+
const readArray = (callback) => {
|
|
91
|
+
const length = readInt32();
|
|
92
|
+
const decoder = (0, exports.createDecoder)({ buffer, offset });
|
|
93
|
+
const results = Array.from({ length }).map(() => callback(decoder));
|
|
94
|
+
offset = decoder.offset;
|
|
95
|
+
return results;
|
|
96
|
+
};
|
|
97
|
+
const readCompactArray = (callback) => {
|
|
98
|
+
const length = readUVarInt() - 1;
|
|
99
|
+
const decoder = (0, exports.createDecoder)({ buffer, offset });
|
|
100
|
+
const results = Array.from({ length }).map(() => callback(decoder));
|
|
101
|
+
offset = decoder.offset;
|
|
102
|
+
return results;
|
|
103
|
+
};
|
|
104
|
+
const readRecords = (callback) => {
|
|
105
|
+
const length = readInt32();
|
|
106
|
+
return Array.from({ length }).map(() => {
|
|
107
|
+
const size = Math.ceil((readVarInt() - 1) / 2);
|
|
108
|
+
const child = (0, exports.createDecoder)({ buffer: buffer.subarray(offset, offset + size) });
|
|
109
|
+
offset += size;
|
|
110
|
+
return callback(child);
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
const read = (length) => {
|
|
114
|
+
const value = buffer.subarray(offset, offset + length);
|
|
115
|
+
offset += length;
|
|
116
|
+
return value;
|
|
117
|
+
};
|
|
118
|
+
const readBytes = () => {
|
|
119
|
+
const length = readInt32();
|
|
120
|
+
return read(length);
|
|
121
|
+
};
|
|
122
|
+
const readCompactBytes = (divide = false) => {
|
|
123
|
+
let length = readUVarInt() - 1;
|
|
124
|
+
if (length < 0) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
if (divide) {
|
|
128
|
+
length = Math.round(length / 2);
|
|
129
|
+
}
|
|
130
|
+
return read(length);
|
|
131
|
+
};
|
|
132
|
+
const readTagBuffer = () => {
|
|
133
|
+
readUVarInt();
|
|
134
|
+
};
|
|
135
|
+
return {
|
|
136
|
+
readInt8,
|
|
137
|
+
readInt16,
|
|
138
|
+
readInt32,
|
|
139
|
+
readInt64,
|
|
140
|
+
readUInt32,
|
|
141
|
+
readVarInt,
|
|
142
|
+
readUVarInt,
|
|
143
|
+
readVarLong,
|
|
144
|
+
readString,
|
|
145
|
+
readBytes,
|
|
146
|
+
readCompactString,
|
|
147
|
+
readUUID,
|
|
148
|
+
readBoolean,
|
|
149
|
+
readArray,
|
|
150
|
+
readCompactArray,
|
|
151
|
+
read,
|
|
152
|
+
readCompactBytes,
|
|
153
|
+
readTagBuffer,
|
|
154
|
+
readRecords,
|
|
155
|
+
buffer,
|
|
156
|
+
get offset() {
|
|
157
|
+
return offset;
|
|
158
|
+
},
|
|
159
|
+
};
|
|
160
|
+
};
|
|
161
|
+
exports.createDecoder = createDecoder;
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createEncoder = void 0;
|
|
4
|
+
const createEncoder = ({ buffer = Buffer.alloc(0) } = {}) => {
|
|
5
|
+
const write = (rightBuffer) => (0, exports.createEncoder)({ buffer: Buffer.concat([buffer, rightBuffer]) });
|
|
6
|
+
const writeInt8 = (value) => {
|
|
7
|
+
const buffer = Buffer.alloc(1);
|
|
8
|
+
buffer.writeInt8(value);
|
|
9
|
+
return write(buffer);
|
|
10
|
+
};
|
|
11
|
+
const writeInt16 = (value) => {
|
|
12
|
+
const buffer = Buffer.alloc(2);
|
|
13
|
+
buffer.writeInt16BE(value);
|
|
14
|
+
return write(buffer);
|
|
15
|
+
};
|
|
16
|
+
const writeInt32 = (value) => {
|
|
17
|
+
const buffer = Buffer.alloc(4);
|
|
18
|
+
buffer.writeInt32BE(value);
|
|
19
|
+
return write(buffer);
|
|
20
|
+
};
|
|
21
|
+
const writeUInt32 = (value) => {
|
|
22
|
+
const buffer = Buffer.alloc(4);
|
|
23
|
+
buffer.writeUInt32BE(value);
|
|
24
|
+
return write(buffer);
|
|
25
|
+
};
|
|
26
|
+
const writeInt64 = (value) => {
|
|
27
|
+
const buffer = Buffer.alloc(8);
|
|
28
|
+
buffer.writeBigInt64BE(value);
|
|
29
|
+
return write(buffer);
|
|
30
|
+
};
|
|
31
|
+
const writeString = (value) => {
|
|
32
|
+
if (value === null) {
|
|
33
|
+
return writeInt16(-1);
|
|
34
|
+
}
|
|
35
|
+
const byteLength = Buffer.byteLength(value, "utf-8");
|
|
36
|
+
const buffer = Buffer.alloc(2 + byteLength);
|
|
37
|
+
buffer.writeInt16BE(byteLength);
|
|
38
|
+
buffer.write(value, 2, byteLength, "utf-8");
|
|
39
|
+
return write(buffer);
|
|
40
|
+
};
|
|
41
|
+
const writeUVarInt = (value) => {
|
|
42
|
+
const byteArray = [];
|
|
43
|
+
while ((value & 0xffffffff) !== 0) {
|
|
44
|
+
byteArray.push((value & 0x7f) | 0x80);
|
|
45
|
+
value >>>= 7;
|
|
46
|
+
}
|
|
47
|
+
byteArray.push(value & 0x7f);
|
|
48
|
+
return write(Buffer.from(byteArray));
|
|
49
|
+
};
|
|
50
|
+
const writeVarInt = (value) => {
|
|
51
|
+
const encodedValue = (value << 1) ^ (value >> 31);
|
|
52
|
+
return writeUVarInt(encodedValue);
|
|
53
|
+
};
|
|
54
|
+
const writeUVarLong = (value) => {
|
|
55
|
+
const byteArray = [];
|
|
56
|
+
while ((value & BigInt(0xffffffffffffffff)) !== BigInt(0)) {
|
|
57
|
+
byteArray.push(Number(value & BigInt(0x7f) | BigInt(0x80)));
|
|
58
|
+
value = value >> BigInt(7);
|
|
59
|
+
}
|
|
60
|
+
byteArray.push(Number(value));
|
|
61
|
+
return write(Buffer.from(byteArray));
|
|
62
|
+
};
|
|
63
|
+
const writeVarLong = (value) => {
|
|
64
|
+
const encodedValue = (value << BigInt(1)) ^ (value >> BigInt(63));
|
|
65
|
+
return writeUVarLong(encodedValue);
|
|
66
|
+
};
|
|
67
|
+
// const writeVarLong = (value: bigint) => {
|
|
68
|
+
// const encodedValue = (value << BigInt(1)) ^ (value >> BigInt(63));
|
|
69
|
+
// return writeUVarInt(Number(encodedValue));
|
|
70
|
+
// };
|
|
71
|
+
const writeCompactString = (value) => {
|
|
72
|
+
if (value === null) {
|
|
73
|
+
return writeUVarInt(0);
|
|
74
|
+
}
|
|
75
|
+
const byteLength = Buffer.byteLength(value, "utf-8");
|
|
76
|
+
const buffer = Buffer.alloc(byteLength);
|
|
77
|
+
buffer.write(value, 0, byteLength, "utf-8");
|
|
78
|
+
return writeUVarInt(byteLength + 1).write(buffer);
|
|
79
|
+
};
|
|
80
|
+
const writeVarIntString = (value) => {
|
|
81
|
+
if (value === null) {
|
|
82
|
+
return writeVarInt(-1);
|
|
83
|
+
}
|
|
84
|
+
return writeVarInt(Buffer.byteLength(value, 'utf-8')).write(Buffer.from(value, 'utf-8'));
|
|
85
|
+
};
|
|
86
|
+
const writeUUID = (value) => {
|
|
87
|
+
if (value === null) {
|
|
88
|
+
return write(Buffer.alloc(16));
|
|
89
|
+
}
|
|
90
|
+
return write(Buffer.from(value, "hex"));
|
|
91
|
+
};
|
|
92
|
+
const writeCompactArray = (arr, callback) => {
|
|
93
|
+
if (arr === null) {
|
|
94
|
+
return writeUVarInt(0);
|
|
95
|
+
}
|
|
96
|
+
const buffers = arr.map((item) => callback((0, exports.createEncoder)(), item).value());
|
|
97
|
+
return writeUVarInt(buffers.length + 1).write(Buffer.concat(buffers));
|
|
98
|
+
};
|
|
99
|
+
const writeArray = (arr, callback) => {
|
|
100
|
+
const buffers = arr.map((item) => callback((0, exports.createEncoder)(), item).value());
|
|
101
|
+
return writeInt32(arr.length).write(Buffer.concat(buffers));
|
|
102
|
+
};
|
|
103
|
+
const writeBoolean = (value) => {
|
|
104
|
+
const buffer = Buffer.alloc(1);
|
|
105
|
+
buffer.writeInt8(value ? 1 : 0);
|
|
106
|
+
return write(buffer);
|
|
107
|
+
};
|
|
108
|
+
const writeBytes = (value) => {
|
|
109
|
+
return writeInt32(value.length).write(value);
|
|
110
|
+
};
|
|
111
|
+
const writeCompactBytes = (value) => {
|
|
112
|
+
return writeUVarInt(value.length + 1).write(value);
|
|
113
|
+
};
|
|
114
|
+
const value = () => buffer;
|
|
115
|
+
return {
|
|
116
|
+
write,
|
|
117
|
+
writeInt8,
|
|
118
|
+
writeInt16,
|
|
119
|
+
writeInt32,
|
|
120
|
+
writeUInt32,
|
|
121
|
+
writeInt64,
|
|
122
|
+
writeCompactString,
|
|
123
|
+
writeVarIntString,
|
|
124
|
+
writeString,
|
|
125
|
+
writeUVarInt,
|
|
126
|
+
writeVarInt,
|
|
127
|
+
writeVarLong,
|
|
128
|
+
writeUUID,
|
|
129
|
+
writeCompactArray,
|
|
130
|
+
writeArray,
|
|
131
|
+
writeBoolean,
|
|
132
|
+
writeBytes,
|
|
133
|
+
writeCompactBytes,
|
|
134
|
+
value,
|
|
135
|
+
};
|
|
136
|
+
};
|
|
137
|
+
exports.createEncoder = createEncoder;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KafkaTSError = void 0;
|
|
4
|
+
class KafkaTSError extends Error {
|
|
5
|
+
constructor(message) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.name = "KafkaTSError";
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.KafkaTSError = KafkaTSError;
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Decoder } from "./decoder";
|
|
2
|
+
import { Encoder } from "./encoder";
|
|
3
|
+
export type Api<Request, Response> = {
|
|
4
|
+
apiKey: number;
|
|
5
|
+
apiVersion: number;
|
|
6
|
+
request: (encoder: Encoder, body: Request) => Encoder;
|
|
7
|
+
response: (buffer: Decoder) => Response;
|
|
8
|
+
};
|
|
9
|
+
export declare const createApi: <Request, Response>(api: Api<Request, Response>) => Api<Request, Response>;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createDebugger = exports.serializer = void 0;
|
|
4
|
+
const serializer = (_, value) => (typeof value === "bigint" ? value.toString() : value);
|
|
5
|
+
exports.serializer = serializer;
|
|
6
|
+
const createDebugger = (module) => (func, message, data) => {
|
|
7
|
+
if (!process.env.DEBUG?.includes("kafkats"))
|
|
8
|
+
return;
|
|
9
|
+
console.debug(`[${module}] ${func}: ${message}`, data && `(${data instanceof Error ? data : JSON.stringify(data, exports.serializer, 4)})`);
|
|
10
|
+
};
|
|
11
|
+
exports.createDebugger = createDebugger;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare class Decoder {
|
|
3
|
+
private buffer;
|
|
4
|
+
private offset;
|
|
5
|
+
constructor(buffer: Buffer);
|
|
6
|
+
getOffset(): number;
|
|
7
|
+
getBufferLength(): number;
|
|
8
|
+
readInt8(): number;
|
|
9
|
+
readInt16(): number;
|
|
10
|
+
readInt32(): number;
|
|
11
|
+
readUInt32(): number;
|
|
12
|
+
readInt64(): bigint;
|
|
13
|
+
readUVarInt(): number;
|
|
14
|
+
readVarInt(): number;
|
|
15
|
+
readUVarLong(): bigint;
|
|
16
|
+
readVarLong(): bigint;
|
|
17
|
+
readString(): string | null;
|
|
18
|
+
readCompactString(): string | null;
|
|
19
|
+
readVarIntString(): string | null;
|
|
20
|
+
readUUID(): string;
|
|
21
|
+
readBoolean(): boolean;
|
|
22
|
+
readArray<T>(callback: (opts: Decoder) => T): T[];
|
|
23
|
+
readCompactArray<T>(callback: (opts: Decoder) => T): T[];
|
|
24
|
+
readRecords<T>(callback: (opts: Decoder) => T): T[];
|
|
25
|
+
read(length: number): Buffer;
|
|
26
|
+
readBytes(): Buffer;
|
|
27
|
+
readCompactBytes(): Buffer | null;
|
|
28
|
+
readTagBuffer(): void;
|
|
29
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Decoder = void 0;
|
|
4
|
+
class Decoder {
|
|
5
|
+
buffer;
|
|
6
|
+
offset = 0;
|
|
7
|
+
constructor(buffer) {
|
|
8
|
+
this.buffer = buffer;
|
|
9
|
+
}
|
|
10
|
+
getOffset() {
|
|
11
|
+
return this.offset;
|
|
12
|
+
}
|
|
13
|
+
getBufferLength() {
|
|
14
|
+
return this.buffer.length;
|
|
15
|
+
}
|
|
16
|
+
readInt8() {
|
|
17
|
+
const value = this.buffer.readInt8(this.offset);
|
|
18
|
+
this.offset += 1;
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
readInt16() {
|
|
22
|
+
const value = this.buffer.readInt16BE(this.offset);
|
|
23
|
+
this.offset += 2;
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
readInt32() {
|
|
27
|
+
const value = this.buffer.readInt32BE(this.offset);
|
|
28
|
+
this.offset += 4;
|
|
29
|
+
return value;
|
|
30
|
+
}
|
|
31
|
+
readUInt32() {
|
|
32
|
+
const value = this.buffer.readUInt32BE(this.offset);
|
|
33
|
+
this.offset += 4;
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
readInt64() {
|
|
37
|
+
const value = this.buffer.readBigInt64BE(this.offset);
|
|
38
|
+
this.offset += 8;
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
readUVarInt() {
|
|
42
|
+
let result = 0;
|
|
43
|
+
let shift = 0;
|
|
44
|
+
let currentByte;
|
|
45
|
+
do {
|
|
46
|
+
currentByte = this.buffer[this.offset++];
|
|
47
|
+
result |= (currentByte & 0x7f) << shift;
|
|
48
|
+
shift += 7;
|
|
49
|
+
} while ((currentByte & 0x80) !== 0);
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
readVarInt() {
|
|
53
|
+
const decodedValue = this.readUVarInt();
|
|
54
|
+
return (decodedValue >>> 1) ^ -(decodedValue & 1);
|
|
55
|
+
}
|
|
56
|
+
readUVarLong() {
|
|
57
|
+
let result = BigInt(0);
|
|
58
|
+
let shift = BigInt(0);
|
|
59
|
+
let currentByte;
|
|
60
|
+
do {
|
|
61
|
+
currentByte = BigInt(this.buffer[this.offset++]);
|
|
62
|
+
result |= (currentByte & BigInt(0x7f)) << shift;
|
|
63
|
+
shift += BigInt(7);
|
|
64
|
+
} while ((currentByte & BigInt(0x80)) !== BigInt(0));
|
|
65
|
+
return result;
|
|
66
|
+
}
|
|
67
|
+
readVarLong() {
|
|
68
|
+
const decodedValue = this.readUVarLong();
|
|
69
|
+
return (decodedValue >> BigInt(1)) ^ -(decodedValue & BigInt(1));
|
|
70
|
+
}
|
|
71
|
+
readString() {
|
|
72
|
+
const length = this.readInt16();
|
|
73
|
+
if (length < 0) {
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
const value = this.buffer.toString("utf-8", this.offset, this.offset + length);
|
|
77
|
+
this.offset += length;
|
|
78
|
+
return value;
|
|
79
|
+
}
|
|
80
|
+
readCompactString() {
|
|
81
|
+
const length = this.readUVarInt() - 1;
|
|
82
|
+
if (length < 0) {
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
const value = this.buffer.toString("utf-8", this.offset, this.offset + length);
|
|
86
|
+
this.offset += length;
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
readVarIntString() {
|
|
90
|
+
const length = this.readVarInt();
|
|
91
|
+
if (length < 0) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
const value = this.buffer.toString("utf-8", this.offset, this.offset + length);
|
|
95
|
+
this.offset += length;
|
|
96
|
+
return value;
|
|
97
|
+
}
|
|
98
|
+
readUUID() {
|
|
99
|
+
const value = this.buffer.toString("hex", this.offset, this.offset + 16);
|
|
100
|
+
this.offset += 16;
|
|
101
|
+
return value;
|
|
102
|
+
}
|
|
103
|
+
readBoolean() {
|
|
104
|
+
const value = this.buffer.readInt8(this.offset) === 1;
|
|
105
|
+
this.offset += 1;
|
|
106
|
+
return value;
|
|
107
|
+
}
|
|
108
|
+
readArray(callback) {
|
|
109
|
+
const length = this.readInt32();
|
|
110
|
+
const results = Array.from({ length }).map(() => callback(this));
|
|
111
|
+
return results;
|
|
112
|
+
}
|
|
113
|
+
readCompactArray(callback) {
|
|
114
|
+
const length = this.readUVarInt() - 1;
|
|
115
|
+
const results = Array.from({ length }).map(() => callback(this));
|
|
116
|
+
return results;
|
|
117
|
+
}
|
|
118
|
+
readRecords(callback) {
|
|
119
|
+
const length = this.readInt32();
|
|
120
|
+
return Array.from({ length }).map(() => {
|
|
121
|
+
const size = this.readVarInt();
|
|
122
|
+
const child = new Decoder(this.buffer.subarray(this.offset, this.offset + size));
|
|
123
|
+
this.offset += size;
|
|
124
|
+
return callback(child);
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
read(length) {
|
|
128
|
+
const value = this.buffer.subarray(this.offset, this.offset + length);
|
|
129
|
+
this.offset += length;
|
|
130
|
+
return value;
|
|
131
|
+
}
|
|
132
|
+
readBytes() {
|
|
133
|
+
const length = this.readInt32();
|
|
134
|
+
return this.read(length);
|
|
135
|
+
}
|
|
136
|
+
readCompactBytes() {
|
|
137
|
+
const length = this.readUVarInt() - 1;
|
|
138
|
+
if (length < 0) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
return this.read(length);
|
|
142
|
+
}
|
|
143
|
+
readTagBuffer() {
|
|
144
|
+
this.readUVarInt();
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
exports.Decoder = Decoder;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const delay: (delayMs: number) => Promise<void>;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare class Encoder {
|
|
3
|
+
private buffer;
|
|
4
|
+
constructor({ buffer }?: {
|
|
5
|
+
buffer?: Buffer;
|
|
6
|
+
});
|
|
7
|
+
write(rightBuffer: Buffer): this;
|
|
8
|
+
writeInt8(value: number): this;
|
|
9
|
+
writeInt16(value: number): this;
|
|
10
|
+
writeInt32(value: number): this;
|
|
11
|
+
writeUInt32(value: number): this;
|
|
12
|
+
writeInt64(value: bigint): this;
|
|
13
|
+
writeUVarInt(value: number): this;
|
|
14
|
+
writeVarInt(value: number): this;
|
|
15
|
+
writeUVarLong(value: bigint): this;
|
|
16
|
+
writeVarLong(value: bigint): this;
|
|
17
|
+
writeString(value: string | null): this;
|
|
18
|
+
writeCompactString(value: string | null): this;
|
|
19
|
+
writeVarIntString(value: string | null): this;
|
|
20
|
+
writeUUID(value: string | null): this;
|
|
21
|
+
writeBoolean(value: boolean): this;
|
|
22
|
+
writeArray<T>(arr: T[], callback: (encoder: Encoder, item: T) => Encoder): this;
|
|
23
|
+
writeCompactArray<T>(arr: T[] | null, callback: (encoder: Encoder, item: T) => Encoder): this;
|
|
24
|
+
writeVarIntArray<T>(arr: T[], callback: (encoder: Encoder, item: T) => Encoder): this;
|
|
25
|
+
writeBytes(value: Buffer): this;
|
|
26
|
+
writeCompactBytes(value: Buffer): this;
|
|
27
|
+
value(): Buffer;
|
|
28
|
+
}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Encoder = void 0;
|
|
4
|
+
class Encoder {
|
|
5
|
+
buffer;
|
|
6
|
+
constructor({ buffer = Buffer.alloc(0) } = {}) {
|
|
7
|
+
this.buffer = buffer;
|
|
8
|
+
}
|
|
9
|
+
write(rightBuffer) {
|
|
10
|
+
this.buffer = Buffer.concat([this.buffer, rightBuffer]);
|
|
11
|
+
return this;
|
|
12
|
+
}
|
|
13
|
+
writeInt8(value) {
|
|
14
|
+
const buffer = Buffer.alloc(1);
|
|
15
|
+
buffer.writeInt8(value);
|
|
16
|
+
return this.write(buffer);
|
|
17
|
+
}
|
|
18
|
+
writeInt16(value) {
|
|
19
|
+
const buffer = Buffer.alloc(2);
|
|
20
|
+
buffer.writeInt16BE(value);
|
|
21
|
+
return this.write(buffer);
|
|
22
|
+
}
|
|
23
|
+
writeInt32(value) {
|
|
24
|
+
const buffer = Buffer.alloc(4);
|
|
25
|
+
buffer.writeInt32BE(value);
|
|
26
|
+
return this.write(buffer);
|
|
27
|
+
}
|
|
28
|
+
writeUInt32(value) {
|
|
29
|
+
const buffer = Buffer.alloc(4);
|
|
30
|
+
buffer.writeUInt32BE(value);
|
|
31
|
+
return this.write(buffer);
|
|
32
|
+
}
|
|
33
|
+
writeInt64(value) {
|
|
34
|
+
const buffer = Buffer.alloc(8);
|
|
35
|
+
buffer.writeBigInt64BE(value);
|
|
36
|
+
return this.write(buffer);
|
|
37
|
+
}
|
|
38
|
+
writeUVarInt(value) {
|
|
39
|
+
const byteArray = [];
|
|
40
|
+
while ((value & 0xffffffff) !== 0) {
|
|
41
|
+
byteArray.push((value & 0x7f) | 0x80);
|
|
42
|
+
value >>>= 7;
|
|
43
|
+
}
|
|
44
|
+
byteArray.push(value & 0x7f);
|
|
45
|
+
return this.write(Buffer.from(byteArray));
|
|
46
|
+
}
|
|
47
|
+
writeVarInt(value) {
|
|
48
|
+
const encodedValue = (value << 1) ^ (value >> 31);
|
|
49
|
+
return this.writeUVarInt(encodedValue);
|
|
50
|
+
}
|
|
51
|
+
writeUVarLong(value) {
|
|
52
|
+
const byteArray = [];
|
|
53
|
+
while ((value & BigInt(0xffffffffffffffff)) !== BigInt(0)) {
|
|
54
|
+
byteArray.push(Number((value & BigInt(0x7f)) | BigInt(0x80)));
|
|
55
|
+
value = value >> BigInt(7);
|
|
56
|
+
}
|
|
57
|
+
byteArray.push(Number(value));
|
|
58
|
+
return this.write(Buffer.from(byteArray));
|
|
59
|
+
}
|
|
60
|
+
writeVarLong(value) {
|
|
61
|
+
const encodedValue = (value << BigInt(1)) ^ (value >> BigInt(63));
|
|
62
|
+
return this.writeUVarLong(encodedValue);
|
|
63
|
+
}
|
|
64
|
+
writeString(value) {
|
|
65
|
+
if (value === null) {
|
|
66
|
+
return this.writeInt16(-1);
|
|
67
|
+
}
|
|
68
|
+
const byteLength = Buffer.byteLength(value, "utf-8");
|
|
69
|
+
const buffer = Buffer.alloc(byteLength);
|
|
70
|
+
buffer.write(value, 0, byteLength, "utf-8");
|
|
71
|
+
return this.writeInt16(byteLength).write(buffer);
|
|
72
|
+
}
|
|
73
|
+
writeCompactString(value) {
|
|
74
|
+
if (value === null) {
|
|
75
|
+
return this.writeUVarInt(0);
|
|
76
|
+
}
|
|
77
|
+
const byteLength = Buffer.byteLength(value, "utf-8");
|
|
78
|
+
const buffer = Buffer.alloc(byteLength);
|
|
79
|
+
buffer.write(value, 0, byteLength, "utf-8");
|
|
80
|
+
return this.writeUVarInt(byteLength + 1).write(buffer);
|
|
81
|
+
}
|
|
82
|
+
writeVarIntString(value) {
|
|
83
|
+
if (value === null) {
|
|
84
|
+
return this.writeVarInt(-1);
|
|
85
|
+
}
|
|
86
|
+
return this.writeVarInt(Buffer.byteLength(value, "utf-8")).write(Buffer.from(value, "utf-8"));
|
|
87
|
+
}
|
|
88
|
+
writeUUID(value) {
|
|
89
|
+
if (value === null) {
|
|
90
|
+
return this.write(Buffer.alloc(16));
|
|
91
|
+
}
|
|
92
|
+
return this.write(Buffer.from(value, "hex"));
|
|
93
|
+
}
|
|
94
|
+
writeBoolean(value) {
|
|
95
|
+
return this.writeInt8(value ? 1 : 0);
|
|
96
|
+
}
|
|
97
|
+
writeArray(arr, callback) {
|
|
98
|
+
const buffers = arr.map((item) => callback(new Encoder(), item).value());
|
|
99
|
+
return this.writeInt32(arr.length).write(Buffer.concat(buffers));
|
|
100
|
+
}
|
|
101
|
+
writeCompactArray(arr, callback) {
|
|
102
|
+
if (arr === null) {
|
|
103
|
+
return this.writeUVarInt(0);
|
|
104
|
+
}
|
|
105
|
+
const buffers = arr.map((item) => callback(new Encoder(), item).value());
|
|
106
|
+
return this.writeUVarInt(buffers.length + 1).write(Buffer.concat(buffers));
|
|
107
|
+
}
|
|
108
|
+
writeVarIntArray(arr, callback) {
|
|
109
|
+
const buffers = arr.map((item) => callback(new Encoder(), item).value());
|
|
110
|
+
return this.writeVarInt(buffers.length).write(Buffer.concat(buffers));
|
|
111
|
+
}
|
|
112
|
+
writeBytes(value) {
|
|
113
|
+
return this.writeInt32(value.length).write(value);
|
|
114
|
+
}
|
|
115
|
+
writeCompactBytes(value) {
|
|
116
|
+
return this.writeUVarInt(value.length + 1).write(value);
|
|
117
|
+
}
|
|
118
|
+
value() {
|
|
119
|
+
return this.buffer;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
exports.Encoder = Encoder;
|