Skip to content
This repository was archived by the owner on Mar 21, 2026. It is now read-only.

Commit fd4edd8

Browse files
committed
Request server may be string, Question type may be string
1 parent 178804e commit fd4edd8

File tree

4 files changed

+79
-7
lines changed

4 files changed

+79
-7
lines changed

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ var dns = require('../dns'),
2727

2828
var question = dns.Question({
2929
name: 'www.google.com',
30-
type: dns.consts.NAME_TO_QTYPE.A,
30+
type: 'A',
3131
});
3232

3333
var start = new Date().getTime();
@@ -59,11 +59,13 @@ req.send();
5959
Request creation takes an object with the following fields
6060

6161
* `question` -- an instance of Question (required)
62-
* `server` -- an object that defines the remote end point (required)
63-
- `address` -- a string ip address (required)
64-
- `port` -- a number for the remote port (required)
65-
- `type` -- a string indicating `udp` or `tcp` (required)
62+
* `server` -- defines the remote end point (required)
63+
- as an object it should be
64+
* `address` -- a string ip address (required)
65+
* `port` -- a number for the remote port (optional, default 53)
66+
* `type` -- a string indicating `udp` or `tcp` (optional, default `udp`)
6667
You do not need to indicate ipv4 or ipv6, the backend will handle that
68+
- a string ip address
6769
* `timeout` -- a number in milliseconds indicating how long to wait for the
6870
request to finish. (optional, default 4000)
6971
* `try_edns` -- a boolean indicating whether to use an `EDNSPacket` (optional)

dns.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,21 @@ exports.registerType = function(name, fields) {
7676
var Question = require('./lib/question');
7777

7878
exports.Question = function (opts) {
79-
var q = new Question();
79+
var q = new Question(), qtype;
80+
8081
q.name = opts.name;
81-
q.type = opts.type;
82+
83+
qtype = opts.type || consts.NAME_TO_QTYPE.A;
84+
if (typeof(qtype) === 'string' || qtype instanceof String)
85+
qtype = consts.nameToQtype(qtype.toUpperCase());
86+
87+
if (!qtype || typeof(qtype) !== 'number')
88+
throw new Error("Question type must be defined and be valid");
89+
90+
q.type = qtype;
91+
8292
q.class = opts.class || consts.NAME_TO_QCLASS.IN;
93+
8394
return q;
8495
};
8596
exports.Request = require('./lib/request');

lib/request.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,25 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2424

2525
var EventEmitter = require('events').EventEmitter,
2626
PendingRequests = require('./pending'),
27+
net = require('net'),
2728
util = require('util');
2829

2930
var Request = function (opts) {
3031
this.question = opts.question;
3132
this.server = opts.server;
33+
34+
if (typeof(this.server) === 'string' || this.server instanceof String)
35+
this.server = { address: this.server, port: 53, type: 'udp'};
36+
37+
if (!this.server || !this.server.address || !net.isIP(this.server.address))
38+
throw new Error("Server object must be supplied with at least address");
39+
40+
if (!this.server.type || ['udp', 'tcp'].indexOf(this.server.type))
41+
this.server.type = 'udp';
42+
43+
if (!this.server.port)
44+
this.server.port = 53;
45+
3246
this.timeout = opts.timeout || 4 * 1000;
3347
this.try_edns = opts.try_edns || false;
3448

test/request.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,51 @@ exports.noPromote = function (test) {
158158
r.send();
159159
};
160160

161+
exports.serverString = function (test) {
162+
var r = Request({
163+
question: q,
164+
server: '8.8.8.8',
165+
});
166+
167+
r.on('message', function (err, answer) {
168+
test.ok(answer.answer.length > 0, 'no answers found');
169+
});
170+
171+
r.on('timeout', function () {
172+
test.ok(false, 'Should not timeout');
173+
});
174+
175+
r.on('end', function () {
176+
test.done();
177+
});
178+
179+
r.send();
180+
};
181+
182+
exports.questionString = function (test) {
183+
var r = Request({
184+
question: Question({
185+
name: 'www.google.com',
186+
type: 'a',
187+
}),
188+
server: '8.8.8.8',
189+
});
190+
191+
r.on('message', function (err, answer) {
192+
test.ok(answer.answer.length > 0, 'no answers found');
193+
});
194+
195+
r.on('timeout', function () {
196+
test.ok(false, 'Should not timeout');
197+
});
198+
199+
r.on('end', function () {
200+
test.done();
201+
});
202+
203+
r.send();
204+
};
205+
161206
exports.tearDown = function (cb) {
162207
cb();
163208
};

0 commit comments

Comments
 (0)