@zeroad.network/token 0.13.7 → 0.13.9
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/README.md +33 -25
- package/dist/browser-BiNZ2c6t.d.cts +47 -0
- package/dist/browser-BiNZ2c6t.d.mts +47 -0
- package/dist/browser-CMCq-bkd.cjs +132 -0
- package/dist/browser-Citg0bh9.mjs +116 -0
- package/dist/browser.d.mts +1 -57
- package/dist/browser.mjs +1 -1
- package/dist/index.cjs +80 -81
- package/dist/index.d.cts +29 -18
- package/dist/index.d.mts +29 -18
- package/dist/index.mjs +78 -82
- package/package.json +2 -2
- package/dist/browser-C2zgnAdK.cjs +0 -171
- package/dist/browser-DDeUUPbU.mjs +0 -156
- package/dist/browser.d.cts +0 -57
package/README.md
CHANGED
|
@@ -18,10 +18,11 @@ For detailed guides and implementation instructions, see the [official Zero Ad N
|
|
|
18
18
|
|
|
19
19
|
The module helps developers to:
|
|
20
20
|
|
|
21
|
+
- Generate a valid "Welcome Header" when `clientId` and `features` are provided.
|
|
21
22
|
- Inject a valid site's HTTP Response Header (**Welcome Header**) into every endpoint. Example:
|
|
22
23
|
|
|
23
24
|
```http
|
|
24
|
-
X-Better-Web-Welcome: "
|
|
25
|
+
X-Better-Web-Welcome: "Z2CclA8oXIT1e0QmqTWF8w^1^3"
|
|
25
26
|
```
|
|
26
27
|
|
|
27
28
|
- Detect and parse Zero Ad Network user tokens sent via HTTP Request Header. Example:
|
|
@@ -30,8 +31,7 @@ The module helps developers to:
|
|
|
30
31
|
X-Better-Web-Hello: "Aav2IXRoh0oKBw==.2yZfC2/pM9DWfgX+von4IgWLmN9t67HJHLiee/gx4+pFIHHurwkC3PCHT1Kaz0yUhx3crUaxST+XLlRtJYacAQ=="
|
|
31
32
|
```
|
|
32
33
|
|
|
33
|
-
- Verify token integrity locally.
|
|
34
|
-
- Optionally generate a valid "Welcome Header" when `siteId` and `features` are provided.
|
|
34
|
+
- Verify client token integrity locally.
|
|
35
35
|
|
|
36
36
|
## Implementation Details
|
|
37
37
|
|
|
@@ -43,11 +43,12 @@ Parsed token example:
|
|
|
43
43
|
|
|
44
44
|
```js
|
|
45
45
|
{
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
HIDE_ADVERTISEMENTS: boolean,
|
|
47
|
+
HIDE_COOKIE_CONSENT_SCREEN: boolean,
|
|
48
|
+
HIDE_MARKETING_DIALOGS: boolean,
|
|
49
|
+
DISABLE_NON_FUNCTIONAL_TRACKING: boolean,
|
|
50
|
+
DISABLE_CONTENT_PAYWALL: boolean,
|
|
51
|
+
ENABLE_SUBSCRIPTION_ACCESS: boolean,
|
|
51
52
|
};
|
|
52
53
|
```
|
|
53
54
|
|
|
@@ -59,9 +60,11 @@ Parsed token example:
|
|
|
59
60
|
|
|
60
61
|
Partnering with Zero Ad Network allows your site to:
|
|
61
62
|
|
|
62
|
-
- Generate a new revenue stream
|
|
63
|
-
-
|
|
64
|
-
-
|
|
63
|
+
- Generate a new revenue stream by:
|
|
64
|
+
- Providing a clean, unobstructed user experience
|
|
65
|
+
- Removing paywalls and enabling free access to your base subscription plan
|
|
66
|
+
- Or both combined
|
|
67
|
+
- Contribute to a truly joyful, user-friendly internet experience
|
|
65
68
|
|
|
66
69
|
## Onboarding Your Site
|
|
67
70
|
|
|
@@ -113,9 +116,12 @@ import { Site } from "@zeroad.network/token";
|
|
|
113
116
|
const app = express();
|
|
114
117
|
|
|
115
118
|
// Initialize the Zero Ad Network module at app startup.
|
|
116
|
-
//
|
|
117
|
-
const
|
|
118
|
-
const site = Site(
|
|
119
|
+
// Your site's `clientId` value is obtained during site registration on the Zero Ad Network platform (https://zeroad.network).
|
|
120
|
+
const ZERO_AD_NETWORK_CLIENT_ID = "Z2CclA8oXIT1e0QmqTWF8w";
|
|
121
|
+
const site = Site({
|
|
122
|
+
clientId: ZERO_AD_NETWORK_CLIENT_ID,
|
|
123
|
+
features: [FEATURES.CLEAN_WEB, FEATURES.ONE_PASS],
|
|
124
|
+
});
|
|
119
125
|
|
|
120
126
|
app
|
|
121
127
|
// Apply middleware for all routes
|
|
@@ -125,7 +131,7 @@ app
|
|
|
125
131
|
|
|
126
132
|
// Parse the incoming user token from the request header
|
|
127
133
|
// Attach the parsed token data to the request object for downstream use
|
|
128
|
-
req.tokenContext = site.
|
|
134
|
+
req.tokenContext = site.parseClientToken(req.get(site.CLIENT_HEADER_NAME));
|
|
129
135
|
|
|
130
136
|
next();
|
|
131
137
|
})
|
|
@@ -135,16 +141,18 @@ app
|
|
|
135
141
|
|
|
136
142
|
// Example structure of tokenContext:
|
|
137
143
|
req.tokenContext = {
|
|
138
|
-
// If true
|
|
139
|
-
|
|
140
|
-
// If true
|
|
141
|
-
|
|
142
|
-
// If true
|
|
143
|
-
|
|
144
|
-
// If true
|
|
145
|
-
|
|
146
|
-
// If true
|
|
147
|
-
|
|
144
|
+
// If `true`: Hide advertisements on the page
|
|
145
|
+
HIDE_ADVERTISEMENTS: boolean,
|
|
146
|
+
// If `true`: Disable all Cookie Consent screens (headers, footers, or dialogs)
|
|
147
|
+
HIDE_COOKIE_CONSENT_SCREEN: boolean,
|
|
148
|
+
// If `true`: Disable all marketing dialogs or popups (newsletters, promotions, etc.)
|
|
149
|
+
HIDE_MARKETING_DIALOGS: boolean,
|
|
150
|
+
// If `true`: Fully opt out the user of non-functional trackers
|
|
151
|
+
DISABLE_NON_FUNCTIONAL_TRACKING: boolean,
|
|
152
|
+
// If `true`: Provide Free access to content behind a paywall (news, articles, etc.)
|
|
153
|
+
DISABLE_CONTENT_PAYWALL: boolean,
|
|
154
|
+
// If `true`: Provide Free access to your base subscription plan (if subscription model is present)
|
|
155
|
+
ENABLE_SUBSCRIPTION_ACCESS: boolean,
|
|
148
156
|
};
|
|
149
157
|
|
|
150
158
|
// Adjust content in your templates based on tokenContext values
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Official Zero Ad Network public key.
|
|
3
|
+
* Used to verify that `X-Better-Web-Hello` header values are authentic
|
|
4
|
+
* and have not been tampered with.
|
|
5
|
+
*/
|
|
6
|
+
declare const ZEROAD_NETWORK_PUBLIC_KEY: string;
|
|
7
|
+
/**
|
|
8
|
+
* IMPORTANT: Requirements listed for each feature class MUST be fulfilled fully.
|
|
9
|
+
* Failure to comply will result in the site getting banned from Zero Ad Network platform.
|
|
10
|
+
*/
|
|
11
|
+
declare enum FEATURES {
|
|
12
|
+
/**
|
|
13
|
+
* Feature requirements:
|
|
14
|
+
* - Disable all advertisements on the page;
|
|
15
|
+
* - Disable all Cookie Consent screens (headers, footers, or dialogs);
|
|
16
|
+
* - Fully opt out the user of non-functional trackers;
|
|
17
|
+
* - Disable all marketing dialogs or popups (newsletters, promotions, etc.).
|
|
18
|
+
*/
|
|
19
|
+
CLEAN_WEB = 1,
|
|
20
|
+
/**
|
|
21
|
+
* Feature requirements:
|
|
22
|
+
* - Provide Free access to content behind a paywall (news, articles, etc.);
|
|
23
|
+
* - Provide Free access to your base subscription plan (if subscription model is present).
|
|
24
|
+
*/
|
|
25
|
+
ONE_PASS = 2
|
|
26
|
+
}
|
|
27
|
+
declare enum SERVER_HEADERS {
|
|
28
|
+
WELCOME = "X-Better-Web-Welcome"
|
|
29
|
+
}
|
|
30
|
+
declare enum CLIENT_HEADERS {
|
|
31
|
+
HELLO = "X-Better-Web-Hello"
|
|
32
|
+
}
|
|
33
|
+
declare enum PROTOCOL_VERSION {
|
|
34
|
+
V_1 = 1
|
|
35
|
+
}
|
|
36
|
+
declare const CURRENT_PROTOCOL_VERSION = PROTOCOL_VERSION.V_1;
|
|
37
|
+
|
|
38
|
+
declare function encodeServerHeader(clientId: string, features: FEATURES[]): string;
|
|
39
|
+
type WelcomeHeader = {
|
|
40
|
+
clientId: string;
|
|
41
|
+
version: PROTOCOL_VERSION;
|
|
42
|
+
features: (keyof typeof FEATURES)[];
|
|
43
|
+
};
|
|
44
|
+
declare function decodeServerHeader(headerValue: string | null | undefined): WelcomeHeader | undefined;
|
|
45
|
+
|
|
46
|
+
export { CLIENT_HEADERS as C, FEATURES as F, PROTOCOL_VERSION as P, SERVER_HEADERS as S, ZEROAD_NETWORK_PUBLIC_KEY as Z, CURRENT_PROTOCOL_VERSION as a, decodeServerHeader as d, encodeServerHeader as e };
|
|
47
|
+
export type { WelcomeHeader as W };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Official Zero Ad Network public key.
|
|
3
|
+
* Used to verify that `X-Better-Web-Hello` header values are authentic
|
|
4
|
+
* and have not been tampered with.
|
|
5
|
+
*/
|
|
6
|
+
declare const ZEROAD_NETWORK_PUBLIC_KEY: string;
|
|
7
|
+
/**
|
|
8
|
+
* IMPORTANT: Requirements listed for each feature class MUST be fulfilled fully.
|
|
9
|
+
* Failure to comply will result in the site getting banned from Zero Ad Network platform.
|
|
10
|
+
*/
|
|
11
|
+
declare enum FEATURES {
|
|
12
|
+
/**
|
|
13
|
+
* Feature requirements:
|
|
14
|
+
* - Disable all advertisements on the page;
|
|
15
|
+
* - Disable all Cookie Consent screens (headers, footers, or dialogs);
|
|
16
|
+
* - Fully opt out the user of non-functional trackers;
|
|
17
|
+
* - Disable all marketing dialogs or popups (newsletters, promotions, etc.).
|
|
18
|
+
*/
|
|
19
|
+
CLEAN_WEB = 1,
|
|
20
|
+
/**
|
|
21
|
+
* Feature requirements:
|
|
22
|
+
* - Provide Free access to content behind a paywall (news, articles, etc.);
|
|
23
|
+
* - Provide Free access to your base subscription plan (if subscription model is present).
|
|
24
|
+
*/
|
|
25
|
+
ONE_PASS = 2
|
|
26
|
+
}
|
|
27
|
+
declare enum SERVER_HEADERS {
|
|
28
|
+
WELCOME = "X-Better-Web-Welcome"
|
|
29
|
+
}
|
|
30
|
+
declare enum CLIENT_HEADERS {
|
|
31
|
+
HELLO = "X-Better-Web-Hello"
|
|
32
|
+
}
|
|
33
|
+
declare enum PROTOCOL_VERSION {
|
|
34
|
+
V_1 = 1
|
|
35
|
+
}
|
|
36
|
+
declare const CURRENT_PROTOCOL_VERSION = PROTOCOL_VERSION.V_1;
|
|
37
|
+
|
|
38
|
+
declare function encodeServerHeader(clientId: string, features: FEATURES[]): string;
|
|
39
|
+
type WelcomeHeader = {
|
|
40
|
+
clientId: string;
|
|
41
|
+
version: PROTOCOL_VERSION;
|
|
42
|
+
features: (keyof typeof FEATURES)[];
|
|
43
|
+
};
|
|
44
|
+
declare function decodeServerHeader(headerValue: string | null | undefined): WelcomeHeader | undefined;
|
|
45
|
+
|
|
46
|
+
export { CLIENT_HEADERS as C, FEATURES as F, PROTOCOL_VERSION as P, SERVER_HEADERS as S, ZEROAD_NETWORK_PUBLIC_KEY as Z, CURRENT_PROTOCOL_VERSION as a, decodeServerHeader as d, encodeServerHeader as e };
|
|
47
|
+
export type { WelcomeHeader as W };
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const levels = {
|
|
4
|
+
error: 0,
|
|
5
|
+
warn: 1,
|
|
6
|
+
info: 2,
|
|
7
|
+
debug: 3
|
|
8
|
+
};
|
|
9
|
+
let currentLevel = "error";
|
|
10
|
+
function setLogLevel(level) {
|
|
11
|
+
if (levels[level] !== void 0) {
|
|
12
|
+
currentLevel = level;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
function log(level, ...args) {
|
|
16
|
+
if (levels[level] <= levels[currentLevel]) {
|
|
17
|
+
console.log(`[${level.toUpperCase()}]`, ...args);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const ZEROAD_NETWORK_PUBLIC_KEY = "MCowBQYDK2VwAyEAignXRaTQtxEDl4ThULucKNQKEEO2Lo5bEO8qKwjSDVs=";
|
|
22
|
+
var FEATURES = /* @__PURE__ */ ((FEATURES2) => {
|
|
23
|
+
FEATURES2[FEATURES2["CLEAN_WEB"] = 1] = "CLEAN_WEB";
|
|
24
|
+
FEATURES2[FEATURES2["ONE_PASS"] = 2] = "ONE_PASS";
|
|
25
|
+
return FEATURES2;
|
|
26
|
+
})(FEATURES || {});
|
|
27
|
+
var SERVER_HEADERS = /* @__PURE__ */ ((SERVER_HEADERS2) => {
|
|
28
|
+
SERVER_HEADERS2["WELCOME"] = "X-Better-Web-Welcome";
|
|
29
|
+
return SERVER_HEADERS2;
|
|
30
|
+
})(SERVER_HEADERS || {});
|
|
31
|
+
var CLIENT_HEADERS = /* @__PURE__ */ ((CLIENT_HEADERS2) => {
|
|
32
|
+
CLIENT_HEADERS2["HELLO"] = "X-Better-Web-Hello";
|
|
33
|
+
return CLIENT_HEADERS2;
|
|
34
|
+
})(CLIENT_HEADERS || {});
|
|
35
|
+
var PROTOCOL_VERSION = /* @__PURE__ */ ((PROTOCOL_VERSION2) => {
|
|
36
|
+
PROTOCOL_VERSION2[PROTOCOL_VERSION2["V_1"] = 1] = "V_1";
|
|
37
|
+
return PROTOCOL_VERSION2;
|
|
38
|
+
})(PROTOCOL_VERSION || {});
|
|
39
|
+
const CURRENT_PROTOCOL_VERSION = 1 /* V_1 */;
|
|
40
|
+
|
|
41
|
+
let SITE_FEATURES_NATIVE$1;
|
|
42
|
+
const getSiteFeaturesNative = () => {
|
|
43
|
+
if (SITE_FEATURES_NATIVE$1?.length) return SITE_FEATURES_NATIVE$1;
|
|
44
|
+
return SITE_FEATURES_NATIVE$1 = Object.entries(FEATURES).filter(([key]) => isNaN(Number(key)));
|
|
45
|
+
};
|
|
46
|
+
const toBase64 = (data) => {
|
|
47
|
+
if (typeof data.toBase64 === "function") return data.toBase64();
|
|
48
|
+
if (typeof Buffer !== "undefined") return Buffer.from(data).toString("base64");
|
|
49
|
+
if (typeof btoa === "function") {
|
|
50
|
+
let binary = "";
|
|
51
|
+
for (let i = 0; i < data.length; i++) {
|
|
52
|
+
binary += String.fromCharCode(data[i]);
|
|
53
|
+
}
|
|
54
|
+
return btoa(binary);
|
|
55
|
+
}
|
|
56
|
+
throw new Error("Base64 encoding not supported in this environment");
|
|
57
|
+
};
|
|
58
|
+
const fromBase64 = (input) => {
|
|
59
|
+
if (typeof Uint8Array.fromBase64 === "function") return Uint8Array.fromBase64(input);
|
|
60
|
+
if (typeof Buffer !== "undefined") return new Uint8Array(Buffer.from(input, "base64"));
|
|
61
|
+
if (typeof atob === "function") {
|
|
62
|
+
const binary = atob(input);
|
|
63
|
+
const bytes = new Uint8Array(binary.length);
|
|
64
|
+
for (let i = 0; i < binary.length; i++) {
|
|
65
|
+
bytes[i] = binary.charCodeAt(i);
|
|
66
|
+
}
|
|
67
|
+
return bytes;
|
|
68
|
+
}
|
|
69
|
+
throw new Error("Base64 decoding not supported in this environment");
|
|
70
|
+
};
|
|
71
|
+
const assert = (value, message) => {
|
|
72
|
+
if (!value) throw new Error(message);
|
|
73
|
+
};
|
|
74
|
+
const hasFlag = (flag, flags) => Boolean(flag & flags);
|
|
75
|
+
const setFlags = (features = []) => features.reduce((acc, feature) => acc | feature, 0);
|
|
76
|
+
|
|
77
|
+
const SEPARATOR = "^";
|
|
78
|
+
const SITE_FEATURES_NATIVE = getSiteFeaturesNative();
|
|
79
|
+
const validFeatureValues = Object.values(FEATURES).filter((key) => !isNaN(Number(key)));
|
|
80
|
+
const validFeatureKeys = Object.values(FEATURES).filter((key) => isNaN(Number(key)));
|
|
81
|
+
function encodeServerHeader(clientId, features) {
|
|
82
|
+
if (!clientId?.length) {
|
|
83
|
+
throw new Error("The provided `clientId` value cannot be an empty string");
|
|
84
|
+
}
|
|
85
|
+
if (!features?.length) {
|
|
86
|
+
throw new Error("At least one Site feature must be provided");
|
|
87
|
+
}
|
|
88
|
+
if (features.filter((feature) => validFeatureValues.includes(feature)).length !== features.length) {
|
|
89
|
+
throw new Error(`Only valid Site features are allowed: ${validFeatureKeys.join(" | ")}`);
|
|
90
|
+
}
|
|
91
|
+
return [clientId, CURRENT_PROTOCOL_VERSION, setFlags(features)].join(SEPARATOR);
|
|
92
|
+
}
|
|
93
|
+
function decodeServerHeader(headerValue) {
|
|
94
|
+
if (!headerValue?.length) return;
|
|
95
|
+
try {
|
|
96
|
+
const parts = headerValue.split(SEPARATOR);
|
|
97
|
+
assert(parts.length === 3, "Invalid header value format");
|
|
98
|
+
const [clientId, protocolVersion, flags] = parts;
|
|
99
|
+
assert(
|
|
100
|
+
Object.values(PROTOCOL_VERSION).includes(Number(protocolVersion)),
|
|
101
|
+
"Invalid or unsupported protocol version"
|
|
102
|
+
);
|
|
103
|
+
assert(Number(flags).toFixed(0).toString() === flags, "Invalid flags number");
|
|
104
|
+
let features = [];
|
|
105
|
+
for (const [feature, shift] of SITE_FEATURES_NATIVE) {
|
|
106
|
+
if (hasFlag(Number(flags), shift)) features.push(feature);
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
version: Number(protocolVersion),
|
|
110
|
+
clientId,
|
|
111
|
+
features
|
|
112
|
+
};
|
|
113
|
+
} catch (err) {
|
|
114
|
+
log("warn", "Could not decode server header value", { reason: err?.message });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
exports.CLIENT_HEADERS = CLIENT_HEADERS;
|
|
119
|
+
exports.CURRENT_PROTOCOL_VERSION = CURRENT_PROTOCOL_VERSION;
|
|
120
|
+
exports.FEATURES = FEATURES;
|
|
121
|
+
exports.PROTOCOL_VERSION = PROTOCOL_VERSION;
|
|
122
|
+
exports.SERVER_HEADERS = SERVER_HEADERS;
|
|
123
|
+
exports.ZEROAD_NETWORK_PUBLIC_KEY = ZEROAD_NETWORK_PUBLIC_KEY;
|
|
124
|
+
exports.decodeServerHeader = decodeServerHeader;
|
|
125
|
+
exports.encodeServerHeader = encodeServerHeader;
|
|
126
|
+
exports.fromBase64 = fromBase64;
|
|
127
|
+
exports.getSiteFeaturesNative = getSiteFeaturesNative;
|
|
128
|
+
exports.hasFlag = hasFlag;
|
|
129
|
+
exports.log = log;
|
|
130
|
+
exports.setFlags = setFlags;
|
|
131
|
+
exports.setLogLevel = setLogLevel;
|
|
132
|
+
exports.toBase64 = toBase64;
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
const levels = {
|
|
2
|
+
error: 0,
|
|
3
|
+
warn: 1,
|
|
4
|
+
info: 2,
|
|
5
|
+
debug: 3
|
|
6
|
+
};
|
|
7
|
+
let currentLevel = "error";
|
|
8
|
+
function setLogLevel(level) {
|
|
9
|
+
if (levels[level] !== void 0) {
|
|
10
|
+
currentLevel = level;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
function log(level, ...args) {
|
|
14
|
+
if (levels[level] <= levels[currentLevel]) {
|
|
15
|
+
console.log(`[${level.toUpperCase()}]`, ...args);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const ZEROAD_NETWORK_PUBLIC_KEY = "MCowBQYDK2VwAyEAignXRaTQtxEDl4ThULucKNQKEEO2Lo5bEO8qKwjSDVs=";
|
|
20
|
+
var FEATURES = /* @__PURE__ */ ((FEATURES2) => {
|
|
21
|
+
FEATURES2[FEATURES2["CLEAN_WEB"] = 1] = "CLEAN_WEB";
|
|
22
|
+
FEATURES2[FEATURES2["ONE_PASS"] = 2] = "ONE_PASS";
|
|
23
|
+
return FEATURES2;
|
|
24
|
+
})(FEATURES || {});
|
|
25
|
+
var SERVER_HEADERS = /* @__PURE__ */ ((SERVER_HEADERS2) => {
|
|
26
|
+
SERVER_HEADERS2["WELCOME"] = "X-Better-Web-Welcome";
|
|
27
|
+
return SERVER_HEADERS2;
|
|
28
|
+
})(SERVER_HEADERS || {});
|
|
29
|
+
var CLIENT_HEADERS = /* @__PURE__ */ ((CLIENT_HEADERS2) => {
|
|
30
|
+
CLIENT_HEADERS2["HELLO"] = "X-Better-Web-Hello";
|
|
31
|
+
return CLIENT_HEADERS2;
|
|
32
|
+
})(CLIENT_HEADERS || {});
|
|
33
|
+
var PROTOCOL_VERSION = /* @__PURE__ */ ((PROTOCOL_VERSION2) => {
|
|
34
|
+
PROTOCOL_VERSION2[PROTOCOL_VERSION2["V_1"] = 1] = "V_1";
|
|
35
|
+
return PROTOCOL_VERSION2;
|
|
36
|
+
})(PROTOCOL_VERSION || {});
|
|
37
|
+
const CURRENT_PROTOCOL_VERSION = 1 /* V_1 */;
|
|
38
|
+
|
|
39
|
+
let SITE_FEATURES_NATIVE$1;
|
|
40
|
+
const getSiteFeaturesNative = () => {
|
|
41
|
+
if (SITE_FEATURES_NATIVE$1?.length) return SITE_FEATURES_NATIVE$1;
|
|
42
|
+
return SITE_FEATURES_NATIVE$1 = Object.entries(FEATURES).filter(([key]) => isNaN(Number(key)));
|
|
43
|
+
};
|
|
44
|
+
const toBase64 = (data) => {
|
|
45
|
+
if (typeof data.toBase64 === "function") return data.toBase64();
|
|
46
|
+
if (typeof Buffer !== "undefined") return Buffer.from(data).toString("base64");
|
|
47
|
+
if (typeof btoa === "function") {
|
|
48
|
+
let binary = "";
|
|
49
|
+
for (let i = 0; i < data.length; i++) {
|
|
50
|
+
binary += String.fromCharCode(data[i]);
|
|
51
|
+
}
|
|
52
|
+
return btoa(binary);
|
|
53
|
+
}
|
|
54
|
+
throw new Error("Base64 encoding not supported in this environment");
|
|
55
|
+
};
|
|
56
|
+
const fromBase64 = (input) => {
|
|
57
|
+
if (typeof Uint8Array.fromBase64 === "function") return Uint8Array.fromBase64(input);
|
|
58
|
+
if (typeof Buffer !== "undefined") return new Uint8Array(Buffer.from(input, "base64"));
|
|
59
|
+
if (typeof atob === "function") {
|
|
60
|
+
const binary = atob(input);
|
|
61
|
+
const bytes = new Uint8Array(binary.length);
|
|
62
|
+
for (let i = 0; i < binary.length; i++) {
|
|
63
|
+
bytes[i] = binary.charCodeAt(i);
|
|
64
|
+
}
|
|
65
|
+
return bytes;
|
|
66
|
+
}
|
|
67
|
+
throw new Error("Base64 decoding not supported in this environment");
|
|
68
|
+
};
|
|
69
|
+
const assert = (value, message) => {
|
|
70
|
+
if (!value) throw new Error(message);
|
|
71
|
+
};
|
|
72
|
+
const hasFlag = (flag, flags) => Boolean(flag & flags);
|
|
73
|
+
const setFlags = (features = []) => features.reduce((acc, feature) => acc | feature, 0);
|
|
74
|
+
|
|
75
|
+
const SEPARATOR = "^";
|
|
76
|
+
const SITE_FEATURES_NATIVE = getSiteFeaturesNative();
|
|
77
|
+
const validFeatureValues = Object.values(FEATURES).filter((key) => !isNaN(Number(key)));
|
|
78
|
+
const validFeatureKeys = Object.values(FEATURES).filter((key) => isNaN(Number(key)));
|
|
79
|
+
function encodeServerHeader(clientId, features) {
|
|
80
|
+
if (!clientId?.length) {
|
|
81
|
+
throw new Error("The provided `clientId` value cannot be an empty string");
|
|
82
|
+
}
|
|
83
|
+
if (!features?.length) {
|
|
84
|
+
throw new Error("At least one Site feature must be provided");
|
|
85
|
+
}
|
|
86
|
+
if (features.filter((feature) => validFeatureValues.includes(feature)).length !== features.length) {
|
|
87
|
+
throw new Error(`Only valid Site features are allowed: ${validFeatureKeys.join(" | ")}`);
|
|
88
|
+
}
|
|
89
|
+
return [clientId, CURRENT_PROTOCOL_VERSION, setFlags(features)].join(SEPARATOR);
|
|
90
|
+
}
|
|
91
|
+
function decodeServerHeader(headerValue) {
|
|
92
|
+
if (!headerValue?.length) return;
|
|
93
|
+
try {
|
|
94
|
+
const parts = headerValue.split(SEPARATOR);
|
|
95
|
+
assert(parts.length === 3, "Invalid header value format");
|
|
96
|
+
const [clientId, protocolVersion, flags] = parts;
|
|
97
|
+
assert(
|
|
98
|
+
Object.values(PROTOCOL_VERSION).includes(Number(protocolVersion)),
|
|
99
|
+
"Invalid or unsupported protocol version"
|
|
100
|
+
);
|
|
101
|
+
assert(Number(flags).toFixed(0).toString() === flags, "Invalid flags number");
|
|
102
|
+
let features = [];
|
|
103
|
+
for (const [feature, shift] of SITE_FEATURES_NATIVE) {
|
|
104
|
+
if (hasFlag(Number(flags), shift)) features.push(feature);
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
version: Number(protocolVersion),
|
|
108
|
+
clientId,
|
|
109
|
+
features
|
|
110
|
+
};
|
|
111
|
+
} catch (err) {
|
|
112
|
+
log("warn", "Could not decode server header value", { reason: err?.message });
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export { CLIENT_HEADERS as C, FEATURES as F, PROTOCOL_VERSION as P, SERVER_HEADERS as S, ZEROAD_NETWORK_PUBLIC_KEY as Z, setLogLevel as a, CURRENT_PROTOCOL_VERSION as b, decodeServerHeader as d, encodeServerHeader as e, fromBase64 as f, getSiteFeaturesNative as g, hasFlag as h, log as l, setFlags as s, toBase64 as t };
|
package/dist/browser.d.mts
CHANGED
|
@@ -1,57 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* Official Zero Ad Network public key.
|
|
3
|
-
* Used to verify that `X-Better-Web-Hello` header values are authentic
|
|
4
|
-
* and have not been tampered with.
|
|
5
|
-
*/
|
|
6
|
-
declare const ZEROAD_NETWORK_PUBLIC_KEY: string;
|
|
7
|
-
declare enum FEATURES {
|
|
8
|
-
/** Disable all advertisements on the page */
|
|
9
|
-
ADS_OFF = 1,
|
|
10
|
-
/** Disable all Cookie Consent screens (headers, footers, or dialogs)
|
|
11
|
-
and fully opt out of non-functional trackers */
|
|
12
|
-
COOKIE_CONSENT_OFF = 2,
|
|
13
|
-
/** Disable all marketing dialogs or popups (e.g., newsletters, promotions) */
|
|
14
|
-
MARKETING_DIALOG_OFF = 4,
|
|
15
|
-
/** Provide automatic access to content that is normally paywalled */
|
|
16
|
-
CONTENT_PAYWALL_OFF = 8,
|
|
17
|
-
/** Grant automatic access to site features behind a SaaS,
|
|
18
|
-
at least the basic subscription plan */
|
|
19
|
-
SUBSCRIPTION_ACCESS_ON = 16
|
|
20
|
-
}
|
|
21
|
-
type UUID = string;
|
|
22
|
-
declare enum SERVER_HEADERS {
|
|
23
|
-
WELCOME = "X-Better-Web-Welcome"
|
|
24
|
-
}
|
|
25
|
-
declare enum CLIENT_HEADERS {
|
|
26
|
-
HELLO = "X-Better-Web-Hello"
|
|
27
|
-
}
|
|
28
|
-
declare enum PROTOCOL_VERSION {
|
|
29
|
-
V_1 = 1
|
|
30
|
-
}
|
|
31
|
-
declare const CURRENT_PROTOCOL_VERSION = PROTOCOL_VERSION.V_1;
|
|
32
|
-
type ServerHeaderExtendedOptions = {
|
|
33
|
-
siteId: UUID;
|
|
34
|
-
features: FEATURES[];
|
|
35
|
-
};
|
|
36
|
-
type ServerHeaderOptions = NonNullable<string | ServerHeaderExtendedOptions>;
|
|
37
|
-
type WelcomeHeader = {
|
|
38
|
-
version: PROTOCOL_VERSION;
|
|
39
|
-
features: (keyof typeof FEATURES)[];
|
|
40
|
-
siteId: UUID;
|
|
41
|
-
};
|
|
42
|
-
type ClientParsedHeader = {
|
|
43
|
-
version: PROTOCOL_VERSION;
|
|
44
|
-
expiresAt: Date;
|
|
45
|
-
flags: number;
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
declare class ServerHeader {
|
|
49
|
-
NAME: SERVER_HEADERS;
|
|
50
|
-
VALUE: string;
|
|
51
|
-
constructor(options: ServerHeaderOptions);
|
|
52
|
-
encode(siteId: UUID, features: FEATURES[]): string;
|
|
53
|
-
static decode(headerValue: string | undefined): WelcomeHeader | undefined;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
export { CLIENT_HEADERS, CURRENT_PROTOCOL_VERSION, FEATURES, PROTOCOL_VERSION, SERVER_HEADERS, ServerHeader, ZEROAD_NETWORK_PUBLIC_KEY };
|
|
57
|
-
export type { ClientParsedHeader, ServerHeaderExtendedOptions, ServerHeaderOptions, UUID, WelcomeHeader };
|
|
1
|
+
export { C as CLIENT_HEADERS, a as CURRENT_PROTOCOL_VERSION, F as FEATURES, P as PROTOCOL_VERSION, S as SERVER_HEADERS, W as WelcomeHeader, Z as ZEROAD_NETWORK_PUBLIC_KEY, d as decodeServerHeader } from './browser-BiNZ2c6t.mjs';
|
package/dist/browser.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { C as CLIENT_HEADERS,
|
|
1
|
+
export { C as CLIENT_HEADERS, b as CURRENT_PROTOCOL_VERSION, F as FEATURES, P as PROTOCOL_VERSION, S as SERVER_HEADERS, Z as ZEROAD_NETWORK_PUBLIC_KEY, d as decodeServerHeader } from './browser-Citg0bh9.mjs';
|