tjfontaine / node-dns

Replacement dns module in pure javascript for node.js
MIT License
585 stars 154 forks source link

wercker status Build Status

native-dns -- A replacement DNS stack for node.js

DEAD. See forks!

This project is dead.

Please see this issue: https://github.com/tjfontaine/node-dns/issues/111

Some forks have been created:

Disclaimer: I, @taoeffect, have not revied the code in this fork, and am not responsible for any issues you may or may not run into. May it be a success!

(Below follows original README)

Installation

npm install native-dns

Client

native-dns exports what should be a 1:1 mapping of the upstream node.js dns module. That is to say if it's listed in the docs it should behave similarly. If it doesn't please file an issue.

Request

Beyond matching the upstream module, native-dns also provides a method for customizing queries.

var dns = require('native-dns');
var util = require('util');

var question = dns.Question({
  name: 'www.google.com',
  type: 'A',
});

var start = Date.now();

var req = dns.Request({
  question: question,
  server: { address: '8.8.8.8', port: 53, type: 'udp' },
  timeout: 1000,
});

req.on('timeout', function () {
  console.log('Timeout in making request');
});

req.on('message', function (err, answer) {
  answer.answer.forEach(function (a) {
    console.log(a.address);
  });
});

req.on('end', function () {
  var delta = (Date.now()) - start;
  console.log('Finished processing request: ' + delta.toString() + 'ms');
});

req.send();

Request creation takes an object with the following fields

There are only two methods

Request emits the following events

Platform

If you want to customize all resolve or lookups with the replacement client stack you can modify the platform settings accessible in the top level platform object.

Methods:

Properties:

Events:

Server

There is also a rudimentary server implementation

var dns = require('native-dns');
var server = dns.createServer();

server.on('request', function (request, response) {
  //console.log(request)
  response.answer.push(dns.A({
    name: request.question[0].name,
    address: '127.0.0.1',
    ttl: 600,
  }));
  response.answer.push(dns.A({
    name: request.question[0].name,
    address: '127.0.0.2',
    ttl: 600,
  }));
  response.additional.push(dns.A({
    name: 'hostA.example.org',
    address: '127.0.0.3',
    ttl: 600,
  }));
  response.send();
});

server.on('error', function (err, buff, req, res) {
  console.log(err.stack);
});

server.serve(15353);

Server creation

Server methods

Server events

Packet

Properties:

Methods:

Question

A Question is instantiated by passing an object like:

ResourceRecord

ResourceRecords are what populate answer, authority, and additional. This is a generic type, and each derived type inherits the following properties:

Available Types: