@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.
@@ -1,171 +0,0 @@
1
- 'use strict';
2
-
3
- const ZEROAD_NETWORK_PUBLIC_KEY = "MCowBQYDK2VwAyEAignXRaTQtxEDl4ThULucKNQKEEO2Lo5bEO8qKwjSDVs=";
4
- var FEATURES = /* @__PURE__ */ ((FEATURES2) => {
5
- FEATURES2[FEATURES2["ADS_OFF"] = 1] = "ADS_OFF";
6
- FEATURES2[FEATURES2["COOKIE_CONSENT_OFF"] = 2] = "COOKIE_CONSENT_OFF";
7
- FEATURES2[FEATURES2["MARKETING_DIALOG_OFF"] = 4] = "MARKETING_DIALOG_OFF";
8
- FEATURES2[FEATURES2["CONTENT_PAYWALL_OFF"] = 8] = "CONTENT_PAYWALL_OFF";
9
- FEATURES2[FEATURES2["SUBSCRIPTION_ACCESS_ON"] = 16] = "SUBSCRIPTION_ACCESS_ON";
10
- return FEATURES2;
11
- })(FEATURES || {});
12
- var SERVER_HEADERS = /* @__PURE__ */ ((SERVER_HEADERS2) => {
13
- SERVER_HEADERS2["WELCOME"] = "X-Better-Web-Welcome";
14
- return SERVER_HEADERS2;
15
- })(SERVER_HEADERS || {});
16
- var CLIENT_HEADERS = /* @__PURE__ */ ((CLIENT_HEADERS2) => {
17
- CLIENT_HEADERS2["HELLO"] = "X-Better-Web-Hello";
18
- return CLIENT_HEADERS2;
19
- })(CLIENT_HEADERS || {});
20
- var PROTOCOL_VERSION = /* @__PURE__ */ ((PROTOCOL_VERSION2) => {
21
- PROTOCOL_VERSION2[PROTOCOL_VERSION2["V_1"] = 1] = "V_1";
22
- return PROTOCOL_VERSION2;
23
- })(PROTOCOL_VERSION || {});
24
- const CURRENT_PROTOCOL_VERSION = 1 /* V_1 */;
25
-
26
- let SITE_FEATURES_NATIVE$1;
27
- const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
28
- const getSiteFeaturesNative = () => {
29
- if (SITE_FEATURES_NATIVE$1?.length) return SITE_FEATURES_NATIVE$1;
30
- return SITE_FEATURES_NATIVE$1 = Object.entries(FEATURES).filter(([key]) => isNaN(Number(key)));
31
- };
32
- const toBase64 = (data) => {
33
- if (typeof data.toBase64 === "function") return data.toBase64();
34
- if (typeof Buffer !== "undefined") return Buffer.from(data).toString("base64");
35
- if (typeof btoa === "function") {
36
- let binary = "";
37
- for (let i = 0; i < data.length; i++) {
38
- binary += String.fromCharCode(data[i]);
39
- }
40
- return btoa(binary);
41
- }
42
- throw new Error("Base64 encoding not supported in this environment");
43
- };
44
- const fromBase64 = (input) => {
45
- if (typeof Uint8Array.fromBase64 === "function") return Uint8Array.fromBase64(input);
46
- if (typeof Buffer !== "undefined") return new Uint8Array(Buffer.from(input, "base64"));
47
- if (typeof atob === "function") {
48
- const binary = atob(input);
49
- const bytes = new Uint8Array(binary.length);
50
- for (let i = 0; i < binary.length; i++) {
51
- bytes[i] = binary.charCodeAt(i);
52
- }
53
- return bytes;
54
- }
55
- throw new Error("Base64 decoding not supported in this environment");
56
- };
57
- const uuidToBase64 = (uuid) => {
58
- const hex = uuid.replace(/-/g, "");
59
- if (hex.length !== 32) throw new Error("Invalid UUID format");
60
- const bytes = new Uint8Array(16);
61
- for (let i = 0; i < 16; i++) {
62
- bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
63
- }
64
- return toBase64(bytes).replace(/=+$/, "");
65
- };
66
- const base64ToUuid = (input) => {
67
- const bytes = fromBase64(input + "===".slice((input.length + 3) % 4));
68
- if (bytes.length !== 16) throw new Error("Invalid byte length for UUID");
69
- const hex = Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
70
- return [
71
- hex.slice(0, 8),
72
- hex.slice(8, 8 + 4),
73
- hex.slice(12, 12 + 4),
74
- hex.slice(16, 16 + 4),
75
- hex.slice(20, 20 + 12)
76
- ].join("-");
77
- };
78
- const assert = (value, message) => {
79
- if (!value) throw new Error(message);
80
- };
81
- const hasFeature = (flags, feature) => Boolean(flags & feature);
82
- const setFeatures = (flags = 0, features = []) => flags |= features.reduce((acc, feature) => acc | feature, flags || 0);
83
-
84
- const levels = {
85
- error: 0,
86
- warn: 1,
87
- info: 2,
88
- debug: 3
89
- };
90
- let currentLevel = "error";
91
- function setLogLevel(level) {
92
- if (levels[level] !== void 0) {
93
- currentLevel = level;
94
- }
95
- }
96
- function log(level, ...args) {
97
- if (levels[level] <= levels[currentLevel]) {
98
- console.log(`[${level.toUpperCase()}]`, ...args);
99
- }
100
- }
101
-
102
- const SEPARATOR = "^";
103
- const SITE_FEATURES_NATIVE = getSiteFeaturesNative();
104
- class ServerHeader {
105
- NAME = SERVER_HEADERS.WELCOME;
106
- VALUE;
107
- constructor(options) {
108
- if (typeof options === "string") {
109
- if (!options.length) {
110
- throw new Error("ServerHeader: non-empty welcome header string value must be provided");
111
- }
112
- this.VALUE = options;
113
- } else if (isObject(options) && "siteId" in options && "features" in options) {
114
- const { siteId, features } = options;
115
- if (typeof siteId !== "string" || !siteId.length || !Array.isArray(features) || !features.length) {
116
- throw new Error("ServerHeader options must be provided");
117
- }
118
- this.VALUE = this.encode(siteId, features);
119
- } else {
120
- throw new Error(
121
- "ServerHeader: non-empty welcome header string value or { siteId, features } options must be provided"
122
- );
123
- }
124
- }
125
- encode(siteId, features) {
126
- const flags = setFeatures(0, features);
127
- const encodedSiteId = uuidToBase64(siteId);
128
- return [encodedSiteId, CURRENT_PROTOCOL_VERSION, flags].join(SEPARATOR);
129
- }
130
- static decode(headerValue) {
131
- if (!headerValue?.length) return;
132
- try {
133
- const parts = headerValue.split(SEPARATOR);
134
- assert(parts.length === 3, "Invalid header value format");
135
- const [encodedSiteId, protocolVersion, flags] = parts;
136
- assert(
137
- Object.values(PROTOCOL_VERSION).includes(Number(protocolVersion)),
138
- "Invalid or unsupported protocol version"
139
- );
140
- const siteId = base64ToUuid(encodedSiteId);
141
- assert(siteId.length === 36, "Invalid siteId value");
142
- assert(Number(flags).toFixed(0).toString() === flags, "Invalid flags number");
143
- let features = [];
144
- for (const [feature, shift] of SITE_FEATURES_NATIVE) {
145
- if (hasFeature(Number(flags), shift)) features.push(feature);
146
- }
147
- return {
148
- version: Number(protocolVersion),
149
- features,
150
- siteId
151
- };
152
- } catch (err) {
153
- log("warn", "Could not decode server header value", { reason: err?.message });
154
- }
155
- }
156
- }
157
-
158
- exports.CLIENT_HEADERS = CLIENT_HEADERS;
159
- exports.CURRENT_PROTOCOL_VERSION = CURRENT_PROTOCOL_VERSION;
160
- exports.FEATURES = FEATURES;
161
- exports.PROTOCOL_VERSION = PROTOCOL_VERSION;
162
- exports.SERVER_HEADERS = SERVER_HEADERS;
163
- exports.ServerHeader = ServerHeader;
164
- exports.ZEROAD_NETWORK_PUBLIC_KEY = ZEROAD_NETWORK_PUBLIC_KEY;
165
- exports.fromBase64 = fromBase64;
166
- exports.getSiteFeaturesNative = getSiteFeaturesNative;
167
- exports.hasFeature = hasFeature;
168
- exports.log = log;
169
- exports.setFeatures = setFeatures;
170
- exports.setLogLevel = setLogLevel;
171
- exports.toBase64 = toBase64;
@@ -1,156 +0,0 @@
1
- const ZEROAD_NETWORK_PUBLIC_KEY = "MCowBQYDK2VwAyEAignXRaTQtxEDl4ThULucKNQKEEO2Lo5bEO8qKwjSDVs=";
2
- var FEATURES = /* @__PURE__ */ ((FEATURES2) => {
3
- FEATURES2[FEATURES2["ADS_OFF"] = 1] = "ADS_OFF";
4
- FEATURES2[FEATURES2["COOKIE_CONSENT_OFF"] = 2] = "COOKIE_CONSENT_OFF";
5
- FEATURES2[FEATURES2["MARKETING_DIALOG_OFF"] = 4] = "MARKETING_DIALOG_OFF";
6
- FEATURES2[FEATURES2["CONTENT_PAYWALL_OFF"] = 8] = "CONTENT_PAYWALL_OFF";
7
- FEATURES2[FEATURES2["SUBSCRIPTION_ACCESS_ON"] = 16] = "SUBSCRIPTION_ACCESS_ON";
8
- return FEATURES2;
9
- })(FEATURES || {});
10
- var SERVER_HEADERS = /* @__PURE__ */ ((SERVER_HEADERS2) => {
11
- SERVER_HEADERS2["WELCOME"] = "X-Better-Web-Welcome";
12
- return SERVER_HEADERS2;
13
- })(SERVER_HEADERS || {});
14
- var CLIENT_HEADERS = /* @__PURE__ */ ((CLIENT_HEADERS2) => {
15
- CLIENT_HEADERS2["HELLO"] = "X-Better-Web-Hello";
16
- return CLIENT_HEADERS2;
17
- })(CLIENT_HEADERS || {});
18
- var PROTOCOL_VERSION = /* @__PURE__ */ ((PROTOCOL_VERSION2) => {
19
- PROTOCOL_VERSION2[PROTOCOL_VERSION2["V_1"] = 1] = "V_1";
20
- return PROTOCOL_VERSION2;
21
- })(PROTOCOL_VERSION || {});
22
- const CURRENT_PROTOCOL_VERSION = 1 /* V_1 */;
23
-
24
- let SITE_FEATURES_NATIVE$1;
25
- const isObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
26
- const getSiteFeaturesNative = () => {
27
- if (SITE_FEATURES_NATIVE$1?.length) return SITE_FEATURES_NATIVE$1;
28
- return SITE_FEATURES_NATIVE$1 = Object.entries(FEATURES).filter(([key]) => isNaN(Number(key)));
29
- };
30
- const toBase64 = (data) => {
31
- if (typeof data.toBase64 === "function") return data.toBase64();
32
- if (typeof Buffer !== "undefined") return Buffer.from(data).toString("base64");
33
- if (typeof btoa === "function") {
34
- let binary = "";
35
- for (let i = 0; i < data.length; i++) {
36
- binary += String.fromCharCode(data[i]);
37
- }
38
- return btoa(binary);
39
- }
40
- throw new Error("Base64 encoding not supported in this environment");
41
- };
42
- const fromBase64 = (input) => {
43
- if (typeof Uint8Array.fromBase64 === "function") return Uint8Array.fromBase64(input);
44
- if (typeof Buffer !== "undefined") return new Uint8Array(Buffer.from(input, "base64"));
45
- if (typeof atob === "function") {
46
- const binary = atob(input);
47
- const bytes = new Uint8Array(binary.length);
48
- for (let i = 0; i < binary.length; i++) {
49
- bytes[i] = binary.charCodeAt(i);
50
- }
51
- return bytes;
52
- }
53
- throw new Error("Base64 decoding not supported in this environment");
54
- };
55
- const uuidToBase64 = (uuid) => {
56
- const hex = uuid.replace(/-/g, "");
57
- if (hex.length !== 32) throw new Error("Invalid UUID format");
58
- const bytes = new Uint8Array(16);
59
- for (let i = 0; i < 16; i++) {
60
- bytes[i] = parseInt(hex.substr(i * 2, 2), 16);
61
- }
62
- return toBase64(bytes).replace(/=+$/, "");
63
- };
64
- const base64ToUuid = (input) => {
65
- const bytes = fromBase64(input + "===".slice((input.length + 3) % 4));
66
- if (bytes.length !== 16) throw new Error("Invalid byte length for UUID");
67
- const hex = Array.from(bytes).map((b) => b.toString(16).padStart(2, "0")).join("");
68
- return [
69
- hex.slice(0, 8),
70
- hex.slice(8, 8 + 4),
71
- hex.slice(12, 12 + 4),
72
- hex.slice(16, 16 + 4),
73
- hex.slice(20, 20 + 12)
74
- ].join("-");
75
- };
76
- const assert = (value, message) => {
77
- if (!value) throw new Error(message);
78
- };
79
- const hasFeature = (flags, feature) => Boolean(flags & feature);
80
- const setFeatures = (flags = 0, features = []) => flags |= features.reduce((acc, feature) => acc | feature, flags || 0);
81
-
82
- const levels = {
83
- error: 0,
84
- warn: 1,
85
- info: 2,
86
- debug: 3
87
- };
88
- let currentLevel = "error";
89
- function setLogLevel(level) {
90
- if (levels[level] !== void 0) {
91
- currentLevel = level;
92
- }
93
- }
94
- function log(level, ...args) {
95
- if (levels[level] <= levels[currentLevel]) {
96
- console.log(`[${level.toUpperCase()}]`, ...args);
97
- }
98
- }
99
-
100
- const SEPARATOR = "^";
101
- const SITE_FEATURES_NATIVE = getSiteFeaturesNative();
102
- class ServerHeader {
103
- NAME = SERVER_HEADERS.WELCOME;
104
- VALUE;
105
- constructor(options) {
106
- if (typeof options === "string") {
107
- if (!options.length) {
108
- throw new Error("ServerHeader: non-empty welcome header string value must be provided");
109
- }
110
- this.VALUE = options;
111
- } else if (isObject(options) && "siteId" in options && "features" in options) {
112
- const { siteId, features } = options;
113
- if (typeof siteId !== "string" || !siteId.length || !Array.isArray(features) || !features.length) {
114
- throw new Error("ServerHeader options must be provided");
115
- }
116
- this.VALUE = this.encode(siteId, features);
117
- } else {
118
- throw new Error(
119
- "ServerHeader: non-empty welcome header string value or { siteId, features } options must be provided"
120
- );
121
- }
122
- }
123
- encode(siteId, features) {
124
- const flags = setFeatures(0, features);
125
- const encodedSiteId = uuidToBase64(siteId);
126
- return [encodedSiteId, CURRENT_PROTOCOL_VERSION, flags].join(SEPARATOR);
127
- }
128
- static decode(headerValue) {
129
- if (!headerValue?.length) return;
130
- try {
131
- const parts = headerValue.split(SEPARATOR);
132
- assert(parts.length === 3, "Invalid header value format");
133
- const [encodedSiteId, protocolVersion, flags] = parts;
134
- assert(
135
- Object.values(PROTOCOL_VERSION).includes(Number(protocolVersion)),
136
- "Invalid or unsupported protocol version"
137
- );
138
- const siteId = base64ToUuid(encodedSiteId);
139
- assert(siteId.length === 36, "Invalid siteId value");
140
- assert(Number(flags).toFixed(0).toString() === flags, "Invalid flags number");
141
- let features = [];
142
- for (const [feature, shift] of SITE_FEATURES_NATIVE) {
143
- if (hasFeature(Number(flags), shift)) features.push(feature);
144
- }
145
- return {
146
- version: Number(protocolVersion),
147
- features,
148
- siteId
149
- };
150
- } catch (err) {
151
- log("warn", "Could not decode server header value", { reason: err?.message });
152
- }
153
- }
154
- }
155
-
156
- export { CLIENT_HEADERS as C, FEATURES as F, PROTOCOL_VERSION as P, ServerHeader as S, ZEROAD_NETWORK_PUBLIC_KEY as Z, setLogLevel as a, SERVER_HEADERS as b, CURRENT_PROTOCOL_VERSION as c, fromBase64 as f, getSiteFeaturesNative as g, hasFeature as h, log as l, setFeatures as s, toBase64 as t };
@@ -1,57 +0,0 @@
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 };