diff --git a/index.js b/index.js index 814f862..eaeedb5 100644 --- a/index.js +++ b/index.js @@ -149,12 +149,19 @@ const whoisDomain = async (domain, {host = null, timeout = 15000, follow = 2} = const parseDomainWhois = whois => { - const shouldBeArray = ['Domain Status', 'Name Server', 'Nameserver', 'Nserver', 'nserver', 'Name servers']; + const renameLabels = { + 'nameserver': 'Name Server', + 'nserver': 'Name Server', + 'name servers': 'Name Server' + }; const ignoreLabels = ['note', 'notes', 'please note', 'important', 'notice', 'terms of use', 'web-based whois', 'https', 'to', 'registration service provider']; const ignoreTexts = ['more information', 'lawful purposes', 'to contact', 'use this data', 'register your domain', 'copy and paste', 'find out more', 'this', 'please', 'important', 'prices', 'payment', 'you agree', 'terms']; let text = []; - let data = {}; + let data = { + 'Domain Status': [], + 'Name Server': [] + }; let lines = whois.trim().split('\n').map(line => line.trim()); // Fix "label: \n value" format @@ -179,13 +186,14 @@ const parseDomainWhois = whois => { lines.forEach(line => { if ((line.includes(': ') || line.endsWith(':')) && !line.startsWith('%')) { - const [label, value] = splitStringBy(line, line.indexOf(':')).map(info => info.trim()) + let [label, value] = splitStringBy(line, line.indexOf(':')).map(info => info.trim()) - if (shouldBeArray.includes(label)) { - if (value) { - data[label] = data[label] || []; - data[label].push(value); - } + if (renameLabels[label.toLowerCase()]) { + label = renameLabels[label.toLowerCase()] + } + + if (data[label] && Array.isArray(data[label])) { + data[label].push(value); } else if (!ignoreLabels.includes(label.toLowerCase()) && !ignoreTexts.some(text => label.toLowerCase().includes(text))) { data[label] = data[label] ? data[label] + ' ' + value : value; } else { @@ -197,6 +205,8 @@ const parseDomainWhois = whois => { }); + // remove invalid Name Servers (not valid hostname) + data['Name Server'] = data['Name Server'].filter(nameServer => validator.isFQDN(nameServer)) // remove multiple empty lines text = text.join("\n").trim(); diff --git a/package-lock.json b/package-lock.json index 3efa420..2227bd9 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "whoiser", - "version": "1.2.0", + "version": "1.2.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 421ee34..19ba8ae 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "whoiser", - "version": "1.2.0", + "version": "1.2.1", "description": "Whois info for TLDs, domains and IPs", "main": "index.js", "scripts": { diff --git a/test/test.js b/test/test.js index 1c1129e..ae4a39b 100644 --- a/test/test.js +++ b/test/test.js @@ -100,7 +100,7 @@ describe('Whoiser', function() { it('should return WHOIS for "google.eu" when whois data is "label:_EOL_value"', async function() { let whois = await whoiser.domain('google.eu', {follow: 1}) assert.equal(whois['whois.eu']['Domain'], 'google.eu', 'Domain name doesn\'t match') - assert.notStrictEqual(whois['whois.eu']['Name servers'].length, 0, 'Does not return NS') + assert.notStrictEqual(whois['whois.eu']['Name Server'].length, 0, 'Does not return NS') }); it('should return WHOIS for "maƱana.com" - IDN', async function() {