kafka-ts 1.3.2-beta.6 → 1.3.3
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/dist/api/join-group.js +2 -1
- package/dist/api/leave-group.js +2 -1
- package/dist/api/sync-group.js +2 -3
- package/dist/cluster.js +1 -1
- package/dist/connection.js +1 -1
- package/package.json +1 -1
- package/dist/api/alter-configs.d.ts +0 -26
- package/dist/api/alter-configs.js +0 -33
- 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
package/dist/api/join-group.js
CHANGED
|
@@ -98,6 +98,7 @@ JoinGroup Response (Version: 6) => throttle_time_ms error_code generation_id pro
|
|
|
98
98
|
const JOIN_GROUP_V6 = (0, api_1.createApi)({
|
|
99
99
|
apiKey: 11,
|
|
100
100
|
apiVersion: 6,
|
|
101
|
+
fallback: JOIN_GROUP_V5,
|
|
101
102
|
requestHeaderVersion: 2,
|
|
102
103
|
responseHeaderVersion: 1,
|
|
103
104
|
request: (encoder, data) => encoder
|
|
@@ -169,7 +170,7 @@ JoinGroup Response (Version: 9) => throttle_time_ms error_code generation_id pro
|
|
|
169
170
|
exports.JOIN_GROUP = (0, api_1.createApi)({
|
|
170
171
|
apiKey: 11,
|
|
171
172
|
apiVersion: 9,
|
|
172
|
-
fallback:
|
|
173
|
+
fallback: JOIN_GROUP_V6,
|
|
173
174
|
requestHeaderVersion: 2,
|
|
174
175
|
responseHeaderVersion: 1,
|
|
175
176
|
request: (encoder, data) => encoder
|
package/dist/api/leave-group.js
CHANGED
|
@@ -67,6 +67,7 @@ LeaveGroup Response (Version: 4) => throttle_time_ms error_code [members] _tagge
|
|
|
67
67
|
const LEAVE_GROUP_V4 = (0, api_1.createApi)({
|
|
68
68
|
apiKey: 13,
|
|
69
69
|
apiVersion: 4,
|
|
70
|
+
fallback: LEAVE_GROUP_V3,
|
|
70
71
|
requestHeaderVersion: 2,
|
|
71
72
|
responseHeaderVersion: 1,
|
|
72
73
|
request: (encoder, body) => encoder
|
|
@@ -116,7 +117,7 @@ LeaveGroup Response (Version: 5) => throttle_time_ms error_code [members] _tagge
|
|
|
116
117
|
exports.LEAVE_GROUP = (0, api_1.createApi)({
|
|
117
118
|
apiKey: 13,
|
|
118
119
|
apiVersion: 5,
|
|
119
|
-
fallback:
|
|
120
|
+
fallback: LEAVE_GROUP_V4,
|
|
120
121
|
requestHeaderVersion: 2,
|
|
121
122
|
responseHeaderVersion: 1,
|
|
122
123
|
request: (encoder, body) => encoder
|
package/dist/api/sync-group.js
CHANGED
|
@@ -5,7 +5,6 @@ const api_1 = require("../utils/api");
|
|
|
5
5
|
const decoder_1 = require("../utils/decoder");
|
|
6
6
|
const encoder_1 = require("../utils/encoder");
|
|
7
7
|
const error_1 = require("../utils/error");
|
|
8
|
-
const logger_1 = require("../utils/logger");
|
|
9
8
|
/*
|
|
10
9
|
SyncGroup Request (Version: 3) => group_id generation_id member_id group_instance_id [assignments]
|
|
11
10
|
group_id => STRING
|
|
@@ -66,6 +65,7 @@ SyncGroup Response (Version: 4) => throttle_time_ms error_code assignment _tagge
|
|
|
66
65
|
const SYNC_GROUP_V4 = (0, api_1.createApi)({
|
|
67
66
|
apiKey: 14,
|
|
68
67
|
apiVersion: 4,
|
|
68
|
+
fallback: SYNC_GROUP_V3,
|
|
69
69
|
requestHeaderVersion: 2,
|
|
70
70
|
responseHeaderVersion: 1,
|
|
71
71
|
request: (encoder, data) => encoder
|
|
@@ -114,7 +114,7 @@ SyncGroup Response (Version: 5) => throttle_time_ms error_code protocol_type pro
|
|
|
114
114
|
exports.SYNC_GROUP = (0, api_1.createApi)({
|
|
115
115
|
apiKey: 14,
|
|
116
116
|
apiVersion: 5,
|
|
117
|
-
fallback:
|
|
117
|
+
fallback: SYNC_GROUP_V4,
|
|
118
118
|
requestHeaderVersion: 2,
|
|
119
119
|
responseHeaderVersion: 1,
|
|
120
120
|
request: (encoder, data) => encoder
|
|
@@ -149,7 +149,6 @@ const encodeAssignment = (data) => new encoder_1.Encoder()
|
|
|
149
149
|
.writeBytes(Buffer.alloc(0))
|
|
150
150
|
.value();
|
|
151
151
|
const decodeAssignment = (data) => {
|
|
152
|
-
logger_1.log.debug('Received assignment hex', { data });
|
|
153
152
|
if (!data) {
|
|
154
153
|
return {};
|
|
155
154
|
}
|
package/dist/cluster.js
CHANGED
|
@@ -111,7 +111,7 @@ class Cluster {
|
|
|
111
111
|
return broker;
|
|
112
112
|
}
|
|
113
113
|
catch (error) {
|
|
114
|
-
logger_1.log.
|
|
114
|
+
logger_1.log.warn(`Failed to connect to seed broker ${options.host}:${options.port}`, {
|
|
115
115
|
reason: error.message,
|
|
116
116
|
});
|
|
117
117
|
}
|
package/dist/connection.js
CHANGED
|
@@ -135,7 +135,7 @@ class Connection {
|
|
|
135
135
|
.writeInt16(api.apiKey)
|
|
136
136
|
.writeInt16(api.apiVersion)
|
|
137
137
|
.writeInt32(correlationId)
|
|
138
|
-
.writeString(this.options.clientId);
|
|
138
|
+
.writeString(this.options.clientId ?? '');
|
|
139
139
|
if (api.requestHeaderVersion === 2)
|
|
140
140
|
encoder.writeTagBuffer();
|
|
141
141
|
const request = api.request(encoder, body);
|
package/package.json
CHANGED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
export declare const ALTER_CONFIGS: import("../utils/api").Api<{
|
|
2
|
-
resources: {
|
|
3
|
-
resourceType: number;
|
|
4
|
-
resourceName: string;
|
|
5
|
-
configs: {
|
|
6
|
-
name: string;
|
|
7
|
-
value: string | null;
|
|
8
|
-
}[];
|
|
9
|
-
}[];
|
|
10
|
-
validateOnly: boolean;
|
|
11
|
-
}, {
|
|
12
|
-
_tag: void;
|
|
13
|
-
responses: {
|
|
14
|
-
name: string | null;
|
|
15
|
-
partitionResponses: {
|
|
16
|
-
index: number;
|
|
17
|
-
errorCode: number;
|
|
18
|
-
baseOffset: bigint;
|
|
19
|
-
logAppendTimeMs: bigint;
|
|
20
|
-
_tag: void;
|
|
21
|
-
}[];
|
|
22
|
-
_tag: void;
|
|
23
|
-
}[];
|
|
24
|
-
throttleTimeMs: number;
|
|
25
|
-
_tag2: void;
|
|
26
|
-
}>;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ALTER_CONFIGS = void 0;
|
|
4
|
-
const api_1 = require("../utils/api");
|
|
5
|
-
exports.ALTER_CONFIGS = (0, api_1.createApi)({
|
|
6
|
-
apiKey: 33,
|
|
7
|
-
apiVersion: 2,
|
|
8
|
-
request: (encoder, data) => encoder
|
|
9
|
-
.writeUVarInt(0)
|
|
10
|
-
.writeCompactArray(data.resources, (encoder, item) => encoder
|
|
11
|
-
.writeInt8(item.resourceType)
|
|
12
|
-
.writeCompactString(item.resourceName)
|
|
13
|
-
.writeCompactArray(item.configs, (encoder, item) => encoder.writeCompactString(item.name).writeCompactString(item.value).writeUVarInt(0))
|
|
14
|
-
.writeUVarInt(0))
|
|
15
|
-
.writeBoolean(data.validateOnly)
|
|
16
|
-
.writeUVarInt(0),
|
|
17
|
-
response: (decoder) => ({
|
|
18
|
-
_tag: decoder.readTagBuffer(),
|
|
19
|
-
responses: decoder.readCompactArray((decoder) => ({
|
|
20
|
-
name: decoder.readCompactString(),
|
|
21
|
-
partitionResponses: decoder.readCompactArray((decoder) => ({
|
|
22
|
-
index: decoder.readInt32(),
|
|
23
|
-
errorCode: decoder.readInt16(),
|
|
24
|
-
baseOffset: decoder.readInt64(),
|
|
25
|
-
logAppendTimeMs: decoder.readInt64(),
|
|
26
|
-
_tag: decoder.readTagBuffer(),
|
|
27
|
-
})),
|
|
28
|
-
_tag: decoder.readTagBuffer(),
|
|
29
|
-
})),
|
|
30
|
-
throttleTimeMs: decoder.readInt32(),
|
|
31
|
-
_tag2: decoder.readTagBuffer(),
|
|
32
|
-
}),
|
|
33
|
-
});
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { IsolationLevel } from "../api/fetch";
|
|
2
|
-
import { Assignment } from "../api/sync-group";
|
|
3
|
-
import { Cluster } from "../cluster";
|
|
4
|
-
import { OffsetManager } from "./offset-manager";
|
|
5
|
-
export type Metadata = ReturnType<typeof createMetadata>;
|
|
6
|
-
type MetadataOptions = {
|
|
7
|
-
cluster: Cluster;
|
|
8
|
-
topics?: string[];
|
|
9
|
-
isolationLevel?: IsolationLevel;
|
|
10
|
-
allowTopicAutoCreation?: boolean;
|
|
11
|
-
fromBeginning?: boolean;
|
|
12
|
-
offsetManager?: OffsetManager;
|
|
13
|
-
};
|
|
14
|
-
export declare const createMetadata: ({ cluster, topics, isolationLevel, allowTopicAutoCreation, fromBeginning, offsetManager, }: MetadataOptions) => {
|
|
15
|
-
init: () => Promise<void>;
|
|
16
|
-
getTopicPartitions: () => Record<string, number[]>;
|
|
17
|
-
getTopicIdByName: (name: string) => string;
|
|
18
|
-
getTopicNameById: (id: string) => string;
|
|
19
|
-
getAssignment: () => Assignment;
|
|
20
|
-
setAssignment: (newAssignment: Assignment) => void;
|
|
21
|
-
getLeaderIdByTopicPartition: (topic: string, partition: number) => number;
|
|
22
|
-
getIsrNodeIdsByTopicPartition: (topic: string, partition: number) => number[];
|
|
23
|
-
};
|
|
24
|
-
export {};
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createMetadata = void 0;
|
|
4
|
-
const api_1 = require("../api");
|
|
5
|
-
const createMetadata = ({ cluster, topics, isolationLevel = 0 /* IsolationLevel.READ_UNCOMMITTED */, allowTopicAutoCreation = true, fromBeginning = false, offsetManager, }) => {
|
|
6
|
-
let topicPartitions = {};
|
|
7
|
-
let topicNameById = {};
|
|
8
|
-
let topicIdByName = {};
|
|
9
|
-
let leaderIdByTopicPartition = {};
|
|
10
|
-
let isrNodesByTopicPartition;
|
|
11
|
-
let assignment = {};
|
|
12
|
-
const fetchMetadata = async () => {
|
|
13
|
-
const response = await cluster.sendRequest(api_1.API.METADATA, {
|
|
14
|
-
allowTopicAutoCreation,
|
|
15
|
-
includeTopicAuthorizedOperations: false,
|
|
16
|
-
topics: topics?.map((name) => ({ id: null, name })) ?? null,
|
|
17
|
-
});
|
|
18
|
-
topicPartitions = Object.fromEntries(response.topics.map((topic) => [topic.name, topic.partitions.map((partition) => partition.partitionIndex)]));
|
|
19
|
-
topicNameById = Object.fromEntries(response.topics.map((topic) => [topic.topicId, topic.name]));
|
|
20
|
-
topicIdByName = Object.fromEntries(response.topics.map((topic) => [topic.name, topic.topicId]));
|
|
21
|
-
leaderIdByTopicPartition = Object.fromEntries(response.topics.map((topic) => [
|
|
22
|
-
topic.name,
|
|
23
|
-
Object.fromEntries(topic.partitions.map((partition) => [partition.partitionIndex, partition.leaderId])),
|
|
24
|
-
]));
|
|
25
|
-
isrNodesByTopicPartition = Object.fromEntries(response.topics.map((topic) => [
|
|
26
|
-
topic.name,
|
|
27
|
-
Object.fromEntries(topic.partitions.map((partition) => [partition.partitionIndex, partition.isrNodes])),
|
|
28
|
-
]));
|
|
29
|
-
assignment = topicPartitions;
|
|
30
|
-
};
|
|
31
|
-
const listOffsets = async () => {
|
|
32
|
-
const offsets = await cluster.sendRequest(api_1.API.LIST_OFFSETS, {
|
|
33
|
-
replicaId: -1,
|
|
34
|
-
isolationLevel,
|
|
35
|
-
topics: Object.entries(assignment)
|
|
36
|
-
.flatMap(([topic, partitions]) => partitions.map((partition) => ({ topic, partition })))
|
|
37
|
-
.map(({ topic, partition }) => ({
|
|
38
|
-
name: topic,
|
|
39
|
-
partitions: [{ partitionIndex: partition, currentLeaderEpoch: -1, timestamp: -1n }],
|
|
40
|
-
})),
|
|
41
|
-
});
|
|
42
|
-
offsets.topics.forEach(({ name, partitions }) => {
|
|
43
|
-
partitions.forEach(({ partitionIndex, offset }) => {
|
|
44
|
-
offsetManager?.resolve(name, partitionIndex, fromBeginning ? 0n : offset);
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
};
|
|
48
|
-
return {
|
|
49
|
-
init: async () => {
|
|
50
|
-
await fetchMetadata();
|
|
51
|
-
await listOffsets();
|
|
52
|
-
},
|
|
53
|
-
getTopicPartitions: () => topicPartitions,
|
|
54
|
-
getTopicIdByName: (name) => topicIdByName[name],
|
|
55
|
-
getTopicNameById: (id) => topicNameById[id],
|
|
56
|
-
getAssignment: () => assignment,
|
|
57
|
-
setAssignment: (newAssignment) => {
|
|
58
|
-
assignment = newAssignment;
|
|
59
|
-
},
|
|
60
|
-
getLeaderIdByTopicPartition: (topic, partition) => leaderIdByTopicPartition[topic][partition],
|
|
61
|
-
getIsrNodeIdsByTopicPartition: (topic, partition) => isrNodesByTopicPartition[topic][partition],
|
|
62
|
-
};
|
|
63
|
-
};
|
|
64
|
-
exports.createMetadata = createMetadata;
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
type TopicPartitionLeader = {
|
|
2
|
-
[topicName: string]: {
|
|
3
|
-
[partitionId: number]: number;
|
|
4
|
-
};
|
|
5
|
-
};
|
|
6
|
-
type MessagesByNodeTopicPartition<T> = {
|
|
7
|
-
[nodeId: number]: {
|
|
8
|
-
[topicName: string]: {
|
|
9
|
-
[partitionId: number]: T[];
|
|
10
|
-
};
|
|
11
|
-
};
|
|
12
|
-
};
|
|
13
|
-
export declare const distributeMessagesToTopicPartitionLeaders: <T extends {
|
|
14
|
-
topic: string;
|
|
15
|
-
partition: number;
|
|
16
|
-
}>(messages: T[], topicPartitionLeader: TopicPartitionLeader) => MessagesByNodeTopicPartition<T>;
|
|
17
|
-
export {};
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.distributeMessagesToTopicPartitionLeaders = void 0;
|
|
4
|
-
const distributeMessagesToTopicPartitionLeaders = (messages, topicPartitionLeader) => {
|
|
5
|
-
const result = {};
|
|
6
|
-
messages.forEach((message) => {
|
|
7
|
-
const leaderId = topicPartitionLeader[message.topic][message.partition];
|
|
8
|
-
result[leaderId] ??= {};
|
|
9
|
-
result[leaderId][message.topic] ??= {};
|
|
10
|
-
result[leaderId][message.topic][message.partition] ??= [];
|
|
11
|
-
result[leaderId][message.topic][message.partition].push(message);
|
|
12
|
-
});
|
|
13
|
-
return result;
|
|
14
|
-
};
|
|
15
|
-
exports.distributeMessagesToTopicPartitionLeaders = distributeMessagesToTopicPartitionLeaders;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const vitest_1 = require("vitest");
|
|
4
|
-
const messages_to_topic_partition_leaders_1 = require("./messages-to-topic-partition-leaders");
|
|
5
|
-
(0, vitest_1.describe)('Distribute messages to partition leader ids', () => {
|
|
6
|
-
(0, vitest_1.describe)('distributeMessagesToTopicPartitionLeaders', () => {
|
|
7
|
-
(0, vitest_1.it)('snoke', () => {
|
|
8
|
-
const result = (0, messages_to_topic_partition_leaders_1.distributeMessagesToTopicPartitionLeaders)([{ topic: 'topic', partition: 0, key: null, value: null, offset: 0n, timestamp: 0n, headers: {} }], { topic: { 0: 1 } });
|
|
9
|
-
(0, vitest_1.expect)(result).toMatchInlineSnapshot(`
|
|
10
|
-
{
|
|
11
|
-
"1": {
|
|
12
|
-
"topic": {
|
|
13
|
-
"0": [
|
|
14
|
-
{
|
|
15
|
-
"headers": {},
|
|
16
|
-
"key": null,
|
|
17
|
-
"offset": 0n,
|
|
18
|
-
"partition": 0,
|
|
19
|
-
"timestamp": 0n,
|
|
20
|
-
"topic": "topic",
|
|
21
|
-
"value": null,
|
|
22
|
-
},
|
|
23
|
-
],
|
|
24
|
-
},
|
|
25
|
-
},
|
|
26
|
-
}
|
|
27
|
-
`);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
30
|
-
});
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const kafkats_1 = require("kafkats");
|
|
4
|
-
const json_1 = require("./utils/json");
|
|
5
|
-
(async () => {
|
|
6
|
-
const brokers = [{ host: "localhost", port: 9092 }];
|
|
7
|
-
const topic = "playground-topic";
|
|
8
|
-
// const producer = createProducer({ brokers });
|
|
9
|
-
// const producerInterval = setInterval(async () => {
|
|
10
|
-
// await producer.send([
|
|
11
|
-
// {
|
|
12
|
-
// topic,
|
|
13
|
-
// partition: 0,
|
|
14
|
-
// offset: 1n,
|
|
15
|
-
// timestamp: BigInt(Date.now()),
|
|
16
|
-
// key: null,
|
|
17
|
-
// value: `PING ${Math.random()}`,
|
|
18
|
-
// headers: { timestamp: Date.now().toString() }
|
|
19
|
-
// }
|
|
20
|
-
// ])
|
|
21
|
-
// }, 5000);
|
|
22
|
-
const consumer = await (0, kafkats_1.startConsumer)({
|
|
23
|
-
topics: [topic],
|
|
24
|
-
brokers,
|
|
25
|
-
onBatch: (messages) => {
|
|
26
|
-
console.log(JSON.stringify(messages, json_1.serializer, 2));
|
|
27
|
-
},
|
|
28
|
-
});
|
|
29
|
-
process.on("SIGINT", async () => {
|
|
30
|
-
await consumer.close();
|
|
31
|
-
// clearInterval(producerInterval);
|
|
32
|
-
// await producer.close();
|
|
33
|
-
});
|
|
34
|
-
})();
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Connection } from "./connection";
|
|
2
|
-
import { Api } from "./utils/api";
|
|
3
|
-
type RequestHandlerOptions = {
|
|
4
|
-
clientId: string | null;
|
|
5
|
-
};
|
|
6
|
-
export declare class RequestHandler {
|
|
7
|
-
private connection;
|
|
8
|
-
private options;
|
|
9
|
-
private queue;
|
|
10
|
-
private currentBuffer;
|
|
11
|
-
constructor(connection: Connection, options: RequestHandlerOptions);
|
|
12
|
-
private handleData;
|
|
13
|
-
sendRequest<Request, Response>(api: Api<Request, Response>, args: Request): Promise<Response>;
|
|
14
|
-
}
|
|
15
|
-
export type SendRequest = typeof RequestHandler.prototype.sendRequest;
|
|
16
|
-
export {};
|
package/dist/request-handler.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.RequestHandler = void 0;
|
|
7
|
-
const node_assert_1 = __importDefault(require("node:assert"));
|
|
8
|
-
const decoder_1 = require("./utils/decoder");
|
|
9
|
-
const encoder_1 = require("./utils/encoder");
|
|
10
|
-
class RequestHandler {
|
|
11
|
-
connection;
|
|
12
|
-
options;
|
|
13
|
-
queue = {};
|
|
14
|
-
currentBuffer = null;
|
|
15
|
-
constructor(connection, options) {
|
|
16
|
-
this.connection = connection;
|
|
17
|
-
this.options = options;
|
|
18
|
-
this.connection.on("data", this.handleData);
|
|
19
|
-
}
|
|
20
|
-
handleData(buffer) {
|
|
21
|
-
this.currentBuffer = this.currentBuffer ? Buffer.concat([this.currentBuffer, buffer]) : buffer;
|
|
22
|
-
if (this.currentBuffer.length < 4) {
|
|
23
|
-
return;
|
|
24
|
-
}
|
|
25
|
-
const decoder = (0, decoder_1.createDecoder)({ buffer: this.currentBuffer });
|
|
26
|
-
const size = decoder.readInt32();
|
|
27
|
-
if (size > decoder.buffer.length) {
|
|
28
|
-
return;
|
|
29
|
-
}
|
|
30
|
-
const correlationId = decoder.readInt32();
|
|
31
|
-
const request = this.queue[correlationId];
|
|
32
|
-
delete this.queue[correlationId];
|
|
33
|
-
request.callback(decoder);
|
|
34
|
-
// debug(handleData.name, 'Response offsets', {
|
|
35
|
-
// offset: decoder.offset,
|
|
36
|
-
// length: decoder.buffer.length,
|
|
37
|
-
// rest: decoder.buffer.subarray(decoder.offset, decoder.buffer.length)?.toString(),
|
|
38
|
-
// });
|
|
39
|
-
(0, node_assert_1.default)(decoder.offset - 4 === size, `Buffer not correctly consumed: ${decoder.offset - 4} !== ${buffer.length}`);
|
|
40
|
-
this.currentBuffer = null;
|
|
41
|
-
}
|
|
42
|
-
async sendRequest(api, args) {
|
|
43
|
-
const correlationId = Math.floor(Math.random() * 1000000);
|
|
44
|
-
const encoder = (0, encoder_1.createEncoder)()
|
|
45
|
-
.writeInt16(api.apiKey)
|
|
46
|
-
.writeInt16(api.apiVersion)
|
|
47
|
-
.writeInt32(correlationId)
|
|
48
|
-
.writeString(this.options.clientId);
|
|
49
|
-
const request = api.request(encoder, args).value();
|
|
50
|
-
const buffer = (0, encoder_1.createEncoder)().writeInt32(request.length).write(request).value();
|
|
51
|
-
return new Promise(async (resolve, reject) => {
|
|
52
|
-
await this.connection.write(buffer);
|
|
53
|
-
this.queue[correlationId] = {
|
|
54
|
-
callback: (decoder) => {
|
|
55
|
-
try {
|
|
56
|
-
const response = api.response(decoder);
|
|
57
|
-
resolve(response);
|
|
58
|
-
}
|
|
59
|
-
catch (error) {
|
|
60
|
-
reject(error);
|
|
61
|
-
}
|
|
62
|
-
},
|
|
63
|
-
};
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
exports.RequestHandler = RequestHandler;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const kafka: import("./client").Client;
|