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