diff --git a/lib/dns.js b/lib/dns.js index be735f2cfe..6c9795384f 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -189,6 +189,9 @@ exports.lookupService = function(host, port, callback) { if (cares.isIP(host) === 0) throw new TypeError('"host" argument needs to be a valid IP address'); + if (typeof port !== 'number') + throw new TypeError(`"port" argument must be a number, got "${port}"`); + callback = makeAsync(callback); var req = new GetNameInfoReqWrap(); diff --git a/test/parallel/test-dns.js b/test/parallel/test-dns.js index 3362e8c534..84b74e022a 100644 --- a/test/parallel/test-dns.js +++ b/test/parallel/test-dns.js @@ -145,3 +145,19 @@ assert.doesNotThrow(function() { hints: dns.ADDRCONFIG | dns.V4MAPPED }, noop); }); + +assert.throws(function() { + dns.lookupService('0.0.0.0'); +}, /Invalid arguments/); + +assert.throws(function() { + dns.lookupService('fasdfdsaf', 0, noop); +}, /"host" argument needs to be a valid IP address/); + +assert.throws(function() { + dns.lookupService('0.0.0.0', '0', noop); +}, /"port" argument must be a number, got "0"/); + +assert.doesNotThrow(function() { + dns.lookupService('0.0.0.0', 0, noop); +});