@@ -392,6 +392,31 @@ export function unregisterChannelzRef(ref: ChannelRef | SubchannelRef | ServerRe
392
392
}
393
393
}
394
394
395
+ /**
396
+ * Parse 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
+
405
+ /**
406
+ * Parse a chunk of an IPv6 address string to some number of bytes
407
+ * @param addressChunk Some number of segments of up to 4 hexadecimal
408
+ * characters each, joined by colons.
409
+ * @returns The list of bytes representing this address chunk
410
+ */
411
+ function parseIPv6Chunk ( addressChunk : string ) : number [ ] {
412
+ if ( addressChunk === '' ) {
413
+ return [ ] ;
414
+ }
415
+ const bytePairs = addressChunk . split ( ':' ) . map ( section => parseIPv6Section ( section ) ) ;
416
+ const result : number [ ] = [ ] ;
417
+ return result . concat ( ...bytePairs ) ;
418
+ }
419
+
395
420
/**
396
421
* Converts an IPv4 or IPv6 address from string representation to binary
397
422
* representation
@@ -403,17 +428,17 @@ function ipAddressStringToBuffer(ipAddress: string): Buffer | null {
403
428
return Buffer . from ( Uint8Array . from ( ipAddress . split ( '.' ) . map ( segment => Number . parseInt ( segment ) ) ) ) ;
404
429
} else if ( isIPv6 ( ipAddress ) ) {
405
430
let leftSection : string ;
406
- let rightSection : string | null ;
431
+ let rightSection : string ;
407
432
const doubleColonIndex = ipAddress . indexOf ( '::' ) ;
408
433
if ( doubleColonIndex === - 1 ) {
409
434
leftSection = ipAddress ;
410
- rightSection = null ;
435
+ rightSection = '' ;
411
436
} else {
412
437
leftSection = ipAddress . substring ( 0 , doubleColonIndex ) ;
413
438
rightSection = ipAddress . substring ( doubleColonIndex + 2 ) ;
414
439
}
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 ( ) ;
440
+ const leftBuffer = Buffer . from ( parseIPv6Chunk ( leftSection ) ) ;
441
+ const rightBuffer = Buffer . from ( parseIPv6Chunk ( rightSection ) ) ;
417
442
const middleBuffer = Buffer . alloc ( 16 - leftBuffer . length - rightBuffer . length , 0 ) ;
418
443
return Buffer . concat ( [ leftBuffer , middleBuffer , rightBuffer ] ) ;
419
444
} else {
0 commit comments