UNPKG

3.32 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.parseKeyPairsIntoRecord = exports.parsePairKeyValue = exports.getKeyPairs = exports.serializeKeyPairs = void 0;
4/*
5 * Copyright The OpenTelemetry Authors
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * https://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19const api_1 = require("@opentelemetry/api");
20const constants_1 = require("./constants");
21function 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}
27exports.serializeKeyPairs = serializeKeyPairs;
28function getKeyPairs(baggage) {
29 return baggage.getAllEntries().map(([key, value]) => {
30 let entry = `${encodeURIComponent(key)}=${encodeURIComponent(value.value)}`;
31 // include opaque metadata if provided
32 // NOTE: we intentionally don't URI-encode the metadata - that responsibility falls on the metadata implementation
33 if (value.metadata !== undefined) {
34 entry += constants_1.BAGGAGE_PROPERTIES_SEPARATOR + value.metadata.toString();
35 }
36 return entry;
37 });
38}
39exports.getKeyPairs = getKeyPairs;
40function 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 keyPair = keyPairPart.split(constants_1.BAGGAGE_KEY_PAIR_SEPARATOR);
48 if (keyPair.length !== 2)
49 return;
50 const key = decodeURIComponent(keyPair[0].trim());
51 const value = decodeURIComponent(keyPair[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}
58exports.parsePairKeyValue = parsePairKeyValue;
59/**
60 * Parse a string serialized in the baggage HTTP Format (without metadata):
61 * https://github.com/w3c/baggage/blob/master/baggage/HTTP_HEADER_FORMAT.md
62 */
63function 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 // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
74 headers[keyPair.key] = keyPair.value;
75 return headers;
76 }, {});
77}
78exports.parseKeyPairsIntoRecord = parseKeyPairsIntoRecord;
79//# sourceMappingURL=utils.js.map
\No newline at end of file