Skip to content

Commit 1a60c4f

Browse files
committed
grpc-js: channelz: Fix algorithm for representing an IPv6 address in binary
1 parent 78466ac commit 1a60c4f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

packages/grpc-js/src/channelz.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,16 @@ export function unregisterChannelzRef(ref: ChannelRef | SubchannelRef | ServerRe
392392
}
393393
}
394394

395+
/**
396+
* Parses a single section of an IPv6 address as two bytes
397+
* @param addressSection A hexadecimal string of length up to 4
398+
* @returns The pair of bytes representing this address section
399+
*/
400+
function parseIPv6Section(addressSection: string): [number, number] {
401+
const numberValue = Number.parseInt(addressSection, 16);
402+
return [numberValue / 256 | 0, numberValue % 256];
403+
}
404+
395405
/**
396406
* Converts an IPv4 or IPv6 address from string representation to binary
397407
* representation
@@ -412,8 +422,8 @@ function ipAddressStringToBuffer(ipAddress: string): Buffer | null {
412422
leftSection = ipAddress.substring(0, doubleColonIndex);
413423
rightSection = ipAddress.substring(doubleColonIndex + 2);
414424
}
415-
const leftBuffer = Uint8Array.from(leftSection.split(':').map(segment => Number.parseInt(segment, 16)));
416-
const rightBuffer = rightSection ? Uint8Array.from(rightSection.split(':').map(segment => Number.parseInt(segment, 16))) : new Uint8Array();
425+
const leftBuffer = Buffer.from(leftSection.split(':').map(segment => parseIPv6Section(segment)).flat());
426+
const rightBuffer = rightSection ? Buffer.from(rightSection.split(':').map(segment => parseIPv6Section(segment)).flat()) : Buffer.alloc(0);
417427
const middleBuffer = Buffer.alloc(16 - leftBuffer.length - rightBuffer.length, 0);
418428
return Buffer.concat([leftBuffer, middleBuffer, rightBuffer]);
419429
} else {

0 commit comments

Comments
 (0)