@zeroad.network/token 0.13.9 → 0.13.11

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.
@@ -36,12 +36,18 @@ var PROTOCOL_VERSION = /* @__PURE__ */ ((PROTOCOL_VERSION2) => {
36
36
  })(PROTOCOL_VERSION || {});
37
37
  const CURRENT_PROTOCOL_VERSION = 1 /* V_1 */;
38
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) => {
39
+ let cachedFeatures;
40
+ function FEATURE_MAP() {
41
+ if (cachedFeatures) return cachedFeatures;
42
+ cachedFeatures = /* @__PURE__ */ new Map();
43
+ for (const key of Object.keys(FEATURES)) {
44
+ if (!isNaN(Number(key))) continue;
45
+ const typedKey = key;
46
+ cachedFeatures.set(typedKey, FEATURES[typedKey]);
47
+ }
48
+ return cachedFeatures;
49
+ }
50
+ function toBase64(data) {
45
51
  if (typeof data.toBase64 === "function") return data.toBase64();
46
52
  if (typeof Buffer !== "undefined") return Buffer.from(data).toString("base64");
47
53
  if (typeof btoa === "function") {
@@ -52,8 +58,8 @@ const toBase64 = (data) => {
52
58
  return btoa(binary);
53
59
  }
54
60
  throw new Error("Base64 encoding not supported in this environment");
55
- };
56
- const fromBase64 = (input) => {
61
+ }
62
+ function fromBase64(input) {
57
63
  if (typeof Uint8Array.fromBase64 === "function") return Uint8Array.fromBase64(input);
58
64
  if (typeof Buffer !== "undefined") return new Uint8Array(Buffer.from(input, "base64"));
59
65
  if (typeof atob === "function") {
@@ -65,15 +71,14 @@ const fromBase64 = (input) => {
65
71
  return bytes;
66
72
  }
67
73
  throw new Error("Base64 decoding not supported in this environment");
68
- };
69
- const assert = (value, message) => {
74
+ }
75
+ function assert(value, message) {
70
76
  if (!value) throw new Error(message);
71
- };
72
- const hasFlag = (flag, flags) => Boolean(flag & flags);
77
+ }
78
+ const hasFlag = (bit, flags) => Boolean(bit & flags);
73
79
  const setFlags = (features = []) => features.reduce((acc, feature) => acc | feature, 0);
74
80
 
75
81
  const SEPARATOR = "^";
76
- const SITE_FEATURES_NATIVE = getSiteFeaturesNative();
77
82
  const validFeatureValues = Object.values(FEATURES).filter((key) => !isNaN(Number(key)));
78
83
  const validFeatureKeys = Object.values(FEATURES).filter((key) => isNaN(Number(key)));
79
84
  function encodeServerHeader(clientId, features) {
@@ -99,9 +104,9 @@ function decodeServerHeader(headerValue) {
99
104
  "Invalid or unsupported protocol version"
100
105
  );
101
106
  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);
107
+ const features = [];
108
+ for (const [feature, bit] of FEATURE_MAP()) {
109
+ if (hasFlag(Number(flags), bit)) features.push(feature);
105
110
  }
106
111
  return {
107
112
  version: Number(protocolVersion),
@@ -113,4 +118,4 @@ function decodeServerHeader(headerValue) {
113
118
  }
114
119
  }
115
120
 
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 };
121
+ 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, hasFlag as h, log as l, setFlags as s, toBase64 as t };
@@ -38,12 +38,18 @@ var PROTOCOL_VERSION = /* @__PURE__ */ ((PROTOCOL_VERSION2) => {
38
38
  })(PROTOCOL_VERSION || {});
39
39
  const CURRENT_PROTOCOL_VERSION = 1 /* V_1 */;
40
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) => {
41
+ let cachedFeatures;
42
+ function FEATURE_MAP() {
43
+ if (cachedFeatures) return cachedFeatures;
44
+ cachedFeatures = /* @__PURE__ */ new Map();
45
+ for (const key of Object.keys(FEATURES)) {
46
+ if (!isNaN(Number(key))) continue;
47
+ const typedKey = key;
48
+ cachedFeatures.set(typedKey, FEATURES[typedKey]);
49
+ }
50
+ return cachedFeatures;
51
+ }
52
+ function toBase64(data) {
47
53
  if (typeof data.toBase64 === "function") return data.toBase64();
48
54
  if (typeof Buffer !== "undefined") return Buffer.from(data).toString("base64");
49
55
  if (typeof btoa === "function") {
@@ -54,8 +60,8 @@ const toBase64 = (data) => {
54
60
  return btoa(binary);
55
61
  }
56
62
  throw new Error("Base64 encoding not supported in this environment");
57
- };
58
- const fromBase64 = (input) => {
63
+ }
64
+ function fromBase64(input) {
59
65
  if (typeof Uint8Array.fromBase64 === "function") return Uint8Array.fromBase64(input);
60
66
  if (typeof Buffer !== "undefined") return new Uint8Array(Buffer.from(input, "base64"));
61
67
  if (typeof atob === "function") {
@@ -67,15 +73,14 @@ const fromBase64 = (input) => {
67
73
  return bytes;
68
74
  }
69
75
  throw new Error("Base64 decoding not supported in this environment");
70
- };
71
- const assert = (value, message) => {
76
+ }
77
+ function assert(value, message) {
72
78
  if (!value) throw new Error(message);
73
- };
74
- const hasFlag = (flag, flags) => Boolean(flag & flags);
79
+ }
80
+ const hasFlag = (bit, flags) => Boolean(bit & flags);
75
81
  const setFlags = (features = []) => features.reduce((acc, feature) => acc | feature, 0);
76
82
 
77
83
  const SEPARATOR = "^";
78
- const SITE_FEATURES_NATIVE = getSiteFeaturesNative();
79
84
  const validFeatureValues = Object.values(FEATURES).filter((key) => !isNaN(Number(key)));
80
85
  const validFeatureKeys = Object.values(FEATURES).filter((key) => isNaN(Number(key)));
81
86
  function encodeServerHeader(clientId, features) {
@@ -101,9 +106,9 @@ function decodeServerHeader(headerValue) {
101
106
  "Invalid or unsupported protocol version"
102
107
  );
103
108
  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);
109
+ const features = [];
110
+ for (const [feature, bit] of FEATURE_MAP()) {
111
+ if (hasFlag(Number(flags), bit)) features.push(feature);
107
112
  }
108
113
  return {
109
114
  version: Number(protocolVersion),
@@ -124,7 +129,6 @@ exports.ZEROAD_NETWORK_PUBLIC_KEY = ZEROAD_NETWORK_PUBLIC_KEY;
124
129
  exports.decodeServerHeader = decodeServerHeader;
125
130
  exports.encodeServerHeader = encodeServerHeader;
126
131
  exports.fromBase64 = fromBase64;
127
- exports.getSiteFeaturesNative = getSiteFeaturesNative;
128
132
  exports.hasFlag = hasFlag;
129
133
  exports.log = log;
130
134
  exports.setFlags = setFlags;
package/dist/browser.mjs CHANGED
@@ -1 +1 @@
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';
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-4DOrMTCi.mjs';
package/dist/index.cjs CHANGED
@@ -1,11 +1,20 @@
1
1
  'use strict';
2
2
 
3
- var browser = require('./browser-CMCq-bkd.cjs');
3
+ var browser = require('./browser-CBvcQzoQ.cjs');
4
4
  var node_buffer = require('node:buffer');
5
5
  var node_crypto = require('node:crypto');
6
6
 
7
7
  const keyCache = /* @__PURE__ */ new Map();
8
- const importPrivateKey = (privateKeyBase64) => {
8
+ function sign(data, privateKey) {
9
+ const key = importPrivateKey(privateKey);
10
+ return node_crypto.sign(null, node_buffer.Buffer.from(data), key);
11
+ }
12
+ function verify(data, signature, publicKey) {
13
+ const key = importPublicKey(publicKey);
14
+ return node_crypto.verify(null, node_buffer.Buffer.from(data), key, node_buffer.Buffer.from(signature));
15
+ }
16
+ const nonce = (size) => new Uint8Array(node_crypto.randomBytes(size));
17
+ function importPrivateKey(privateKeyBase64) {
9
18
  if (keyCache.has(privateKeyBase64)) return keyCache.get(privateKeyBase64);
10
19
  const key = node_crypto.createPrivateKey({
11
20
  key: node_buffer.Buffer.from(privateKeyBase64, "base64"),
@@ -14,8 +23,8 @@ const importPrivateKey = (privateKeyBase64) => {
14
23
  });
15
24
  keyCache.set(privateKeyBase64, key);
16
25
  return key;
17
- };
18
- const importPublicKey = (publicKeyBase64) => {
26
+ }
27
+ function importPublicKey(publicKeyBase64) {
19
28
  if (keyCache.has(publicKeyBase64)) return keyCache.get(publicKeyBase64);
20
29
  const key = node_crypto.createPublicKey({
21
30
  key: node_buffer.Buffer.from(publicKeyBase64, "base64"),
@@ -24,38 +33,34 @@ const importPublicKey = (publicKeyBase64) => {
24
33
  });
25
34
  keyCache.set(publicKeyBase64, key);
26
35
  return key;
27
- };
28
- const sign = (data, privateKey) => {
29
- const key = importPrivateKey(privateKey);
30
- return node_crypto.sign(null, node_buffer.Buffer.from(data), key);
31
- };
32
- const verify = (data, signature, publicKey) => {
33
- const key = importPublicKey(publicKey);
34
- return node_crypto.verify(null, node_buffer.Buffer.from(data), key, node_buffer.Buffer.from(signature));
35
- };
36
- const nonce = (size) => new Uint8Array(node_crypto.randomBytes(size));
36
+ }
37
37
 
38
38
  const VERSION_BYTES = 1;
39
39
  const NONCE_BYTES = 4;
40
40
  const SEPARATOR = ".";
41
- const SITE_FEATURES_NATIVE = browser.getSiteFeaturesNative();
42
- function parseClientToken(headerValue, clientId, publicKey) {
41
+ const FEATURES_TO_ACTIONS = {
42
+ [browser.FEATURES.CLEAN_WEB]: [
43
+ "HIDE_ADVERTISEMENTS",
44
+ "HIDE_COOKIE_CONSENT_SCREEN",
45
+ "HIDE_MARKETING_DIALOGS",
46
+ "DISABLE_NON_FUNCTIONAL_TRACKING"
47
+ ],
48
+ [browser.FEATURES.ONE_PASS]: ["DISABLE_CONTENT_PAYWALL", "ENABLE_SUBSCRIPTION_ACCESS"]
49
+ };
50
+ function parseClientToken(headerValue, options) {
43
51
  const headerValueAsString = Array.isArray(headerValue) ? headerValue[0] : headerValue;
44
- const data = decodeClientHeader(headerValueAsString, publicKey);
52
+ const data = decodeClientHeader(headerValueAsString, options.publicKey || browser.ZEROAD_NETWORK_PUBLIC_KEY);
45
53
  let flags = 0;
46
54
  if (data && data.expiresAt.getTime() >= Date.now()) flags = data.flags;
47
- if (flags && data?.clientId && data.clientId !== clientId) flags = 0;
48
- const features = SITE_FEATURES_NATIVE.map(([feature, shift]) => browser.hasFlag(flags, shift) && feature).filter((e) => !!e);
49
- const hasCleanWeb = features.includes("CLEAN_WEB");
50
- const hasOnePass = features.includes("ONE_PASS");
51
- return {
52
- HIDE_ADVERTISEMENTS: hasCleanWeb,
53
- HIDE_COOKIE_CONSENT_SCREEN: hasCleanWeb,
54
- HIDE_MARKETING_DIALOGS: hasCleanWeb,
55
- DISABLE_NON_FUNCTIONAL_TRACKING: hasCleanWeb,
56
- DISABLE_CONTENT_PAYWALL: hasOnePass,
57
- ENABLE_SUBSCRIPTION_ACCESS: hasOnePass
58
- };
55
+ if (flags && data?.clientId && data.clientId !== options.clientId) flags = 0;
56
+ const context = /* @__PURE__ */ new Map();
57
+ for (const [feature, actionNames] of Object.entries(FEATURES_TO_ACTIONS)) {
58
+ const decision = options.features.includes(Number(feature)) && browser.hasFlag(Number(feature), flags);
59
+ for (const actionName of actionNames) {
60
+ context.set(actionName, decision);
61
+ }
62
+ }
63
+ return Object.fromEntries(context);
59
64
  }
60
65
  function decodeClientHeader(headerValue, publicKey) {
61
66
  if (!headerValue?.length) return;
@@ -114,8 +119,8 @@ function as32BitNumber(byteArray, begin) {
114
119
  function Site(options) {
115
120
  const serverHeaderValue = browser.encodeServerHeader(options.clientId, options.features);
116
121
  return {
117
- parseClientToken: (headerValue) => parseClientToken(headerValue, options.clientId, browser.ZEROAD_NETWORK_PUBLIC_KEY),
118
- CLIENT_HEADER_NAME: browser.CLIENT_HEADERS.HELLO,
122
+ parseClientToken: (headerValue) => parseClientToken(headerValue, { clientId: options.clientId, features: options.features }),
123
+ CLIENT_HEADER_NAME: browser.CLIENT_HEADERS.HELLO.toLowerCase(),
119
124
  SERVER_HEADER_NAME: browser.SERVER_HEADERS.WELCOME,
120
125
  SERVER_HEADER_VALUE: serverHeaderValue
121
126
  };
package/dist/index.d.cts CHANGED
@@ -1,13 +1,18 @@
1
- import { P as PROTOCOL_VERSION, F as FEATURES, C as CLIENT_HEADERS, S as SERVER_HEADERS } from './browser-BiNZ2c6t.cjs';
2
- export { a as CURRENT_PROTOCOL_VERSION, W as WelcomeHeader, Z as ZEROAD_NETWORK_PUBLIC_KEY, d as decodeServerHeader, e as encodeServerHeader } from './browser-BiNZ2c6t.cjs';
1
+ import { F as FEATURES, P as PROTOCOL_VERSION, S as SERVER_HEADERS } from './browser-BiNZ2c6t.cjs';
2
+ export { C as CLIENT_HEADERS, a as CURRENT_PROTOCOL_VERSION, W as WelcomeHeader, Z as ZEROAD_NETWORK_PUBLIC_KEY, d as decodeServerHeader, e as encodeServerHeader } from './browser-BiNZ2c6t.cjs';
3
3
 
4
4
  type LogLevel = "error" | "warn" | "info" | "debug";
5
5
  declare function setLogLevel(level: LogLevel): void;
6
6
 
7
- type FEATURE_FLAG = "HIDE_ADVERTISEMENTS" | "HIDE_COOKIE_CONSENT_SCREEN" | "HIDE_MARKETING_DIALOGS" | "DISABLE_NON_FUNCTIONAL_TRACKING" | "DISABLE_CONTENT_PAYWALL" | "ENABLE_SUBSCRIPTION_ACCESS";
7
+ type FEATURE_ACTIONS = "HIDE_ADVERTISEMENTS" | "HIDE_COOKIE_CONSENT_SCREEN" | "HIDE_MARKETING_DIALOGS" | "DISABLE_NON_FUNCTIONAL_TRACKING" | "DISABLE_CONTENT_PAYWALL" | "ENABLE_SUBSCRIPTION_ACCESS";
8
8
  type ClientHeaderValue = string | string[] | undefined;
9
- type FeatureFlags = Record<FEATURE_FLAG, boolean>;
10
- declare function parseClientToken(headerValue: ClientHeaderValue, clientId: string, publicKey: string): FeatureFlags;
9
+ type TokenContext = Record<FEATURE_ACTIONS, boolean>;
10
+ type ParseClientTokenOptions = {
11
+ clientId: string;
12
+ features: FEATURES[];
13
+ publicKey?: string;
14
+ };
15
+ declare function parseClientToken(headerValue: ClientHeaderValue, options: ParseClientTokenOptions): TokenContext;
11
16
  type DecodedClientHeader = {
12
17
  version: PROTOCOL_VERSION;
13
18
  expiresAt: Date;
@@ -28,11 +33,11 @@ type SiteOptions = {
28
33
  features: FEATURES[];
29
34
  };
30
35
  declare function Site(options: SiteOptions): {
31
- parseClientToken: (headerValue: ClientHeaderValue) => FeatureFlags;
32
- CLIENT_HEADER_NAME: CLIENT_HEADERS;
36
+ parseClientToken: (headerValue: ClientHeaderValue) => TokenContext;
37
+ CLIENT_HEADER_NAME: string;
33
38
  SERVER_HEADER_NAME: SERVER_HEADERS;
34
39
  SERVER_HEADER_VALUE: string;
35
40
  };
36
41
 
37
- export { CLIENT_HEADERS, FEATURES, PROTOCOL_VERSION, SERVER_HEADERS, Site, decodeClientHeader, encodeClientHeader, parseClientToken, setLogLevel };
38
- export type { ClientHeaderValue, DecodedClientHeader, FEATURE_FLAG, FeatureFlags };
42
+ export { FEATURES, PROTOCOL_VERSION, SERVER_HEADERS, Site, decodeClientHeader, encodeClientHeader, parseClientToken, setLogLevel };
43
+ export type { ClientHeaderValue, DecodedClientHeader, FEATURE_ACTIONS, ParseClientTokenOptions, TokenContext };
package/dist/index.d.mts CHANGED
@@ -1,13 +1,18 @@
1
- import { P as PROTOCOL_VERSION, F as FEATURES, C as CLIENT_HEADERS, S as SERVER_HEADERS } from './browser-BiNZ2c6t.mjs';
2
- export { a as CURRENT_PROTOCOL_VERSION, W as WelcomeHeader, Z as ZEROAD_NETWORK_PUBLIC_KEY, d as decodeServerHeader, e as encodeServerHeader } from './browser-BiNZ2c6t.mjs';
1
+ import { F as FEATURES, P as PROTOCOL_VERSION, S as SERVER_HEADERS } from './browser-BiNZ2c6t.mjs';
2
+ export { C as CLIENT_HEADERS, a as CURRENT_PROTOCOL_VERSION, W as WelcomeHeader, Z as ZEROAD_NETWORK_PUBLIC_KEY, d as decodeServerHeader, e as encodeServerHeader } from './browser-BiNZ2c6t.mjs';
3
3
 
4
4
  type LogLevel = "error" | "warn" | "info" | "debug";
5
5
  declare function setLogLevel(level: LogLevel): void;
6
6
 
7
- type FEATURE_FLAG = "HIDE_ADVERTISEMENTS" | "HIDE_COOKIE_CONSENT_SCREEN" | "HIDE_MARKETING_DIALOGS" | "DISABLE_NON_FUNCTIONAL_TRACKING" | "DISABLE_CONTENT_PAYWALL" | "ENABLE_SUBSCRIPTION_ACCESS";
7
+ type FEATURE_ACTIONS = "HIDE_ADVERTISEMENTS" | "HIDE_COOKIE_CONSENT_SCREEN" | "HIDE_MARKETING_DIALOGS" | "DISABLE_NON_FUNCTIONAL_TRACKING" | "DISABLE_CONTENT_PAYWALL" | "ENABLE_SUBSCRIPTION_ACCESS";
8
8
  type ClientHeaderValue = string | string[] | undefined;
9
- type FeatureFlags = Record<FEATURE_FLAG, boolean>;
10
- declare function parseClientToken(headerValue: ClientHeaderValue, clientId: string, publicKey: string): FeatureFlags;
9
+ type TokenContext = Record<FEATURE_ACTIONS, boolean>;
10
+ type ParseClientTokenOptions = {
11
+ clientId: string;
12
+ features: FEATURES[];
13
+ publicKey?: string;
14
+ };
15
+ declare function parseClientToken(headerValue: ClientHeaderValue, options: ParseClientTokenOptions): TokenContext;
11
16
  type DecodedClientHeader = {
12
17
  version: PROTOCOL_VERSION;
13
18
  expiresAt: Date;
@@ -28,11 +33,11 @@ type SiteOptions = {
28
33
  features: FEATURES[];
29
34
  };
30
35
  declare function Site(options: SiteOptions): {
31
- parseClientToken: (headerValue: ClientHeaderValue) => FeatureFlags;
32
- CLIENT_HEADER_NAME: CLIENT_HEADERS;
36
+ parseClientToken: (headerValue: ClientHeaderValue) => TokenContext;
37
+ CLIENT_HEADER_NAME: string;
33
38
  SERVER_HEADER_NAME: SERVER_HEADERS;
34
39
  SERVER_HEADER_VALUE: string;
35
40
  };
36
41
 
37
- export { CLIENT_HEADERS, FEATURES, PROTOCOL_VERSION, SERVER_HEADERS, Site, decodeClientHeader, encodeClientHeader, parseClientToken, setLogLevel };
38
- export type { ClientHeaderValue, DecodedClientHeader, FEATURE_FLAG, FeatureFlags };
42
+ export { FEATURES, PROTOCOL_VERSION, SERVER_HEADERS, Site, decodeClientHeader, encodeClientHeader, parseClientToken, setLogLevel };
43
+ export type { ClientHeaderValue, DecodedClientHeader, FEATURE_ACTIONS, ParseClientTokenOptions, TokenContext };
package/dist/index.mjs CHANGED
@@ -1,10 +1,19 @@
1
- import { g as getSiteFeaturesNative, h as hasFlag, f as fromBase64, P as PROTOCOL_VERSION, l as log, s as setFlags, t as toBase64, e as encodeServerHeader, S as SERVER_HEADERS, C as CLIENT_HEADERS, Z as ZEROAD_NETWORK_PUBLIC_KEY } from './browser-Citg0bh9.mjs';
2
- export { b as CURRENT_PROTOCOL_VERSION, F as FEATURES, d as decodeServerHeader, a as setLogLevel } from './browser-Citg0bh9.mjs';
1
+ import { F as FEATURES, Z as ZEROAD_NETWORK_PUBLIC_KEY, h as hasFlag, f as fromBase64, P as PROTOCOL_VERSION, l as log, s as setFlags, t as toBase64, e as encodeServerHeader, S as SERVER_HEADERS, C as CLIENT_HEADERS } from './browser-4DOrMTCi.mjs';
2
+ export { b as CURRENT_PROTOCOL_VERSION, d as decodeServerHeader, a as setLogLevel } from './browser-4DOrMTCi.mjs';
3
3
  import { Buffer } from 'node:buffer';
4
4
  import { verify as verify$1, randomBytes, sign as sign$1, createPublicKey, createPrivateKey } from 'node:crypto';
5
5
 
6
6
  const keyCache = /* @__PURE__ */ new Map();
7
- const importPrivateKey = (privateKeyBase64) => {
7
+ function sign(data, privateKey) {
8
+ const key = importPrivateKey(privateKey);
9
+ return sign$1(null, Buffer.from(data), key);
10
+ }
11
+ function verify(data, signature, publicKey) {
12
+ const key = importPublicKey(publicKey);
13
+ return verify$1(null, Buffer.from(data), key, Buffer.from(signature));
14
+ }
15
+ const nonce = (size) => new Uint8Array(randomBytes(size));
16
+ function importPrivateKey(privateKeyBase64) {
8
17
  if (keyCache.has(privateKeyBase64)) return keyCache.get(privateKeyBase64);
9
18
  const key = createPrivateKey({
10
19
  key: Buffer.from(privateKeyBase64, "base64"),
@@ -13,8 +22,8 @@ const importPrivateKey = (privateKeyBase64) => {
13
22
  });
14
23
  keyCache.set(privateKeyBase64, key);
15
24
  return key;
16
- };
17
- const importPublicKey = (publicKeyBase64) => {
25
+ }
26
+ function importPublicKey(publicKeyBase64) {
18
27
  if (keyCache.has(publicKeyBase64)) return keyCache.get(publicKeyBase64);
19
28
  const key = createPublicKey({
20
29
  key: Buffer.from(publicKeyBase64, "base64"),
@@ -23,38 +32,34 @@ const importPublicKey = (publicKeyBase64) => {
23
32
  });
24
33
  keyCache.set(publicKeyBase64, key);
25
34
  return key;
26
- };
27
- const sign = (data, privateKey) => {
28
- const key = importPrivateKey(privateKey);
29
- return sign$1(null, Buffer.from(data), key);
30
- };
31
- const verify = (data, signature, publicKey) => {
32
- const key = importPublicKey(publicKey);
33
- return verify$1(null, Buffer.from(data), key, Buffer.from(signature));
34
- };
35
- const nonce = (size) => new Uint8Array(randomBytes(size));
35
+ }
36
36
 
37
37
  const VERSION_BYTES = 1;
38
38
  const NONCE_BYTES = 4;
39
39
  const SEPARATOR = ".";
40
- const SITE_FEATURES_NATIVE = getSiteFeaturesNative();
41
- function parseClientToken(headerValue, clientId, publicKey) {
40
+ const FEATURES_TO_ACTIONS = {
41
+ [FEATURES.CLEAN_WEB]: [
42
+ "HIDE_ADVERTISEMENTS",
43
+ "HIDE_COOKIE_CONSENT_SCREEN",
44
+ "HIDE_MARKETING_DIALOGS",
45
+ "DISABLE_NON_FUNCTIONAL_TRACKING"
46
+ ],
47
+ [FEATURES.ONE_PASS]: ["DISABLE_CONTENT_PAYWALL", "ENABLE_SUBSCRIPTION_ACCESS"]
48
+ };
49
+ function parseClientToken(headerValue, options) {
42
50
  const headerValueAsString = Array.isArray(headerValue) ? headerValue[0] : headerValue;
43
- const data = decodeClientHeader(headerValueAsString, publicKey);
51
+ const data = decodeClientHeader(headerValueAsString, options.publicKey || ZEROAD_NETWORK_PUBLIC_KEY);
44
52
  let flags = 0;
45
53
  if (data && data.expiresAt.getTime() >= Date.now()) flags = data.flags;
46
- if (flags && data?.clientId && data.clientId !== clientId) flags = 0;
47
- const features = SITE_FEATURES_NATIVE.map(([feature, shift]) => hasFlag(flags, shift) && feature).filter((e) => !!e);
48
- const hasCleanWeb = features.includes("CLEAN_WEB");
49
- const hasOnePass = features.includes("ONE_PASS");
50
- return {
51
- HIDE_ADVERTISEMENTS: hasCleanWeb,
52
- HIDE_COOKIE_CONSENT_SCREEN: hasCleanWeb,
53
- HIDE_MARKETING_DIALOGS: hasCleanWeb,
54
- DISABLE_NON_FUNCTIONAL_TRACKING: hasCleanWeb,
55
- DISABLE_CONTENT_PAYWALL: hasOnePass,
56
- ENABLE_SUBSCRIPTION_ACCESS: hasOnePass
57
- };
54
+ if (flags && data?.clientId && data.clientId !== options.clientId) flags = 0;
55
+ const context = /* @__PURE__ */ new Map();
56
+ for (const [feature, actionNames] of Object.entries(FEATURES_TO_ACTIONS)) {
57
+ const decision = options.features.includes(Number(feature)) && hasFlag(Number(feature), flags);
58
+ for (const actionName of actionNames) {
59
+ context.set(actionName, decision);
60
+ }
61
+ }
62
+ return Object.fromEntries(context);
58
63
  }
59
64
  function decodeClientHeader(headerValue, publicKey) {
60
65
  if (!headerValue?.length) return;
@@ -113,11 +118,11 @@ function as32BitNumber(byteArray, begin) {
113
118
  function Site(options) {
114
119
  const serverHeaderValue = encodeServerHeader(options.clientId, options.features);
115
120
  return {
116
- parseClientToken: (headerValue) => parseClientToken(headerValue, options.clientId, ZEROAD_NETWORK_PUBLIC_KEY),
117
- CLIENT_HEADER_NAME: CLIENT_HEADERS.HELLO,
121
+ parseClientToken: (headerValue) => parseClientToken(headerValue, { clientId: options.clientId, features: options.features }),
122
+ CLIENT_HEADER_NAME: CLIENT_HEADERS.HELLO.toLowerCase(),
118
123
  SERVER_HEADER_NAME: SERVER_HEADERS.WELCOME,
119
124
  SERVER_HEADER_VALUE: serverHeaderValue
120
125
  };
121
126
  }
122
127
 
123
- export { CLIENT_HEADERS, PROTOCOL_VERSION, SERVER_HEADERS, Site, ZEROAD_NETWORK_PUBLIC_KEY, decodeClientHeader, encodeClientHeader, encodeServerHeader, parseClientToken };
128
+ export { CLIENT_HEADERS, FEATURES, PROTOCOL_VERSION, SERVER_HEADERS, Site, ZEROAD_NETWORK_PUBLIC_KEY, decodeClientHeader, encodeClientHeader, encodeServerHeader, parseClientToken };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zeroad.network/token",
3
- "version": "0.13.9",
3
+ "version": "0.13.11",
4
4
  "license": "Apache-2.0",
5
5
  "repository": "github:laurynas-karvelis/zeroad-token-typescript",
6
6
  "homepage": "https://zeroad.network",
@@ -44,8 +44,9 @@
44
44
  "hono"
45
45
  ],
46
46
  "engines": {
47
- "node": ">= 18.0.0",
48
- "bun": ">=1.2.6"
47
+ "node": ">=16.0.0",
48
+ "bun": ">=1.1.0",
49
+ "deno": ">=2.0.0"
49
50
  },
50
51
  "main": "./dist/index.cjs",
51
52
  "module": "./dist/index.mjs",
@@ -74,8 +75,8 @@
74
75
  },
75
76
  "dependencies": {},
76
77
  "devDependencies": {
77
- "@types/bun": "^1.3.3",
78
- "@types/node": "^24.10.0",
78
+ "@types/bun": "^1.3.4",
79
+ "@types/node": "^24.10.2",
79
80
  "pkgroll": "^2.21.4",
80
81
  "prettier": "^3.7.4",
81
82
  "typescript": "^5.9.3"