import * as source from '../referers-latest.yaml' ; interface RefererInfo { known : Boolean ; referer ? : string ; medium ? : string ; params ? : string [ ] ; terms ? : string ; } interface RefererData { name : string ; medium : string ; params : Array < string > ; } let REFERERS = { } ; for ( const medium in source ) { let list = source [ medium ] ; for ( const name in list ) { let config = list [ name ] ; let params = null ; if ( config . parameters ) { params = config . parameters . map ( function ( p ) { return p . toLowerCase ( ) ; } ) ; } for ( const idx in config . domains ) { if ( config . domains . hasOwnProperty ( idx ) ) { const domain = config . domains [ idx ] ; REFERERS [ domain ] = { name , medium , params , } ; } } } } function lookup ( hostname , path ? ) { let referer = REFERERS [ hostname ] ; if ( path != null ) { referer = REFERERS [ hostname + path ] ; } https : if ( referer == null && path != null ) { let parts = path . split ( '/' ) ; if ( parts . length > 0 ) { referer = REFERERS [ `${ hostname }/${ parts [ 1 ] }` ] ; } } if ( referer == null ) { let idx = hostname . indexOf ( '.' ) ; if ( idx != - 1 ) { let newHost = hostname . slice ( idx + 1 ) ; return lookup ( newHost , path ) ; } } return referer ; } export function parse ( url , domain ? ) { let uri = new URL ( url ) ; let ref = { medium : 'unknown' , known : Boolean ( ~ [ 'http:' , 'https:' ] . indexOf ( uri . protocol ) ) , } ; if ( ! ref . known ) { return null ; } let data = lookup ( uri . hostname ) ; if ( data != null ) { ref . medium = data . medium ; ref . referer = data . name ; } return ref ; }