-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
Fixes #1164.
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,6 +91,25 @@ var channel = new dns.Channel({SOCK_STATE_CB: function(socket, read, write) { | |
updateTimer(); | ||
}}); | ||
|
||
// Make sure that the callback is invoked asynchronously. | ||
function makeAsync(callback) { | ||
if (typeof callback !== 'function') { | ||
return callback; | ||
} | ||
return function asyncCallback() { | ||
if (asyncCallback.immediately) { | ||
// The API already returned, we can invoke the callback immediately. | ||
callback.apply(null, arguments); | ||
} else { | ||
var args = arguments; | ||
process.nextTick(function() { | ||
callback.apply(null, args); | ||
}); | ||
} | ||
}; | ||
} | ||
|
||
|
||
exports.resolve = function(domain, type_, callback_) { | ||
var type, callback; | ||
if (typeof(type_) == 'string') { | ||
|
@@ -121,13 +140,17 @@ function familyToSym(family) { | |
|
||
exports.getHostByName = function(domain, family/*=4*/, callback) { | ||
if (typeof family === 'function') { callback = family; family = null; } | ||
callback = makeAsync(callback); | ||
channel.getHostByName(domain, familyToSym(family), callback); | ||
callback.immediately = true; | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
koichik
Author
|
||
}; | ||
|
||
|
||
exports.getHostByAddr = function(address, family/*=4*/, callback) { | ||
if (typeof family === 'function') { callback = family; family = null; } | ||
callback = makeAsync(callback); | ||
channel.getHostByAddr(address, familyToSym(family), callback); | ||
callback.immediately = true; | ||
}; | ||
|
||
|
||
|
@@ -148,6 +171,7 @@ exports.lookup = function(domain, family, callback) { | |
throw new Error('invalid argument: "family" must be 4 or 6'); | ||
} | ||
} | ||
callback = makeAsync(callback); | ||
|
||
if (!domain) { | ||
callback(null, null, family === 6 ? 6 : 4); | ||
|
@@ -166,6 +190,7 @@ exports.lookup = function(domain, family, callback) { | |
process.binding('net').getaddrinfo(domain, 4, function(err, domains4) { | ||
callback(err, domains4[0], 4); | ||
}); | ||
callback.immediately = true; | ||
return; | ||
} | ||
|
||
|
@@ -183,6 +208,7 @@ exports.lookup = function(domain, family, callback) { | |
callback(err, []); | ||
} | ||
}); | ||
callback.immediately = true; | ||
return; | ||
} | ||
|
||
|
@@ -200,46 +226,63 @@ exports.lookup = function(domain, family, callback) { | |
}); | ||
} | ||
}); | ||
callback.immediately = true; | ||
}; | ||
|
||
|
||
exports.resolve4 = function(domain, callback) { | ||
callback = makeAsync(callback); | ||
channel.query(domain, dns.A, callback); | ||
callback.immediately = true; | ||
}; | ||
|
||
|
||
exports.resolve6 = function(domain, callback) { | ||
callback = makeAsync(callback); | ||
channel.query(domain, dns.AAAA, callback); | ||
callback.immediately = true; | ||
}; | ||
|
||
|
||
exports.resolveMx = function(domain, callback) { | ||
callback = makeAsync(callback); | ||
channel.query(domain, dns.MX, callback); | ||
callback.immediately = true; | ||
}; | ||
|
||
|
||
exports.resolveTxt = function(domain, callback) { | ||
callback = makeAsync(callback); | ||
channel.query(domain, dns.TXT, callback); | ||
callback.immediately = true; | ||
}; | ||
|
||
|
||
exports.resolveSrv = function(domain, callback) { | ||
callback = makeAsync(callback); | ||
channel.query(domain, dns.SRV, callback); | ||
callback.immediately = true; | ||
}; | ||
|
||
|
||
exports.reverse = function(domain, callback) { | ||
callback = makeAsync(callback); | ||
channel.query(domain, dns.PTR, callback); | ||
callback.immediately = true; | ||
}; | ||
|
||
|
||
exports.resolveNs = function(domain, callback) { | ||
callback = makeAsync(callback); | ||
channel.query(domain, dns.NS, callback); | ||
callback.immediately = true; | ||
}; | ||
|
||
|
||
exports.resolveCname = function(domain, callback) { | ||
callback = makeAsync(callback); | ||
channel.query(domain, dns.CNAME, callback); | ||
callback.immediately = true; | ||
}; | ||
|
||
var resolveMap = { A: exports.resolve4, | ||
|
@@ -264,3 +307,4 @@ exports.DESTRUCTION = dns.DESTRUCTION; | |
exports.NOTIMP = dns.NOTIMP; | ||
exports.EREFUSED = dns.EREFUSED; | ||
exports.SERVFAIL = dns.SERVFAIL; | ||
|
Why is
callback.immediately = true
necessary and why that particular approach? Add a doc comment tomakeAsync
that explains the how and why.