Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
125 lines (117 loc) · 5.2 KB
/
Copy pathindex.ts
File metadata and controls
125 lines (117 loc) · 5.2 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
/*
* @analytics-debugger/consent-string
*
* MIT License
* Copyright (c) 2026 David Vallejo
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import * as definitions from "./definitions";
import { mapConsentTypes, readBits, readCharValues, readField, readState, toGranted } from "./helpers";
import type { ConsentDetails, ConsentType, ConsentValues, DecodedGcd } from "./types";
export type {
ConsentDetails,
ConsentState,
ConsentType,
DecodedGcd,
GcdFlags,
GcdMeta,
GcdRaw,
} from "./types";
export function decode(gcd: string): DecodedGcd {
if (typeof gcd !== "string") {
throw new Error(`gcd must be a string, got ${gcd === null ? "null" : typeof gcd}`);
}
if (gcd.length < definitions.GCD_LENGTH) {
throw new Error(`gcd must be at least ${definitions.GCD_LENGTH} characters, got ${gcd.length}`);
}
const version = gcd.charAt(definitions.STRING_POSITIONS.version.position);
if (version !== definitions.CONSENT_STRING_VERSION) {
throw new Error(`Unsupported gcd version "${version}"`);
}
const chars = readCharValues(gcd);
const stored = mapConsentTypes((type) => {
const { position } = definitions.STRING_POSITIONS[type];
const first = chars[position];
const second = chars[position + 1];
const delegation = readBits(first, definitions.PAIR_OFFSETS.delegation, definitions.DELEGATION_BITS);
const values: ConsentValues = {
implicit: readState(first, definitions.PAIR_OFFSETS.implicit),
declare: readState(second, definitions.PAIR_OFFSETS.declare),
default: readState(second, definitions.PAIR_OFFSETS.default),
update: readState(second, definitions.PAIR_OFFSETS.update),
};
const delegatedTo: ConsentType | "other" | undefined =
delegation >= definitions.DELEGATED_TO_KNOWN_TYPE
? definitions.CONSENT_TYPES[delegation - definitions.DELEGATED_TO_KNOWN_TYPE]
: delegation === definitions.DELEGATED_TO_OTHER
? "other"
: undefined;
return { values, delegatedTo };
});
const effective = mapConsentTypes((type) => {
const seen: ConsentType[] = [type];
let decider = type;
for (let parent = stored[decider].delegatedTo; parent && parent !== "other" && seen.indexOf(parent) < 0; parent = stored[decider].delegatedTo) {
seen.push(parent);
decider = parent;
}
const { values } = stored[decider];
for (const source of definitions.CONSENT_SOURCE_PRECEDENCE) {
if (values[source] !== undefined) return values[source];
}
return undefined;
});
const scoped = mapConsentTypes((type) => definitions.STATE_BY_CODE[readField(chars, definitions.CONTAINER_SCOPED_DEFAULT_FIELDS[type])]);
const active = readField(chars, definitions.FLAG_FIELDS.active) === 1;
const gpc = readField(chars, definitions.FLAG_FIELDS.gpc) === 1;
const adsSignals = definitions.STATE_BY_CODE[readField(chars, definitions.FLAG_FIELDS.adsSignals)];
const usedContainerScopedDefaults = readField(chars, definitions.FLAG_FIELDS.usedContainerScopedDefaults) === 1;
return {
...effective,
containerScopedDefaults: scoped,
flags: {
isConsentModeActive: active,
isGlobalPrivacyControlEnabled: gpc,
adsSignals,
usedContainerScopedDefaults,
},
meta: {
usedDeclare: definitions.CONSENT_TYPES.some((type) => stored[type].values.declare !== undefined),
usedDefault: definitions.CONSENT_TYPES.some((type) => stored[type].values.default !== undefined),
usedImplicit: definitions.CONSENT_TYPES.some((type) => stored[type].values.implicit !== undefined),
usedUpdate: definitions.CONSENT_TYPES.some((type) => stored[type].values.update !== undefined),
},
raw: {
gcd,
...mapConsentTypes((type): ConsentDetails => {
const { values, delegatedTo } = stored[type];
return {
...(delegatedTo !== undefined && { delegatedTo }),
implicit: toGranted(values.implicit),
declare: toGranted(values.declare),
default: toGranted(values.default),
update: toGranted(values.update),
};
}),
containerScopedDefaults: mapConsentTypes((type) => toGranted(scoped[type])),
flags: { active, gpc, adsSignals: toGranted(adsSignals), usedContainerScopedDefaults },
},
};
}