@structured-world/structured-public-domains 0.0.25 → 0.0.26

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/psl.bin CHANGED
Binary file
package/dist/tiny.cjs CHANGED
@@ -2,55 +2,71 @@
2
2
 
3
3
  // src/trie.ts
4
4
  var utf8 = new TextDecoder("utf-8", { fatal: true });
5
+ var COUNT_ESCAPE = 31;
6
+ var INDEX_ENTRY = 3;
5
7
  function parseTrie(data) {
6
- const cursor = { pos: 0 };
7
- const root = parseNode(data, cursor);
8
- if (cursor.pos !== data.length) {
9
- throw new Error("PSL data: trailing bytes after root node");
10
- }
11
- return root;
8
+ return { image: data, at: 0 };
12
9
  }
13
- function parseNode(data, cursor) {
14
- const flags = byteAt(data, cursor.pos++);
15
- if ((flags & -2) !== 0) {
16
- throw new Error("PSL data: reserved flag bits set");
17
- }
18
- const lo = byteAt(data, cursor.pos++);
19
- const hi = byteAt(data, cursor.pos++);
20
- let numChildren = lo | hi << 8;
21
- const MIN_CHILD_ENCODED_LEN = 5;
22
- const remaining = data.length - cursor.pos;
23
- if (remaining < 0 || numChildren > Math.floor(remaining / MIN_CHILD_ENCODED_LEN)) {
24
- throw new Error("PSL data: num_children exceeds remaining bytes");
10
+ function headerOf(node) {
11
+ const byte = node.image[node.at];
12
+ if (byte === void 0) return void 0;
13
+ const packed = byte & COUNT_ESCAPE;
14
+ let children;
15
+ let body;
16
+ if (packed === COUNT_ESCAPE) {
17
+ const lo = node.image[node.at + 1];
18
+ const hi = node.image[node.at + 2];
19
+ if (lo === void 0 || hi === void 0) return void 0;
20
+ children = lo | hi << 8;
21
+ body = node.at + 3;
22
+ } else {
23
+ children = packed;
24
+ body = node.at + 1;
25
25
  }
26
- const labels = [];
27
- const children = [];
28
- let prev;
29
- for (; numChildren > 0; numChildren--) {
30
- const labelLen = byteAt(data, cursor.pos++);
31
- if (labelLen === 0) {
32
- throw new Error("PSL data: empty label");
33
- }
34
- const labelEnd = cursor.pos + labelLen;
35
- if (labelEnd > data.length) {
36
- throw new Error("PSL data: label runs past end of data");
37
- }
38
- const label = decodeLabel(data, cursor.pos, labelEnd);
39
- cursor.pos = labelEnd;
40
- if (prev !== void 0 && !(label > prev)) {
41
- throw new Error("PSL data: children not strictly sorted");
42
- }
43
- prev = label;
44
- labels.push(label);
45
- children.push(parseNode(data, cursor));
26
+ return {
27
+ suffixBoundary: (byte & 128) !== 0,
28
+ indexed: (byte & 64) !== 0,
29
+ wildcard: (byte & 32) !== 0,
30
+ children,
31
+ body
32
+ };
33
+ }
34
+ function varint(image, at) {
35
+ let value = 0;
36
+ let shift = 0;
37
+ for (let step = 0; step < 5; step++) {
38
+ const byte = image[at + step];
39
+ if (byte === void 0) return void 0;
40
+ value += (byte & 127) * 2 ** shift;
41
+ if ((byte & 128) === 0) return [value, at + step + 1];
42
+ shift += 7;
46
43
  }
47
- return { suffixBoundary: (flags & 1) !== 0, labels, children };
44
+ return void 0;
48
45
  }
49
- function byteAt(data, i) {
50
- if (i >= data.length) {
51
- throw new Error("PSL data: unexpected end of data");
46
+ function entryAt(node, at, skippable) {
47
+ const len = node.image[at];
48
+ if (len === void 0) return void 0;
49
+ const labelEnd = at + 1 + len;
50
+ if (labelEnd > node.image.length) return void 0;
51
+ const label = decodeLabel(node.image, at + 1, labelEnd);
52
+ if (!skippable) {
53
+ return { label, child: { image: node.image, at: labelEnd }, next: labelEnd };
52
54
  }
53
- return data[i];
55
+ const read = varint(node.image, labelEnd);
56
+ if (read === void 0) return void 0;
57
+ const [subtreeLen, childAt] = read;
58
+ const next = childAt + subtreeLen;
59
+ if (next > node.image.length) return void 0;
60
+ return { label, child: { image: node.image, at: childAt }, next };
61
+ }
62
+ function indexEntry(node, header, i) {
63
+ const slot = header.body + i * INDEX_ENTRY;
64
+ const b0 = node.image[slot];
65
+ const b1 = node.image[slot + 1];
66
+ const b2 = node.image[slot + 2];
67
+ if (b0 === void 0 || b1 === void 0 || b2 === void 0) return void 0;
68
+ const at = node.at + (b0 | b1 << 8 | b2 << 16);
69
+ return at < node.image.length ? at : void 0;
54
70
  }
55
71
  function decodeLabel(data, start, end) {
56
72
  for (let i = start; i < end; i++) {
@@ -60,24 +76,100 @@ function decodeLabel(data, start, end) {
60
76
  for (let i = start; i < end; i++) s += String.fromCharCode(data[i]);
61
77
  return s;
62
78
  }
63
- function indexOfChild(node, label) {
64
- let lo = 0;
65
- let hi = node.labels.length - 1;
66
- while (lo <= hi) {
67
- const mid = lo + hi >>> 1;
68
- const cur = node.labels[mid];
69
- if (cur === label) return mid;
70
- if (cur < label) lo = mid + 1;
71
- else hi = mid - 1;
72
- }
73
- return -1;
74
- }
75
79
  function childOf(node, label) {
76
- const i = indexOfChild(node, label);
77
- return i < 0 ? void 0 : node.children[i];
80
+ const header = headerOf(node);
81
+ if (header === void 0) return void 0;
82
+ if (header.indexed) {
83
+ let lo = 0;
84
+ let hi = header.children;
85
+ while (lo < hi) {
86
+ const mid = lo + hi >>> 1;
87
+ const at2 = indexEntry(node, header, mid);
88
+ if (at2 === void 0) return void 0;
89
+ const entry = entryAt(node, at2, false);
90
+ if (entry === void 0) return void 0;
91
+ if (entry.label === label) return entry.child;
92
+ if (entry.label < label) lo = mid + 1;
93
+ else hi = mid;
94
+ }
95
+ return void 0;
96
+ }
97
+ let at = header.body;
98
+ for (let i = 0; i < header.children; i++) {
99
+ const entry = entryAt(node, at, true);
100
+ if (entry === void 0) return void 0;
101
+ if (entry.label === label) return entry.child;
102
+ if (entry.label > label) return void 0;
103
+ at = entry.next;
104
+ }
105
+ return void 0;
78
106
  }
79
107
  function hasChild(node, label) {
80
- return indexOfChild(node, label) >= 0;
108
+ return childOf(node, label) !== void 0;
109
+ }
110
+ function hasWildcardChild(node) {
111
+ return headerOf(node)?.wildcard ?? false;
112
+ }
113
+ function isSuffixBoundary(node) {
114
+ return headerOf(node)?.suffixBoundary ?? false;
115
+ }
116
+ function validateTrie(data) {
117
+ const end = validateNode({ image: data, at: 0 });
118
+ if (end !== data.length) {
119
+ throw new Error("PSL data: trailing bytes after root node");
120
+ }
121
+ }
122
+ function validateNode(node) {
123
+ const header = headerOf(node);
124
+ if (header === void 0) {
125
+ throw new Error("PSL data: unexpected end of data");
126
+ }
127
+ let at = header.body;
128
+ if (header.indexed) at += header.children * INDEX_ENTRY;
129
+ let previous;
130
+ let sawWildcard = false;
131
+ for (let i = 0; i < header.children; i++) {
132
+ if (header.indexed) {
133
+ const slot = indexEntry(node, header, i);
134
+ if (slot === void 0) {
135
+ throw new Error("PSL data: index offset out of bounds");
136
+ }
137
+ if (slot !== at) {
138
+ throw new Error("PSL data: index offset does not point at its entry");
139
+ }
140
+ }
141
+ const len = node.image[at];
142
+ if (len === void 0) throw new Error("PSL data: unexpected end of data");
143
+ if (len === 0) throw new Error("PSL data: empty label");
144
+ const labelEnd = at + 1 + len;
145
+ if (labelEnd > node.image.length) {
146
+ throw new Error("PSL data: label runs past end of data");
147
+ }
148
+ const label = decodeLabel(node.image, at + 1, labelEnd);
149
+ if (previous !== void 0 && !(label > previous)) {
150
+ throw new Error("PSL data: children not strictly sorted");
151
+ }
152
+ previous = label;
153
+ if (label === "*") sawWildcard = true;
154
+ let childAt = labelEnd;
155
+ if (!header.indexed) {
156
+ const read = varint(node.image, labelEnd);
157
+ if (read === void 0) throw new Error("PSL data: malformed subtree length");
158
+ childAt = read[1];
159
+ }
160
+ const childEnd = validateNode({ image: node.image, at: childAt });
161
+ if (!header.indexed) {
162
+ const read = varint(node.image, labelEnd);
163
+ if (childEnd !== read[1] + read[0]) {
164
+ throw new Error("PSL data: subtree length disagrees with its contents");
165
+ }
166
+ }
167
+ at = childEnd;
168
+ }
169
+ if (header.wildcard !== sawWildcard) {
170
+ throw new Error("PSL data: wildcard flag disagrees with the children");
171
+ }
172
+ return at;
81
173
  }
82
174
  function lookupTrie(root, domain) {
83
175
  const trimmed = domain.trim();
@@ -92,7 +184,7 @@ function lookupTrie(root, domain) {
92
184
  let known = false;
93
185
  for (let depth = 0; depth < labels.length; depth++) {
94
186
  const label = labels[depth].toLowerCase();
95
- if (hasChild(node, "*")) {
187
+ if (hasWildcardChild(node)) {
96
188
  if (hasChild(node, "!" + label)) {
97
189
  suffixDepth = depth;
98
190
  } else {
@@ -102,7 +194,7 @@ function lookupTrie(root, domain) {
102
194
  }
103
195
  const child = childOf(node, label);
104
196
  if (child !== void 0) {
105
- if (child.suffixBoundary) {
197
+ if (isSuffixBoundary(child)) {
106
198
  suffixDepth = depth + 1;
107
199
  known = true;
108
200
  }
@@ -167,6 +259,7 @@ async function fetchAndParse(opts) {
167
259
  const cached = await readCache(url, ttlMs, opts.cacheDir);
168
260
  if (cached !== void 0) {
169
261
  try {
262
+ validateTrie(cached);
170
263
  return [cached, parseTrie(cached)];
171
264
  } catch {
172
265
  await deleteCache(url, opts.cacheDir).catch(() => void 0);
@@ -177,6 +270,7 @@ async function fetchAndParse(opts) {
177
270
  throw new Error("no fetch implementation available; pass `fetch` in LoadOptions");
178
271
  }
179
272
  const data = await fetchBytes(doFetch, url);
273
+ validateTrie(data);
180
274
  const trie = parseTrie(data);
181
275
  if (useCache) await writeCache(url, data, opts.cacheDir).catch(() => void 0);
182
276
  return [data, trie];
package/dist/tiny.js CHANGED
@@ -1,4 +1,4 @@
1
- import { parseTrie, lookupTrie } from './chunk-EON4VJGA.js';
1
+ import { validateTrie, parseTrie, lookupTrie } from './chunk-ZK4NSAPK.js';
2
2
 
3
3
  // src/tiny.ts
4
4
  var PSL_RANGE = "0.0" ;
@@ -47,6 +47,7 @@ async function fetchAndParse(opts) {
47
47
  const cached = await readCache(url, ttlMs, opts.cacheDir);
48
48
  if (cached !== void 0) {
49
49
  try {
50
+ validateTrie(cached);
50
51
  return [cached, parseTrie(cached)];
51
52
  } catch {
52
53
  await deleteCache(url, opts.cacheDir).catch(() => void 0);
@@ -57,6 +58,7 @@ async function fetchAndParse(opts) {
57
58
  throw new Error("no fetch implementation available; pass `fetch` in LoadOptions");
58
59
  }
59
60
  const data = await fetchBytes(doFetch, url);
61
+ validateTrie(data);
60
62
  const trie = parseTrie(data);
61
63
  if (useCache) await writeCache(url, data, opts.cacheDir).catch(() => void 0);
62
64
  return [data, trie];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@structured-world/structured-public-domains",
3
- "version": "0.0.25",
3
+ "version": "0.0.26",
4
4
  "description": "Compact Public Suffix List (PSL) as a native TypeScript package: embedded ~108KB binary trie, synchronous zero-dependency lookups, ESM + CommonJS. Checked daily against publicsuffix.org.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -1,121 +0,0 @@
1
- // src/trie.ts
2
- var utf8 = new TextDecoder("utf-8", { fatal: true });
3
- function parseTrie(data) {
4
- const cursor = { pos: 0 };
5
- const root = parseNode(data, cursor);
6
- if (cursor.pos !== data.length) {
7
- throw new Error("PSL data: trailing bytes after root node");
8
- }
9
- return root;
10
- }
11
- function parseNode(data, cursor) {
12
- const flags = byteAt(data, cursor.pos++);
13
- if ((flags & -2) !== 0) {
14
- throw new Error("PSL data: reserved flag bits set");
15
- }
16
- const lo = byteAt(data, cursor.pos++);
17
- const hi = byteAt(data, cursor.pos++);
18
- let numChildren = lo | hi << 8;
19
- const MIN_CHILD_ENCODED_LEN = 5;
20
- const remaining = data.length - cursor.pos;
21
- if (remaining < 0 || numChildren > Math.floor(remaining / MIN_CHILD_ENCODED_LEN)) {
22
- throw new Error("PSL data: num_children exceeds remaining bytes");
23
- }
24
- const labels = [];
25
- const children = [];
26
- let prev;
27
- for (; numChildren > 0; numChildren--) {
28
- const labelLen = byteAt(data, cursor.pos++);
29
- if (labelLen === 0) {
30
- throw new Error("PSL data: empty label");
31
- }
32
- const labelEnd = cursor.pos + labelLen;
33
- if (labelEnd > data.length) {
34
- throw new Error("PSL data: label runs past end of data");
35
- }
36
- const label = decodeLabel(data, cursor.pos, labelEnd);
37
- cursor.pos = labelEnd;
38
- if (prev !== void 0 && !(label > prev)) {
39
- throw new Error("PSL data: children not strictly sorted");
40
- }
41
- prev = label;
42
- labels.push(label);
43
- children.push(parseNode(data, cursor));
44
- }
45
- return { suffixBoundary: (flags & 1) !== 0, labels, children };
46
- }
47
- function byteAt(data, i) {
48
- if (i >= data.length) {
49
- throw new Error("PSL data: unexpected end of data");
50
- }
51
- return data[i];
52
- }
53
- function decodeLabel(data, start, end) {
54
- for (let i = start; i < end; i++) {
55
- if (data[i] >= 128) return utf8.decode(data.subarray(start, end));
56
- }
57
- let s = "";
58
- for (let i = start; i < end; i++) s += String.fromCharCode(data[i]);
59
- return s;
60
- }
61
- function indexOfChild(node, label) {
62
- let lo = 0;
63
- let hi = node.labels.length - 1;
64
- while (lo <= hi) {
65
- const mid = lo + hi >>> 1;
66
- const cur = node.labels[mid];
67
- if (cur === label) return mid;
68
- if (cur < label) lo = mid + 1;
69
- else hi = mid - 1;
70
- }
71
- return -1;
72
- }
73
- function childOf(node, label) {
74
- const i = indexOfChild(node, label);
75
- return i < 0 ? void 0 : node.children[i];
76
- }
77
- function hasChild(node, label) {
78
- return indexOfChild(node, label) >= 0;
79
- }
80
- function lookupTrie(root, domain) {
81
- const trimmed = domain.trim();
82
- const stripped = trimmed.endsWith(".") ? trimmed.slice(0, -1) : trimmed;
83
- if (stripped === "") return void 0;
84
- const labels = stripped.split(".").reverse();
85
- for (const label of labels) {
86
- if (label === "" || label === "*" || label.startsWith("!")) return void 0;
87
- }
88
- let node = root;
89
- let suffixDepth = 0;
90
- let known = false;
91
- for (let depth = 0; depth < labels.length; depth++) {
92
- const label = labels[depth].toLowerCase();
93
- if (hasChild(node, "*")) {
94
- if (hasChild(node, "!" + label)) {
95
- suffixDepth = depth;
96
- } else {
97
- suffixDepth = depth + 1;
98
- }
99
- known = true;
100
- }
101
- const child = childOf(node, label);
102
- if (child !== void 0) {
103
- if (child.suffixBoundary) {
104
- suffixDepth = depth + 1;
105
- known = true;
106
- }
107
- node = child;
108
- continue;
109
- }
110
- break;
111
- }
112
- if (suffixDepth === 0) {
113
- suffixDepth = 1;
114
- known = false;
115
- }
116
- const suffix = labels.slice(0, suffixDepth).reverse().map((l) => l.toLowerCase()).join(".");
117
- const registrableDomain = labels.length > suffixDepth ? `${labels[suffixDepth].toLowerCase()}.${suffix}` : void 0;
118
- return { suffix, registrableDomain, known };
119
- }
120
-
121
- export { lookupTrie, parseTrie };