mirror of https://github.com/grpc/grpc-node.git
Make IPv6 parsing code compatible with Node 10
This commit is contained in:
parent
1a60c4f3a9
commit
b1be84a021
|
@ -393,7 +393,7 @@ export function unregisterChannelzRef(ref: ChannelRef | SubchannelRef | ServerRe
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parses a single section of an IPv6 address as two bytes
|
* Parse a single section of an IPv6 address as two bytes
|
||||||
* @param addressSection A hexadecimal string of length up to 4
|
* @param addressSection A hexadecimal string of length up to 4
|
||||||
* @returns The pair of bytes representing this address section
|
* @returns The pair of bytes representing this address section
|
||||||
*/
|
*/
|
||||||
|
@ -402,6 +402,21 @@ function parseIPv6Section(addressSection: string): [number, number] {
|
||||||
return [numberValue / 256 | 0, numberValue % 256];
|
return [numberValue / 256 | 0, numberValue % 256];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse a chunk of an IPv6 address string to some number of bytes
|
||||||
|
* @param addressChunk Some number of segments of up to 4 hexadecimal
|
||||||
|
* characters each, joined by colons.
|
||||||
|
* @returns The list of bytes representing this address chunk
|
||||||
|
*/
|
||||||
|
function parseIPv6Chunk(addressChunk: string): number[] {
|
||||||
|
if (addressChunk === '') {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
const bytePairs = addressChunk.split(':').map(section => parseIPv6Section(section));
|
||||||
|
const result: number[] = [];
|
||||||
|
return result.concat(...bytePairs);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts an IPv4 or IPv6 address from string representation to binary
|
* Converts an IPv4 or IPv6 address from string representation to binary
|
||||||
* representation
|
* representation
|
||||||
|
@ -413,17 +428,17 @@ function ipAddressStringToBuffer(ipAddress: string): Buffer | null {
|
||||||
return Buffer.from(Uint8Array.from(ipAddress.split('.').map(segment => Number.parseInt(segment))));
|
return Buffer.from(Uint8Array.from(ipAddress.split('.').map(segment => Number.parseInt(segment))));
|
||||||
} else if (isIPv6(ipAddress)) {
|
} else if (isIPv6(ipAddress)) {
|
||||||
let leftSection: string;
|
let leftSection: string;
|
||||||
let rightSection: string | null;
|
let rightSection: string;
|
||||||
const doubleColonIndex = ipAddress.indexOf('::');
|
const doubleColonIndex = ipAddress.indexOf('::');
|
||||||
if (doubleColonIndex === -1) {
|
if (doubleColonIndex === -1) {
|
||||||
leftSection = ipAddress;
|
leftSection = ipAddress;
|
||||||
rightSection = null;
|
rightSection = '';
|
||||||
} else {
|
} else {
|
||||||
leftSection = ipAddress.substring(0, doubleColonIndex);
|
leftSection = ipAddress.substring(0, doubleColonIndex);
|
||||||
rightSection = ipAddress.substring(doubleColonIndex + 2);
|
rightSection = ipAddress.substring(doubleColonIndex + 2);
|
||||||
}
|
}
|
||||||
const leftBuffer = Buffer.from(leftSection.split(':').map(segment => parseIPv6Section(segment)).flat());
|
const leftBuffer = Buffer.from(parseIPv6Chunk(leftSection));
|
||||||
const rightBuffer = rightSection ? Buffer.from(rightSection.split(':').map(segment => parseIPv6Section(segment)).flat()) : Buffer.alloc(0);
|
const rightBuffer = Buffer.from(parseIPv6Chunk(rightSection));
|
||||||
const middleBuffer = Buffer.alloc(16 - leftBuffer.length - rightBuffer.length, 0);
|
const middleBuffer = Buffer.alloc(16 - leftBuffer.length - rightBuffer.length, 0);
|
||||||
return Buffer.concat([leftBuffer, middleBuffer, rightBuffer]);
|
return Buffer.concat([leftBuffer, middleBuffer, rightBuffer]);
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in New Issue