1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.parseKeyPairsIntoRecord = exports.parsePairKeyValue = exports.getKeyPairs = exports.serializeKeyPairs = void 0;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | const api_1 = require("@opentelemetry/api");
|
20 | const constants_1 = require("./constants");
|
21 | function serializeKeyPairs(keyPairs) {
|
22 | return keyPairs.reduce((hValue, current) => {
|
23 | const value = `${hValue}${hValue !== '' ? constants_1.BAGGAGE_ITEMS_SEPARATOR : ''}${current}`;
|
24 | return value.length > constants_1.BAGGAGE_MAX_TOTAL_LENGTH ? hValue : value;
|
25 | }, '');
|
26 | }
|
27 | exports.serializeKeyPairs = serializeKeyPairs;
|
28 | function getKeyPairs(baggage) {
|
29 | return baggage.getAllEntries().map(([key, value]) => {
|
30 | let entry = `${encodeURIComponent(key)}=${encodeURIComponent(value.value)}`;
|
31 |
|
32 |
|
33 | if (value.metadata !== undefined) {
|
34 | entry += constants_1.BAGGAGE_PROPERTIES_SEPARATOR + value.metadata.toString();
|
35 | }
|
36 | return entry;
|
37 | });
|
38 | }
|
39 | exports.getKeyPairs = getKeyPairs;
|
40 | function parsePairKeyValue(entry) {
|
41 | const valueProps = entry.split(constants_1.BAGGAGE_PROPERTIES_SEPARATOR);
|
42 | if (valueProps.length <= 0)
|
43 | return;
|
44 | const keyPairPart = valueProps.shift();
|
45 | if (!keyPairPart)
|
46 | return;
|
47 | const separatorIndex = keyPairPart.indexOf(constants_1.BAGGAGE_KEY_PAIR_SEPARATOR);
|
48 | if (separatorIndex <= 0)
|
49 | return;
|
50 | const key = decodeURIComponent(keyPairPart.substring(0, separatorIndex).trim());
|
51 | const value = decodeURIComponent(keyPairPart.substring(separatorIndex + 1).trim());
|
52 | let metadata;
|
53 | if (valueProps.length > 0) {
|
54 | metadata = (0, api_1.baggageEntryMetadataFromString)(valueProps.join(constants_1.BAGGAGE_PROPERTIES_SEPARATOR));
|
55 | }
|
56 | return { key, value, metadata };
|
57 | }
|
58 | exports.parsePairKeyValue = parsePairKeyValue;
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | function parseKeyPairsIntoRecord(value) {
|
64 | if (typeof value !== 'string' || value.length === 0)
|
65 | return {};
|
66 | return value
|
67 | .split(constants_1.BAGGAGE_ITEMS_SEPARATOR)
|
68 | .map(entry => {
|
69 | return parsePairKeyValue(entry);
|
70 | })
|
71 | .filter(keyPair => keyPair !== undefined && keyPair.value.length > 0)
|
72 | .reduce((headers, keyPair) => {
|
73 |
|
74 | headers[keyPair.key] = keyPair.value;
|
75 | return headers;
|
76 | }, {});
|
77 | }
|
78 | exports.parseKeyPairsIntoRecord = parseKeyPairsIntoRecord;
|
79 |
|
\ | No newline at end of file |