Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
'dns' no longer uses Promise
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Feb 20, 2010
1 parent d0f2d46 commit c04b679
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 85 deletions.
72 changes: 36 additions & 36 deletions doc/api.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1590,85 +1590,84 @@ resolves the IP addresses which are returned.
var dns = require("dns"),
sys = require("sys");
var resolution = dns.resolve4("www.google.com");
dns.resolve4("www.google.com", function (err, addresses, ttl, cname) {
if (err) throw err;
resolution.addCallback(function (addresses, ttl, cname) {
sys.puts("addresses: " + JSON.stringify(addresses));
sys.puts("ttl: " + JSON.stringify(ttl));
sys.puts("cname: " + JSON.stringify(cname));
for (var i = 0; i < addresses.length; i++) {
var a = addresses[i];
var reversing = dns.reverse(a);
reversing.addCallback( function (domains, ttl, cname) {
sys.puts("reverse for " + a + ": " + JSON.stringify(domains));
});
reversing.addErrback( function (e) {
puts("reverse for " + a + " failed: " + e.message);
dns.reverse(a, function (err, domains, ttl, cname) {
if (err) {
puts("reverse for " + a + " failed: " + e.message);
} else {
sys.puts("reverse for " + a + ": " + JSON.stringify(domains));
}
});
}
});
resolution.addErrback(function (e) {
puts("error: " + e.message);
});
-------------------------------------------------------------------------

+dns.resolve(domain, rrtype = 'A')+::
+dns.resolve(domain, rrtype = 'A', callback)+::

Resolves a domain (e.g. +"google.com"+) into an array of the record types
specified by rrtype. Valid rrtypes are +A+ (IPV4 addresses), +AAAA+ (IPV6
addresses), +MX+ (mail exchange records), +TXT+ (text records), +SRV+
(SRV records), and +PTR+ (used for reverse IP lookups).
This function returns a promise.
- on success: returns +addresses, ttl, cname+. +ttl+ (time-to-live) is an integer
specifying the number of seconds this result is valid for. +cname+ is the
canonical name for the query.
The type of each item in +addresses+ is determined by the record type, and
described in the documentation for the corresponding lookup methods below.
- on error: Returns an instanceof Error object, where the "errno" field is one
of the error codes listed below and the "message" field is a string
describing the error in English.

+dns.resolve4(domain)+::
+
The callback has arguments +(err, addresses, ttl, cname)+. +ttl+
(time-to-live) is an integer specifying the number of seconds this result is
valid for. +cname+ is the canonical name for the query. The type of each
item in +addresses+ is determined by the record type, and
described in the documentation for the corresponding lookup methods below.
+
On error, +err+ would be an instanceof +Error+ object, where +err.errno+ is
one of the error codes listed below and +err.message+ is a string describing
the error in English.


+dns.resolve4(domain, callback)+::

The same as +dns.resolve()+, but only for IPv4 queries (+A+ records).
+addresses+ is an array of IPv4 addresses (e.g. +["74.125.79.104",
"74.125.79.105", "74.125.79.106"]+).

+dns.resolve6(domain)+::
+dns.resolve6(domain, callback)+::

The same as +dns.resolve4()+ except for IPv6 queries (an +AAAA+ query).

+dns.resolveMx(domain)+::

+dns.resolveMx(domain, callback)+::

The same as +dns.resolve()+, but only for mail exchange queries (+MX+ records).
+addresses+ is an array of MX records, each with a priority and an exchange
attribute (e.g. +[{"priority": 10, "exchange": "mx.example.com"},...]+).

+dns.resolveTxt(domain)+::
+dns.resolveTxt(domain, callback)+::

The same as +dns.resolve()+, but only for text queries (+TXT+ records).
+addresses+ is an array of the text records available for +domain+ (e.g.,
+["v=spf1 ip4:0.0.0.0 ~all"]+).

+dns.resolveSrv(domain)+::
+dns.resolveSrv(domain, callback)+::

The same as +dns.resolve()+, but only for service records (+SRV+ records).
+addresses+ is an array of the SRV records available for +domain+. Properties
of SRV records are priority, weight, port, and name (e.g., +[{"priority": 10,
{"weight": 5, "port": 21223, "name": "service.example.com"}, ...]+).

+dns.reverse(ip)+::
+dns.reverse(ip, callback)+::

Reverse resolves an ip address to an array of domain names.

- on success: returns +domains, ttl, cname+. +ttl+ (time-to-live) is an integer
specifying the number of seconds this result is valid for. +cname+ is the
canonical name for the query. +domains+ is an array of domains.
- on error: Returns an instanceof Error object, where the "errno" field is one
of the error codes listed below and the "message" field is a string
describing the error in English.
+
The callback has arguments +(err, domains, ttl, cname)+. +ttl+ (time-to-live) is an integer
specifying the number of seconds this result is valid for. +cname+ is the
canonical name for the query. +domains+ is an array of domains.
+
If there an an error, +err+ will be non-null and an instanceof the Error
object.


Each DNS query can return an error code.
Expand All @@ -1680,6 +1679,7 @@ Each DNS query can return an error code.
- +dns.NOMEM+: out of memory while processing.
- +dns.BADQUERY+: the query is malformed.


== Assert Module

This module is used for writing unit tests for your applications, you can access it with +require("assert")+.
Expand Down
66 changes: 17 additions & 49 deletions lib/dns.js
Original file line number Diff line number Diff line change
@@ -1,62 +1,30 @@
var events = require('events');

function callback (promise) {
return function () {
if (arguments[0] instanceof Error) {
promise.emitError.apply(promise, arguments);
} else {
promise.emitSuccess.apply(promise, arguments);
}
exports.resolve = function (domain, type_, callback_) {
var type, callback;
if (typeof(type_) == 'string') {
type = type_;
callback = callback_;
} else {
type = 'A';
callback = arguments[1];
}
}

exports.resolve = function (domain, type) {
type = (type || 'a').toUpperCase();

var resolveFunc = resolveMap[type];

if (typeof(resolveFunc) == 'function') {
return resolveFunc(domain);
resolveFunc(domain, callback);
} else {
return undefined;
throw new Error('Unknown type "' + type + '"');
}
}

exports.resolve4 = function (domain) {
var promise = new events.Promise();
process.dns.resolve4(domain, callback(promise));
return promise;
};

exports.resolve6 = function (domain) {
var promise = new events.Promise();
process.dns.resolve6(domain, callback(promise));
return promise;
};

exports.resolveMx = function (domain) {
var promise = new process.Promise();
process.dns.resolveMx(domain, callback(promise));
return promise;
};

exports.resolveTxt = function (domain) {
var promise = new process.Promise();
process.dns.resolveTxt(domain, callback(promise));
return promise;
};

exports.resolveSrv = function (domain) {
var promise = new process.Promise();
process.dns.resolveSrv(domain, callback(promise));
return promise;
}

exports.reverse = function (ip) {
var promise = new events.Promise();
process.dns.reverse(ip, callback(promise));
return promise;
};
exports.resolve4 = process.dns.resolve4;
exports.resolve6 = process.dns.resolve6;
exports.resolveMx = process.dns.resolveMx;
exports.resolveTxt = process.dns.resolveTxt;
exports.resolveSrv = process.dns.resolveSrv;
exports.reverse = process.dns.reverse;

// ERROR CODES

Expand All @@ -78,7 +46,7 @@ exports.NOMEM = process.dns.NOMEM;
// the query is malformed.
exports.BADQUERY = process.dns.BADQUERY;

resolveMap = {
var resolveMap = {
'A': exports.resolve4,
'AAAA': exports.resolve6,
'MX': exports.resolveMx,
Expand Down

0 comments on commit c04b679

Please sign in to comment.