forked from LayeredStudio/whoiser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
45 lines (38 loc) · 1.14 KB
/
utils.js
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
const punycode = require('punycode/')
const https = require('https')
const splitStringBy = (string, by) => [string.slice(0, by), string.slice(by + 1)]
const requestGetBody = (url) => {
return new Promise((resolve, reject) => {
https
.get(url, (resp) => {
let data = ''
resp.on('data', (chunk) => (data += chunk))
resp.on('end', () => resolve(data))
resp.on('error', reject)
})
.on('error', reject)
})
}
const isTld = (tld) => {
if (tld.startsWith('.')) {
tld = tld.substring(1)
}
return /^([a-z]{2,64}|xn[a-z0-9-]{5,})$/i.test(punycode.toASCII(tld))
}
const isDomain = (domain) => {
if (domain.endsWith('.')) {
domain = domain.substring(0, domain.length - 1)
}
const labels = punycode.toASCII(domain).split('.').reverse()
const labelTest = /^([a-z0-9-]{1,64}|xn[a-z0-9-]{5,})$/i
return (
labels.length > 1 &&
labels.every((label, index) => {
return index ? labelTest.test(label) && !label.startsWith('-') && !label.endsWith('-') : isTld(label)
})
)
}
module.exports.splitStringBy = splitStringBy
module.exports.requestGetBody = requestGetBody
module.exports.isTld = isTld
module.exports.isDomain = isDomain