Repository navigation
Description
Environment Information
- OS [e.g. Mac, Arch, Windows 10]:
- Node Version [e.g. 8.2.1]:
- NPM Version [e.g. 5.4.2]:
- C++ Toolchain [e.g. Visual Studio, llvm, g++]:
- confluent-kafka-javascript version [e.g. 2.3.3]: 1.9.0
Additional context
The TypeScript declaration for MemberDescription (returned by admin.describeGroups()) disagrees with the value the native binding actually constructs. Four fields are affected.
The shape-disagreement on assignment is the painful one: callers reading member.assignment.map(...) per the .d.ts get a TypeError: member.assignment.map is not a function at runtime, because what the binding returns is a wrapping object, not an array.
Evidence
Declared type — types/rdkafka.d.ts (lines 385-394)
export type MemberDescription = {
clientHost: string;
clientId: string;
memberId: string;
memberAssignment: Buffer; // ← claims a populated Buffer
memberMetadata: Buffer; // ← claims a populated Buffer
groupInstanceId?: string;
assignment: TopicPartition[]; // ← claims a flat array
targetAssignment?: TopicPartition[]; // ← claims a flat array
};Actual runtime shape — src/common.cc FromMemberDescription() (lines 1024-1106)
The native binding's own JSDoc-style comment, at the top of FromMemberDescription, documents the real shape and contradicts the .d.ts:
v8::Local<v8::Object> FromMemberDescription(
const rd_kafka_MemberDescription_t* member) {
/* Return object type:
{
clientHost: string
clientId: string
memberId: string
memberAssignment: Buffer // will be always null
memberMetadata: Buffer // will be always null
groupInstanceId: string
assignment: {
topicPartitions: TopicPartition[]
},
targetAssignment?: {
topicPartitions: TopicPartition[]
}
}
*/
// ...
// memberAssignment - not passed to user, always null
Nan::Set(returnObject, Nan::New("memberAssignment").ToLocalChecked(),
Nan::Null());
// memberMetadata - not passed to user, always null
Nan::Set(returnObject, Nan::New("memberMetadata").ToLocalChecked(),
Nan::Null());
// ...
v8::Local<v8::Object> assignmentObject = Nan::New<v8::Object>();
Nan::Set(assignmentObject, Nan::New("topicPartitions").ToLocalChecked(),
topicPartitions);
Nan::Set(returnObject, Nan::New("assignment").ToLocalChecked(),
assignmentObject);
// ...
}Four discrepancies
| Field | .d.ts declares |
Native binding returns |
|---|---|---|
assignment |
TopicPartition[] |
{ topicPartitions: TopicPartition[] } |
targetAssignment |
TopicPartition[] (optional) |
{ topicPartitions: TopicPartition[] } (optional) |
memberAssignment |
Buffer (required) |
null always (per the C++ comment) |
memberMetadata |
Buffer (required) |
null always (per the C++ comment) |
Reproduction
A live consumer using the new KIP-848 ("Consumer") protocol against Confluent Cloud, described via admin.describeGroups():
const { KafkaJS } = require("@confluentinc/kafka-javascript");
const kafka = new KafkaJS.Kafka({
/* ... your cluster config ... */
});
const admin = kafka.admin();
await admin.connect();
const { groups } = await admin.describeGroups(["my-live-consumer-group"]);
const member = groups[0].members[0];
// The .d.ts says this is `TopicPartition[]`, so:
console.log(member.assignment.map((tp) => `${tp.topic}/${tp.partition}`));
// ↑ TypeError: member.assignment.map is not a function
// What the binding actually returned:
console.log(typeof member.assignment); // 'object'
console.log(Object.keys(member.assignment)); // [ 'topicPartitions' ]
console.log(member.assignment.topicPartitions); // [ { topic: ..., partition: ... }, ... ]
// And the Buffers that aren't:
console.log(member.memberAssignment); // null (.d.ts: Buffer)
console.log(member.memberMetadata); // null (.d.ts: Buffer)The simulated member.assignment.map is not a function error appears for every member of a real consumer group; classic-protocol groups exhibit the same shape (the wrapping isn't KIP-848-specific — the C++ binding always wraps).
Expected
The runtime shape matches the declared type, in either direction:
- Either update
types/rdkafka.d.tsto reflect what the binding actually does —assignment: { topicPartitions: TopicPartition[] },targetAssignment?: { topicPartitions: TopicPartition[] },memberAssignment: null(orBuffer | nullwith a doc clarifying the "always null in practice"),memberMetadata: null(same). - Or flatten the binding to match the existing
.d.ts—assignment: TopicPartition[](drop the wrapping object), and decide whether to populate theBufferfields or remove them from the type.
The first option is cheaper (only the .d.ts changes) and preserves the existing field that callers using .assignment.topicPartitions already work with.
The second option breaks all existing callers that already learned the wrapping.
If the wrapping object exists to leave room for future fields (e.g., to attach metadata to the assignment as a whole rather than per-partition), updating the type to acknowledge it is the right path; the type is just stale.
Actual
The four-field shape mismatch above.
Calling .map(...) on member.assignment per the .d.ts throws at runtime.
Reading member.memberAssignment per the .d.ts returns null rather than the documented Buffer.
Suggested fix
types/rdkafka.d.ts:
export type MemberAssignment = {
topicPartitions: TopicPartition[];
};
export type MemberDescription = {
clientHost: string;
clientId: string;
memberId: string;
/** @deprecated Always null in current binding; reserved for future use. */
memberAssignment: Buffer | null;
/** @deprecated Always null in current binding; reserved for future use. */
memberMetadata: Buffer | null;
groupInstanceId?: string;
assignment: MemberAssignment;
targetAssignment?: MemberAssignment;
};The MemberAssignment named type makes the wrapping a documented concept rather than an inline curiosity, and the @deprecated tags on the always-null fields tell callers not to rely on them while preserving source compatibility.
Workaround (what we adopted downstream)
Our handler reads member.assignment as unknown, then defensively branches on the shape:
function buildAssignment(member) {
const raw = member.assignment;
if (Array.isArray(raw)) {
// Documented (flat) shape — handles a future type/runtime reconciliation
return raw.map(toAssignedPartition);
}
if (raw && typeof raw === "object" && Array.isArray(raw.topicPartitions)) {
// Actual current runtime shape
return raw.topicPartitions.map(toAssignedPartition);
}
// Wacky -- log + empty fallback so future surprises stay visible
return [];
}This survives the binding flattening the shape, the type being corrected to acknowledge the wrapper, or some third future shape we haven't seen yet.
Notes
- Discovered while implementing a
describe-consumer-groupMCP tool that wrapsadmin.describeGroups(); the unit tests passed (they used the typed shape per the.d.ts) and the integration test happened to land only on aDead-state empty group (no members, soassignmentwas never accessed). The crash surfaced only on the first live use against a real, populated consumer group. - The undocumented
targetAssignmentfield is meaningful — it's the assignment the member is being moved toward during a KIP-848 incremental-cooperative rebalance. Calling code that wants to surface "current vs. target" view (e.g., for diagnostics) currently can't see it through TypeScript at all.
Activity
Fix the typescript return type for admin.describeGroups() per #487. (#…