-
Notifications
You must be signed in to change notification settings - Fork 0
Collapse file tree
Files
Search this repository(forward slash) forward slash/
/
Copy pathpublic-suffixes.js
More file actions
More file actions
Latest commit
398 lines (339 loc) · 7.76 KB
/
Copy pathpublic-suffixes.js
File metadata and controls
398 lines (339 loc) · 7.76 KB
You must be signed in to make or propose changes
More edit options
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
"use strict";
(() => {
const api = globalThis.browser ?? globalThis.chrome;
const PSL_FILE = "public_suffix_list.dat";
const MAX_PSL_BYTES = 4 * 1024 * 1024;
const MIN_RULE_COUNT = 1000;
let loadingPromise = null;
let exactRules = null;
let wildcardRules = null;
let exceptionRules = null;
let listVersion = "not-loaded";
let ruleCount = 0;
let loadedByteLength = 0;
function normalizeRule(value) {
return String(value)
.trim()
.toLowerCase()
.replace(/^\.+/, "")
.replace(/\.+$/, "");
}
function normalizeLabels(value) {
const input = Array.isArray(value)
? value
: String(value)
.trim()
.toLowerCase()
.replace(/\.$/, "")
.split(".");
return input
.map((label) =>
String(label)
.trim()
.toLowerCase()
)
.filter(Boolean);
}
function extractVersion(text) {
const firstLines = text
.split(/\r?\n/, 100);
for (const line of firstLines) {
const match = line.match(
/^\/\/\s*VERSION:\s*(.+?)\s*$/i
);
if (match) {
return match[1]
.trim()
.slice(0, 200);
}
}
return "bundled";
}
function parsePublicSuffixList(text) {
if (
typeof text !== "string" ||
text.length === 0
) {
throw new Error(
"The bundled Public Suffix List is empty."
);
}
if (
!text.includes(
"// ===BEGIN ICANN DOMAINS==="
)
) {
throw new Error(
"The ICANN section is missing from the Public Suffix List."
);
}
if (
!text.includes(
"// ===BEGIN PRIVATE DOMAINS==="
)
) {
throw new Error(
"The private-domain section is missing from the Public Suffix List."
);
}
const parsedExactRules = new Set();
const parsedWildcardRules = new Set();
const parsedExceptionRules = new Set();
for (
let line of text.split(/\r?\n/)
) {
line = line.trim();
if (
!line ||
line.startsWith("//")
) {
continue;
}
const inlineCommentIndex =
line.indexOf(" //");
if (inlineCommentIndex !== -1) {
line = line
.slice(0, inlineCommentIndex)
.trim();
}
if (!line) {
continue;
}
if (line.startsWith("!")) {
const rule = normalizeRule(
line.slice(1)
);
if (rule) {
parsedExceptionRules.add(rule);
}
continue;
}
if (line.startsWith("*.")) {
const rule = normalizeRule(
line.slice(2)
);
if (rule) {
parsedWildcardRules.add(rule);
}
continue;
}
)
) {
return Math.max(
1,
labels.length -
start -
1
);
}
if (
exactRules.has(candidate)
) {
bestRuleLength = Math.max(
bestRuleLength,
labels.length - start
);
}
if (
start + 1 <
labels.length
) {
const wildcardBase =
labels
.slice(start + 1)
.join(".");
if (
wildcardRules.has(
wildcardBase
)
) {
bestRuleLength = Math.max(
bestRuleLength,
labels.length - start
);
}
}
}
return Math.min(
bestRuleLength,
labels.length - 1
);
}
function getStatus() {
return Object.freeze({
loaded: Boolean(
exactRules &&
wildcardRules &&
exceptionRules
),
version: listVersion,
ruleCount,
exactRuleCount:
exactRules?.size ?? 0,
wildcardRuleCount:
wildcardRules?.size ?? 0,
exceptionRuleCount:
exceptionRules?.size ?? 0,
byteLength:
loadedByteLength
});
}
globalThis.PublicSuffixData =
Object.freeze({
load,
getPublicSuffixLabelCount,
getStatus
});
})();