From d3fce04a344f455d6d75bf4d59c5f6f9bfb70ea4 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Mon, 30 Jan 2017 16:30:08 -0700 Subject: [PATCH] refactor as part of #8 compat with native-dns-packet / dns-js --- bin/dns-parse.js | 19 ++------------ dns.js | 45 +++++++++++++++++++++++++++++++++ dns.packer.js | 10 ++++++++ pure-parser.js => dns.parser.js | 3 ++- package.json | 8 +++--- 5 files changed, 63 insertions(+), 22 deletions(-) create mode 100644 dns.js create mode 100644 dns.packer.js rename pure-parser.js => dns.parser.js (99%) diff --git a/bin/dns-parse.js b/bin/dns-parse.js index b15df91..298af41 100644 --- a/bin/dns-parse.js +++ b/bin/dns-parse.js @@ -14,7 +14,7 @@ if (!filename) { var PromiseA = require('bluebird'); var fs = PromiseA.promisifyAll(require('fs')); -var pdns = require('../'); +var dnsjs = require('../').DNSPacket; fs.readFileAsync(filename, null).then(function (nb) { // @@ -30,23 +30,8 @@ fs.readFileAsync(filename, null).then(function (nb) { // nb is a Uint8Array (ArrayBufferView) for nb.buffer // nb.buffer is the actual ArrayBuffer - var packet = pdns.unpack(nb.buffer); - function tryParseRdata(record) { - record.data = null; - record.error = null; - - try { - record.data = pdns.unpackRdata(nb.buffer, packet, record); - } catch (e) { - console.error('[Error] unpackRdata: ' + e.message); - record.error = e; - } - } - - packet.answers.forEach(tryParseRdata); - packet.authority.forEach(tryParseRdata); - packet.additional.forEach(tryParseRdata); + var packet = dnsjs.parse(nb.buffer); console.log('[packet]', nb.byteLength, 'bytes:'); console.log(JSON.stringify(packet, null, 2)); diff --git a/dns.js b/dns.js new file mode 100644 index 0000000..87553b7 --- /dev/null +++ b/dns.js @@ -0,0 +1,45 @@ +;(function (exports) { +'use strict'; + +// Value: IP Address +// Meaning:Use: 16 octets represting the IP address + +var Parser = (exports.DNS_PARSER || require('./dns.parser.js').DNS_PARSER); +var Packer = (exports.DNS_PACKER || require('./dns.packer.js').DNS_PACKER); + +exports.DNSPacket = { + parse: function (nb) { + // backwards compat with node buffer + var ab = nb.buffer || nb; + var packet = Parser.unpack(ab); + + function tryParseRdata(record) { + record.data = null; + record.error = null; + + try { + record.data = Parser.unpackRdata(ab, packet, record); + } catch (e) { + console.error('[Error] unpackRdata: ' + e.message); + record.error = e; + } + } + + packet.answers.forEach(tryParseRdata); + packet.authority.forEach(tryParseRdata); + packet.additional.forEach(tryParseRdata); + + return packet; + } +, write: function (json) { + return Packer.pack(json); + } +}; + +if ('undefined' !== typeof module) { + // backwards compat node dns.js + exports.Parser = exports.DNS_PARSER; + exports.Packer = exports.DNS_PACKER; +} + +}('undefined' !== typeof window ? window : exports)); diff --git a/dns.packer.js b/dns.packer.js new file mode 100644 index 0000000..17c8ef2 --- /dev/null +++ b/dns.packer.js @@ -0,0 +1,10 @@ +(function (exports) { +'use strict'; + +exports.DNS_PACKER = { + pack: function () { + throw new Error("DNS Packer Not Implemented"); + } +}; + +}('undefined' !== typeof window ? window : exports)); diff --git a/pure-parser.js b/dns.parser.js similarity index 99% rename from pure-parser.js rename to dns.parser.js index 992fde5..b9364ba 100644 --- a/pure-parser.js +++ b/dns.parser.js @@ -1,7 +1,7 @@ ;(function (exports) { 'use strict'; -var pdns = module.exports; +var pdns = module.exports.DNS_PARSER = {}; var classes = exports.DNS_CLASSES || require('./dns.classes.js').DNS_CLASSES; var types = exports.DNS_TYPES || require('./dns.types.js').DNS_TYPES; @@ -211,4 +211,5 @@ pdns.unpack = function (ab) { return header; }; pdns.unpackRdata = require('./dns.rdata.parse.js').DNS_RDATA_PARSE; + }('undefined' !== typeof window ? window : exports)); diff --git a/package.json b/package.json index 6e5441f..b6c495f 100644 --- a/package.json +++ b/package.json @@ -1,15 +1,15 @@ { - "name": "dns-testing", + "name": "dns-lint", "version": "1.0.0", "description": "testing dns", - "main": "pure-parser.js", + "main": "dns.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", - "url": "git@git.daplie.com:Daplie/DNS_test.git" + "url": "git@git.daplie.com:Daplie/dns-lint.git" }, "author": "", - "license": "ISC" + "license": "(MIT or Apache2)" }