|
1 | 1 | import * as TLDTS from 'tldts' |
| 2 | +import { PublicSuffixList } from './wildcard-suffix-converter.js' |
2 | 3 |
|
3 | | -export function DiscardResolvedDupWildcard(OriginSet: Set<string>): Set<string> { |
4 | | - // Step 1: Remove subdomains whose registered domain already exists in the set |
5 | | - const WithoutCoveredSubdomains = new Set<string>() |
6 | | - for (const Entry of OriginSet) { |
7 | | - const Parsed = TLDTS.parse(Entry) |
8 | | - if (Parsed.subdomain && Parsed.domain && OriginSet.has(Parsed.domain)) { |
9 | | - continue |
| 4 | +type ParsedEntry = { |
| 5 | + Entry: string |
| 6 | + RootLabel: string |
| 7 | + Stem: string |
| 8 | + PublicSuffix: string | null |
| 9 | + WildcardSuffix: boolean |
| 10 | +} |
| 11 | + |
| 12 | +function ParseEntry(Entry: string): ParsedEntry { |
| 13 | + if (Entry.endsWith('.*')) { |
| 14 | + const Stem = Entry.slice(0, -2) |
| 15 | + const RootLabel = Stem.split('.').at(-1) ?? Stem |
| 16 | + return { |
| 17 | + Entry, |
| 18 | + RootLabel, |
| 19 | + Stem, |
| 20 | + PublicSuffix: null, |
| 21 | + WildcardSuffix: true |
10 | 22 | } |
11 | | - WithoutCoveredSubdomains.add(Entry) |
12 | 23 | } |
13 | 24 |
|
14 | | - // Step 2: Group by domainWithoutSuffix |
15 | | - const Groups = new Map<string, string[]>() |
16 | | - for (const Entry of WithoutCoveredSubdomains) { |
17 | | - const Parsed = TLDTS.parse(Entry) |
18 | | - const Key = Parsed.domainWithoutSuffix ?? Entry |
19 | | - const Group = Groups.get(Key) |
20 | | - if (typeof Group === 'undefined') { |
21 | | - Groups.set(Key, [Entry]) |
22 | | - } else { |
23 | | - Group.push(Entry) |
| 25 | + const Parsed = TLDTS.parse(Entry) |
| 26 | + if (Parsed.publicSuffix) { |
| 27 | + PublicSuffixList.add(Parsed.publicSuffix) |
| 28 | + } |
| 29 | + |
| 30 | + if (Parsed.hostname && Parsed.publicSuffix && Parsed.hostname.endsWith(`.${Parsed.publicSuffix}`)) { |
| 31 | + return { |
| 32 | + Entry, |
| 33 | + RootLabel: Parsed.domainWithoutSuffix ?? Parsed.hostname, |
| 34 | + Stem: Parsed.hostname.slice(0, -(Parsed.publicSuffix.length + 1)), |
| 35 | + PublicSuffix: Parsed.publicSuffix, |
| 36 | + WildcardSuffix: false |
24 | 37 | } |
25 | 38 | } |
26 | 39 |
|
27 | | - // Step 3: Consolidate groups with 2+ top-level entries into wildcards |
| 40 | + const Stem = Parsed.hostname ?? Entry |
| 41 | + return { |
| 42 | + Entry, |
| 43 | + RootLabel: Stem.split('.').at(-1) ?? Stem, |
| 44 | + Stem, |
| 45 | + PublicSuffix: Parsed.publicSuffix, |
| 46 | + WildcardSuffix: false |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function IsCoveredByParent(Child: ParsedEntry, Parent: ParsedEntry): boolean { |
| 51 | + if (Child.Stem === Parent.Stem || !Child.Stem.endsWith(`.${Parent.Stem}`)) { |
| 52 | + return false |
| 53 | + } |
| 54 | + |
| 55 | + if (Parent.WildcardSuffix) { |
| 56 | + return true |
| 57 | + } |
| 58 | + |
| 59 | + return !Child.WildcardSuffix && Child.PublicSuffix === Parent.PublicSuffix |
| 60 | +} |
| 61 | + |
| 62 | +export function DiscardResolvedDupWildcard(OriginSet: Set<string>): Set<string> { |
| 63 | + const ParsedEntries = [...OriginSet].map(ParseEntry) |
| 64 | + const RootsWithWildcard = new Set( |
| 65 | + ParsedEntries |
| 66 | + .filter(Entry => Entry.WildcardSuffix) |
| 67 | + .map(Entry => Entry.RootLabel) |
| 68 | + ) |
| 69 | + |
| 70 | + const ConcreteStemCounts = new Map<string, number>() |
| 71 | + ParsedEntries |
| 72 | + .filter(Entry => !Entry.WildcardSuffix) |
| 73 | + .forEach(Entry => ConcreteStemCounts.set(Entry.Stem, (ConcreteStemCounts.get(Entry.Stem) ?? 0) + 1)) |
| 74 | + |
| 75 | + const NormalizedEntries = ParsedEntries.map(Entry => { |
| 76 | + if (Entry.WildcardSuffix) { |
| 77 | + return Entry |
| 78 | + } |
| 79 | + |
| 80 | + if (RootsWithWildcard.has(Entry.RootLabel) || (ConcreteStemCounts.get(Entry.Stem) ?? 0) >= 2) { |
| 81 | + return ParseEntry(`${Entry.Stem}.*`) |
| 82 | + } |
| 83 | + |
| 84 | + return Entry |
| 85 | + }) |
| 86 | + |
28 | 87 | const Result = new Set<string>() |
29 | | - for (const [Key, Entries] of Groups) { |
30 | | - if (Entries.length >= 2 && Entries.every(E => !TLDTS.parse(E).subdomain)) { |
31 | | - Result.add(`${Key}.*`) |
32 | | - } else { |
33 | | - for (const Entry of Entries) { |
34 | | - Result.add(Entry) |
35 | | - } |
| 88 | + const UniqueNormalizedEntries = [...new Map(NormalizedEntries.map(Entry => [Entry.Entry, Entry])).values()] |
| 89 | + for (const Entry of UniqueNormalizedEntries) { |
| 90 | + if (UniqueNormalizedEntries.some(Parent => Parent.Entry !== Entry.Entry && IsCoveredByParent(Entry, Parent))) { |
| 91 | + continue |
36 | 92 | } |
| 93 | + |
| 94 | + Result.add(Entry.Entry) |
37 | 95 | } |
38 | 96 |
|
39 | 97 | return Result |
|
0 commit comments