Skip to content

Commit b2971b1

Browse files
committed
chore: add DiscardResolvedDupWildcard func
1 parent 2e26070 commit b2971b1

4 files changed

Lines changed: 84 additions & 2 deletions

File tree

builder/package.json

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,38 @@
55
"scripts": {
66
"lint": "tsc --noEmit && eslint **/*.ts",
77
"build": "tsx source/buildci.ts",
8-
"debug": "tsx source/debug.ts"
8+
"debug": "tsx source/debug.ts",
9+
"test": "npm run test:utils",
10+
"test:utils": "ava test/utils/**/*.test.ts"
911
},
1012
"dependencies": {
1113
"@types/node": "^24.12.2"
1214
},
15+
"ava": {
16+
"files": [
17+
"test/**/*.test.ts"
18+
],
19+
"nodeArguments": [
20+
"--import=tsx"
21+
],
22+
"workerThreads": false,
23+
"typescript": {
24+
"rewritePaths": {
25+
"@builder/": "./sources/"
26+
},
27+
"compile": false
28+
}
29+
},
1330
"devDependencies": {
1431
"@adguard/agtree": "^4.0.4",
32+
"@ava/typescript": "^7.0.0",
1533
"@npmcli/package-json": "^7.0.5",
1634
"@types/npmcli__package-json": "^4.0.4",
1735
"@typescript-eslint/eslint-plugin": "^8.58.2",
1836
"@typescript-eslint/parser": "^8.58.2",
1937
"@typescriptprime/parsing": "^2.0.1",
2038
"@typescriptprime/securereq": "^2.0.0",
39+
"ava": "^7.0.0",
2140
"chokidar": "^5.0.0",
2241
"esbuild": "^0.28.0",
2342
"eslint": "^10.2.1",
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as TLDTS from 'tldts'
2+
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
10+
}
11+
WithoutCoveredSubdomains.add(Entry)
12+
}
13+
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)
24+
}
25+
}
26+
27+
// Step 3: Consolidate groups with 2+ top-level entries into wildcards
28+
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+
}
36+
}
37+
}
38+
39+
return Result
40+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import Test from 'ava'
2+
import { DiscardResolvedDupWildcard } from '@builder/utils/discard-resolved-dup-wildcard.js'
3+
4+
Test('DiscardResolvedDupWildcard removes resolved duplicate wildcards', T => {
5+
const Input = new Set(['google.com', 'google.co.kr', 'google.org', 'example.com', 'example.org', 'duck.com'])
6+
const Expected = new Set(['google.*', 'example.*', 'duck.com'])
7+
8+
return T.deepEqual(DiscardResolvedDupWildcard(Input), Expected)
9+
})
10+
11+
Test('DiscardResolvedDupWildcard does not remove non-duplicate wildcards', T => {
12+
const Input = new Set(['google.com', 'chatgpt.com', 'claude.ai', 'gemini.google.com', 'duck.com'])
13+
const Expected = new Set(['google.com', 'chatgpt.com', 'claude.ai', 'duck.com'])
14+
15+
return T.deepEqual(DiscardResolvedDupWildcard(Input), Expected)
16+
})
17+
18+
Test('DiscardResolvedDupWildcard does not remove non-duplicate wildcards with multiple subdomains', T => {
19+
const Input = new Set(['access.chatgpt.com', 'info.chatgpt.com', 'access.claude.ai', 'info.claude.ai', 'access.huggingface.co', 'info.huggingface.co'])
20+
21+
return T.deepEqual(DiscardResolvedDupWildcard(Input), Input)
22+
})

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"build:userscript": "npm run build -w builder -- --minify true --build-type production --SubscriptionUrl https://cdn.jsdelivr.net/npm/@filteringdev/tinyshield@latest/dist/tinyShield.user.js",
88
"build": "npm run build:userscript",
99
"debug": "npm run debug -w builder",
10-
"lint": "npm run lint -w builder && npm run lint -w userscript"
10+
"lint": "npm run lint -w builder && npm run lint -w userscript",
11+
"test": "npm run test -w builder"
1112
},
1213
"keywords": [
1314
"Ad-Shield"

0 commit comments

Comments
 (0)