forked from 7c/whoisserver-world
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
40 lines (37 loc) · 1012 Bytes
/
index.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
const path = require('path')
const fs = require('fs')
const tlds_folder = path.join(__dirname,'tlds')
const json_path = path.join(__dirname,'whoisservers.json')
/**
* returns object of TLD or false if tld is not known
* @param {string} tld
* @returns {} tld object|false
*/
function tldDetails(tld) {
if (typeof tld==='string') {
tld=tld.toLowerCase().trim().replace(/[^a-z0-9-]/,'')
var fn = path.join(tlds_folder,tld+'.json')
if (fs.existsSync(fn)) {
try {
var data = fs.readFileSync(fn,'ascii')
return JSON.parse(data)
}
catch(err) { }
}
}
return false
}
/**
* returns all TLDs as iteration JSON object or false in case of error
*/
function tlds() {
// reading as file will make sure that we read new file upon package renewal
try {
return JSON.parse(fs.readFileSync(json_path,'utf8'))
}catch(err) {}
return false
}
module.exports={
tldDetails,
tlds
}