summaryrefslogtreecommitdiff
path: root/lib/dns.js
diff options
context:
space:
mode:
authorBen Noordhuis <info@bnoordhuis.nl>2013-08-12 17:54:11 +0200
committerBen Noordhuis <info@bnoordhuis.nl>2013-08-12 19:08:03 +0200
commit6cd7fd7d243fcf36b047b9c22a79927fb714d6cd (patch)
treefeb9407dfdcd2a897a8c0cca98d287e0d64a3e2c /lib/dns.js
parent4ffa943c3f25d801bb89744c94850484dc99231b (diff)
downloadnode-new-6cd7fd7d243fcf36b047b9c22a79927fb714d6cd.tar.gz
cares_wrap: don't set oncomplete property from c++
Don't set the oncomplete property in src/cares_wrap.cc, we can do it just as easily in lib/dns.js. Switch two closures to the 'function with _this_ object' model. Makes it impossible for an overzealous closure to capture too much context and accidentally hold on to too much memory.
Diffstat (limited to 'lib/dns.js')
-rw-r--r--lib/dns.js64
1 files changed, 33 insertions, 31 deletions
diff --git a/lib/dns.js b/lib/dns.js
index 8e6ed5c061..dd274825b2 100644
--- a/lib/dns.js
+++ b/lib/dns.js
@@ -78,6 +78,18 @@ function makeAsync(callback) {
}
+function onlookup(err, addresses) {
+ if (err) {
+ return this.callback(errnoException(err, 'getaddrinfo'));
+ }
+ if (this.family) {
+ this.callback(null, addresses[0], this.family);
+ } else {
+ this.callback(null, addresses[0], addresses[0].indexOf(':') >= 0 ? 6 : 4);
+ }
+}
+
+
// Easy DNS A/AAAA look up
// lookup(domain, [family,] callback)
exports.lookup = function(domain, family, callback) {
@@ -115,49 +127,39 @@ exports.lookup = function(domain, family, callback) {
return {};
}
- function onanswer(err, addresses) {
- if (err) {
- return callback(errnoException(err, 'getaddrinfo'));
- }
- if (family) {
- callback(null, addresses[0], family);
- } else {
- callback(null, addresses[0], addresses[0].indexOf(':') >= 0 ? 6 : 4);
- }
- }
-
- var req = {};
+ var req = {
+ callback: callback,
+ family: family,
+ oncomplete: onlookup
+ };
var err = cares.getaddrinfo(req, domain, family);
-
- if (err) {
- throw errnoException(err, 'getaddrinfo');
- }
-
- req.oncomplete = onanswer;
+ if (err) throw errnoException(err, 'getaddrinfo');
callback.immediately = true;
return req;
};
+function onresolve(err, result) {
+ if (err)
+ this.callback(errnoException(err, this.bindingName));
+ else
+ this.callback(null, result);
+}
+
+
function resolver(bindingName) {
var binding = cares[bindingName];
return function query(name, callback) {
- function onanswer(err, result) {
- if (err)
- callback(errnoException(err, bindingName));
- else
- callback(null, result);
- }
-
callback = makeAsync(callback);
- var req = {};
- var err = binding(req, name, onanswer);
- if (err) {
- throw errnoException(err, bindingName);
- }
-
+ var req = {
+ bindingName: bindingName,
+ callback: callback,
+ oncomplete: onresolve
+ };
+ var err = binding(req, name);
+ if (err) throw errnoException(err, bindingName);
callback.immediately = true;
return req;
}