forked from LittleChest/dns-packet
Fix buffer management for stream encoding. (#29)
* Fix buffer management for stream encoding. * use slice() instead of Buffer.from() for Node v4
This commit is contained in:
parent
1192f944f0
commit
cd7caa6619
23
index.js
23
index.js
@ -853,23 +853,24 @@ exports.encodingLength = function (result) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.streamEncode = function (result) {
|
exports.streamEncode = function (result) {
|
||||||
const len = exports.encodingLength(result)
|
const buf = exports.encode(result)
|
||||||
const buf = Buffer.allocUnsafe(len + 2)
|
const sbuf = Buffer.allocUnsafe(2)
|
||||||
exports.encode(result, buf, 2)
|
sbuf.writeUInt16BE(buf.byteLength)
|
||||||
buf.writeUInt16BE(len, 0)
|
const combine = Buffer.concat([sbuf, buf])
|
||||||
exports.streamEncode.bytes = len + 2
|
exports.streamEncode.bytes = combine.byteLength
|
||||||
return buf
|
return combine
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.streamEncode.bytes = 0
|
exports.streamEncode.bytes = 0
|
||||||
|
|
||||||
exports.streamDecode = function (buf) {
|
exports.streamDecode = function (sbuf) {
|
||||||
const len = buf.readUInt16BE(0)
|
const len = sbuf.readUInt16BE(0)
|
||||||
if (buf.length < len + 2) {
|
if (sbuf.byteLength < len + 2) {
|
||||||
|
// not enough data
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
const result = exports.decode(buf, 2)
|
const result = exports.decode(sbuf.slice(2))
|
||||||
exports.streamDecode.bytes = exports.decode.bytes + 2
|
exports.streamDecode.bytes = exports.decode.bytes
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
33
test.js
33
test.js
@ -310,7 +310,6 @@ tape('stream', function (t) {
|
|||||||
const val2 = packet.streamDecode(buf)
|
const val2 = packet.streamDecode(buf)
|
||||||
|
|
||||||
t.same(buf.length, packet.streamEncode.bytes, 'streamEncode.bytes was set correctly')
|
t.same(buf.length, packet.streamEncode.bytes, 'streamEncode.bytes was set correctly')
|
||||||
t.same(packet.streamDecode.bytes, packet.streamEncode.bytes, 'streamDecode.bytes was set correctly')
|
|
||||||
t.ok(compare(t, val2.type, val.type), 'streamDecoded type match')
|
t.ok(compare(t, val2.type, val.type), 'streamDecoded type match')
|
||||||
t.ok(compare(t, val2.id, val.id), 'streamDecoded id match')
|
t.ok(compare(t, val2.id, val.id), 'streamDecoded id match')
|
||||||
t.ok(parseInt(val2.flags) === parseInt(val.flags & 0x7FFF), 'streamDecoded flags match')
|
t.ok(parseInt(val2.flags) === parseInt(val.flags & 0x7FFF), 'streamDecoded flags match')
|
||||||
@ -322,6 +321,38 @@ tape('stream', function (t) {
|
|||||||
t.end()
|
t.end()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
tape('unpack', function (t) {
|
||||||
|
const buf = Buffer.from([
|
||||||
|
0x00, 0x79,
|
||||||
|
0xde, 0xad, 0x85, 0x00, 0x00, 0x01, 0x00, 0x01,
|
||||||
|
0x00, 0x02, 0x00, 0x02, 0x02, 0x6f, 0x6a, 0x05,
|
||||||
|
0x62, 0x61, 0x6e, 0x67, 0x6a, 0x03, 0x63, 0x6f,
|
||||||
|
0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c,
|
||||||
|
0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x0e, 0x10,
|
||||||
|
0x00, 0x04, 0x81, 0xfa, 0x0b, 0xaa, 0xc0, 0x0f,
|
||||||
|
0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x0e, 0x10,
|
||||||
|
0x00, 0x05, 0x02, 0x63, 0x6a, 0xc0, 0x0f, 0xc0,
|
||||||
|
0x0f, 0x00, 0x02, 0x00, 0x01, 0x00, 0x00, 0x0e,
|
||||||
|
0x10, 0x00, 0x02, 0xc0, 0x0c, 0xc0, 0x3a, 0x00,
|
||||||
|
0x01, 0x00, 0x01, 0x00, 0x00, 0x0e, 0x10, 0x00,
|
||||||
|
0x04, 0x45, 0x4d, 0x9b, 0x9c, 0xc0, 0x0c, 0x00,
|
||||||
|
0x1c, 0x00, 0x01, 0x00, 0x00, 0x0e, 0x10, 0x00,
|
||||||
|
0x10, 0x20, 0x01, 0x04, 0x18, 0x00, 0x00, 0x50,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf9
|
||||||
|
])
|
||||||
|
const val = packet.streamDecode(buf)
|
||||||
|
const answer = val.answers[0]
|
||||||
|
const authority = val.authorities[1]
|
||||||
|
t.ok(val.rcode === 'NOERROR', 'decode rcode')
|
||||||
|
t.ok(compare(t, answer.type, 'A'), 'streamDecoded RR type match')
|
||||||
|
t.ok(compare(t, answer.name, 'oj.bangj.com'), 'streamDecoded RR name match')
|
||||||
|
t.ok(compare(t, answer.data, '129.250.11.170'), 'streamDecoded RR rdata match')
|
||||||
|
t.ok(compare(t, authority.type, 'NS'), 'streamDecoded RR type match')
|
||||||
|
t.ok(compare(t, authority.name, 'bangj.com'), 'streamDecoded RR name match')
|
||||||
|
t.ok(compare(t, authority.data, 'oj.bangj.com'), 'streamDecoded RR rdata match')
|
||||||
|
t.end()
|
||||||
|
})
|
||||||
|
|
||||||
function testEncoder (t, rpacket, val) {
|
function testEncoder (t, rpacket, val) {
|
||||||
const buf = rpacket.encode(val)
|
const buf = rpacket.encode(val)
|
||||||
const val2 = rpacket.decode(buf)
|
const val2 = rpacket.decode(buf)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user