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.TraceStateImpl = void 0;
|
19 | var tracestate_validators_1 = require("./tracestate-validators");
|
20 | var MAX_TRACE_STATE_ITEMS = 32;
|
21 | var MAX_TRACE_STATE_LEN = 512;
|
22 | var LIST_MEMBERS_SEPARATOR = ',';
|
23 | var LIST_MEMBER_KEY_VALUE_SPLITTER = '=';
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | var TraceStateImpl = (function () {
|
34 | function TraceStateImpl(rawTraceState) {
|
35 | this._internalState = new Map();
|
36 | if (rawTraceState)
|
37 | this._parse(rawTraceState);
|
38 | }
|
39 | TraceStateImpl.prototype.set = function (key, value) {
|
40 |
|
41 |
|
42 | var traceState = this._clone();
|
43 | if (traceState._internalState.has(key)) {
|
44 | traceState._internalState.delete(key);
|
45 | }
|
46 | traceState._internalState.set(key, value);
|
47 | return traceState;
|
48 | };
|
49 | TraceStateImpl.prototype.unset = function (key) {
|
50 | var traceState = this._clone();
|
51 | traceState._internalState.delete(key);
|
52 | return traceState;
|
53 | };
|
54 | TraceStateImpl.prototype.get = function (key) {
|
55 | return this._internalState.get(key);
|
56 | };
|
57 | TraceStateImpl.prototype.serialize = function () {
|
58 | var _this = this;
|
59 | return this._keys()
|
60 | .reduce(function (agg, key) {
|
61 | agg.push(key + LIST_MEMBER_KEY_VALUE_SPLITTER + _this.get(key));
|
62 | return agg;
|
63 | }, [])
|
64 | .join(LIST_MEMBERS_SEPARATOR);
|
65 | };
|
66 | TraceStateImpl.prototype._parse = function (rawTraceState) {
|
67 | if (rawTraceState.length > MAX_TRACE_STATE_LEN)
|
68 | return;
|
69 | this._internalState = rawTraceState
|
70 | .split(LIST_MEMBERS_SEPARATOR)
|
71 | .reverse()
|
72 | .reduce(function (agg, part) {
|
73 | var listMember = part.trim();
|
74 | var i = listMember.indexOf(LIST_MEMBER_KEY_VALUE_SPLITTER);
|
75 | if (i !== -1) {
|
76 | var key = listMember.slice(0, i);
|
77 | var value = listMember.slice(i + 1, part.length);
|
78 | if (tracestate_validators_1.validateKey(key) && tracestate_validators_1.validateValue(value)) {
|
79 | agg.set(key, value);
|
80 | }
|
81 | else {
|
82 |
|
83 | }
|
84 | }
|
85 | return agg;
|
86 | }, new Map());
|
87 |
|
88 | if (this._internalState.size > MAX_TRACE_STATE_ITEMS) {
|
89 | this._internalState = new Map(Array.from(this._internalState.entries())
|
90 | .reverse()
|
91 | .slice(0, MAX_TRACE_STATE_ITEMS));
|
92 | }
|
93 | };
|
94 | TraceStateImpl.prototype._keys = function () {
|
95 | return Array.from(this._internalState.keys()).reverse();
|
96 | };
|
97 | TraceStateImpl.prototype._clone = function () {
|
98 | var traceState = new TraceStateImpl();
|
99 | traceState._internalState = new Map(this._internalState);
|
100 | return traceState;
|
101 | };
|
102 | return TraceStateImpl;
|
103 | }());
|
104 | exports.TraceStateImpl = TraceStateImpl;
|
105 |
|
\ | No newline at end of file |