Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON output standard format #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/whoiser.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ const whoisTld = async (query, { timeout = 15000, raw = false, domainTld = '' }
const whoisDomain = async (domain, { host = null, timeout = 15000, follow = 2, raw = false, ignorePrivacy = true } = {}) => {
domain = punycode.toASCII(domain)
const [domainName, domainTld] = splitStringBy(domain.toLowerCase(), domain.lastIndexOf('.'))
let results = {}
let results = []

// find WHOIS server in cache
if (!host && cacheTldWhoisServer[domainTld]) {
Expand All @@ -144,6 +144,8 @@ const whoisDomain = async (domain, { host = null, timeout = 15000, follow = 2, r
cacheTldWhoisServer[domainTld] = tld.whois
}

let index = 0
let serverChecked = [];
// query WHOIS servers for data
while (host && follow) {
let query = domain
Expand All @@ -168,7 +170,7 @@ const whoisDomain = async (domain, { host = null, timeout = 15000, follow = 2, r
result.__raw = resultRaw
}

results[host] = result
serverChecked[host] = true
follow--

// check for next WHOIS server
Expand Down Expand Up @@ -198,13 +200,31 @@ const whoisDomain = async (domain, { host = null, timeout = 15000, follow = 2, r
nextWhoisServer = misspelledWhoisServer[nextWhoisServer] || nextWhoisServer

// check if found server was queried already
nextWhoisServer = !results[nextWhoisServer] ? nextWhoisServer : false
nextWhoisServer = !serverChecked[nextWhoisServer] ? nextWhoisServer : false
}

results[index] = {
server: host,
data: Object.fromEntries(
Object.entries(result).map(([key, value]) =>
// Modify key here
[camelize(key), value]
)
)
}

host = nextWhoisServer
index++
}

return results
return JSON.stringify(results, null, 2)
}

const camelize = (str) => {
return str.toLowerCase().replace(/(?:^\w|[A-Z]|\b\w|\s+)/g, function(match, index) {
if (+match === 0) return ""; // or if (/\s+/.test(match)) for white spaces
return index === 0 ? match.toLowerCase() : match.toUpperCase();
});
}

const whoisIpOrAsn = async (query, { host = null, timeout = 15000, follow = 2, raw = false } = {}) => {
Expand Down