import { Station } from './Packet' ; import { createHash } from 'crypto' ; export function timeout ( millis ) { return new Promise ( ( resolve ) => { setTimeout ( resolve , millis ) ; } ) ; } export function md5 ( buffer ) { return createHash ( 'md5' ) . update ( buffer ) . digest ( 'hex' ) ; } export function stationToCallsignSSID ( station ) { return typeof station . ssid === 'number' && station . ssid !== 0 ? `${ station . callsign }-${ station . ssid }` : station . callsign ; } function param { string } callsignSSID throws returns export function callsignSSIDToStation ( callsignSSID ) { if ( isCallsignSSID ( callsignSSID ) ) { const [ callsign , ssidStr ] = callsignSSID . split ( '-' ) ; const ssid = parseInt ( ssidStr ) ; return { callsign : callsign , ssid : ssid } ; } else if ( isCallsign ( callsignSSID ) ) { return { callsign : callsignSSID , ssid : 0 } ; } else { throw TypeError ( `Invalid callsignSSID: ${ callsignSSID }` ) ; } } export function isCallsign ( callsign ) { if ( typeof callsign !== 'string' || callsign . length > 6 ) return false ; callsign = callsign . toUpperCase ( ) . replace ( /\s*$/g , "" ) ; if ( callsign . length < 1 || callsign . length > 6 ) return false ; for ( let c = 0 ; c < callsign . length ; c ++ ) { let a = callsign [ c ] . charCodeAt ( 0 ) ; if ( ( a >= 48 && a <= 57 ) || ( a >= 65 && a <= 90 ) ) continue ; return false ; } return true ; } export function isCallsignSSID ( callsignSSID ) { if ( ( callsignSSID . match ( /-/g ) || [ ] ) . length !== 1 ) return false ; const [ callsign , ssid ] = callsignSSID . split ( '-' ) ; return isCallsign ( callsign ) && isSSID ( parseInt ( ssid ) ) ; } export function isSSID ( ssid ) { if ( typeof ssid !== 'number' ) throw TypeError ( 'ssid must be a number type' ) ; return ssid >= 0 && ssid <= 15 ; } export function isBrokenPipeError ( err ) { return err . code == 'EPIPE' || err . code == 'EIO' ; }