-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconsent.ts
More file actions
56 lines (54 loc) · 1.67 KB
/
consent.ts
File metadata and controls
56 lines (54 loc) · 1.67 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
/*
This module implements Humbug's user consent mechanisms.
*/
type MechanismFunc = () => boolean;
type Mechanism = MechanismFunc | boolean
const yes = ["1", "t", "y", "T", "Y", "true", "yes", "True", "Yes", "TRUE", "YES"]
/**
* HumbugConsent stores the client's consent settings.
*/
export default class HumbugConsent {
buggerOffStatus: () => boolean
public mechanisms : Mechanism[] = [];
/**
* Humbug consent constructor
* @param mechanisms list of booleans and function that return boolean
*/
constructor(...mechanisms : Mechanism[]) {
this.mechanisms = mechanisms;
this.buggerOffStatus = environmentVariableOptOut("BUGGER_OFF", yes)
}
/**
* Checks if all consent mechanisms signal the user's consent.
* If any of them signal false, returns false. Otherwise,
* returns True.
* If the user has set BUGGER_OFF=yes then do not assume consent.
* Otherwise, at this point, we can assume consent.
*/
check(): boolean {
for (const el of this.mechanisms) {
if (typeof el === "boolean") {
if (!el)
return false;
}
else if (typeof el === "function") {
if (!el())
return false;
}
else
throw new Error("Unknown type of consent mechanism")
}
return this.buggerOffStatus()
}
}
function environmentVariableOptOut(
envVar: string, optOutValues: string[]
) {
return () => {
const envVal = process.env[envVar];
if (envVal !== undefined) {
return !optOutValues.includes(envVal);
}
return true
}
}