email-disposable 1.0.1 → 1.0.3

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/src/fetch.js CHANGED
@@ -1,71 +1,117 @@
1
- import pLimit from "p-limit"
2
- import fs from "fs/promises"
3
-
4
- import { whitelist } from "./config/whitelist.js"
5
- import { blacklist } from "./config/blacklist.js"
6
- import { sources } from "./config/sources.js"
7
-
8
- const limit = pLimit(10)
9
- const promises = []
10
-
11
- const fetchList = async ({ url, parser }) => {
12
-
13
- let res
14
- let json
15
-
16
- const items = []
17
-
18
- try {
19
- res = await fetch(url)
20
- } catch(err) {
21
- console.error(err)
22
- return []
23
- }
24
-
25
- switch(parser) {
26
-
27
- case "text":
28
- const text = await res.text()
29
- const rows = text.split("\n").filter(line => line !== "")
30
- rows.forEach(row => {
31
- if(row.startsWith("#")) return true
32
- items.push(row.trim())
33
- })
34
- break
35
-
36
- case "json":
37
- json = await res.json()
38
- items.push(...json)
39
- break
40
-
41
- case "json-hosts":
42
- json = await res.json()
43
- const services = Object.keys(json)
44
- services.forEach(serviceId => {
45
- if(json[serviceId]?.hosts) {
46
- items.push(serviceId, ...json[serviceId].hosts)
47
- }
48
- })
49
- break
50
- }
51
-
52
- return items
53
-
54
- }
55
-
56
- for(let i = 0; i < sources.length; i++) {
57
- promises.push(limit(() => fetchList(sources[i])))
58
- }
59
-
60
- const results = await Promise.all(promises)
61
- const resultsSet = new Set(results.flat())
62
-
63
- resultsSet.add(...blacklist)
64
-
65
- whitelist.forEach(item => resultsSet.delete(item))
66
-
67
- const finalList = [...resultsSet]
68
-
69
- await fs.writeFile("./disposable.json", JSON.stringify(finalList, null, 4), "utf-8")
70
- await fs.writeFile("./disposable.txt", finalList.join("\n"), "utf-8")
71
- process.exit()
1
+ import pLimit from "p-limit"
2
+ import fs from "fs/promises"
3
+ import { domainToASCII } from "node:url"
4
+
5
+ import { whitelist } from "./config/whitelist.js"
6
+ import { blacklist } from "./config/blacklist.js"
7
+ import { sources } from "./config/sources.js"
8
+
9
+ const limit = pLimit(10)
10
+ const promises = []
11
+ const DOMAIN_PATTERN = /^(?:[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?\.)+[a-z0-9][a-z0-9-]{1,62}$/
12
+
13
+ const normalizeDomain = (value) => {
14
+ if(typeof value !== "string") return null
15
+
16
+ const candidate = value
17
+ .trim()
18
+ .toLowerCase()
19
+ .split(/\s+/)[0]
20
+ .replace(/^@+/, "")
21
+ .replace(/^\*\./, "")
22
+ .replace(/\.+$/g, "")
23
+
24
+ if(!candidate || candidate.startsWith("#") || candidate.startsWith("//")) return null
25
+
26
+ const ascii = domainToASCII(candidate)
27
+ const normalized = (ascii || candidate).toLowerCase()
28
+
29
+ return DOMAIN_PATTERN.test(normalized) ? normalized : null
30
+ }
31
+
32
+ const addNormalized = (items, value) => {
33
+ const normalized = normalizeDomain(value)
34
+ if(normalized) items.push(normalized)
35
+ }
36
+
37
+ const fetchList = async ({ url, parser }) => {
38
+
39
+ let res
40
+ let json
41
+
42
+ const items = []
43
+
44
+ try {
45
+ res = await fetch(url, {
46
+ signal: AbortSignal.timeout(30000)
47
+ })
48
+ } catch(err) {
49
+ console.warn(`[skip] ${url}: ${err.message}`)
50
+ return []
51
+ }
52
+
53
+ if(!res.ok) {
54
+ console.warn(`[skip] ${url}: HTTP ${res.status}`)
55
+ return []
56
+ }
57
+
58
+ try {
59
+ switch(parser) {
60
+
61
+ case "text":
62
+ const text = await res.text()
63
+ const rows = text.split("\n").filter(line => line !== "")
64
+ rows.forEach(row => {
65
+ addNormalized(items, row)
66
+ })
67
+ break
68
+
69
+ case "json":
70
+ json = await res.json()
71
+ if(Array.isArray(json)) {
72
+ json.forEach(item => addNormalized(items, item))
73
+ }
74
+ break
75
+
76
+ case "json-hosts":
77
+ json = await res.json()
78
+ const services = Object.keys(json || {})
79
+ services.forEach(serviceId => {
80
+ addNormalized(items, serviceId)
81
+ if(json[serviceId]?.hosts) {
82
+ json[serviceId].hosts.forEach(host => addNormalized(items, host))
83
+ }
84
+ })
85
+ break
86
+ }
87
+ } catch(err) {
88
+ console.warn(`[skip] ${url}: ${err.message}`)
89
+ return []
90
+ }
91
+
92
+ return items
93
+
94
+ }
95
+
96
+ for(let i = 0; i < sources.length; i++) {
97
+ promises.push(limit(() => fetchList(sources[i])))
98
+ }
99
+
100
+ const results = await Promise.all(promises)
101
+ const resultsSet = new Set(results.flat())
102
+
103
+ blacklist.forEach(item => {
104
+ const normalized = normalizeDomain(item)
105
+ if(normalized) resultsSet.add(normalized)
106
+ })
107
+
108
+ whitelist.forEach(item => {
109
+ const normalized = normalizeDomain(item)
110
+ if(normalized) resultsSet.delete(normalized)
111
+ })
112
+
113
+ const finalList = [...resultsSet].sort((left, right) => left.localeCompare(right))
114
+
115
+ await fs.writeFile("./disposable.json", JSON.stringify(finalList, null, 4), "utf-8")
116
+ await fs.writeFile("./disposable.txt", `${finalList.join("\n")}\n`, "utf-8")
117
+ process.exit()