Skip to content

Commit

Permalink
Embeddedd mail in an option object
Browse files Browse the repository at this point in the history
  • Loading branch information
M4t7e committed Aug 25, 2023
1 parent 0c8e8e5 commit 2741e95
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 13 deletions.
12 changes: 6 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const NOT_QU_MASK = ~QU_MASK

const name = exports.name = {}

name.encode = function (str, buf, offset, mail = false) {
name.encode = function (str, buf, offset, { mail = false } = {}) {
if (!buf) buf = Buffer.alloc(name.encodingLength(str))
if (!offset) offset = 0
const oldOffset = offset
Expand Down Expand Up @@ -58,7 +58,7 @@ name.encode = function (str, buf, offset, mail = false) {

name.encode.bytes = 0

name.decode = function (buf, offset, mail = false) {
name.decode = function (buf, offset, { mail = false } = {}) {
if (!offset) offset = 0

const list = []
Expand Down Expand Up @@ -274,7 +274,7 @@ rsoa.encode = function (data, buf, offset) {
offset += 2
name.encode(data.mname, buf, offset)
offset += name.encode.bytes
name.encode(data.rname, buf, offset, true)
name.encode(data.rname, buf, offset, { mail: true })
offset += name.encode.bytes
buf.writeUInt32BE(data.serial || 0, offset)
offset += 4
Expand Down Expand Up @@ -303,7 +303,7 @@ rsoa.decode = function (buf, offset) {
offset += 2
data.mname = name.decode(buf, offset)
offset += name.decode.bytes
data.rname = name.decode(buf, offset, true)
data.rname = name.decode(buf, offset, { mail: true })
offset += name.decode.bytes
data.serial = buf.readUInt32BE(offset)
offset += 4
Expand Down Expand Up @@ -1027,7 +1027,7 @@ rrp.encode = function (data, buf, offset) {
const oldOffset = offset

offset += 2 // Leave space for length
name.encode(data.mbox || '.', buf, offset, true)
name.encode(data.mbox || '.', buf, offset, { mail: true })
offset += name.encode.bytes
name.encode(data.txt || '.', buf, offset)
offset += name.encode.bytes
Expand All @@ -1044,7 +1044,7 @@ rrp.decode = function (buf, offset) {

const data = {}
offset += 2
data.mbox = name.decode(buf, offset, true) || '.'
data.mbox = name.decode(buf, offset, { mail: true }) || '.'
offset += name.decode.bytes
data.txt = name.decode(buf, offset) || '.'
offset += name.decode.bytes
Expand Down
21 changes: 14 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,30 +313,37 @@ tape('name_encoding', function (t) {
offset += packet.name.encode.bytes

data = 'a.b.c.d.example.com'
packet.name.encode(data, buf, offset, true)
packet.name.encode(data, buf, offset, { mail: false })
t.ok(packet.name.encode.bytes === 21, 'name (mail) encoding length matches')
dd = packet.name.decode(buf, offset)
t.ok(data === dd, 'encode/decode matches')
offset += packet.name.encode.bytes

data = 'a.b.c.d.example.com'
packet.name.encode(data, buf, offset, { mail: true })
t.ok(packet.name.encode.bytes === 21, 'name (mail) encoding length matches')
dd = packet.name.decode(buf, offset)
t.ok(data === dd, 'encode/decode matches')
offset += packet.name.encode.bytes

data = 'a\\.b.c.d.example.com'
packet.name.encode(data, buf, offset, true)
packet.name.encode(data, buf, offset, { mail: true })
t.ok(packet.name.encode.bytes === 21, 'name (mail) encoding length matches')
dd = packet.name.decode(buf, offset, true)
dd = packet.name.decode(buf, offset, { mail: true })
t.ok(data === dd, 'encode/decode matches')
offset += packet.name.encode.bytes

data = 'a\\.b\\.c.d.example.com'
packet.name.encode(data, buf, offset, true)
packet.name.encode(data, buf, offset, { mail: true })
t.ok(packet.name.encode.bytes === 21, 'name (mail) encoding length matches')
dd = packet.name.decode(buf, offset, true)
dd = packet.name.decode(buf, offset, { mail: true })
t.ok(data === dd, 'encode/decode matches')
offset += packet.name.encode.bytes

data = 'root\\.mail'
packet.name.encode(data, buf, offset, true)
packet.name.encode(data, buf, offset, { mail: true })
t.ok(packet.name.encode.bytes === 11, 'name (mail) encoding length matches')
dd = packet.name.decode(buf, offset, true)
dd = packet.name.decode(buf, offset, { mail: true })
t.ok(data === dd, 'encode/decode matches')
offset += packet.name.encode.bytes

Expand Down

0 comments on commit 2741e95

Please sign in to comment.