kafka-ts 1.1.8 → 1.2.0
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/README.md +1 -1
- package/dist/consumer/consumer-group.js +84 -99
- package/dist/consumer/consumer.js +23 -32
- package/dist/consumer/offset-manager.js +6 -5
- package/dist/distributors/group-by-leader-id.d.ts +10 -0
- package/dist/distributors/group-by-leader-id.js +13 -0
- package/dist/distributors/group-partitions-by-topic.d.ts +6 -0
- package/dist/distributors/group-partitions-by-topic.js +12 -0
- package/dist/metadata.d.ts +1 -1
- package/dist/metadata.js +1 -1
- package/dist/producer/producer-buffer.d.ts +20 -0
- package/dist/producer/producer-buffer.js +118 -0
- package/dist/producer/producer-state.d.ts +15 -0
- package/dist/producer/producer-state.js +33 -0
- package/dist/producer/producer.d.ts +6 -11
- package/dist/producer/producer.js +32 -108
- package/dist/utils/decoder.js +13 -8
- package/dist/utils/encoder.d.ts +10 -8
- package/dist/utils/encoder.js +95 -58
- package/dist/utils/promise-chain.d.ts +5 -0
- package/dist/utils/promise-chain.js +39 -0
- package/dist/utils/retry.d.ts +1 -0
- package/dist/utils/retry.js +19 -0
- package/dist/utils/shared.d.ts +1 -1
- package/dist/utils/shared.js +8 -7
- package/package.json +1 -1
- package/dist/consumer/metadata.d.ts +0 -24
- package/dist/consumer/metadata.js +0 -64
- package/dist/distributors/messages-to-topic-partition-leaders.d.ts +0 -17
- package/dist/distributors/messages-to-topic-partition-leaders.js +0 -15
- package/dist/distributors/messages-to-topic-partition-leaders.test.d.ts +0 -1
- package/dist/distributors/messages-to-topic-partition-leaders.test.js +0 -30
- package/dist/examples/src/replicator.js +0 -34
- package/dist/examples/src/utils/json.js +0 -5
- package/dist/request-handler.d.ts +0 -16
- package/dist/request-handler.js +0 -67
- package/dist/request-handler.test.d.ts +0 -1
- package/dist/request-handler.test.js +0 -340
- package/dist/src/api/api-versions.js +0 -18
- package/dist/src/api/create-topics.js +0 -46
- package/dist/src/api/delete-topics.js +0 -26
- package/dist/src/api/fetch.js +0 -95
- package/dist/src/api/find-coordinator.js +0 -34
- package/dist/src/api/heartbeat.js +0 -22
- package/dist/src/api/index.js +0 -38
- package/dist/src/api/init-producer-id.js +0 -24
- package/dist/src/api/join-group.js +0 -48
- package/dist/src/api/leave-group.js +0 -30
- package/dist/src/api/list-offsets.js +0 -39
- package/dist/src/api/metadata.js +0 -47
- package/dist/src/api/offset-commit.js +0 -39
- package/dist/src/api/offset-fetch.js +0 -44
- package/dist/src/api/produce.js +0 -119
- package/dist/src/api/sync-group.js +0 -31
- package/dist/src/broker.js +0 -35
- package/dist/src/connection.js +0 -21
- package/dist/src/consumer/consumer-group.js +0 -131
- package/dist/src/consumer/consumer.js +0 -103
- package/dist/src/consumer/metadata.js +0 -52
- package/dist/src/consumer/offset-manager.js +0 -23
- package/dist/src/index.js +0 -19
- package/dist/src/producer/producer.js +0 -84
- package/dist/src/request-handler.js +0 -57
- package/dist/src/request-handler.test.js +0 -321
- package/dist/src/types.js +0 -2
- package/dist/src/utils/api.js +0 -5
- package/dist/src/utils/decoder.js +0 -161
- package/dist/src/utils/encoder.js +0 -137
- package/dist/src/utils/error.js +0 -10
- package/dist/utils/debug.d.ts +0 -2
- package/dist/utils/debug.js +0 -11
- package/dist/utils/lock.d.ts +0 -8
- package/dist/utils/lock.js +0 -44
- package/dist/utils/memo.d.ts +0 -1
- package/dist/utils/memo.js +0 -16
- package/dist/utils/mutex.d.ts +0 -3
- package/dist/utils/mutex.js +0 -32
|
@@ -1,321 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const vitest_1 = require("vitest");
|
|
4
|
-
const api_versions_1 = require("./api/api-versions");
|
|
5
|
-
const create_topics_1 = require("./api/create-topics");
|
|
6
|
-
const fetch_1 = require("./api/fetch");
|
|
7
|
-
const metadata_1 = require("./api/metadata");
|
|
8
|
-
const connection_1 = require("./connection");
|
|
9
|
-
const request_handler_1 = require("./request-handler");
|
|
10
|
-
const delete_topics_1 = require("./api/delete-topics");
|
|
11
|
-
const produce_1 = require("./api/produce");
|
|
12
|
-
const find_coordinator_1 = require("./api/find-coordinator");
|
|
13
|
-
const join_group_1 = require("./api/join-group");
|
|
14
|
-
const crypto_1 = require("crypto");
|
|
15
|
-
const sync_group_1 = require("./api/sync-group");
|
|
16
|
-
const offset_commit_1 = require("./api/offset-commit");
|
|
17
|
-
const offset_fetch_1 = require("./api/offset-fetch");
|
|
18
|
-
const heartbeat_1 = require("./api/heartbeat");
|
|
19
|
-
const leave_group_1 = require("./api/leave-group");
|
|
20
|
-
const init_producer_id_1 = require("./api/init-producer-id");
|
|
21
|
-
vitest_1.describe.sequential("Request handler", () => {
|
|
22
|
-
const connection = (0, connection_1.createConnection)({ host: "localhost", port: 9092 });
|
|
23
|
-
let sendRequest;
|
|
24
|
-
const groupId = (0, crypto_1.randomBytes)(16).toString("hex");
|
|
25
|
-
(0, vitest_1.beforeAll)(async () => {
|
|
26
|
-
await connection.connect();
|
|
27
|
-
const handler = (0, request_handler_1.createRequestHandler)({ clientId: 'kafkats', connection });
|
|
28
|
-
sendRequest = handler.sendRequest;
|
|
29
|
-
const metadataResult = await sendRequest(metadata_1.METADATA, {
|
|
30
|
-
topics: null,
|
|
31
|
-
allowTopicAutoCreation: false,
|
|
32
|
-
includeTopicAuthorizedOperations: false,
|
|
33
|
-
});
|
|
34
|
-
if (metadataResult.topics.some((topic) => topic.name === "kafkats-test-topic")) {
|
|
35
|
-
await sendRequest(delete_topics_1.DELETE_TOPICS, {
|
|
36
|
-
topics: [{ name: "kafkats-test-topic", topicId: null }],
|
|
37
|
-
timeoutMs: 10000,
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
(0, vitest_1.afterAll)(async () => {
|
|
42
|
-
await connection.disconnect();
|
|
43
|
-
});
|
|
44
|
-
(0, vitest_1.it)("should request api versions", async () => {
|
|
45
|
-
const result = await sendRequest(api_versions_1.API_VERSIONS, {});
|
|
46
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
47
|
-
});
|
|
48
|
-
let topicId = "d6718d178e1b47c886441ad2d19faea5";
|
|
49
|
-
(0, vitest_1.it)("should create topics", async () => {
|
|
50
|
-
const result = await sendRequest(create_topics_1.CREATE_TOPICS, {
|
|
51
|
-
topics: [
|
|
52
|
-
{
|
|
53
|
-
name: "kafkats-test-topic",
|
|
54
|
-
numPartitions: 1,
|
|
55
|
-
replicationFactor: 1,
|
|
56
|
-
assignments: [],
|
|
57
|
-
configs: [],
|
|
58
|
-
},
|
|
59
|
-
],
|
|
60
|
-
timeoutMs: 10000,
|
|
61
|
-
validateOnly: false,
|
|
62
|
-
});
|
|
63
|
-
topicId = result.topics[0].topicId;
|
|
64
|
-
console.log({ topicId });
|
|
65
|
-
result.topics.forEach((topic) => {
|
|
66
|
-
topic.topicId = "Any<UUID>";
|
|
67
|
-
});
|
|
68
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
69
|
-
});
|
|
70
|
-
(0, vitest_1.it)("should request metadata for all topics", async () => {
|
|
71
|
-
const result = await sendRequest(metadata_1.METADATA, {
|
|
72
|
-
topics: null,
|
|
73
|
-
allowTopicAutoCreation: false,
|
|
74
|
-
includeTopicAuthorizedOperations: false,
|
|
75
|
-
});
|
|
76
|
-
result.topics.forEach((topic) => {
|
|
77
|
-
topic.topicId = "Any<UUID>";
|
|
78
|
-
});
|
|
79
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
80
|
-
});
|
|
81
|
-
(0, vitest_1.it)("should request metadata for a topic", async () => {
|
|
82
|
-
const result = await sendRequest(metadata_1.METADATA, {
|
|
83
|
-
topics: [{ id: topicId, name: "kafkats-test-topic" }],
|
|
84
|
-
allowTopicAutoCreation: false,
|
|
85
|
-
includeTopicAuthorizedOperations: false,
|
|
86
|
-
});
|
|
87
|
-
result.topics.forEach((topic) => {
|
|
88
|
-
topic.topicId = "Any<UUID>";
|
|
89
|
-
});
|
|
90
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
91
|
-
});
|
|
92
|
-
(0, vitest_1.it)('should init producer id', async () => {
|
|
93
|
-
const result = await sendRequest(init_producer_id_1.INIT_PRODUCER_ID, {
|
|
94
|
-
transactionalId: null,
|
|
95
|
-
transactionTimeoutMs: 0,
|
|
96
|
-
producerId: 0n,
|
|
97
|
-
producerEpoch: 0,
|
|
98
|
-
});
|
|
99
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
100
|
-
});
|
|
101
|
-
(0, vitest_1.it)("should produce messages", async () => {
|
|
102
|
-
const now = Date.now();
|
|
103
|
-
const result = await sendRequest(produce_1.PRODUCE, {
|
|
104
|
-
transactionalId: null,
|
|
105
|
-
timeoutMs: 10000,
|
|
106
|
-
acks: 1,
|
|
107
|
-
topicData: [
|
|
108
|
-
{
|
|
109
|
-
name: "kafkats-test-topic",
|
|
110
|
-
partitionData: [
|
|
111
|
-
{
|
|
112
|
-
index: 0,
|
|
113
|
-
baseOffset: 0n,
|
|
114
|
-
partitionLeaderEpoch: 0,
|
|
115
|
-
attributes: 0,
|
|
116
|
-
baseSequence: 0,
|
|
117
|
-
baseTimestamp: BigInt(now),
|
|
118
|
-
lastOffsetDelta: 0,
|
|
119
|
-
maxTimestamp: BigInt(now),
|
|
120
|
-
producerEpoch: 0,
|
|
121
|
-
producerId: 9n,
|
|
122
|
-
records: [
|
|
123
|
-
{
|
|
124
|
-
attributes: 0,
|
|
125
|
-
offsetDelta: 0,
|
|
126
|
-
timestampDelta: 0n,
|
|
127
|
-
key: "key",
|
|
128
|
-
value: "value",
|
|
129
|
-
headers: [
|
|
130
|
-
{
|
|
131
|
-
key: "header-key",
|
|
132
|
-
value: "header-value",
|
|
133
|
-
},
|
|
134
|
-
],
|
|
135
|
-
},
|
|
136
|
-
],
|
|
137
|
-
},
|
|
138
|
-
],
|
|
139
|
-
},
|
|
140
|
-
],
|
|
141
|
-
});
|
|
142
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
143
|
-
});
|
|
144
|
-
(0, vitest_1.it)("should fetch messages", async () => {
|
|
145
|
-
const result = await sendRequest(fetch_1.FETCH, {
|
|
146
|
-
maxWaitMs: 100,
|
|
147
|
-
minBytes: 1,
|
|
148
|
-
maxBytes: 10485760,
|
|
149
|
-
isolationLevel: 1,
|
|
150
|
-
sessionId: 0,
|
|
151
|
-
sessionEpoch: -1,
|
|
152
|
-
topics: [
|
|
153
|
-
{
|
|
154
|
-
topicId,
|
|
155
|
-
partitions: [
|
|
156
|
-
{
|
|
157
|
-
partition: 0,
|
|
158
|
-
currentLeaderEpoch: -1,
|
|
159
|
-
fetchOffset: 0n,
|
|
160
|
-
lastFetchedEpoch: 0,
|
|
161
|
-
logStartOffset: -1n,
|
|
162
|
-
partitionMaxBytes: 10485760,
|
|
163
|
-
},
|
|
164
|
-
],
|
|
165
|
-
},
|
|
166
|
-
],
|
|
167
|
-
forgottenTopicsData: [],
|
|
168
|
-
rackId: "",
|
|
169
|
-
});
|
|
170
|
-
result.responses.forEach((response) => {
|
|
171
|
-
response.topicId = "Any<UUID>";
|
|
172
|
-
response.partitions.forEach((partition) => {
|
|
173
|
-
partition.records.forEach((record) => {
|
|
174
|
-
(0, vitest_1.expect)(record.baseTimestamp).toBeGreaterThan(1721926744730n);
|
|
175
|
-
(0, vitest_1.expect)(record.maxTimestamp).toBeGreaterThan(1721926744730n);
|
|
176
|
-
(0, vitest_1.expect)(record.crc).toBeGreaterThan(0);
|
|
177
|
-
record.baseTimestamp = 0n;
|
|
178
|
-
record.maxTimestamp = 0n;
|
|
179
|
-
record.crc = 0;
|
|
180
|
-
});
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
184
|
-
});
|
|
185
|
-
(0, vitest_1.it)("should find coordinator", async () => {
|
|
186
|
-
const result = await sendRequest(find_coordinator_1.FIND_COORDINATOR, { keyType: find_coordinator_1.KEY_TYPE.GROUP, keys: [groupId] });
|
|
187
|
-
result.coordinators.forEach((coordinator) => {
|
|
188
|
-
coordinator.key = "Any<String>";
|
|
189
|
-
});
|
|
190
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
191
|
-
});
|
|
192
|
-
let memberId = "";
|
|
193
|
-
(0, vitest_1.it)("should fail join group request with new memberId", async () => {
|
|
194
|
-
const result = await sendRequest(join_group_1.JOIN_GROUP, {
|
|
195
|
-
groupId: groupId,
|
|
196
|
-
sessionTimeoutMs: 30000,
|
|
197
|
-
rebalanceTimeoutMs: 60000,
|
|
198
|
-
memberId,
|
|
199
|
-
groupInstanceId: null,
|
|
200
|
-
protocolType: "consumer",
|
|
201
|
-
protocols: [
|
|
202
|
-
{
|
|
203
|
-
name: "RoundRobinAssigner",
|
|
204
|
-
metadata: { version: 0, topics: ["kafkats-test-topic"] },
|
|
205
|
-
},
|
|
206
|
-
],
|
|
207
|
-
reason: null,
|
|
208
|
-
});
|
|
209
|
-
memberId = String(result.memberId ?? "");
|
|
210
|
-
result.memberId = "Any<UUID>";
|
|
211
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
212
|
-
});
|
|
213
|
-
(0, vitest_1.it)("should join group", async () => {
|
|
214
|
-
const result = await sendRequest(join_group_1.JOIN_GROUP, {
|
|
215
|
-
groupId: groupId,
|
|
216
|
-
sessionTimeoutMs: 30000,
|
|
217
|
-
rebalanceTimeoutMs: 60000,
|
|
218
|
-
memberId,
|
|
219
|
-
groupInstanceId: null,
|
|
220
|
-
protocolType: "consumer",
|
|
221
|
-
protocols: [
|
|
222
|
-
{
|
|
223
|
-
name: "RoundRobinAssigner",
|
|
224
|
-
metadata: { version: 0, topics: ["kafkats-test-topic"] },
|
|
225
|
-
},
|
|
226
|
-
],
|
|
227
|
-
reason: null,
|
|
228
|
-
});
|
|
229
|
-
result.memberId = "Any<UUID>";
|
|
230
|
-
result.leader = "Any<UUID>";
|
|
231
|
-
result.members.forEach((member) => {
|
|
232
|
-
member.memberId = "Any<UUID>";
|
|
233
|
-
});
|
|
234
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
235
|
-
});
|
|
236
|
-
(0, vitest_1.it)("should sync group", async () => {
|
|
237
|
-
const result = await sendRequest(sync_group_1.SYNC_GROUP, {
|
|
238
|
-
groupId,
|
|
239
|
-
generationId: 1,
|
|
240
|
-
memberId,
|
|
241
|
-
groupInstanceId: null,
|
|
242
|
-
protocolType: "consumer",
|
|
243
|
-
protocolName: "RoundRobinAssigner",
|
|
244
|
-
assignments: [
|
|
245
|
-
{
|
|
246
|
-
memberId,
|
|
247
|
-
assignment: { "kafka-test-topic": [0] },
|
|
248
|
-
},
|
|
249
|
-
],
|
|
250
|
-
});
|
|
251
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
252
|
-
});
|
|
253
|
-
(0, vitest_1.it)("should commit offsets", async () => {
|
|
254
|
-
const result = await sendRequest(offset_commit_1.OFFSET_COMMIT, {
|
|
255
|
-
groupId,
|
|
256
|
-
generationIdOrMemberEpoch: 1,
|
|
257
|
-
memberId,
|
|
258
|
-
groupInstanceId: null,
|
|
259
|
-
topics: [
|
|
260
|
-
{
|
|
261
|
-
name: "kafkats-test-topic",
|
|
262
|
-
partitions: [
|
|
263
|
-
{ partitionIndex: 0, committedOffset: 1n, committedLeaderEpoch: 0, committedMetadata: null },
|
|
264
|
-
],
|
|
265
|
-
},
|
|
266
|
-
],
|
|
267
|
-
});
|
|
268
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
269
|
-
});
|
|
270
|
-
(0, vitest_1.it)("should fetch offsets", async () => {
|
|
271
|
-
const result = await sendRequest(offset_fetch_1.OFFSET_FETCH, {
|
|
272
|
-
groups: [
|
|
273
|
-
{
|
|
274
|
-
groupId,
|
|
275
|
-
memberId,
|
|
276
|
-
memberEpoch: 0,
|
|
277
|
-
topics: [
|
|
278
|
-
{
|
|
279
|
-
name: "kafkats-test-topic",
|
|
280
|
-
partitionIndexes: [0],
|
|
281
|
-
},
|
|
282
|
-
],
|
|
283
|
-
},
|
|
284
|
-
],
|
|
285
|
-
requireStable: false,
|
|
286
|
-
});
|
|
287
|
-
result.groups.forEach((group) => {
|
|
288
|
-
group.groupId = "Any<String>";
|
|
289
|
-
});
|
|
290
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
291
|
-
});
|
|
292
|
-
(0, vitest_1.it)("should heartbeat", async () => {
|
|
293
|
-
const result = await sendRequest(heartbeat_1.HEARTBEAT, {
|
|
294
|
-
groupId,
|
|
295
|
-
generationId: 1,
|
|
296
|
-
memberId,
|
|
297
|
-
groupInstanceId: null,
|
|
298
|
-
});
|
|
299
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
300
|
-
});
|
|
301
|
-
(0, vitest_1.it)("should leave group", async () => {
|
|
302
|
-
const result = await sendRequest(leave_group_1.LEAVE_GROUP, {
|
|
303
|
-
groupId,
|
|
304
|
-
members: [{ memberId, groupInstanceId: null, reason: null }],
|
|
305
|
-
});
|
|
306
|
-
result.members.forEach((member) => {
|
|
307
|
-
member.memberId = "Any<UUID>";
|
|
308
|
-
});
|
|
309
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
310
|
-
});
|
|
311
|
-
vitest_1.it.skip("should delete topics", async () => {
|
|
312
|
-
const result = await sendRequest(delete_topics_1.DELETE_TOPICS, {
|
|
313
|
-
topics: [{ name: "kafkats-test-topic", topicId: null }],
|
|
314
|
-
timeoutMs: 10000,
|
|
315
|
-
});
|
|
316
|
-
result.responses.forEach((response) => {
|
|
317
|
-
response.topicId = "Any<UUID>";
|
|
318
|
-
});
|
|
319
|
-
(0, vitest_1.expect)(result).toMatchSnapshot();
|
|
320
|
-
});
|
|
321
|
-
});
|
package/dist/src/types.js
DELETED
package/dist/src/utils/api.js
DELETED
|
@@ -1,161 +0,0 @@
|
|
|
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;
|
|
@@ -1,137 +0,0 @@
|
|
|
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;
|
package/dist/src/utils/error.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
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/utils/debug.d.ts
DELETED
package/dist/utils/debug.js
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
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('kafka-ts'))
|
|
8
|
-
return;
|
|
9
|
-
console.debug(`[${module}] ${func}: ${message}`, data && `(${data instanceof Error ? data : JSON.stringify(data, exports.serializer, 4)})`);
|
|
10
|
-
};
|
|
11
|
-
exports.createDebugger = createDebugger;
|
package/dist/utils/lock.d.ts
DELETED