@financial-times/privacy-us-privacy 2.0.2 → 2.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.d.ts +32 -0
- package/dist/privacy-us-privacy.cjs +91 -0
- package/dist/privacy-us-privacy.js +91 -0
- package/package.json +1 -1
package/dist/main.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export declare function __uspapi(command: string, version: number, callback?: USPDataCallback): Promise<void>;
|
|
2
|
+
|
|
3
|
+
export declare function getUsPrivacyCookie(): string | null;
|
|
4
|
+
|
|
5
|
+
export declare function getUsPrivacyForTracking(): string;
|
|
6
|
+
|
|
7
|
+
export declare const NO_PRIVACY_COOKIE = "no-privacy-cookie";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Expose a javascript function on the global `window` object that can be called
|
|
11
|
+
* by third party scripts to determine if the current user has opted out of CCPA
|
|
12
|
+
* https://github.com/InteractiveAdvertisingBureau/USPrivacy/blob/HEAD/CCPA/USP%20API.md
|
|
13
|
+
*/
|
|
14
|
+
export declare function setUSPrivacy(): void;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Set a cookie based on the IAB US Privacy spec
|
|
18
|
+
* https://github.com/InteractiveAdvertisingBureau/USPrivacy/blob/HEAD/CCPA/US%20Privacy%20String.md
|
|
19
|
+
*
|
|
20
|
+
* @param consent - Does the user have consent for CCPA
|
|
21
|
+
* @param domain - An optional explicit domain for the cookie
|
|
22
|
+
*/
|
|
23
|
+
export declare function setUSPrivacyCookie(consent: boolean, domain?: string): void;
|
|
24
|
+
|
|
25
|
+
declare interface USPData {
|
|
26
|
+
version: number;
|
|
27
|
+
uspString: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
declare type USPDataCallback = (uspData: USPData, success: boolean) => void;
|
|
31
|
+
|
|
32
|
+
export { }
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const API_URL = "https://privacy.ft.com/api/v1";
|
|
4
|
+
const API_ENDPOINTS = {
|
|
5
|
+
complianceRegion: "/compliance-region.json"
|
|
6
|
+
};
|
|
7
|
+
function formatResponse(obj) {
|
|
8
|
+
return {
|
|
9
|
+
region: obj.region,
|
|
10
|
+
legislation: new Set(obj.legislation.split(","))
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function sessionStorageOk() {
|
|
14
|
+
const test = "test";
|
|
15
|
+
try {
|
|
16
|
+
sessionStorage.setItem(test, test);
|
|
17
|
+
sessionStorage.removeItem(test);
|
|
18
|
+
return true;
|
|
19
|
+
} catch (e) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
async function fetchLegislation() {
|
|
24
|
+
const complianceStr = sessionStorageOk() && sessionStorage.getItem("user-compliance");
|
|
25
|
+
const complianceObj = complianceStr && JSON.parse(complianceStr);
|
|
26
|
+
if (complianceObj) {
|
|
27
|
+
return Promise.resolve(formatResponse(complianceObj));
|
|
28
|
+
} else {
|
|
29
|
+
const url = `${API_URL}${API_ENDPOINTS.complianceRegion}`;
|
|
30
|
+
const res = await fetch(url);
|
|
31
|
+
const compliance = await (res.ok ? res.json() : Promise.reject(res));
|
|
32
|
+
if (sessionStorageOk()) {
|
|
33
|
+
sessionStorage.setItem("user-compliance", JSON.stringify(compliance));
|
|
34
|
+
}
|
|
35
|
+
return formatResponse(compliance);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const NO_PRIVACY_COOKIE = "no-privacy-cookie";
|
|
39
|
+
function getUsPrivacyCookie() {
|
|
40
|
+
const uspCookie = document.cookie.split("; ").find((row) => row.startsWith("usprivacy"));
|
|
41
|
+
if (!uspCookie) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
return uspCookie.split("=")[1];
|
|
45
|
+
}
|
|
46
|
+
function getUsPrivacyForTracking() {
|
|
47
|
+
return getUsPrivacyCookie() || NO_PRIVACY_COOKIE;
|
|
48
|
+
}
|
|
49
|
+
async function __uspapi(command, version, callback) {
|
|
50
|
+
if (typeof callback !== "function") {
|
|
51
|
+
return console.error(
|
|
52
|
+
"Please provide a valid callback to the __uspapi() function. __uspapi(command, version, callback)"
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
if (version !== 1 || command !== "getUSPData") {
|
|
56
|
+
return console.error("Invalid version or command specified for the __uspapi() function");
|
|
57
|
+
}
|
|
58
|
+
try {
|
|
59
|
+
const uspCookie = getUsPrivacyCookie();
|
|
60
|
+
if (uspCookie) {
|
|
61
|
+
return callback({ version, uspString: uspCookie }, true);
|
|
62
|
+
} else {
|
|
63
|
+
const { legislation } = await fetchLegislation();
|
|
64
|
+
if (legislation.has("ccpa")) {
|
|
65
|
+
return callback({ version, uspString: "1NNN" }, true);
|
|
66
|
+
} else {
|
|
67
|
+
return callback({ version, uspString: "1---" }, true);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
} catch (e) {
|
|
71
|
+
return console.error(e);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
function setUSPrivacy() {
|
|
75
|
+
if (typeof window.__uspapi === "function") {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
window.__uspapi = __uspapi;
|
|
79
|
+
}
|
|
80
|
+
function setUSPrivacyCookie(consent, domain) {
|
|
81
|
+
domain = domain || (document.location.hostname || "").replace("www.", "");
|
|
82
|
+
const uspString = `1Y${consent ? "N" : "Y"}N`;
|
|
83
|
+
const maxAge = 60 * 60 * 24 * 365 * 5;
|
|
84
|
+
document.cookie = `usprivacy=${uspString};max-age=${maxAge};path=/;domain=${domain}`;
|
|
85
|
+
}
|
|
86
|
+
exports.NO_PRIVACY_COOKIE = NO_PRIVACY_COOKIE;
|
|
87
|
+
exports.__uspapi = __uspapi;
|
|
88
|
+
exports.getUsPrivacyCookie = getUsPrivacyCookie;
|
|
89
|
+
exports.getUsPrivacyForTracking = getUsPrivacyForTracking;
|
|
90
|
+
exports.setUSPrivacy = setUSPrivacy;
|
|
91
|
+
exports.setUSPrivacyCookie = setUSPrivacyCookie;
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
const API_URL = "https://privacy.ft.com/api/v1";
|
|
2
|
+
const API_ENDPOINTS = {
|
|
3
|
+
complianceRegion: "/compliance-region.json"
|
|
4
|
+
};
|
|
5
|
+
function formatResponse(obj) {
|
|
6
|
+
return {
|
|
7
|
+
region: obj.region,
|
|
8
|
+
legislation: new Set(obj.legislation.split(","))
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
function sessionStorageOk() {
|
|
12
|
+
const test = "test";
|
|
13
|
+
try {
|
|
14
|
+
sessionStorage.setItem(test, test);
|
|
15
|
+
sessionStorage.removeItem(test);
|
|
16
|
+
return true;
|
|
17
|
+
} catch (e) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
async function fetchLegislation() {
|
|
22
|
+
const complianceStr = sessionStorageOk() && sessionStorage.getItem("user-compliance");
|
|
23
|
+
const complianceObj = complianceStr && JSON.parse(complianceStr);
|
|
24
|
+
if (complianceObj) {
|
|
25
|
+
return Promise.resolve(formatResponse(complianceObj));
|
|
26
|
+
} else {
|
|
27
|
+
const url = `${API_URL}${API_ENDPOINTS.complianceRegion}`;
|
|
28
|
+
const res = await fetch(url);
|
|
29
|
+
const compliance = await (res.ok ? res.json() : Promise.reject(res));
|
|
30
|
+
if (sessionStorageOk()) {
|
|
31
|
+
sessionStorage.setItem("user-compliance", JSON.stringify(compliance));
|
|
32
|
+
}
|
|
33
|
+
return formatResponse(compliance);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
const NO_PRIVACY_COOKIE = "no-privacy-cookie";
|
|
37
|
+
function getUsPrivacyCookie() {
|
|
38
|
+
const uspCookie = document.cookie.split("; ").find((row) => row.startsWith("usprivacy"));
|
|
39
|
+
if (!uspCookie) {
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
return uspCookie.split("=")[1];
|
|
43
|
+
}
|
|
44
|
+
function getUsPrivacyForTracking() {
|
|
45
|
+
return getUsPrivacyCookie() || NO_PRIVACY_COOKIE;
|
|
46
|
+
}
|
|
47
|
+
async function __uspapi(command, version, callback) {
|
|
48
|
+
if (typeof callback !== "function") {
|
|
49
|
+
return console.error(
|
|
50
|
+
"Please provide a valid callback to the __uspapi() function. __uspapi(command, version, callback)"
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
if (version !== 1 || command !== "getUSPData") {
|
|
54
|
+
return console.error("Invalid version or command specified for the __uspapi() function");
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
const uspCookie = getUsPrivacyCookie();
|
|
58
|
+
if (uspCookie) {
|
|
59
|
+
return callback({ version, uspString: uspCookie }, true);
|
|
60
|
+
} else {
|
|
61
|
+
const { legislation } = await fetchLegislation();
|
|
62
|
+
if (legislation.has("ccpa")) {
|
|
63
|
+
return callback({ version, uspString: "1NNN" }, true);
|
|
64
|
+
} else {
|
|
65
|
+
return callback({ version, uspString: "1---" }, true);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
} catch (e) {
|
|
69
|
+
return console.error(e);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
function setUSPrivacy() {
|
|
73
|
+
if (typeof window.__uspapi === "function") {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
window.__uspapi = __uspapi;
|
|
77
|
+
}
|
|
78
|
+
function setUSPrivacyCookie(consent, domain) {
|
|
79
|
+
domain = domain || (document.location.hostname || "").replace("www.", "");
|
|
80
|
+
const uspString = `1Y${consent ? "N" : "Y"}N`;
|
|
81
|
+
const maxAge = 60 * 60 * 24 * 365 * 5;
|
|
82
|
+
document.cookie = `usprivacy=${uspString};max-age=${maxAge};path=/;domain=${domain}`;
|
|
83
|
+
}
|
|
84
|
+
export {
|
|
85
|
+
NO_PRIVACY_COOKIE,
|
|
86
|
+
__uspapi,
|
|
87
|
+
getUsPrivacyCookie,
|
|
88
|
+
getUsPrivacyForTracking,
|
|
89
|
+
setUSPrivacy,
|
|
90
|
+
setUSPrivacyCookie
|
|
91
|
+
};
|