1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | Object.defineProperty(exports, "__esModule", { value: true });
|
18 | exports.deserializeTextFormat = exports.serializeTextFormat = exports.MAX_NUMBER_OF_TAGS = void 0;
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | const tag_map_1 = require("../tag-map");
|
28 | const types_1 = require("../types");
|
29 | exports.MAX_NUMBER_OF_TAGS = 180;
|
30 | const TAG_SERIALIZED_SIZE_LIMIT = 4096;
|
31 | const TAGMAP_SERIALIZED_SIZE_LIMIT = 8192;
|
32 | const TAG_KEY_VALUE_DELIMITER = '=';
|
33 | const TAG_DELIMITER = ',';
|
34 | const UNLIMITED_PROPAGATION_MD = {
|
35 | tagTtl: types_1.TagTtl.UNLIMITED_PROPAGATION,
|
36 | };
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | function serializeTextFormat(tagMap) {
|
43 | let ret = '';
|
44 | let totalChars = 0;
|
45 | let totalTags = 0;
|
46 | const tags = tagMap.tagsWithMetadata;
|
47 | tags.forEach((tagsWithMetadata, tagKey) => {
|
48 | if (tagsWithMetadata.tagMetadata.tagTtl !== types_1.TagTtl.NO_PROPAGATION) {
|
49 | if (ret.length > 0)
|
50 | ret += TAG_DELIMITER;
|
51 | totalChars += validateTag(tagKey, tagsWithMetadata.tagValue);
|
52 | ret +=
|
53 | tagKey.name + TAG_KEY_VALUE_DELIMITER + tagsWithMetadata.tagValue.value;
|
54 | totalTags++;
|
55 | }
|
56 | });
|
57 | if (totalTags > exports.MAX_NUMBER_OF_TAGS) {
|
58 | throw new Error(`Number of tags in the TagMap exceeds limit ${exports.MAX_NUMBER_OF_TAGS}`);
|
59 | }
|
60 | if (totalChars > TAGMAP_SERIALIZED_SIZE_LIMIT) {
|
61 | throw new Error(`Size of TagMap exceeds the maximum serialized size ${TAGMAP_SERIALIZED_SIZE_LIMIT}`);
|
62 | }
|
63 | return ret;
|
64 | }
|
65 | exports.serializeTextFormat = serializeTextFormat;
|
66 |
|
67 |
|
68 |
|
69 |
|
70 | function deserializeTextFormat(str) {
|
71 | const tags = new tag_map_1.TagMap();
|
72 | if (!str)
|
73 | return tags;
|
74 | const listOfTags = str.split(TAG_DELIMITER);
|
75 | listOfTags.forEach(tag => {
|
76 | const keyValuePair = tag.split(TAG_KEY_VALUE_DELIMITER);
|
77 | if (keyValuePair.length !== 2)
|
78 | throw new Error(`Malformed tag ${tag}`);
|
79 | const [name, value] = keyValuePair;
|
80 | tags.set({ name }, { value }, UNLIMITED_PROPAGATION_MD);
|
81 | });
|
82 | return tags;
|
83 | }
|
84 | exports.deserializeTextFormat = deserializeTextFormat;
|
85 | function validateTag(tagKey, tagValue) {
|
86 | const charsOfTag = tagKey.name.length + tagValue.value.length;
|
87 | if (charsOfTag > TAG_SERIALIZED_SIZE_LIMIT) {
|
88 | throw new Error(`Serialized size of tag exceeds limit ${TAG_SERIALIZED_SIZE_LIMIT}`);
|
89 | }
|
90 | return charsOfTag;
|
91 | }
|
92 |
|
\ | No newline at end of file |