use new Buffer APIs (#1)

This commit is contained in:
silverwind 2016-11-11 05:21:27 +01:00 committed by Mathias Buus
parent b20b693267
commit b2f320b83d
3 changed files with 25 additions and 22 deletions

View File

@ -1,5 +1,6 @@
var types = require('./types') var types = require('./types')
var ip = require('ip') var ip = require('ip')
var Buffer = require('safe-buffer').Buffer
var QUERY_FLAG = 0 var QUERY_FLAG = 0
var RESPONSE_FLAG = 1 << 15 var RESPONSE_FLAG = 1 << 15
@ -11,7 +12,7 @@ var NOT_QU_MASK = ~QU_MASK
var name = {} var name = {}
name.encode = function (n, buf, offset) { name.encode = function (n, buf, offset) {
if (!buf) buf = Buffer(name.encodingLength(n)) if (!buf) buf = Buffer.allocUnsafe(name.encodingLength(n))
if (!offset) offset = 0 if (!offset) offset = 0
var list = n.split('.') var list = n.split('.')
@ -69,7 +70,7 @@ name.encodingLength = function (n) {
var string = {} var string = {}
string.encode = function (s, buf, offset) { string.encode = function (s, buf, offset) {
if (!buf) buf = Buffer(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) var len = buf.write(s, offset + 1)
@ -141,7 +142,7 @@ header.encodingLength = function (h) {
var runknown = exports.unknown = {} var runknown = exports.unknown = {}
runknown.encode = function (data, buf, offset) { runknown.encode = function (data, buf, offset) {
if (!buf) buf = Buffer(runknown.encodingLength(data)) if (!buf) buf = Buffer.allocUnsafe(runknown.encodingLength(data))
if (!offset) offset = 0 if (!offset) offset = 0
buf.writeUInt16BE(data.length, offset) buf.writeUInt16BE(data.length, offset)
@ -172,11 +173,11 @@ var rtxt = exports.txt = exports.null = {}
var rnull = rtxt var rnull = rtxt
rtxt.encode = function (data, buf, offset) { rtxt.encode = function (data, buf, offset) {
if (!buf) buf = Buffer(rtxt.encodingLength(data)) if (!buf) buf = Buffer.allocUnsafe(rtxt.encodingLength(data))
if (!offset) offset = 0 if (!offset) offset = 0
if (typeof data === 'string') data = Buffer(data) if (typeof data === 'string') data = Buffer.from(data)
if (!data) data = Buffer(0) if (!data) data = Buffer.allocUnsafe(0)
var oldOffset = offset var oldOffset = offset
offset += 2 offset += 2
@ -216,7 +217,7 @@ rtxt.encodingLength = function (data) {
var rhinfo = exports.hinfo = {} var rhinfo = exports.hinfo = {}
rhinfo.encode = function (data, buf, offset) { rhinfo.encode = function (data, buf, offset) {
if (!buf) buf = Buffer(rhinfo.encodingLength(data)) if (!buf) buf = Buffer.allocUnsafe(rhinfo.encodingLength(data))
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset var oldOffset = offset
@ -258,7 +259,7 @@ var rcname = exports.cname = rptr
var rdname = exports.dname = rptr var rdname = exports.dname = rptr
rptr.encode = function (data, buf, offset) { rptr.encode = function (data, buf, offset) {
if (!buf) buf = Buffer(rptr.encodingLength(data)) if (!buf) buf = Buffer.allocUnsafe(rptr.encodingLength(data))
if (!offset) offset = 0 if (!offset) offset = 0
name.encode(data, buf, offset + 2) name.encode(data, buf, offset + 2)
@ -286,7 +287,7 @@ rptr.encodingLength = function (data) {
var rsrv = exports.srv = {} var rsrv = exports.srv = {}
rsrv.encode = function (data, buf, offset) { rsrv.encode = function (data, buf, offset) {
if (!buf) buf = Buffer(rsrv.encodingLength(data)) if (!buf) buf = Buffer.allocUnsafe(rsrv.encodingLength(data))
if (!offset) offset = 0 if (!offset) offset = 0
buf.writeUInt16BE(data.priority || 0, offset + 2) buf.writeUInt16BE(data.priority || 0, offset + 2)
@ -327,7 +328,7 @@ rsrv.encodingLength = function (data) {
var ra = exports.a = {} var ra = exports.a = {}
ra.encode = function (host, buf, offset) { ra.encode = function (host, buf, offset) {
if (!buf) buf = Buffer(ra.encodingLength(host)) if (!buf) buf = Buffer.allocUnsafe(ra.encodingLength(host))
if (!offset) offset = 0 if (!offset) offset = 0
buf.writeUInt16BE(4, offset) buf.writeUInt16BE(4, offset)
@ -357,7 +358,7 @@ ra.encodingLength = function (host) {
var raaaa = exports.aaaa = {} var raaaa = exports.aaaa = {}
raaaa.encode = function (host, buf, offset) { raaaa.encode = function (host, buf, offset) {
if (!buf) buf = Buffer(raaaa.encodingLength(host)) if (!buf) buf = Buffer.allocUnsafe(raaaa.encodingLength(host))
if (!offset) offset = 0 if (!offset) offset = 0
buf.writeUInt16BE(16, offset) buf.writeUInt16BE(16, offset)
@ -402,7 +403,7 @@ var renc = exports.record = function (type) {
var answer = exports.answer = {} var answer = exports.answer = {}
answer.encode = function (a, buf, offset) { answer.encode = function (a, buf, offset) {
if (!buf) buf = Buffer(answer.encodingLength(a)) if (!buf) buf = Buffer.allocUnsafe(answer.encodingLength(a))
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset var oldOffset = offset
@ -460,7 +461,7 @@ answer.encodingLength = function (a) {
var question = exports.question = {} var question = exports.question = {}
question.encode = function (q, buf, offset) { question.encode = function (q, buf, offset) {
if (!buf) buf = Buffer(question.encodingLength(q)) if (!buf) buf = Buffer.allocUnsafe(question.encodingLength(q))
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset var oldOffset = offset
@ -516,7 +517,7 @@ exports.AUTHENTIC_DATA = 1 << 5
exports.CHECKING_DISABLED = 1 << 4 exports.CHECKING_DISABLED = 1 << 4
exports.encode = function (result, buf, offset) { exports.encode = function (result, buf, offset) {
if (!buf) buf = Buffer(exports.encodingLength(result)) if (!buf) buf = Buffer.allocUnsafe(exports.encodingLength(result))
if (!offset) offset = 0 if (!offset) offset = 0
var oldOffset = offset var oldOffset = offset

View File

@ -7,7 +7,8 @@
"url": "https://github.com/mafintosh/dns-packet" "url": "https://github.com/mafintosh/dns-packet"
}, },
"dependencies": { "dependencies": {
"ip": "^1.1.0" "ip": "^1.1.0",
"safe-buffer": "^5.0.1"
}, },
"devDependencies": { "devDependencies": {
"standard": "^6.0.5", "standard": "^6.0.5",

15
test.js
View File

@ -1,20 +1,21 @@
var tape = require('tape') var tape = require('tape')
var packet = require('./') var packet = require('./')
var Buffer = require('safe-buffer').Buffer
tape('unknown', function (t) { tape('unknown', function (t) {
testEncoder(t, packet.unknown, Buffer('hello world')) testEncoder(t, packet.unknown, Buffer.from('hello world'))
t.end() t.end()
}) })
tape('txt', function (t) { tape('txt', function (t) {
testEncoder(t, packet.txt, Buffer(0)) testEncoder(t, packet.txt, Buffer.allocUnsafe(0))
testEncoder(t, packet.txt, Buffer('hello world')) testEncoder(t, packet.txt, Buffer.from('hello world'))
testEncoder(t, packet.txt, Buffer([0, 1, 2, 3, 4, 5])) testEncoder(t, packet.txt, Buffer.from([0, 1, 2, 3, 4, 5]))
t.end() t.end()
}) })
tape('null', function (t) { tape('null', function (t) {
testEncoder(t, packet.null, Buffer([0, 1, 2, 3, 4, 5])) testEncoder(t, packet.null, Buffer.from([0, 1, 2, 3, 4, 5]))
t.end() t.end()
}) })
@ -140,7 +141,7 @@ tape('response', function (t) {
answers: [{ answers: [{
type: 'NULL', type: 'NULL',
name: 'hello.null.com', name: 'hello.null.com',
data: Buffer([1, 2, 3, 4, 5]) data: Buffer.from([1, 2, 3, 4, 5])
}] }]
}) })
@ -164,7 +165,7 @@ function testEncoder (t, packet, 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(buf2.length + 10) var bigger = Buffer.allocUnsafe(buf2.length + 10)
var buf3 = packet.encode(val, bigger, 10) var buf3 = packet.encode(val, bigger, 10)
var val4 = packet.decode(buf3, 10) var val4 = packet.decode(buf3, 10)