convert to ES2015, tweak package.json

This commit is contained in:
silverwind 2018-01-11 21:34:58 +01:00
parent d8f93e94fc
commit 6606c00fb3
No known key found for this signature in database
GPG Key ID: 2E62B41C93869443
8 changed files with 152 additions and 141 deletions

View File

@ -1,5 +1,11 @@
root: true root: true
parserOptions:
ecmaVersion: 2017
env:
node: true
rules: rules:
accessor-pairs: [2] accessor-pairs: [2]
array-bracket-newline: [0] array-bracket-newline: [0]
@ -182,7 +188,7 @@ rules:
no-useless-concat: [2] no-useless-concat: [2]
no-useless-constructor: [2] no-useless-constructor: [2]
no-useless-escape: [2] no-useless-escape: [2]
no-var: [0] no-var: [2]
no-warning-comments: [0] no-warning-comments: [0]
no-whitespace-before-property: [2] no-whitespace-before-property: [2]
no-with: [2] no-with: [2]
@ -221,7 +227,7 @@ rules:
space-infix-ops: [2] space-infix-ops: [2]
space-unary-ops: [2] space-unary-ops: [2]
spaced-comment: [2, always, {markers: ["!"]}] spaced-comment: [2, always, {markers: ["!"]}]
strict: [0] strict: [2]
switch-colon-spacing: [2] switch-colon-spacing: [2]
symbol-description: [2] symbol-description: [2]
template-curly-spacing: [2, never] template-curly-spacing: [2, never]
@ -233,6 +239,3 @@ rules:
wrap-regex: [0] wrap-regex: [0]
yield-star-spacing: [2, {before: true, after: false}] yield-star-spacing: [2, {before: true, after: false}]
yoda: [2, never] yoda: [2, never]
env:
node: true

View File

@ -1,9 +1,11 @@
var packet = require('./') 'use strict'
var dgram = require('dgram')
var socket = dgram.createSocket('udp4') const packet = require('./')
const dgram = require('dgram')
var buf = packet.encode({ const socket = dgram.createSocket('udp4')
const buf = packet.encode({
type: 'query', type: 'query',
id: 1, id: 1,
flags: packet.RECURSION_DESIRED, flags: packet.RECURSION_DESIRED,

176
index.js
View File

@ -1,30 +1,32 @@
var types = require('./types') 'use strict'
var rcodes = require('./rcodes')
var opcodes = require('./opcodes')
var ip = require('ip')
var Buffer = require('safe-buffer').Buffer
var QUERY_FLAG = 0 const types = require('./types')
var RESPONSE_FLAG = 1 << 15 const rcodes = require('./rcodes')
var FLUSH_MASK = 1 << 15 const opcodes = require('./opcodes')
var NOT_FLUSH_MASK = ~FLUSH_MASK const ip = require('ip')
var QU_MASK = 1 << 15 const Buffer = require('safe-buffer').Buffer
var NOT_QU_MASK = ~QU_MASK
var name = exports.txt = exports.name = {} const QUERY_FLAG = 0
const RESPONSE_FLAG = 1 << 15
const FLUSH_MASK = 1 << 15
const NOT_FLUSH_MASK = ~FLUSH_MASK
const QU_MASK = 1 << 15
const NOT_QU_MASK = ~QU_MASK
const name = exports.txt = exports.name = {}
name.encode = function (str, buf, offset) { name.encode = function (str, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(name.encodingLength(str)) if (!buf) buf = Buffer.allocUnsafe(name.encodingLength(str))
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset const oldOffset = offset
// strip leading and trailing . // strip leading and trailing .
var n = str.replace(/^\.|\.$/gm, '') const n = str.replace(/^\.|\.$/gm, '')
if (n.length) { if (n.length) {
var list = n.split('.') const list = n.split('.')
for (var i = 0; i < list.length; i++) { for (let i = 0; i < list.length; i++) {
var len = buf.write(list[i], offset + 1) const len = buf.write(list[i], offset + 1)
buf[offset] = len buf[offset] = len
offset += len + 1 offset += len + 1
} }
@ -41,16 +43,16 @@ name.encode.bytes = 0
name.decode = function (buf, offset) { name.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
var list = [] const list = []
var oldOffset = offset const oldOffset = offset
var len = buf[offset++] let len = buf[offset++]
if (len === 0) { if (len === 0) {
name.decode.bytes = 1 name.decode.bytes = 1
return '.' return '.'
} }
if (len >= 0xc0) { if (len >= 0xc0) {
var res = name.decode(buf, buf.readUInt16BE(offset - 1) - 0xc000) const res = name.decode(buf, buf.readUInt16BE(offset - 1) - 0xc000)
name.decode.bytes = 2 name.decode.bytes = 2
return res return res
} }
@ -77,13 +79,13 @@ name.encodingLength = function (n) {
return Buffer.byteLength(n) + 2 return Buffer.byteLength(n) + 2
} }
var string = {} const string = {}
string.encode = function (s, buf, offset) { string.encode = function (s, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(string.encodingLength(s)) if (!buf) buf = Buffer.allocUnsafe(string.encodingLength(s))
if (!offset) offset = 0 if (!offset) offset = 0
var len = buf.write(s, offset + 1) const len = buf.write(s, offset + 1)
buf[offset] = len buf[offset] = len
string.encode.bytes = len + 1 string.encode.bytes = len + 1
return buf return buf
@ -94,8 +96,8 @@ string.encode.bytes = 0
string.decode = function (buf, offset) { string.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
var len = buf[offset] const len = buf[offset]
var s = buf.toString('utf-8', offset + 1, offset + 1 + len) const s = buf.toString('utf-8', offset + 1, offset + 1 + len)
string.decode.bytes = len + 1 string.decode.bytes = len + 1
return s return s
} }
@ -106,14 +108,14 @@ string.encodingLength = function (s) {
return Buffer.byteLength(s) + 1 return Buffer.byteLength(s) + 1
} }
var header = {} const header = {}
header.encode = function (h, buf, offset) { header.encode = function (h, buf, offset) {
if (!buf) buf = header.encodingLength(h) if (!buf) buf = header.encodingLength(h)
if (!offset) offset = 0 if (!offset) offset = 0
var flags = (h.flags || 0) & 32767 const flags = (h.flags || 0) & 32767
var type = h.type === 'response' ? RESPONSE_FLAG : QUERY_FLAG const type = h.type === 'response' ? RESPONSE_FLAG : QUERY_FLAG
buf.writeUInt16BE(h.id || 0, offset) buf.writeUInt16BE(h.id || 0, offset)
buf.writeUInt16BE(flags | type, offset + 2) buf.writeUInt16BE(flags | type, offset + 2)
@ -130,7 +132,7 @@ header.encode.bytes = 12
header.decode = function (buf, offset) { header.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
if (buf.length < 12) throw new Error('Header must be 12 bytes') if (buf.length < 12) throw new Error('Header must be 12 bytes')
var flags = buf.readUInt16BE(offset + 2) const flags = buf.readUInt16BE(offset + 2)
return { return {
id: buf.readUInt16BE(offset), id: buf.readUInt16BE(offset),
@ -159,7 +161,7 @@ header.encodingLength = function () {
return 12 return 12
} }
var runknown = exports.unknown = {} const runknown = exports.unknown = {}
runknown.encode = function (data, buf, offset) { runknown.encode = function (data, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(runknown.encodingLength(data)) if (!buf) buf = Buffer.allocUnsafe(runknown.encodingLength(data))
@ -177,8 +179,8 @@ runknown.encode.bytes = 0
runknown.decode = function (buf, offset) { runknown.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
var len = buf.readUInt16BE(offset) const len = buf.readUInt16BE(offset)
var data = buf.slice(offset + 2, offset + 2 + len) const data = buf.slice(offset + 2, offset + 2 + len)
runknown.decode.bytes = len + 2 runknown.decode.bytes = len + 2
return data return data
} }
@ -189,7 +191,7 @@ runknown.encodingLength = function (data) {
return data.length + 2 return data.length + 2
} }
var rns = exports.ns = {} const rns = exports.ns = {}
rns.encode = function (data, buf, offset) { rns.encode = function (data, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(rns.encodingLength(data)) if (!buf) buf = Buffer.allocUnsafe(rns.encodingLength(data))
@ -206,8 +208,8 @@ rns.encode.bytes = 0
rns.decode = function (buf, offset) { rns.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
var len = buf.readUInt16BE(offset) const len = buf.readUInt16BE(offset)
var dd = name.decode(buf, offset + 2) const dd = name.decode(buf, offset + 2)
rns.decode.bytes = len + 2 rns.decode.bytes = len + 2
return dd return dd
@ -219,13 +221,13 @@ rns.encodingLength = function (data) {
return name.encodingLength(data) + 2 return name.encodingLength(data) + 2
} }
var rsoa = exports.soa = {} const rsoa = exports.soa = {}
rsoa.encode = function (data, buf, offset) { rsoa.encode = function (data, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(rsoa.encodingLength(data)) if (!buf) buf = Buffer.allocUnsafe(rsoa.encodingLength(data))
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset const oldOffset = offset
offset += 2 offset += 2
name.encode(data.mname, buf, offset) name.encode(data.mname, buf, offset)
offset += name.encode.bytes offset += name.encode.bytes
@ -252,9 +254,9 @@ rsoa.encode.bytes = 0
rsoa.decode = function (buf, offset) { rsoa.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset const oldOffset = offset
var data = {} const data = {}
offset += 2 offset += 2
data.mname = name.decode(buf, offset) data.mname = name.decode(buf, offset)
offset += name.decode.bytes offset += name.decode.bytes
@ -281,8 +283,8 @@ rsoa.encodingLength = function (data) {
return 22 + name.encodingLength(data.mname) + name.encodingLength(data.rname) return 22 + name.encodingLength(data.mname) + name.encodingLength(data.rname)
} }
var rtxt = exports.txt = exports.null = {} const rtxt = exports.txt = exports.null = {}
var rnull = rtxt const rnull = rtxt
rtxt.encode = function (data, buf, offset) { rtxt.encode = function (data, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(rtxt.encodingLength(data)) if (!buf) buf = Buffer.allocUnsafe(rtxt.encodingLength(data))
@ -291,10 +293,10 @@ rtxt.encode = function (data, buf, offset) {
if (typeof data === 'string') data = Buffer.from(data) if (typeof data === 'string') data = Buffer.from(data)
if (!data) data = Buffer.allocUnsafe(0) if (!data) data = Buffer.allocUnsafe(0)
var oldOffset = offset const oldOffset = offset
offset += 2 offset += 2
var len = data.length const len = data.length
data.copy(buf, offset, 0, len) data.copy(buf, offset, 0, len)
offset += len offset += len
@ -307,12 +309,12 @@ rtxt.encode.bytes = 0
rtxt.decode = function (buf, offset) { rtxt.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset const oldOffset = offset
var len = buf.readUInt16BE(offset) const len = buf.readUInt16BE(offset)
offset += 2 offset += 2
var data = buf.slice(offset, offset + len) const data = buf.slice(offset, offset + len)
offset += len offset += len
rtxt.decode.bytes = offset - oldOffset rtxt.decode.bytes = offset - oldOffset
@ -326,13 +328,13 @@ rtxt.encodingLength = function (data) {
return (Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data)) + 2 return (Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data)) + 2
} }
var rhinfo = exports.hinfo = {} const rhinfo = exports.hinfo = {}
rhinfo.encode = function (data, buf, offset) { rhinfo.encode = function (data, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(rhinfo.encodingLength(data)) if (!buf) buf = Buffer.allocUnsafe(rhinfo.encodingLength(data))
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset const oldOffset = offset
offset += 2 offset += 2
string.encode(data.cpu, buf, offset) string.encode(data.cpu, buf, offset)
offset += string.encode.bytes offset += string.encode.bytes
@ -348,9 +350,9 @@ rhinfo.encode.bytes = 0
rhinfo.decode = function (buf, offset) { rhinfo.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset const oldOffset = offset
var data = {} const data = {}
offset += 2 offset += 2
data.cpu = string.decode(buf, offset) data.cpu = string.decode(buf, offset)
offset += string.decode.bytes offset += string.decode.bytes
@ -366,9 +368,9 @@ rhinfo.encodingLength = function (data) {
return string.encodingLength(data.cpu) + string.encodingLength(data.os) + 2 return string.encodingLength(data.cpu) + string.encodingLength(data.os) + 2
} }
var rptr = exports.ptr = {} const rptr = exports.ptr = {}
var rcname = exports.cname = rptr const rcname = exports.cname = rptr
var rdname = exports.dname = rptr const rdname = exports.dname = rptr
rptr.encode = function (data, buf, offset) { rptr.encode = function (data, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(rptr.encodingLength(data)) if (!buf) buf = Buffer.allocUnsafe(rptr.encodingLength(data))
@ -385,7 +387,7 @@ rptr.encode.bytes = 0
rptr.decode = function (buf, offset) { rptr.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
var data = name.decode(buf, offset + 2) const data = name.decode(buf, offset + 2)
rptr.decode.bytes = name.decode.bytes + 2 rptr.decode.bytes = name.decode.bytes + 2
return data return data
} }
@ -396,7 +398,7 @@ rptr.encodingLength = function (data) {
return name.encodingLength(data) + 2 return name.encodingLength(data) + 2
} }
var rsrv = exports.srv = {} const rsrv = exports.srv = {}
rsrv.encode = function (data, buf, offset) { rsrv.encode = function (data, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(rsrv.encodingLength(data)) if (!buf) buf = Buffer.allocUnsafe(rsrv.encodingLength(data))
@ -407,7 +409,7 @@ rsrv.encode = function (data, buf, offset) {
buf.writeUInt16BE(data.port || 0, offset + 6) buf.writeUInt16BE(data.port || 0, offset + 6)
name.encode(data.target, buf, offset + 8) name.encode(data.target, buf, offset + 8)
var len = name.encode.bytes + 6 const len = name.encode.bytes + 6
buf.writeUInt16BE(len, offset) buf.writeUInt16BE(len, offset)
rsrv.encode.bytes = len + 2 rsrv.encode.bytes = len + 2
@ -419,9 +421,9 @@ rsrv.encode.bytes = 0
rsrv.decode = function (buf, offset) { rsrv.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
var len = buf.readUInt16BE(offset) const len = buf.readUInt16BE(offset)
var data = {} const data = {}
data.priority = buf.readUInt16BE(offset + 2) data.priority = buf.readUInt16BE(offset + 2)
data.weight = buf.readUInt16BE(offset + 4) data.weight = buf.readUInt16BE(offset + 4)
data.port = buf.readUInt16BE(offset + 6) data.port = buf.readUInt16BE(offset + 6)
@ -437,12 +439,12 @@ rsrv.encodingLength = function (data) {
return 8 + name.encodingLength(data.target) return 8 + name.encodingLength(data.target)
} }
var rcaa = exports.caa = {} const rcaa = exports.caa = {}
rcaa.ISSUER_CRITICAL = 1 << 7 rcaa.ISSUER_CRITICAL = 1 << 7
rcaa.encode = function (data, buf, offset) { rcaa.encode = function (data, buf, offset) {
var len = rcaa.encodingLength(data) const len = rcaa.encodingLength(data)
if (!buf) buf = Buffer.allocUnsafe(rcaa.encodingLength(data)) if (!buf) buf = Buffer.allocUnsafe(rcaa.encodingLength(data))
if (!offset) offset = 0 if (!offset) offset = 0
@ -469,11 +471,11 @@ rcaa.encode.bytes = 0
rcaa.decode = function (buf, offset) { rcaa.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
var len = buf.readUInt16BE(offset) const len = buf.readUInt16BE(offset)
offset += 2 offset += 2
var oldOffset = offset const oldOffset = offset
var data = {} const data = {}
data.flags = buf.readUInt8(offset) data.flags = buf.readUInt8(offset)
offset += 1 offset += 1
data.tag = string.decode(buf, offset) data.tag = string.decode(buf, offset)
@ -493,7 +495,7 @@ rcaa.encodingLength = function (data) {
return string.encodingLength(data.tag) + string.encodingLength(data.value) + 2 return string.encodingLength(data.tag) + string.encodingLength(data.value) + 2
} }
var ra = exports.a = {} const ra = exports.a = {}
ra.encode = function (host, buf, offset) { ra.encode = function (host, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(ra.encodingLength(host)) if (!buf) buf = Buffer.allocUnsafe(ra.encodingLength(host))
@ -512,7 +514,7 @@ ra.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
offset += 2 offset += 2
var host = ip.toString(buf, offset, 4) const host = ip.toString(buf, offset, 4)
ra.decode.bytes = 6 ra.decode.bytes = 6
return host return host
} }
@ -523,7 +525,7 @@ ra.encodingLength = function () {
return 6 return 6
} }
var raaaa = exports.aaaa = {} const raaaa = exports.aaaa = {}
raaaa.encode = function (host, buf, offset) { raaaa.encode = function (host, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(raaaa.encodingLength(host)) if (!buf) buf = Buffer.allocUnsafe(raaaa.encodingLength(host))
@ -542,7 +544,7 @@ raaaa.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
offset += 2 offset += 2
var host = ip.toString(buf, offset, 16) const host = ip.toString(buf, offset, 16)
raaaa.decode.bytes = 18 raaaa.decode.bytes = 18
return host return host
} }
@ -553,7 +555,7 @@ raaaa.encodingLength = function () {
return 18 return 18
} }
var renc = exports.record = function (type) { const renc = exports.record = function (type) {
switch (type.toUpperCase()) { switch (type.toUpperCase()) {
case 'A': return ra case 'A': return ra
case 'PTR': return rptr case 'PTR': return rptr
@ -571,26 +573,26 @@ var renc = exports.record = function (type) {
return runknown return runknown
} }
var answer = exports.answer = {} const answer = exports.answer = {}
answer.encode = function (a, buf, offset) { answer.encode = function (a, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(answer.encodingLength(a)) if (!buf) buf = Buffer.allocUnsafe(answer.encodingLength(a))
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset const oldOffset = offset
name.encode(a.name, buf, offset) name.encode(a.name, buf, offset)
offset += name.encode.bytes offset += name.encode.bytes
buf.writeUInt16BE(types.toType(a.type), offset) buf.writeUInt16BE(types.toType(a.type), offset)
var klass = a.class === undefined ? 1 : a.class let klass = a.class === undefined ? 1 : a.class
if (a.flush) klass |= FLUSH_MASK // the 1st bit of the class is the flush bit if (a.flush) klass |= FLUSH_MASK // the 1st bit of the class is the flush bit
buf.writeUInt16BE(klass, offset + 2) buf.writeUInt16BE(klass, offset + 2)
buf.writeUInt32BE(a.ttl || 0, offset + 4) buf.writeUInt32BE(a.ttl || 0, offset + 4)
var enc = renc(a.type) const enc = renc(a.type)
enc.encode(a.data, buf, offset + 8) enc.encode(a.data, buf, offset + 8)
offset += 8 + enc.encode.bytes offset += 8 + enc.encode.bytes
@ -603,8 +605,8 @@ answer.encode.bytes = 0
answer.decode = function (buf, offset) { answer.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
var a = {} const a = {}
var oldOffset = offset const oldOffset = offset
a.name = name.decode(buf, offset) a.name = name.decode(buf, offset)
offset += name.decode.bytes offset += name.decode.bytes
@ -615,7 +617,7 @@ answer.decode = function (buf, offset) {
a.flush = !!(a.class & FLUSH_MASK) a.flush = !!(a.class & FLUSH_MASK)
if (a.flush) a.class &= NOT_FLUSH_MASK if (a.flush) a.class &= NOT_FLUSH_MASK
var enc = renc(a.type) const enc = renc(a.type)
a.data = enc.decode(buf, offset + 8) a.data = enc.decode(buf, offset + 8)
offset += 8 + enc.decode.bytes offset += 8 + enc.decode.bytes
@ -629,13 +631,13 @@ answer.encodingLength = function (a) {
return name.encodingLength(a.name) + 8 + renc(a.type).encodingLength(a.data) return name.encodingLength(a.name) + 8 + renc(a.type).encodingLength(a.data)
} }
var question = exports.question = {} const question = exports.question = {}
question.encode = function (q, buf, offset) { question.encode = function (q, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(question.encodingLength(q)) if (!buf) buf = Buffer.allocUnsafe(question.encodingLength(q))
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset const oldOffset = offset
name.encode(q.name, buf, offset) name.encode(q.name, buf, offset)
offset += name.encode.bytes offset += name.encode.bytes
@ -655,8 +657,8 @@ question.encode.bytes = 0
question.decode = function (buf, offset) { question.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset const oldOffset = offset
var q = {} const q = {}
q.name = name.decode(buf, offset) q.name = name.decode(buf, offset)
offset += name.decode.bytes offset += name.decode.bytes
@ -667,7 +669,7 @@ question.decode = function (buf, offset) {
q.class = buf.readUInt16BE(offset) q.class = buf.readUInt16BE(offset)
offset += 2 offset += 2
var qu = !!(q.class & QU_MASK) const qu = !!(q.class & QU_MASK)
if (qu) q.class &= NOT_QU_MASK if (qu) q.class &= NOT_QU_MASK
question.decode.bytes = offset - oldOffset question.decode.bytes = offset - oldOffset
@ -691,7 +693,7 @@ exports.encode = function (result, buf, offset) {
if (!buf) buf = Buffer.allocUnsafe(exports.encodingLength(result)) if (!buf) buf = Buffer.allocUnsafe(exports.encodingLength(result))
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset const oldOffset = offset
if (!result.questions) result.questions = [] if (!result.questions) result.questions = []
if (!result.answers) result.answers = [] if (!result.answers) result.answers = []
@ -716,8 +718,8 @@ exports.encode.bytes = 0
exports.decode = function (buf, offset) { exports.decode = function (buf, offset) {
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset const oldOffset = offset
var result = header.decode(buf, offset) const result = header.decode(buf, offset)
offset += header.decode.bytes offset += header.decode.bytes
offset = decodeList(result.questions, question, buf, offset) offset = decodeList(result.questions, question, buf, offset)
@ -741,13 +743,13 @@ exports.encodingLength = function (result) {
} }
function encodingLengthList (list, enc) { function encodingLengthList (list, enc) {
var len = 0 let len = 0
for (var i = 0; i < list.length; i++) len += enc.encodingLength(list[i]) for (let i = 0; i < list.length; i++) len += enc.encodingLength(list[i])
return len return len
} }
function encodeList (list, enc, buf, offset) { function encodeList (list, enc, buf, offset) {
for (var i = 0; i < list.length; i++) { for (let i = 0; i < list.length; i++) {
enc.encode(list[i], buf, offset) enc.encode(list[i], buf, offset)
offset += enc.encode.bytes offset += enc.encode.bytes
} }
@ -755,7 +757,7 @@ function encodeList (list, enc, buf, offset) {
} }
function decodeList (list, enc, buf, offset) { function decodeList (list, enc, buf, offset) {
for (var i = 0; i < list.length; i++) { for (let i = 0; i < list.length; i++) {
list[i] = enc.decode(buf, offset) list[i] = enc.decode(buf, offset)
offset += enc.decode.bytes offset += enc.decode.bytes
} }

View File

@ -1,3 +1,5 @@
'use strict'
/* /*
* Traditional DNS header OPCODEs (4-bits) defined by IANA in * Traditional DNS header OPCODEs (4-bits) defined by IANA in
* https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-5 * https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-5

View File

@ -2,27 +2,25 @@
"name": "dns-packet", "name": "dns-packet",
"version": "1.3.1", "version": "1.3.1",
"description": "An abstract-encoding compliant module for encoding / decoding DNS packets", "description": "An abstract-encoding compliant module for encoding / decoding DNS packets",
"repository": { "author": "Mathias Buus",
"type": "git", "license": "MIT",
"url": "https://github.com/mafintosh/dns-packet" "repository": "mafintosh/dns-packet",
}, "homepage": "https://github.com/mafintosh/dns-packet",
"dependencies": { "engines": {
"ip": "^1.1.0", "node": ">=4"
"safe-buffer": "^5.0.1"
},
"devDependencies": {
"eslint": "^4.15.0",
"standard": "^6.0.5",
"tape": "^4.4.0"
}, },
"scripts": { "scripts": {
"test": "standard && eslint --color *.js && tape test.js" "test": "standard && eslint --color *.js && tape test.js"
}, },
"bugs": { "dependencies": {
"url": "https://github.com/mafintosh/dns-packet/issues" "ip": "^1.1.5",
"safe-buffer": "^5.1.1"
},
"devDependencies": {
"eslint": "^4.15.0",
"standard": "^10.0.3",
"tape": "^4.8.0"
}, },
"homepage": "https://github.com/mafintosh/dns-packet",
"main": "index.js",
"keywords": [ "keywords": [
"dns", "dns",
"packet", "packet",
@ -36,7 +34,5 @@
"types.js", "types.js",
"rcodes.js", "rcodes.js",
"opcodes.js" "opcodes.js"
], ]
"author": "Mathias Buus",
"license": "MIT"
} }

View File

@ -1,3 +1,5 @@
'use strict'
/* /*
* Traditional DNS header RCODEs (4-bits) defined by IANA in * Traditional DNS header RCODEs (4-bits) defined by IANA in
* https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml * https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml

54
test.js
View File

@ -1,8 +1,10 @@
var tape = require('tape') 'use strict'
var packet = require('./')
var rcodes = require('./rcodes') const tape = require('tape')
var opcodes = require('./opcodes') const packet = require('./')
var Buffer = require('safe-buffer').Buffer const rcodes = require('./rcodes')
const opcodes = require('./opcodes')
const Buffer = require('safe-buffer').Buffer
tape('unknown', function (t) { tape('unknown', function (t) {
testEncoder(t, packet.unknown, Buffer.from('hello world')) testEncoder(t, packet.unknown, Buffer.from('hello world'))
@ -176,19 +178,19 @@ tape('response', function (t) {
}) })
tape('rcode', function (t) { tape('rcode', function (t) {
var errors = ['NOERROR', 'FORMERR', 'SERVFAIL', 'NXDOMAIN', 'NOTIMP', 'REFUSED', 'YXDOMAIN', 'YXRRSET', 'NXRRSET', 'NOTAUTH', 'NOTZONE', 'RCODE_11', 'RCODE_12', 'RCODE_13', 'RCODE_14', 'RCODE_15'] const errors = ['NOERROR', 'FORMERR', 'SERVFAIL', 'NXDOMAIN', 'NOTIMP', 'REFUSED', 'YXDOMAIN', 'YXRRSET', 'NXRRSET', 'NOTAUTH', 'NOTZONE', 'RCODE_11', 'RCODE_12', 'RCODE_13', 'RCODE_14', 'RCODE_15']
for (var i in errors) { for (const i in errors) {
var code = rcodes.toRcode(errors[i]) const code = rcodes.toRcode(errors[i])
t.ok(errors[i] === rcodes.toString(code), 'rcode conversion from/to string matches: ' + rcodes.toString(code)) t.ok(errors[i] === rcodes.toString(code), 'rcode conversion from/to string matches: ' + rcodes.toString(code))
} }
var ops = ['QUERY', 'IQUERY', 'STATUS', 'OPCODE_3', 'NOTIFY', 'UPDATE', 'OPCODE_6', 'OPCODE_7', 'OPCODE_8', 'OPCODE_9', 'OPCODE_10', 'OPCODE_11', 'OPCODE_12', 'OPCODE_13', 'OPCODE_14', 'OPCODE_15'] const ops = ['QUERY', 'IQUERY', 'STATUS', 'OPCODE_3', 'NOTIFY', 'UPDATE', 'OPCODE_6', 'OPCODE_7', 'OPCODE_8', 'OPCODE_9', 'OPCODE_10', 'OPCODE_11', 'OPCODE_12', 'OPCODE_13', 'OPCODE_14', 'OPCODE_15']
for (var j in ops) { for (const j in ops) {
var ocode = opcodes.toOpcode(ops[j]) const ocode = opcodes.toOpcode(ops[j])
t.ok(ops[j] === opcodes.toString(ocode), 'opcode conversion from/to string matches: ' + opcodes.toString(ocode)) t.ok(ops[j] === opcodes.toString(ocode), 'opcode conversion from/to string matches: ' + opcodes.toString(ocode))
} }
var buf = packet.encode({ const buf = packet.encode({
type: 'response', type: 'response',
id: 45632, id: 45632,
flags: 0x8480, flags: 0x8480,
@ -198,7 +200,7 @@ tape('rcode', function (t) {
data: '127.0.0.1' data: '127.0.0.1'
}] }]
}) })
var val = packet.decode(buf) const val = packet.decode(buf)
t.ok(val.type === 'response', 'decode type') t.ok(val.type === 'response', 'decode type')
t.ok(val.opcode === 'QUERY', 'decode opcode') t.ok(val.opcode === 'QUERY', 'decode opcode')
t.ok(val.flag_qr === true, 'decode flag_auth') t.ok(val.flag_qr === true, 'decode flag_auth')
@ -214,12 +216,12 @@ tape('rcode', function (t) {
}) })
tape('name_encoding', function (t) { tape('name_encoding', function (t) {
var data = 'foo.example.com' let data = 'foo.example.com'
var buf = Buffer.allocUnsafe(255) const buf = Buffer.allocUnsafe(255)
var offset = 0 let offset = 0
packet.name.encode(data, buf, offset) packet.name.encode(data, buf, offset)
t.ok(packet.name.encode.bytes === 17, 'name encoding length matches') t.ok(packet.name.encode.bytes === 17, 'name encoding length matches')
var dd = packet.name.decode(buf, offset) let dd = packet.name.decode(buf, offset)
t.ok(data === dd, 'encode/decode matches') t.ok(data === dd, 'encode/decode matches')
offset += packet.name.encode.bytes offset += packet.name.encode.bytes
@ -246,15 +248,15 @@ tape('name_encoding', function (t) {
}) })
function testEncoder (t, rpacket, val) { function testEncoder (t, rpacket, val) {
var buf = rpacket.encode(val) const buf = rpacket.encode(val)
var val2 = rpacket.decode(buf) const val2 = rpacket.decode(buf)
t.same(buf.length, rpacket.encode.bytes, 'encode.bytes was set correctly') t.same(buf.length, rpacket.encode.bytes, 'encode.bytes was set correctly')
t.same(buf.length, rpacket.encodingLength(val), 'encoding length matches') t.same(buf.length, rpacket.encodingLength(val), 'encoding length matches')
t.ok(compare(t, val, val2), 'decoded object match') t.ok(compare(t, val, val2), 'decoded object match')
var buf2 = rpacket.encode(val2) const buf2 = rpacket.encode(val2)
var val3 = rpacket.decode(buf2) const val3 = rpacket.decode(buf2)
t.same(buf2.length, rpacket.encode.bytes, 'encode.bytes was set correctly on re-encode') t.same(buf2.length, rpacket.encode.bytes, 'encode.bytes was set correctly on re-encode')
t.same(buf2.length, rpacket.encodingLength(val), 'encoding length matches on re-encode') t.same(buf2.length, rpacket.encodingLength(val), 'encoding length matches on re-encode')
@ -262,10 +264,10 @@ function testEncoder (t, rpacket, val) {
t.ok(compare(t, val, val3), 'decoded object match on re-encode') t.ok(compare(t, val, val3), 'decoded object match on re-encode')
t.ok(compare(t, val2, val3), 're-encoded decoded object match on re-encode') t.ok(compare(t, val2, val3), 're-encoded decoded object match on re-encode')
var bigger = Buffer.allocUnsafe(buf2.length + 10) const bigger = Buffer.allocUnsafe(buf2.length + 10)
var buf3 = rpacket.encode(val, bigger, 10) const buf3 = rpacket.encode(val, bigger, 10)
var val4 = rpacket.decode(buf3, 10) const val4 = rpacket.decode(buf3, 10)
t.ok(buf3 === bigger, 'echoes buffer on external buffer') t.ok(buf3 === bigger, 'echoes buffer on external buffer')
t.same(rpacket.encode.bytes, buf.length, 'encode.bytes is the same on external buffer') t.same(rpacket.encode.bytes, buf.length, 'encode.bytes is the same on external buffer')
@ -275,8 +277,8 @@ function testEncoder (t, rpacket, val) {
function compare (t, a, b) { function compare (t, a, b) {
if (Buffer.isBuffer(a)) return a.toString('hex') === b.toString('hex') if (Buffer.isBuffer(a)) return a.toString('hex') === b.toString('hex')
if (typeof a === 'object' && a && b) { if (typeof a === 'object' && a && b) {
var keys = Object.keys(a) const keys = Object.keys(a)
for (var i = 0; i < keys.length; i++) { for (let i = 0; i < keys.length; i++) {
if (!compare(t, a[keys[i]], b[keys[i]])) return false if (!compare(t, a[keys[i]], b[keys[i]])) return false
} }
} else { } else {

View File

@ -1,3 +1,5 @@
'use strict'
exports.toString = function (type) { exports.toString = function (type) {
switch (type) { switch (type) {
case 1: return 'A' case 1: return 'A'