remarks export function ipv6StringFromBuffer ( buffer ) { const sections = getHexSections ( buffer ) ; const result = sectionsToString ( sections ) ; return result ; } function getHexSections ( buffer ) { const bytesPerSection = 2 ; const sections = [ ] ; for ( let i = 0 ; i < buffer . length ; i += bytesPerSection ) { let section = buffer . slice ( i , i + bytesPerSection ) . toString ( "hex" ) ; section = discardLeadingZeros ( section ) ; sections . push ( section ) ; } return sections ; } function discardLeadingZeros ( section ) { return section . replace ( /^0+/g , "" ) ; } function sectionsToString ( sections ) { let result = "" ; let hasCollapsed = false ; for ( const s of sections ) { if ( result . endsWith ( "::" ) && s === "" ) continue ; if ( s === "" && ! hasCollapsed ) { hasCollapsed = true ; if ( result === "" ) result += "::" ; else result += ":" ; continue ; } if ( hasCollapsed && s === "" ) { result += "0:" ; continue ; } result += s + ":" ; } if ( ! result . endsWith ( "::" ) ) result = result . slice ( 0 , - 1 ) ; return result ; }