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