import isIp from 'is-ip' ; import { toString as uint8ArrayToString } from 'uint8arrays/to-string' ; export const isIP = isIp ; export const isV4 = isIp . v4 ; export const isV6 = isIp . v6 ; export const toBytes = function ( ip ) : Uint8Array { let offset = 0 ; let result ; if ( isV4 ( ip ) ) { result = new Uint8Array ( offset + 4 ) ; ip . split ( /\./g ) . forEach ( ( byte ) => { result [ offset ++ ] = parseInt ( byte , 10 ) & 0xff ; } ) ; } else if ( isV6 ( ip ) ) { const sections = ip . split ( ':' , 8 ) ; let i ; for ( i = 0 ; i < sections . length ; i ++ ) { const isv4 = isV4 ( sections [ i ] ) ; let v4Buffer ; if ( isv4 ) { v4Buffer = toBytes ( sections [ i ] ) ; sections [ i ] = uint8ArrayToString ( v4Buffer . slice ( 0 , 2 ) , 'base16' ) ; } if ( v4Buffer != null && ++ i < 8 ) { sections . splice ( i , 0 , uint8ArrayToString ( v4Buffer . slice ( 2 , 4 ) , 'base16' ) ) ; } } if ( sections [ 0 ] === '' ) { while ( sections . length < 8 ) sections . unshift ( '0' ) ; } else if ( sections [ sections . length - 1 ] === '' ) { while ( sections . length < 8 ) sections . push ( '0' ) ; } else if ( sections . length < 8 ) { for ( i = 0 ; i < sections . length && sections [ i ] !== '' ; i ++ ) ; const argv = [ i , 1 ] ; for ( i = 9 - sections . length ; i > 0 ; i -- ) { argv . push ( '0' ) ; } sections . splice . apply ( sections , argv ) ; } result = new Uint8Array ( offset + 16 ) ; for ( i = 0 ; i < sections . length ; i ++ ) { const word = parseInt ( sections [ i ] , 16 ) ; result [ offset ++ ] = ( word >> 8 ) & 0xff ; result [ offset ++ ] = word & 0xff ; } } if ( result == null ) { throw Error ( 'Invalid ip address: ' + ip ) ; } return result ; } ; export const toString = function ( buf , offset , length ) { offset = ~ ~ offset ; length = length ?? ( buf . length - offset ) ; const result = [ ] ; let string = '' ; const view = new DataView ( buf . buffer ) ; if ( length === 4 ) { for ( let i = 0 ; i < length ; i ++ ) { result . push ( buf [ offset + i ] ) ; } string = result . join ( '.' ) ; } else if ( length === 16 ) { for ( let i = 0 ; i < length ; i += 2 ) { result . push ( view . getUint16 ( offset + i ) . toString ( 16 ) ) ; } string = result . join ( ':' ) ; string = string . replace ( /(^|:)0(:0)*:0(:|$)/ , '$1::$3' ) ; string = string . replace ( /:{3,4}/ , '::' ) ; } return string ; } ;