import ID from '../../trace/ID' ; import { CarrierItem } from './CarrierItem' ; export class ContextCarrier extends CarrierItem { constructor ( public traceId ? , public segmentId ? , public spanId ? , public service ? , public serviceInstance ? , public endpoint ? , public clientAddress ? , public items = [ ] ) { super ( 'sw8' ) ; this . items . push ( this ) ; } private encode = ( s ) : string => { return Buffer . from ( s ) . toString ( 'base64' ) ; } ; private decode = ( s ) : string => { return Buffer . from ( s , 'base64' ) . toString ( ) ; } ; get value ( ) : string { return [ '1' , this . encode ( this . traceId ?. toString ( ) || '' ) , this . encode ( this . segmentId ?. toString ( ) || '' ) , this . spanId ?. toString ( ) , this . encode ( this . service || '' ) , this . encode ( this . serviceInstance || '' ) , this . encode ( this . endpoint || '' ) , this . encode ( this . clientAddress || '' ) , ] . join ( '-' ) ; } set value ( val ) { const parts = val . split ( '-' ) ; this . traceId = new ID ( this . decode ( parts [ 1 ] ) ) ; this . segmentId = new ID ( this . decode ( parts [ 2 ] ) ) ; this . spanId = Number . parseInt ( parts [ 3 ] , 10 ) ; this . service = this . decode ( parts [ 4 ] ) ; this . serviceInstance = this . decode ( parts [ 5 ] ) ; this . endpoint = this . decode ( parts [ 6 ] ) ; this . clientAddress = this . decode ( parts [ 7 ] ) ; } isValid ( ) { return Boolean ( this . traceId ?. rawId && this . segmentId ?. rawId && this . spanId !== undefined && ! isNaN ( this . spanId ) && this . service && this . endpoint && this . clientAddress !== undefined ) ; } public static from ( map ) { if ( ! map . hasOwnProperty ( 'sw8' ) ) return ; const carrier = new ContextCarrier ( ) ; carrier . items . filter ( ( item ) => map . hasOwnProperty ( item . key ) ) . forEach ( ( item ) => ( item . value = map [ item . key ] ) ) ; return carrier ; } }