11export type AllowedBaggageKeys = 'environment' | 'release' ; // TODO: Add remaining allowed baggage keys | 'transaction' | 'userid' | 'usersegment';
22export type BaggageObj = Partial < Record < AllowedBaggageKeys , string > & Record < string , string > > ;
3+
4+ /**
5+ * The baggage data structure represents key,value pairs based on the baggage
6+ * spec: https://www.w3.org/TR/baggage
7+ *
8+ * It is expected that users interact with baggage using the helpers methods:
9+ * `createBaggage`, `getBaggageValue`, and `setBaggageValue`.
10+ *
11+ * Internally, the baggage data structure is a tuple of length 2, seperating baggage values
12+ * based on if they are related to Sentry or not. If the baggage values are
13+ * set/used by sentry, they will be stored in an object to be easily accessed.
14+ * If they are not, they are kept as a string to be only accessed when serialized
15+ * at baggage propogation time.
16+ */
317export type Baggage = [ BaggageObj , string ] ;
418
519export const BAGGAGE_HEADER_NAME = 'baggage' ;
@@ -8,8 +22,6 @@ export const SENTRY_BAGGAGE_KEY_PREFIX = 'sentry-';
822
923export const SENTRY_BAGGAGE_KEY_PREFIX_REGEX = / ^ s e n t r y - / ;
1024
11- // baggage = sentry-environment=prod;my-info=true;
12-
1325/**
1426 * Max length of a serialized baggage string
1527 *
@@ -35,7 +47,8 @@ export function setBaggageValue(baggage: Baggage, key: keyof BaggageObj, value:
3547/** Serialize a baggage object */
3648export function serializeBaggage ( baggage : Baggage ) : string {
3749 return Object . keys ( baggage [ 0 ] ) . reduce ( ( prev , key ) => {
38- const baggageEntry = `${ SENTRY_BAGGAGE_KEY_PREFIX } ${ key } =${ baggage [ 0 ] [ key as keyof BaggageObj ] } ` ;
50+ const val = baggage [ 0 ] [ key as keyof BaggageObj ] as string ;
51+ const baggageEntry = `${ SENTRY_BAGGAGE_KEY_PREFIX } ${ encodeURIComponent ( key ) } =${ encodeURIComponent ( val ) } ` ;
3952 const newVal = prev === '' ? baggageEntry : `${ prev } ,${ baggageEntry } ` ;
4053 return newVal . length > MAX_BAGGAGE_STRING_LENGTH ? prev : newVal ;
4154 } , baggage [ 1 ] ) ;
@@ -47,10 +60,11 @@ export function parseBaggageString(baggageString: string): Baggage {
4760 ( [ baggageObj , baggageString ] , curr ) => {
4861 const [ key , val ] = curr . split ( '=' ) ;
4962 if ( SENTRY_BAGGAGE_KEY_PREFIX_REGEX . test ( key ) ) {
63+ const baggageKey = decodeURIComponent ( key . split ( '-' ) [ 1 ] ) ;
5064 return [
5165 {
5266 ...baggageObj ,
53- [ key . split ( '-' ) [ 1 ] ] : val ,
67+ [ baggageKey ] : decodeURIComponent ( val ) ,
5468 } ,
5569 baggageString ,
5670 ] ;
0 commit comments