forked from InteractiveAdvertisingBureau/iabtcf-es
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTCString.ts
More file actions
109 lines (77 loc) · 2.79 KB
/
TCString.ts
File metadata and controls
109 lines (77 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import {
Base64Url,
BitLength,
EncodingOptions,
SegmentEncoder,
SegmentSequence,
SemanticPreEncoder,
} from './encoder/index.js';
import {Segment, SegmentIDs} from './model/index.js';
import {IntEncoder} from './encoder/field/IntEncoder.js';
import {TCModel} from './TCModel.js';
/**
* Main class for encoding and decoding a
* TCF Transparency and Consent String
*/
export class TCString {
/**
* encodes a model into a TCString
*
* @param {TCModel} tcModel - model to convert into encoded string
* @param {EncodingOptions} options - for encoding options other than default
* @return {string} - base64url encoded Transparency and Consent String
*/
public static encode(tcModel: TCModel, options?: EncodingOptions): string {
let out = '';
let sequence: Segment[];
tcModel = SemanticPreEncoder.process(tcModel, options);
/**
* If they pass in a special segment sequence.
*/
if (Array.isArray(options?.segments)) {
sequence = options.segments;
} else {
sequence = new SegmentSequence(tcModel, options)[''+tcModel.version];
}
sequence.forEach((segment: Segment, idx: number): void => {
let dotMaybe = '';
if (idx < sequence.length - 1) {
dotMaybe = '.';
}
out += SegmentEncoder.encode(tcModel, segment) + dotMaybe;
});
return out;
}
/**
* Decodes a string into a TCModel
*
* @param {string} encodedTCString - base64url encoded Transparency and
* Consent String to decode - can also be a single or group of segments of
* the string
* @param {string} [tcModel] - model to enhance with the information. If
* none is passed a new instance of TCModel will be created.
* @return {TCModel} - Returns populated TCModel
*/
public static decode(encodedTCString: string, tcModel?: TCModel): TCModel {
const segments: string[] = encodedTCString.split('.');
const len: number = segments.length;
if (!tcModel) {
tcModel = new TCModel();
}
for (let i = 0; i < len; i ++) {
const segString: string = segments[i];
/**
* first char will contain 6 bits, we only need the first 3. In version 1
* and 2 of the TC string there is no segment type for the CORE string.
* Instead the first 6 bits are reserved for the encoding version, but
* because we're only on a maximum of encoding version 2 the first 3 bits
* in the core segment will evaluate to 0.
*/
const firstChar: string = Base64Url.decode(segString.charAt(0));
const segTypeBits: string = firstChar.substr(0, BitLength.segmentType);
const segment = SegmentIDs.ID_TO_KEY[IntEncoder.decode(segTypeBits, BitLength.segmentType).toString()];
SegmentEncoder.decode(segString, tcModel, segment);
}
return tcModel;
}
}