forked from InteractiveAdvertisingBureau/iabtcf-es
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentEncoder.ts
More file actions
169 lines (102 loc) · 4.12 KB
/
SegmentEncoder.ts
File metadata and controls
169 lines (102 loc) · 4.12 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import {Base64Url} from './Base64Url.js';
import {BitLength} from './BitLength.js';
import {FieldEncoderMap, IntEncoder, VendorVectorEncoder} from './field/index.js';
import {FieldSequence} from './sequence/index.js';
import {EncodingError, DecodingError} from '../errors/index.js';
import {Fields} from '../model/Fields.js';
import {Segment, SegmentIDs} from '../model/index.js';
import {TCModel, TCModelPropType} from '../index.js';
export class SegmentEncoder {
private static fieldSequence: FieldSequence = new FieldSequence();
public static encode(tcModel: TCModel, segment: Segment): string {
let sequence;
try {
sequence = this.fieldSequence[String(tcModel.version)][segment];
} catch (err) {
throw new EncodingError(`Unable to encode version: ${tcModel.version}, segment: ${segment}`);
}
let bitField = '';
/**
* If this is anything other than the core segment we have a "segment id"
* to append to the front of the string
*/
if (segment !== Segment.CORE) {
bitField = IntEncoder.encode(SegmentIDs.KEY_TO_ID[segment], BitLength.segmentType);
}
const fieldEncoderMap = FieldEncoderMap();
sequence.forEach((key: string): void => {
const value: TCModelPropType = tcModel[key];
const encoder = fieldEncoderMap[key];
let numBits: number = BitLength[key];
if (numBits === undefined) {
if (this.isPublisherCustom(key)) {
/**
* publisherCustom[Consents | LegitimateInterests] are an edge case
* because they are of variable length. The length is defined in a
* separate field named numCustomPurposes.
*/
numBits = Number(tcModel[Fields.numCustomPurposes]);
}
}
try {
bitField += encoder.encode(value, numBits);
} catch (err) {
throw new EncodingError(`Error encoding ${segment}->${key}: ${err.message}`);
}
});
// base64url encode the string and return
return Base64Url.encode(bitField);
}
public static decode(encodedString: string, tcModel: TCModel, segment: string): TCModel {
const bitField = Base64Url.decode(encodedString);
let bStringIdx = 0;
if (segment === Segment.CORE) {
tcModel.version = IntEncoder.decode(bitField.substr(bStringIdx, BitLength[Fields.version]), BitLength[Fields.version]);
}
if (segment !== Segment.CORE) {
bStringIdx += BitLength.segmentType;
}
const sequence = this.fieldSequence[String(tcModel.version)][segment];
const fieldEncoderMap = FieldEncoderMap();
sequence.forEach((key: string): void => {
const encoder = fieldEncoderMap[key];
let numBits = BitLength[key];
if (numBits === undefined) {
if (this.isPublisherCustom(key)) {
/**
* publisherCustom[Consents | LegitimateInterests] are an edge case
* because they are of variable length. The length is defined in a
* separate field named numCustomPurposes.
*/
numBits = Number(tcModel[Fields.numCustomPurposes]);
}
}
if (numBits !== 0) {
/**
* numBits could be 0 if this is a publisher custom purposes field and
* no custom purposes are defined. If that is the case, we don't need
* to gather no bits and we don't need to increment our bStringIdx
* pointer because those would all be 0 increments and would mess up
* the next logical if statement.
*/
const bits = bitField.substr(bStringIdx, numBits);
if (encoder === VendorVectorEncoder) {
tcModel[key] = encoder.decode(bits, tcModel.version);
} else {
tcModel[key] = encoder.decode(bits, numBits);
}
if (Number.isInteger(numBits)) {
bStringIdx += numBits;
} else if (Number.isInteger(tcModel[key].bitLength)) {
bStringIdx += tcModel[key].bitLength;
} else {
throw new DecodingError(key);
}
}
});
return tcModel;
}
private static isPublisherCustom(key: string): boolean {
return key.indexOf('publisherCustom') === 0;
}
}