-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathbaggage.ts
31 lines (26 loc) · 1013 Bytes
/
baggage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { objectToBaggageHeader, parseBaggageHeader } from '@sentry/core';
/**
* Merge two baggage headers into one, where the existing one takes precedence.
* The order of the existing baggage will be preserved, and new entries will be added to the end.
*/
export function mergeBaggageHeaders<Existing extends string | string[] | number | undefined>(
existing: Existing,
baggage: string,
): string | undefined | Existing {
if (!existing) {
return baggage;
}
const existingBaggageEntries = parseBaggageHeader(existing);
const newBaggageEntries = parseBaggageHeader(baggage);
if (!newBaggageEntries) {
return existing;
}
// Existing entries take precedence, ensuring order remains stable for minimal changes
const mergedBaggageEntries = { ...existingBaggageEntries };
Object.entries(newBaggageEntries).forEach(([key, value]) => {
if (!mergedBaggageEntries[key]) {
mergedBaggageEntries[key] = value;
}
});
return objectToBaggageHeader(mergedBaggageEntries);
}