From d95b456ddf350794bce31644d760904b854270cc Mon Sep 17 00:00:00 2001 From: LittleChest Date: Tue, 7 Apr 2026 23:01:07 +0800 Subject: [PATCH] Bye buffer --- buffer.js | 193 ++++++++++++++++++ hexdump.js | 13 +- index.js | 561 +++++++++++++++++++++++++++-------------------------- test.js | 85 ++++---- 4 files changed, 539 insertions(+), 313 deletions(-) create mode 100644 buffer.js diff --git a/buffer.js b/buffer.js new file mode 100644 index 0000000..ca9382d --- /dev/null +++ b/buffer.js @@ -0,0 +1,193 @@ +'use strict' + +// Allocate a new Uint8Array buffer +function allocBuffer (size) { + return new Uint8Array(size) +} + +// Allocate an uninitialized Uint8Array (same as allocBuffer for Uint8Array) +function allocUnsafeBuffer (size) { + return new Uint8Array(size) +} + +// Check if an object is a Uint8Array +function isBuffer (obj) { + return obj instanceof Uint8Array +} + +// Create Uint8Array from data with optional encoding +function fromBuffer (data, encoding) { + if (data instanceof Uint8Array) { + return new Uint8Array(data) + } + if (Array.isArray(data)) { + return new Uint8Array(data) + } + if (typeof data === 'string') { + if (encoding === 'base64') { + return base64ToBuffer(data) + } else if (encoding === 'hex') { + return hexToBuffer(data) + } else { + return stringToBuffer(data) + } + } + return new Uint8Array(data) +} + +// Convert string to Uint8Array (UTF-8 encoding) +function stringToBuffer (str) { + const encoder = new TextEncoder() + return encoder.encode(str) +} + +// Convert Base64 string to Uint8Array +function base64ToBuffer (base64Str) { + if (typeof window !== 'undefined' && window.atob) { + // Browser environment + const binaryString = window.atob(base64Str) + const bytes = new Uint8Array(binaryString.length) + for (let i = 0; i < binaryString.length; i++) { + bytes[i] = binaryString.charCodeAt(i) + } + return bytes + } else { + // Node.js environment + return new Uint8Array(Buffer.from(base64Str, 'base64')) + } +} + +// Convert hex string to Uint8Array +function hexToBuffer (hexStr) { + const bytes = new Uint8Array(hexStr.length / 2) + for (let i = 0; i < hexStr.length; i += 2) { + bytes[i / 2] = parseInt(hexStr.substr(i, 2), 16) + } + return bytes +} + +// Convert Uint8Array to string +function bufferToString (buf, encoding, start, end) { + encoding = encoding || 'utf-8' + start = start || 0 + end = end || buf.length + + if (encoding !== 'utf-8' && encoding !== 'utf8') { + throw new Error('Unsupported encoding: ' + encoding) + } + + const slice = buf.slice(start, end) + const decoder = new TextDecoder('utf-8') + return decoder.decode(slice) +} + +// Write string to buffer, return number of bytes written +function writeString (buf, str, offset) { + offset = offset || 0 + const encoded = stringToBuffer(str) + for (let i = 0; i < encoded.length && offset + i < buf.length; i++) { + buf[offset + i] = encoded[i] + } + return encoded.length +} + +// Get byte length of a string +function byteLength (str) { + const encoder = new TextEncoder() + return encoder.encode(str).length +} + +// Read big-endian 16-bit unsigned integer +function readUInt16BE (buf, offset) { + offset = offset || 0 + return (buf[offset] << 8) | buf[offset + 1] +} + +// Write big-endian 16-bit unsigned integer +function writeUInt16BE (buf, value, offset) { + offset = offset || 0 + buf[offset] = (value >> 8) & 0xff + buf[offset + 1] = value & 0xff +} + +// Read big-endian 32-bit unsigned integer +function readUInt32BE (buf, offset) { + offset = offset || 0 + return ((buf[offset] << 24) | (buf[offset + 1] << 16) | (buf[offset + 2] << 8) | buf[offset + 3]) >>> 0 +} + +// Write big-endian 32-bit unsigned integer +function writeUInt32BE (buf, value, offset) { + offset = offset || 0 + buf[offset] = (value >> 24) & 0xff + buf[offset + 1] = (value >> 16) & 0xff + buf[offset + 2] = (value >> 8) & 0xff + buf[offset + 3] = value & 0xff +} + +// Read 8-bit unsigned integer +function readUInt8 (buf, offset) { + offset = offset || 0 + return buf[offset] +} + +// Write 8-bit unsigned integer +function writeUInt8 (buf, value, offset) { + offset = offset || 0 + buf[offset] = value & 0xff +} + +// Copy buffer contents +function bufferCopy (source, target, targetStart, sourceStart, sourceEnd) { + targetStart = targetStart || 0 + sourceStart = sourceStart || 0 + sourceEnd = sourceEnd !== undefined ? sourceEnd : source.length + + for (let i = sourceStart; i < sourceEnd; i++) { + target[targetStart + (i - sourceStart)] = source[i] + } +} + +// Concatenate multiple buffers +function bufferConcat (buffers, totalLength) { + if (!Array.isArray(buffers)) { + throw new Error('buffers must be an Array') + } + + if (buffers.length === 0) { + return new Uint8Array(0) + } + + totalLength = totalLength || buffers.reduce((sum, buf) => sum + buf.length, 0) + const result = new Uint8Array(totalLength) + + let offset = 0 + for (let i = 0; i < buffers.length; i++) { + const buf = buffers[i] + result.set(buf, offset) + offset += buf.length + } + + return result +} + +module.exports = { + allocBuffer, + allocUnsafeBuffer, + isBuffer, + fromBuffer, + stringToBuffer, + base64ToBuffer, + hexToBuffer, + bufferToString, + writeString, + byteLength, + readUInt8, + writeUInt8, + readUInt16BE, + writeUInt16BE, + readUInt32BE, + writeUInt32BE, + bufferCopy, + bufferConcat +} diff --git a/hexdump.js b/hexdump.js index 0ac08ce..73452cd 100644 --- a/hexdump.js +++ b/hexdump.js @@ -1,7 +1,16 @@ 'use strict' const wout = (s) => { - process.stdout.write(s) + if (typeof process !== 'undefined' && process.stdout) { + process.stdout.write(s) + } else if (typeof console !== 'undefined') { + if (s === '\n') { + console.log('') + } else { + process.stdout = process.stdout || '' + process.stdout += s + } + } } const padStr = (s, len, ch) => { @@ -55,7 +64,7 @@ const hexdump = (buf, len) => { } /* -let buf = Buffer.alloc(500) +let buf = new Uint8Array(500) for (let i = 0; i < buf.length; i++) { buf[i] = i & 0xff } diff --git a/index.js b/index.js index 40b0ce1..1a3f3ae 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,23 @@ 'use strict' -const Buffer = require('buffer').Buffer +const { + allocBuffer, + allocUnsafeBuffer, + isBuffer, + fromBuffer, + bufferToString, + writeString, + byteLength, + readUInt8, + writeUInt8, + readUInt16BE, + writeUInt16BE, + readUInt32BE, + writeUInt32BE, + bufferCopy, + bufferConcat +} = require('./buffer') + const types = require('./types') const rcodes = require('./rcodes') const opcodes = require('./opcodes') @@ -17,7 +34,7 @@ const QU_MASK = 1 << 15 const name = exports.name = {} name.encode = function (str, buf, offset, { mail = false } = {}) { - if (!buf) buf = Buffer.alloc(name.encodingLength(str)) + if (!buf) buf = allocBuffer(name.encodingLength(str)) if (!offset) offset = 0 const oldOffset = offset @@ -43,7 +60,7 @@ name.encode = function (str, buf, offset, { mail = false } = {}) { } for (let i = 0; i < list.length; i++) { - const len = buf.write(list[i], offset + 1) + const len = writeString(buf, list[i], offset + 1) buf[offset] = len offset += len + 1 } @@ -83,7 +100,7 @@ name.decode = function (buf, offset, { mail = false } = {}) { if (totalLength > 254) { throw new Error('Cannot decode name (name too long)') } - let label = buf.toString('utf-8', offset, offset + len) + let label = bufferToString(buf, 'utf-8', offset, offset + len) if (mail) { label = label.replace(/\./g, '\\.') } @@ -94,7 +111,7 @@ name.decode = function (buf, offset, { mail = false } = {}) { if (offset + 1 > buf.length) { throw new Error('Cannot decode name (buffer overflow)') } - const jumpOffset = buf.readUInt16BE(offset - 1) - 0xc000 + const jumpOffset = readUInt16BE(buf, offset - 1) - 0xc000 if (jumpOffset >= oldOffset) { // Allow only pointers to prior data. RFC 1035, section 4.1.4 states: // "[...] an entire domain name or a list of labels at the end of a domain name @@ -118,16 +135,16 @@ name.decode.bytes = 0 name.encodingLength = function (n) { if (n === '.' || n === '..') return 1 - return Buffer.byteLength(n.replace(/^\.|\.$/gm, '')) + 2 + return byteLength(n.replace(/^\.|\.$/gm, '')) + 2 } const string = {} string.encode = function (s, buf, offset) { - if (!buf) buf = Buffer.alloc(string.encodingLength(s)) + if (!buf) buf = allocBuffer(string.encodingLength(s)) if (!offset) offset = 0 - const len = buf.write(s, offset + 1) + const len = writeString(buf, s, offset + 1) buf[offset] = len string.encode.bytes = len + 1 return buf @@ -139,7 +156,7 @@ string.decode = function (buf, offset) { if (!offset) offset = 0 const len = buf[offset] - const s = buf.toString('utf-8', offset + 1, offset + 1 + len) + const s = bufferToString(buf, 'utf-8', offset + 1, offset + 1 + len) string.decode.bytes = len + 1 return s } @@ -147,7 +164,7 @@ string.decode = function (buf, offset) { string.decode.bytes = 0 string.encodingLength = function (s) { - return Buffer.byteLength(s) + 1 + return byteLength(s) + 1 } const header = {} @@ -159,12 +176,12 @@ header.encode = function (h, buf, offset) { const flags = (h.flags || 0) & 32767 const type = h.type === 'response' ? RESPONSE_FLAG : QUERY_FLAG - buf.writeUInt16BE(h.id || 0, offset) - buf.writeUInt16BE(flags | type, offset + 2) - buf.writeUInt16BE(h.questions.length, offset + 4) - buf.writeUInt16BE(h.answers.length, offset + 6) - buf.writeUInt16BE(h.authorities.length, offset + 8) - buf.writeUInt16BE(h.additionals.length, offset + 10) + writeUInt16BE(buf, h.id || 0, offset) + writeUInt16BE(buf, flags | type, offset + 2) + writeUInt16BE(buf, h.questions.length, offset + 4) + writeUInt16BE(buf, h.answers.length, offset + 6) + writeUInt16BE(buf, h.authorities.length, offset + 8) + writeUInt16BE(buf, h.additionals.length, offset + 10) return buf } @@ -174,10 +191,10 @@ header.encode.bytes = 12 header.decode = function (buf, offset) { if (!offset) offset = 0 if (buf.length < 12) throw new Error('Header must be 12 bytes') - const flags = buf.readUInt16BE(offset + 2) + const flags = readUInt16BE(buf, offset + 2) return { - id: buf.readUInt16BE(offset), + id: readUInt16BE(buf, offset), type: flags & RESPONSE_FLAG ? 'response' : 'query', flags: flags & 32767, flag_qr: ((flags >> 15) & 0x1) === 1, @@ -190,10 +207,10 @@ header.decode = function (buf, offset) { flag_ad: ((flags >> 5) & 0x1) === 1, flag_cd: ((flags >> 4) & 0x1) === 1, rcode: rcodes.toString(flags & 0xf), - questions: new Array(buf.readUInt16BE(offset + 4)), - answers: new Array(buf.readUInt16BE(offset + 6)), - authorities: new Array(buf.readUInt16BE(offset + 8)), - additionals: new Array(buf.readUInt16BE(offset + 10)) + questions: new Array(readUInt16BE(buf, offset + 4)), + answers: new Array(readUInt16BE(buf, offset + 6)), + authorities: new Array(readUInt16BE(buf, offset + 8)), + additionals: new Array(readUInt16BE(buf, offset + 10)) } } @@ -206,11 +223,11 @@ header.encodingLength = function () { const runknown = exports.unknown = {} runknown.encode = function (data, buf, offset) { - if (!buf) buf = Buffer.alloc(runknown.encodingLength(data)) + if (!buf) buf = allocBuffer(runknown.encodingLength(data)) if (!offset) offset = 0 - buf.writeUInt16BE(data.length, offset) - data.copy(buf, offset + 2) + writeUInt16BE(buf, data.length, offset) + bufferCopy(data, buf, offset + 2) runknown.encode.bytes = data.length + 2 return buf @@ -221,7 +238,7 @@ runknown.encode.bytes = 0 runknown.decode = function (buf, offset) { if (!offset) offset = 0 - const len = buf.readUInt16BE(offset) + const len = readUInt16BE(buf, offset) const data = buf.slice(offset + 2, offset + 2 + len) runknown.decode.bytes = len + 2 return data @@ -236,11 +253,11 @@ runknown.encodingLength = function (data) { const rns = exports.ns = {} rns.encode = function (data, buf, offset) { - if (!buf) buf = Buffer.alloc(rns.encodingLength(data)) + if (!buf) buf = allocBuffer(rns.encodingLength(data)) if (!offset) offset = 0 name.encode(data, buf, offset + 2) - buf.writeUInt16BE(name.encode.bytes, offset) + writeUInt16BE(buf, name.encode.bytes, offset) rns.encode.bytes = name.encode.bytes + 2 return buf } @@ -250,7 +267,7 @@ rns.encode.bytes = 0 rns.decode = function (buf, offset) { if (!offset) offset = 0 - const len = buf.readUInt16BE(offset) + const len = readUInt16BE(buf, offset) const dd = name.decode(buf, offset + 2) rns.decode.bytes = len + 2 @@ -266,7 +283,7 @@ rns.encodingLength = function (data) { const rsoa = exports.soa = {} rsoa.encode = function (data, buf, offset) { - if (!buf) buf = Buffer.alloc(rsoa.encodingLength(data)) + if (!buf) buf = allocBuffer(rsoa.encodingLength(data)) if (!offset) offset = 0 const oldOffset = offset @@ -275,18 +292,18 @@ rsoa.encode = function (data, buf, offset) { offset += name.encode.bytes name.encode(data.rname, buf, offset, { mail: true }) offset += name.encode.bytes - buf.writeUInt32BE(data.serial || 0, offset) + writeUInt32BE(buf, data.serial || 0, offset) offset += 4 - buf.writeUInt32BE(data.refresh || 0, offset) + writeUInt32BE(buf, data.refresh || 0, offset) offset += 4 - buf.writeUInt32BE(data.retry || 0, offset) + writeUInt32BE(buf, data.retry || 0, offset) offset += 4 - buf.writeUInt32BE(data.expire || 0, offset) + writeUInt32BE(buf, data.expire || 0, offset) offset += 4 - buf.writeUInt32BE(data.minimum || 0, offset) + writeUInt32BE(buf, data.minimum || 0, offset) offset += 4 - buf.writeUInt16BE(offset - oldOffset - 2, oldOffset) + writeUInt16BE(buf, offset - oldOffset - 2, oldOffset) rsoa.encode.bytes = offset - oldOffset return buf } @@ -304,15 +321,15 @@ rsoa.decode = function (buf, offset) { offset += name.decode.bytes data.rname = name.decode(buf, offset, { mail: true }) offset += name.decode.bytes - data.serial = buf.readUInt32BE(offset) + data.serial = readUInt32BE(buf, offset) offset += 4 - data.refresh = buf.readUInt32BE(offset) + data.refresh = readUInt32BE(buf, offset) offset += 4 - data.retry = buf.readUInt32BE(offset) + data.retry = readUInt32BE(buf, offset) offset += 4 - data.expire = buf.readUInt32BE(offset) + data.expire = readUInt32BE(buf, offset) offset += 4 - data.minimum = buf.readUInt32BE(offset) + data.minimum = readUInt32BE(buf, offset) offset += 4 rsoa.decode.bytes = offset - oldOffset @@ -331,14 +348,14 @@ rtxt.encode = function (data, buf, offset) { if (!Array.isArray(data)) data = [data] for (let i = 0; i < data.length; i++) { if (typeof data[i] === 'string') { - data[i] = Buffer.from(data[i]) + data[i] = fromBuffer(data[i]) } - if (!Buffer.isBuffer(data[i])) { + if (!isBuffer(data[i])) { throw new Error('Must be a Buffer') } } - if (!buf) buf = Buffer.alloc(rtxt.encodingLength(data)) + if (!buf) buf = allocBuffer(rtxt.encodingLength(data)) if (!offset) offset = 0 const oldOffset = offset @@ -346,11 +363,11 @@ rtxt.encode = function (data, buf, offset) { data.forEach(function (d) { buf[offset++] = d.length - d.copy(buf, offset, 0, d.length) + bufferCopy(d, buf, offset, 0, d.length) offset += d.length }) - buf.writeUInt16BE(offset - oldOffset - 2, oldOffset) + writeUInt16BE(buf, offset - oldOffset - 2, oldOffset) rtxt.encode.bytes = offset - oldOffset return buf } @@ -360,7 +377,7 @@ rtxt.encode.bytes = 0 rtxt.decode = function (buf, offset) { if (!offset) offset = 0 const oldOffset = offset - let remaining = buf.readUInt16BE(offset) + let remaining = readUInt16BE(buf, offset) offset += 2 let data = [] @@ -386,7 +403,7 @@ rtxt.encodingLength = function (data) { let length = 2 data.forEach(function (buf) { if (typeof buf === 'string') { - length += Buffer.byteLength(buf) + 1 + length += byteLength(buf) + 1 } else { length += buf.length + 1 } @@ -397,20 +414,20 @@ rtxt.encodingLength = function (data) { const rnull = exports.null = {} rnull.encode = function (data, buf, offset) { - if (!buf) buf = Buffer.alloc(rnull.encodingLength(data)) + if (!buf) buf = allocBuffer(rnull.encodingLength(data)) if (!offset) offset = 0 - if (typeof data === 'string') data = Buffer.from(data) - if (!data) data = Buffer.alloc(0) + if (typeof data === 'string') data = fromBuffer(data) + if (!data) data = allocBuffer(0) const oldOffset = offset offset += 2 const len = data.length - data.copy(buf, offset, 0, len) + bufferCopy(data, buf, offset, 0, len) offset += len - buf.writeUInt16BE(offset - oldOffset - 2, oldOffset) + writeUInt16BE(buf, offset - oldOffset - 2, oldOffset) rnull.encode.bytes = offset - oldOffset return buf } @@ -420,7 +437,7 @@ rnull.encode.bytes = 0 rnull.decode = function (buf, offset) { if (!offset) offset = 0 const oldOffset = offset - const len = buf.readUInt16BE(offset) + const len = readUInt16BE(buf, offset) offset += 2 @@ -435,13 +452,13 @@ rnull.decode.bytes = 0 rnull.encodingLength = function (data) { if (!data) return 2 - return (Buffer.isBuffer(data) ? data.length : Buffer.byteLength(data)) + 2 + return (isBuffer(data) ? data.length : byteLength(data)) + 2 } const rhinfo = exports.hinfo = {} rhinfo.encode = function (data, buf, offset) { - if (!buf) buf = Buffer.alloc(rhinfo.encodingLength(data)) + if (!buf) buf = allocBuffer(rhinfo.encodingLength(data)) if (!offset) offset = 0 const oldOffset = offset @@ -450,7 +467,7 @@ rhinfo.encode = function (data, buf, offset) { offset += string.encode.bytes string.encode(data.os, buf, offset) offset += string.encode.bytes - buf.writeUInt16BE(offset - oldOffset - 2, oldOffset) + writeUInt16BE(buf, offset - oldOffset - 2, oldOffset) rhinfo.encode.bytes = offset - oldOffset return buf } @@ -483,11 +500,11 @@ const rcname = exports.cname = rptr const rdname = exports.dname = rptr rptr.encode = function (data, buf, offset) { - if (!buf) buf = Buffer.alloc(rptr.encodingLength(data)) + if (!buf) buf = allocBuffer(rptr.encodingLength(data)) if (!offset) offset = 0 name.encode(data, buf, offset + 2) - buf.writeUInt16BE(name.encode.bytes, offset) + writeUInt16BE(buf, name.encode.bytes, offset) rptr.encode.bytes = name.encode.bytes + 2 return buf } @@ -511,16 +528,16 @@ rptr.encodingLength = function (data) { const rsrv = exports.srv = {} rsrv.encode = function (data, buf, offset) { - if (!buf) buf = Buffer.alloc(rsrv.encodingLength(data)) + if (!buf) buf = allocBuffer(rsrv.encodingLength(data)) if (!offset) offset = 0 - buf.writeUInt16BE(data.priority || 0, offset + 2) - buf.writeUInt16BE(data.weight || 0, offset + 4) - buf.writeUInt16BE(data.port || 0, offset + 6) + writeUInt16BE(buf, data.priority || 0, offset + 2) + writeUInt16BE(buf, data.weight || 0, offset + 4) + writeUInt16BE(buf, data.port || 0, offset + 6) name.encode(data.target, buf, offset + 8) const len = name.encode.bytes + 6 - buf.writeUInt16BE(len, offset) + writeUInt16BE(buf, len, offset) rsrv.encode.bytes = len + 2 return buf @@ -531,12 +548,12 @@ rsrv.encode.bytes = 0 rsrv.decode = function (buf, offset) { if (!offset) offset = 0 - const len = buf.readUInt16BE(offset) + const len = readUInt16BE(buf, offset) const data = {} - data.priority = buf.readUInt16BE(offset + 2) - data.weight = buf.readUInt16BE(offset + 4) - data.port = buf.readUInt16BE(offset + 6) + data.priority = readUInt16BE(buf, offset + 2) + data.weight = readUInt16BE(buf, offset + 4) + data.port = readUInt16BE(buf, offset + 6) data.target = name.decode(buf, offset + 8) rsrv.decode.bytes = len + 2 @@ -556,21 +573,21 @@ rcaa.ISSUER_CRITICAL = 1 << 7 rcaa.encode = function (data, buf, offset) { const len = rcaa.encodingLength(data) - if (!buf) buf = Buffer.alloc(rcaa.encodingLength(data)) + if (!buf) buf = allocBuffer(rcaa.encodingLength(data)) if (!offset) offset = 0 if (data.issuerCritical) { data.flags = rcaa.ISSUER_CRITICAL } - buf.writeUInt16BE(len - 2, offset) + writeUInt16BE(buf, len - 2, offset) offset += 2 - buf.writeUInt8(data.flags || 0, offset) + writeUInt8(buf, data.flags || 0, offset) offset += 1 string.encode(data.tag, buf, offset) offset += string.encode.bytes - buf.write(data.value, offset) - offset += Buffer.byteLength(data.value) + writeString(buf, data.value, offset) + offset += byteLength(data.value) rcaa.encode.bytes = len return buf @@ -581,16 +598,16 @@ rcaa.encode.bytes = 0 rcaa.decode = function (buf, offset) { if (!offset) offset = 0 - const len = buf.readUInt16BE(offset) + const len = readUInt16BE(buf, offset) offset += 2 const oldOffset = offset const data = {} - data.flags = buf.readUInt8(offset) + data.flags = readUInt8(buf, offset) offset += 1 data.tag = string.decode(buf, offset) offset += string.decode.bytes - data.value = buf.toString('utf-8', offset, oldOffset + len) + data.value = bufferToString(buf, 'utf-8', offset, oldOffset + len) data.issuerCritical = !!(data.flags & rcaa.ISSUER_CRITICAL) @@ -608,17 +625,17 @@ rcaa.encodingLength = function (data) { const rmx = exports.mx = {} rmx.encode = function (data, buf, offset) { - if (!buf) buf = Buffer.alloc(rmx.encodingLength(data)) + if (!buf) buf = allocBuffer(rmx.encodingLength(data)) if (!offset) offset = 0 const oldOffset = offset offset += 2 - buf.writeUInt16BE(data.preference || 0, offset) + writeUInt16BE(buf, data.preference || 0, offset) offset += 2 name.encode(data.exchange, buf, offset) offset += name.encode.bytes - buf.writeUInt16BE(offset - oldOffset - 2, oldOffset) + writeUInt16BE(buf, offset - oldOffset - 2, oldOffset) rmx.encode.bytes = offset - oldOffset return buf } @@ -632,7 +649,7 @@ rmx.decode = function (buf, offset) { const data = {} offset += 2 - data.preference = buf.readUInt16BE(offset) + data.preference = readUInt16BE(buf, offset) offset += 2 data.exchange = name.decode(buf, offset) offset += name.decode.bytes @@ -648,10 +665,10 @@ rmx.encodingLength = function (data) { const ra = exports.a = {} ra.encode = function (host, buf, offset) { - if (!buf) buf = Buffer.alloc(ra.encodingLength(host)) + if (!buf) buf = allocBuffer(ra.encodingLength(host)) if (!offset) offset = 0 - buf.writeUInt16BE(4, offset) + writeUInt16BE(buf, 4, offset) offset += 2 ip.v4.encode(host, buf, offset) ra.encode.bytes = 6 @@ -678,10 +695,10 @@ ra.encodingLength = function () { const raaaa = exports.aaaa = {} raaaa.encode = function (host, buf, offset) { - if (!buf) buf = Buffer.alloc(raaaa.encodingLength(host)) + if (!buf) buf = allocBuffer(raaaa.encodingLength(host)) if (!offset) offset = 0 - buf.writeUInt16BE(16, offset) + writeUInt16BE(buf, 16, offset) offset += 2 ip.v6.encode(host, buf, offset) raaaa.encode.bytes = 18 @@ -708,17 +725,17 @@ raaaa.encodingLength = function () { const roption = exports.option = {} roption.encode = function (option, buf, offset) { - if (!buf) buf = Buffer.alloc(roption.encodingLength(option)) + if (!buf) buf = allocBuffer(roption.encodingLength(option)) if (!offset) offset = 0 const oldOffset = offset const code = optioncodes.toCode(option.code) - buf.writeUInt16BE(code, offset) + writeUInt16BE(buf, code, offset) offset += 2 if (option.data) { - buf.writeUInt16BE(option.data.length, offset) + writeUInt16BE(buf, option.data.length, offset) offset += 2 - option.data.copy(buf, offset) + bufferCopy(option.data, buf, offset) offset += option.data.length } else { switch (code) { @@ -728,34 +745,34 @@ roption.encode = function (option, buf, offset) { // note: do IP math before calling const spl = option.sourcePrefixLength || 0 const fam = option.family || ip.familyOf(option.ip) - const ipBuf = ip.encode(option.ip, Buffer.alloc) + const ipBuf = ip.encode(option.ip, allocBuffer) const ipLen = Math.ceil(spl / 8) - buf.writeUInt16BE(ipLen + 4, offset) + writeUInt16BE(buf, ipLen + 4, offset) offset += 2 - buf.writeUInt16BE(fam, offset) + writeUInt16BE(buf, fam, offset) offset += 2 - buf.writeUInt8(spl, offset++) - buf.writeUInt8(option.scopePrefixLength || 0, offset++) + writeUInt8(buf, spl, offset++) + writeUInt8(buf, option.scopePrefixLength || 0, offset++) - ipBuf.copy(buf, offset, 0, ipLen) + bufferCopy(ipBuf, buf, offset, 0, ipLen) offset += ipLen break // case 9: EXPIRE (experimental) // case 10: COOKIE. No encode makes sense. case 11: // KEEP-ALIVE if (option.timeout) { - buf.writeUInt16BE(2, offset) + writeUInt16BE(buf, 2, offset) offset += 2 - buf.writeUInt16BE(option.timeout, offset) + writeUInt16BE(buf, option.timeout, offset) offset += 2 } else { - buf.writeUInt16BE(0, offset) + writeUInt16BE(buf, 0, offset) offset += 2 } break case 12: // PADDING const len = option.length || 0 - buf.writeUInt16BE(len, offset) + writeUInt16BE(buf, len, offset) offset += 2 buf.fill(0, offset, offset + len) offset += len @@ -763,10 +780,10 @@ roption.encode = function (option, buf, offset) { // case 13: CHAIN. Experimental. case 14: // KEY-TAG const tagsLen = option.tags.length * 2 - buf.writeUInt16BE(tagsLen, offset) + writeUInt16BE(buf, tagsLen, offset) offset += 2 for (const tag of option.tags) { - buf.writeUInt16BE(tag, offset) + writeUInt16BE(buf, tag, offset) offset += 2 } break @@ -784,34 +801,34 @@ roption.encode.bytes = 0 roption.decode = function (buf, offset) { if (!offset) offset = 0 const option = {} - option.code = buf.readUInt16BE(offset) + option.code = readUInt16BE(buf, offset) option.type = optioncodes.toString(option.code) offset += 2 - const len = buf.readUInt16BE(offset) + const len = readUInt16BE(buf, offset) offset += 2 option.data = buf.slice(offset, offset + len) switch (option.code) { // case 3: NSID. No decode makes sense. case 8: // ECS - option.family = buf.readUInt16BE(offset) + option.family = readUInt16BE(buf, offset) offset += 2 - option.sourcePrefixLength = buf.readUInt8(offset++) - option.scopePrefixLength = buf.readUInt8(offset++) - const padded = Buffer.alloc((option.family === 1) ? 4 : 16) - buf.copy(padded, 0, offset, offset + len - 4) + option.sourcePrefixLength = readUInt8(buf, offset++) + option.scopePrefixLength = readUInt8(buf, offset++) + const padded = allocBuffer((option.family === 1) ? 4 : 16) + bufferCopy(buf, padded, 0, offset, offset + len - 4) option.ip = ip.decode(padded) break // case 12: Padding. No decode makes sense. case 11: // KEEP-ALIVE if (len > 0) { - option.timeout = buf.readUInt16BE(offset) + option.timeout = readUInt16BE(buf, offset) offset += 2 } break case 14: option.tags = [] for (let i = 0; i < len; i += 2) { - option.tags.push(buf.readUInt16BE(offset)) + option.tags.push(readUInt16BE(buf, offset)) offset += 2 } // don't worry about default. caller will use data if desired @@ -845,12 +862,12 @@ roption.encodingLength = function (option) { const ropt = exports.opt = {} ropt.encode = function (options, buf, offset) { - if (!buf) buf = Buffer.alloc(ropt.encodingLength(options)) + if (!buf) buf = allocBuffer(ropt.encodingLength(options)) if (!offset) offset = 0 const oldOffset = offset const rdlen = encodingLengthList(options, roption) - buf.writeUInt16BE(rdlen, offset) + writeUInt16BE(buf, rdlen, offset) offset = encodeList(options, roption, buf, offset + 2) ropt.encode.bytes = offset - oldOffset @@ -864,7 +881,7 @@ ropt.decode = function (buf, offset) { const oldOffset = offset const options = [] - let rdlen = buf.readUInt16BE(offset) + let rdlen = readUInt16BE(buf, offset) offset += 2 let o = 0 while (rdlen > 0) { @@ -889,24 +906,24 @@ rdnskey.ZONE_KEY = 0x80 rdnskey.SECURE_ENTRYPOINT = 0x8000 rdnskey.encode = function (key, buf, offset) { - if (!buf) buf = Buffer.alloc(rdnskey.encodingLength(key)) + if (!buf) buf = allocBuffer(rdnskey.encodingLength(key)) if (!offset) offset = 0 const oldOffset = offset - const keydata = Buffer.from(key.key, 'base64') + const keydata = fromBuffer(key.key, 'base64') offset += 2 // Leave space for length - buf.writeUInt16BE(key.flags, offset) + writeUInt16BE(buf, key.flags, offset) offset += 2 - buf.writeUInt8(rdnskey.PROTOCOL_DNSSEC, offset) + writeUInt8(buf, rdnskey.PROTOCOL_DNSSEC, offset) offset += 1 - buf.writeUInt8(key.algorithm, offset) + writeUInt8(buf, key.algorithm, offset) offset += 1 - keydata.copy(buf, offset, 0, keydata.length) + bufferCopy(keydata, buf, offset, 0, keydata.length) offset += keydata.length rdnskey.encode.bytes = offset - oldOffset - buf.writeUInt16BE(rdnskey.encode.bytes - 2, oldOffset) + writeUInt16BE(buf, rdnskey.encode.bytes - 2, oldOffset) return buf } @@ -917,15 +934,15 @@ rdnskey.decode = function (buf, offset) { const oldOffset = offset var key = {} - var length = buf.readUInt16BE(offset) + var length = readUInt16BE(buf, offset) offset += 2 - key.flags = buf.readUInt16BE(offset) + key.flags = readUInt16BE(buf, offset) offset += 2 - if (buf.readUInt8(offset) !== rdnskey.PROTOCOL_DNSSEC) { + if (readUInt8(buf, offset) !== rdnskey.PROTOCOL_DNSSEC) { throw new Error('Protocol must be 3') } offset += 1 - key.algorithm = buf.readUInt8(offset) + key.algorithm = readUInt8(buf, offset) offset += 1 key.key = buf.slice(offset, oldOffset + length + 2) offset += key.key.length @@ -936,40 +953,40 @@ rdnskey.decode = function (buf, offset) { rdnskey.decode.bytes = 0 rdnskey.encodingLength = function (key) { - return 6 + Buffer.byteLength(key.key, 'base64') + return 6 + byteLength(key.key, 'base64') } const rrrsig = exports.rrsig = {} rrrsig.encode = function (sig, buf, offset) { - if (!buf) buf = Buffer.alloc(rrrsig.encodingLength(sig)) + if (!buf) buf = allocBuffer(rrrsig.encodingLength(sig)) if (!offset) offset = 0 const oldOffset = offset - const signature = Buffer.from(sig.signature, 'base64') + const signature = fromBuffer(sig.signature, 'base64') offset += 2 // Leave space for length - buf.writeUInt16BE(types.toType(sig.typeCovered), offset) + writeUInt16BE(buf, types.toType(sig.typeCovered), offset) offset += 2 - buf.writeUInt8(sig.algorithm, offset) + writeUInt8(buf, sig.algorithm, offset) offset += 1 - buf.writeUInt8(sig.labels, offset) + writeUInt8(buf, sig.labels, offset) offset += 1 - buf.writeUInt32BE(sig.originalTTL, offset) + writeUInt32BE(buf, sig.originalTTL, offset) offset += 4 - buf.writeUInt32BE(sig.expiration, offset) + writeUInt32BE(buf, sig.expiration, offset) offset += 4 - buf.writeUInt32BE(sig.inception, offset) + writeUInt32BE(buf, sig.inception, offset) offset += 4 - buf.writeUInt16BE(sig.keyTag, offset) + writeUInt16BE(buf, sig.keyTag, offset) offset += 2 name.encode(sig.signersName, buf, offset) offset += name.encode.bytes - signature.copy(buf, offset, 0, signature.length) + bufferCopy(signature, buf, offset, 0, signature.length) offset += signature.length rrrsig.encode.bytes = offset - oldOffset - buf.writeUInt16BE(rrrsig.encode.bytes - 2, oldOffset) + writeUInt16BE(buf, rrrsig.encode.bytes - 2, oldOffset) return buf } @@ -980,21 +997,21 @@ rrrsig.decode = function (buf, offset) { const oldOffset = offset var sig = {} - var length = buf.readUInt16BE(offset) + var length = readUInt16BE(buf, offset) offset += 2 - sig.typeCovered = types.toString(buf.readUInt16BE(offset)) + sig.typeCovered = types.toString(readUInt16BE(buf, offset)) offset += 2 - sig.algorithm = buf.readUInt8(offset) + sig.algorithm = readUInt8(buf, offset) offset += 1 - sig.labels = buf.readUInt8(offset) + sig.labels = readUInt8(buf, offset) offset += 1 - sig.originalTTL = buf.readUInt32BE(offset) + sig.originalTTL = readUInt32BE(buf, offset) offset += 4 - sig.expiration = buf.readUInt32BE(offset) + sig.expiration = readUInt32BE(buf, offset) offset += 4 - sig.inception = buf.readUInt32BE(offset) + sig.inception = readUInt32BE(buf, offset) offset += 4 - sig.keyTag = buf.readUInt16BE(offset) + sig.keyTag = readUInt16BE(buf, offset) offset += 2 sig.signersName = name.decode(buf, offset) offset += name.decode.bytes @@ -1009,13 +1026,13 @@ rrrsig.decode.bytes = 0 rrrsig.encodingLength = function (sig) { return 20 + name.encodingLength(sig.signersName) + - Buffer.byteLength(sig.signature, 'base64') + byteLength(sig.signature, 'base64') } const rrp = exports.rp = {} rrp.encode = function (data, buf, offset) { - if (!buf) buf = Buffer.alloc(rrp.encodingLength(data)) + if (!buf) buf = allocBuffer(rrp.encodingLength(data)) if (!offset) offset = 0 const oldOffset = offset @@ -1025,7 +1042,7 @@ rrp.encode = function (data, buf, offset) { name.encode(data.txt || '.', buf, offset) offset += name.encode.bytes rrp.encode.bytes = offset - oldOffset - buf.writeUInt16BE(rrp.encode.bytes - 2, oldOffset) + writeUInt16BE(buf, rrp.encode.bytes - 2, oldOffset) return buf } @@ -1054,7 +1071,7 @@ rrp.encodingLength = function (data) { const typebitmap = {} typebitmap.encode = function (typelist, buf, offset) { - if (!buf) buf = Buffer.alloc(typebitmap.encodingLength(typelist)) + if (!buf) buf = allocBuffer(typebitmap.encodingLength(typelist)) if (!offset) offset = 0 const oldOffset = offset @@ -1069,12 +1086,12 @@ typebitmap.encode = function (typelist, buf, offset) { for (i = 0; i < typesByWindow.length; i++) { if (typesByWindow[i] !== undefined) { - var windowBuf = Buffer.from(typesByWindow[i]) - buf.writeUInt8(i, offset) + var windowBuf = fromBuffer(typesByWindow[i]) + writeUInt8(buf, i, offset) offset += 1 - buf.writeUInt8(windowBuf.length, offset) + writeUInt8(buf, windowBuf.length, offset) offset += 1 - windowBuf.copy(buf, offset) + bufferCopy(windowBuf, buf, offset) offset += windowBuf.length } } @@ -1091,12 +1108,12 @@ typebitmap.decode = function (buf, offset, length) { var typelist = [] while (offset - oldOffset < length) { - var window = buf.readUInt8(offset) + var window = readUInt8(buf, offset) offset += 1 - var windowLength = buf.readUInt8(offset) + var windowLength = readUInt8(buf, offset) offset += 1 for (var i = 0; i < windowLength; i++) { - var b = buf.readUInt8(offset + i) + var b = readUInt8(buf, offset + i) for (var j = 0; j < 8; j++) { if (b & (1 << (7 - j))) { var typeid = types.toString((window << 8) | (i << 3) | j) @@ -1133,7 +1150,7 @@ typebitmap.encodingLength = function (typelist) { const rnsec = exports.nsec = {} rnsec.encode = function (record, buf, offset) { - if (!buf) buf = Buffer.alloc(rnsec.encodingLength(record)) + if (!buf) buf = allocBuffer(rnsec.encodingLength(record)) if (!offset) offset = 0 const oldOffset = offset @@ -1144,7 +1161,7 @@ rnsec.encode = function (record, buf, offset) { offset += typebitmap.encode.bytes rnsec.encode.bytes = offset - oldOffset - buf.writeUInt16BE(rnsec.encode.bytes - 2, oldOffset) + writeUInt16BE(buf, rnsec.encode.bytes - 2, oldOffset) return buf } @@ -1155,7 +1172,7 @@ rnsec.decode = function (buf, offset) { const oldOffset = offset var record = {} - var length = buf.readUInt16BE(offset) + var length = readUInt16BE(buf, offset) offset += 2 record.nextDomain = name.decode(buf, offset) offset += name.decode.bytes @@ -1177,40 +1194,40 @@ rnsec.encodingLength = function (record) { const rnsec3 = exports.nsec3 = {} rnsec3.encode = function (record, buf, offset) { - if (!buf) buf = Buffer.alloc(rnsec3.encodingLength(record)) + if (!buf) buf = allocBuffer(rnsec3.encodingLength(record)) if (!offset) offset = 0 const oldOffset = offset const salt = record.salt - if (!Buffer.isBuffer(salt)) { + if (!isBuffer(salt)) { throw new Error('salt must be a Buffer') } const nextDomain = record.nextDomain - if (!Buffer.isBuffer(nextDomain)) { + if (!isBuffer(nextDomain)) { throw new Error('nextDomain must be a Buffer') } offset += 2 // Leave space for length - buf.writeUInt8(record.algorithm, offset) + writeUInt8(buf, record.algorithm, offset) offset += 1 - buf.writeUInt8(record.flags, offset) + writeUInt8(buf, record.flags, offset) offset += 1 - buf.writeUInt16BE(record.iterations, offset) + writeUInt16BE(buf, record.iterations, offset) offset += 2 - buf.writeUInt8(salt.length, offset) + writeUInt8(buf, salt.length, offset) offset += 1 - salt.copy(buf, offset, 0, salt.length) + bufferCopy(salt, buf, offset, 0, salt.length) offset += salt.length - buf.writeUInt8(nextDomain.length, offset) + writeUInt8(buf, nextDomain.length, offset) offset += 1 - nextDomain.copy(buf, offset, 0, nextDomain.length) + bufferCopy(nextDomain, buf, offset, 0, nextDomain.length) offset += nextDomain.length typebitmap.encode(record.rrtypes, buf, offset) offset += typebitmap.encode.bytes rnsec3.encode.bytes = offset - oldOffset - buf.writeUInt16BE(rnsec3.encode.bytes - 2, oldOffset) + writeUInt16BE(buf, rnsec3.encode.bytes - 2, oldOffset) return buf } @@ -1221,19 +1238,19 @@ rnsec3.decode = function (buf, offset) { const oldOffset = offset var record = {} - var length = buf.readUInt16BE(offset) + var length = readUInt16BE(buf, offset) offset += 2 - record.algorithm = buf.readUInt8(offset) + record.algorithm = readUInt8(buf, offset) offset += 1 - record.flags = buf.readUInt8(offset) + record.flags = readUInt8(buf, offset) offset += 1 - record.iterations = buf.readUInt16BE(offset) + record.iterations = readUInt16BE(buf, offset) offset += 2 - const saltLength = buf.readUInt8(offset) + const saltLength = readUInt8(buf, offset) offset += 1 record.salt = buf.slice(offset, offset + saltLength) offset += saltLength - const hashLength = buf.readUInt8(offset) + const hashLength = readUInt8(buf, offset) offset += 1 record.nextDomain = buf.slice(offset, offset + hashLength) offset += hashLength @@ -1256,24 +1273,24 @@ rnsec3.encodingLength = function (record) { const rds = exports.ds = {} rds.encode = function (digest, buf, offset) { - if (!buf) buf = Buffer.alloc(rds.encodingLength(digest)) + if (!buf) buf = allocBuffer(rds.encodingLength(digest)) if (!offset) offset = 0 const oldOffset = offset - const digestdata = Buffer.from(digest.digest, 'hex') + const digestdata = fromBuffer(digest.digest, 'hex') offset += 2 // Leave space for length - buf.writeUInt16BE(digest.keyTag, offset) + writeUInt16BE(buf, digest.keyTag, offset) offset += 2 - buf.writeUInt8(digest.algorithm, offset) + writeUInt8(buf, digest.algorithm, offset) offset += 1 - buf.writeUInt8(digest.digestType, offset) + writeUInt8(buf, digest.digestType, offset) offset += 1 - digestdata.copy(buf, offset, 0, digestdata.length) + bufferCopy(digestdata, buf, offset, 0, digestdata.length) offset += digestdata.length rds.encode.bytes = offset - oldOffset - buf.writeUInt16BE(rds.encode.bytes - 2, oldOffset) + writeUInt16BE(buf, rds.encode.bytes - 2, oldOffset) return buf } @@ -1284,13 +1301,13 @@ rds.decode = function (buf, offset) { const oldOffset = offset var digest = {} - var length = buf.readUInt16BE(offset) + var length = readUInt16BE(buf, offset) offset += 2 - digest.keyTag = buf.readUInt16BE(offset) + digest.keyTag = readUInt16BE(buf, offset) offset += 2 - digest.algorithm = buf.readUInt8(offset) + digest.algorithm = readUInt8(buf, offset) offset += 1 - digest.digestType = buf.readUInt8(offset) + digest.digestType = readUInt8(buf, offset) offset += 1 digest.digest = buf.slice(offset, oldOffset + length + 2) offset += digest.digest.length @@ -1301,7 +1318,7 @@ rds.decode = function (buf, offset) { rds.decode.bytes = 0 rds.encodingLength = function (digest) { - return 6 + Buffer.byteLength(digest.digest, 'hex') + return 6 + byteLength(digest.digest, 'hex') } const rsshfp = exports.sshfp = {} @@ -1314,7 +1331,7 @@ rsshfp.getFingerprintLengthForHashType = function getFingerprintLengthForHashTyp } rsshfp.encode = function encode (record, buf, offset) { - if (!buf) buf = Buffer.alloc(rsshfp.encodingLength(record)) + if (!buf) buf = allocBuffer(rsshfp.encodingLength(record)) if (!offset) offset = 0 const oldOffset = offset @@ -1324,15 +1341,15 @@ rsshfp.encode = function encode (record, buf, offset) { buf[offset] = record.hash offset += 1 - const fingerprintBuf = Buffer.from(record.fingerprint.toUpperCase(), 'hex') + const fingerprintBuf = fromBuffer(record.fingerprint.toUpperCase(), 'hex') if (fingerprintBuf.length !== rsshfp.getFingerprintLengthForHashType(record.hash)) { throw new Error('Invalid fingerprint length') } - fingerprintBuf.copy(buf, offset) + bufferCopy(fingerprintBuf, buf, offset) offset += fingerprintBuf.byteLength rsshfp.encode.bytes = offset - oldOffset - buf.writeUInt16BE(rsshfp.encode.bytes - 2, oldOffset) + writeUInt16BE(buf, rsshfp.encode.bytes - 2, oldOffset) return buf } @@ -1360,19 +1377,19 @@ rsshfp.decode = function decode (buf, offset) { rsshfp.decode.bytes = 0 rsshfp.encodingLength = function (record) { - return 4 + Buffer.from(record.fingerprint, 'hex').byteLength + return 4 + fromBuffer(record.fingerprint, 'hex').byteLength } const rnaptr = exports.naptr = {} rnaptr.encode = function (data, buf, offset) { - if (!buf) buf = Buffer.alloc(rnaptr.encodingLength(data)) + if (!buf) buf = allocBuffer(rnaptr.encodingLength(data)) if (!offset) offset = 0 const oldOffset = offset offset += 2 - buf.writeUInt16BE(data.order || 0, offset) + writeUInt16BE(buf, data.order || 0, offset) offset += 2 - buf.writeUInt16BE(data.preference || 0, offset) + writeUInt16BE(buf, data.preference || 0, offset) offset += 2 string.encode(data.flags, buf, offset) offset += string.encode.bytes @@ -1383,7 +1400,7 @@ rnaptr.encode = function (data, buf, offset) { name.encode(data.replacement, buf, offset) offset += name.encode.bytes rnaptr.encode.bytes = offset - oldOffset - buf.writeUInt16BE(rnaptr.encode.bytes - 2, oldOffset) + writeUInt16BE(buf, rnaptr.encode.bytes - 2, oldOffset) return buf } @@ -1394,9 +1411,9 @@ rnaptr.decode = function (buf, offset) { const oldOffset = offset const data = {} offset += 2 - data.order = buf.readUInt16BE(offset) + data.order = readUInt16BE(buf, offset) offset += 2 - data.preference = buf.readUInt16BE(offset) + data.preference = readUInt16BE(buf, offset) offset += 2 data.flags = string.decode(buf, offset) offset += string.decode.bytes @@ -1422,27 +1439,27 @@ rnaptr.encodingLength = function (data) { const rtlsa = exports.tlsa = {} rtlsa.encode = function (cert, buf, offset) { - if (!buf) buf = Buffer.alloc(rtlsa.encodingLength(cert)) + if (!buf) buf = allocBuffer(rtlsa.encodingLength(cert)) if (!offset) offset = 0 const oldOffset = offset const certdata = cert.certificate - if (!Buffer.isBuffer(certdata)) { + if (!isBuffer(certdata)) { throw new Error('Certificate must be a Buffer') } offset += 2 // Leave space for length - buf.writeUInt8(cert.usage, offset) + writeUInt8(buf, cert.usage, offset) offset += 1 - buf.writeUInt8(cert.selector, offset) + writeUInt8(buf, cert.selector, offset) offset += 1 - buf.writeUInt8(cert.matchingType, offset) + writeUInt8(buf, cert.matchingType, offset) offset += 1 - certdata.copy(buf, offset, 0, certdata.length) + bufferCopy(certdata, buf, offset, 0, certdata.length) offset += certdata.length rtlsa.encode.bytes = offset - oldOffset - buf.writeUInt16BE(rtlsa.encode.bytes - 2, oldOffset) + writeUInt16BE(buf, rtlsa.encode.bytes - 2, oldOffset) return buf } @@ -1453,13 +1470,13 @@ rtlsa.decode = function (buf, offset) { const oldOffset = offset const cert = {} - const length = buf.readUInt16BE(offset) + const length = readUInt16BE(buf, offset) offset += 2 - cert.usage = buf.readUInt8(offset) + cert.usage = readUInt8(buf, offset) offset += 1 - cert.selector = buf.readUInt8(offset) + cert.selector = readUInt8(buf, offset) offset += 1 - cert.matchingType = buf.readUInt8(offset) + cert.matchingType = readUInt8(buf, offset) offset += 1 cert.certificate = buf.slice(offset, oldOffset + length + 2) offset += cert.certificate.length @@ -1470,7 +1487,7 @@ rtlsa.decode = function (buf, offset) { rtlsa.decode.bytes = 0 rtlsa.encodingLength = function (cert) { - return 5 + Buffer.byteLength(cert.certificate) + return 5 + byteLength(cert.certificate) } const svcparam = exports.svcparam = {} @@ -1512,21 +1529,21 @@ svcparam.numberToKeyName = function (number) { } svcparam.encode = function (param, buf, offset) { - if (!buf) buf = Buffer.allocUnsafe(svcparam.encodingLength(param)) + if (!buf) buf = allocUnsafeBuffer(svcparam.encodingLength(param)) if (!offset) offset = 0 let key = param.key if (typeof param.key !== 'number') { key = svcparam.keyToNumber(param.key) } - buf.writeUInt16BE(key || 0, offset) + writeUInt16BE(buf, key || 0, offset) offset += 2 svcparam.encode.bytes = 2 if (key === 0) { // mandatory let values = param.value if (!Array.isArray(values)) values = [values] - buf.writeUInt16BE(values.length * 2, offset) + writeUInt16BE(buf, values.length * 2, offset) offset += 2 svcparam.encode.bytes += 2 @@ -1534,7 +1551,7 @@ svcparam.encode = function (param, buf, offset) { if (typeof val !== 'number') { val = svcparam.keyToNumber(val) } - buf.writeUInt16BE(val, offset) + writeUInt16BE(buf, val, offset) offset += 2 svcparam.encode.bytes += 2 } @@ -1548,34 +1565,34 @@ svcparam.encode = function (param, buf, offset) { return result }, val.length) - buf.writeUInt16BE(total, offset) + writeUInt16BE(buf, total, offset) offset += 2 svcparam.encode.bytes += 2 for (let id of val) { - buf.writeUInt8(id.length, offset) + writeUInt8(buf, id.length, offset) offset += 1 svcparam.encode.bytes += 1 - buf.write(id, offset) + writeString(buf, id, offset) offset += id.length svcparam.encode.bytes += id.length } } else if (key === 2) { // no-default-alpn - buf.writeUInt16BE(0, offset) + writeUInt16BE(buf, 0, offset) offset += 2 svcparam.encode.bytes += 2 } else if (key === 3) { // port - buf.writeUInt16BE(2, offset) + writeUInt16BE(buf, 2, offset) offset += 2 svcparam.encode.bytes += 2 - buf.writeUInt16BE(param.value || 0, offset) + writeUInt16BE(buf, param.value || 0, offset) offset += 2 svcparam.encode.bytes += 2 } else if (key === 4) { // ipv4hint let val = param.value if (!Array.isArray(val)) val = [val] - buf.writeUInt16BE(val.length * 4, offset) + writeUInt16BE(buf, val.length * 4, offset) offset += 2 svcparam.encode.bytes += 2 @@ -1586,26 +1603,26 @@ svcparam.encode = function (param, buf, offset) { } } else if (key === 5) { // echconfig if (svcparam.ech) { - buf.writeUInt16BE(svcparam.ech.length, offset) + writeUInt16BE(buf, svcparam.ech.length, offset) offset += 2 svcparam.encode.bytes += 2 for (let i = 0; i < svcparam.ech.length; i++) { - buf.writeUInt8(svcparam.ech[i], offset) + writeUInt8(buf, svcparam.ech[i], offset) offset++ } svcparam.encode.bytes += svcparam.ech.length } else { - buf.writeUInt16BE(param.value.length, offset) + writeUInt16BE(buf, param.value.length, offset) offset += 2 svcparam.encode.bytes += 2 - buf.write(param.value, offset) + writeString(buf, param.value, offset) offset += param.value.length svcparam.encode.bytes += param.value.length } } else if (key === 6) { // ipv6hint let val = param.value if (!Array.isArray(val)) val = [val] - buf.writeUInt16BE(val.length * 16, offset) + writeUInt16BE(buf, val.length * 16, offset) offset += 2 svcparam.encode.bytes += 2 @@ -1615,35 +1632,35 @@ svcparam.encode = function (param, buf, offset) { svcparam.encode.bytes += 16 } } else if (key === 7) { // dohpath - buf.writeUInt16BE(param.value.length, offset) + writeUInt16BE(buf, param.value.length, offset) offset += 2 svcparam.encode.bytes += 2 - buf.write(param.value, offset) + writeString(buf, param.value, offset) offset += param.value.length svcparam.encode.bytes += param.value.length } else if (key === 32769) { // odoh if (svcparam.odoh) { - buf.writeUInt16BE(svcparam.odoh.length, offset) + writeUInt16BE(buf, svcparam.odoh.length, offset) offset += 2 svcparam.encode.bytes += 2 for (let i = 0; i < svcparam.odoh.length; i++) { - buf.writeUInt8(svcparam.odoh[i], offset) + writeUInt8(buf, svcparam.odoh[i], offset) offset++ } svcparam.encode.bytes += svcparam.odoh.length svcparam.odoh = null } else { - buf.writeUInt16BE(param.value.length, offset) + writeUInt16BE(buf, param.value.length, offset) offset += 2 svcparam.encode.bytes += 2 - buf.write(param.value, offset) + writeString(buf, param.value, offset) offset += param.value.length svcparam.encode.bytes += param.value.length } } else { // XXX: why would we ever succeed here, why not fail with an exception to let the user know? // Unknown option - buf.writeUInt16BE(0, offset) // 0 length since we don't know how to encode + writeUInt16BE(buf, 0, offset) // 0 length since we don't know how to encode offset += 2 svcparam.encode.bytes += 2 } @@ -1654,12 +1671,12 @@ svcparam.encode.bytes = 0 svcparam.decode = function (buf, offset) { if (!offset) offset = 0 let param = {} - let id = buf.readUInt16BE(offset) + let id = readUInt16BE(buf, offset) param.key = svcparam.numberToKeyName(id) offset += 2 svcparam.decode.bytes = 2 - let len = buf.readUInt16BE(offset) + let len = readUInt16BE(buf, offset) offset += 2 svcparam.decode.bytes += 2 @@ -1671,7 +1688,7 @@ svcparam.decode = function (buf, offset) { let paramsoff = offset let rem = len while (rem >= 2) { - let paramtype = buf.readUInt16BE(paramsoff) + let paramtype = readUInt16BE(buf, paramsoff) mp.push(svcparam.numberToKeyName(paramtype)) paramsoff += 2 rem -= 2 @@ -1685,13 +1702,13 @@ svcparam.decode = function (buf, offset) { let nameoff = offset let rem = len while (rem >= 2) { - const namelen = buf.readUInt8(nameoff) + const namelen = readUInt8(buf, nameoff) nameoff++ rem-- if (namelen > rem) { throw new Error(`Invalid SVCB param ALPN length: ${namelen}. Not enough space left in buffer`) } - names.push(buf.toString('utf-8', nameoff, nameoff + namelen)) + names.push(bufferToString(buf, 'utf-8', nameoff, nameoff + namelen)) nameoff += namelen rem -= namelen } @@ -1703,7 +1720,7 @@ svcparam.decode = function (buf, offset) { param.value = 0 break case 'port': - param.value = buf.readUInt16BE(offset) + param.value = readUInt16BE(buf, offset) break case 'ipv4hint': { @@ -1733,7 +1750,7 @@ svcparam.decode = function (buf, offset) { break case 'dohpath': default: - param.value = buf.toString('utf-8', offset, offset + len) + param.value = bufferToString(buf, 'utf-8', offset, offset + len) break } @@ -1764,7 +1781,7 @@ svcparam.encodingLength = function (param) { case 'ipv4hint' : return 4 + 4 * (Array.isArray(param.value) ? param.value.length : 1) case 'echconfig' : { if (param.needBase64Decode) { - svcparam.ech = Buffer.from(param.value, 'base64') + svcparam.ech = fromBuffer(param.value, 'base64') return 4 + svcparam.ech.length } return 4 + param.value.length @@ -1772,7 +1789,7 @@ svcparam.encodingLength = function (param) { case 'ipv6hint' : return 4 + 16 * (Array.isArray(param.value) ? param.value.length : 1) case 'odoh' : { if (param.needBase64Decode) { - svcparam.odoh = Buffer.from(param.value, 'base64') + svcparam.odoh = fromBuffer(param.value, 'base64') return 4 + svcparam.odoh.length } return 4 + param.value.length @@ -1788,13 +1805,13 @@ svcparam.encodingLength = function (param) { const rhttpssvc = exports.httpssvc = {} rhttpssvc.encode = function (data, buf, offset) { - if (!buf) buf = Buffer.allocUnsafe(rhttpssvc.encodingLength(data)) + if (!buf) buf = allocUnsafeBuffer(rhttpssvc.encodingLength(data)) if (!offset) offset = 0 - buf.writeUInt16BE(rhttpssvc.encodingLength(data) - 2, offset) + writeUInt16BE(buf, rhttpssvc.encodingLength(data) - 2, offset) offset += 2 - buf.writeUInt16BE(data.priority || 0, offset) + writeUInt16BE(buf, data.priority || 0, offset) rhttpssvc.encode.bytes = 4 offset += 2 name.encode(data.name, buf, offset) @@ -1817,10 +1834,10 @@ rhttpssvc.encode.bytes = 0 rhttpssvc.decode = function (buf, offset) { if (!offset) offset = 0 - let rdlen = buf.readUInt16BE(offset) + let rdlen = readUInt16BE(buf, offset) offset += 2 let record = {} - record.priority = buf.readUInt16BE(offset) + record.priority = readUInt16BE(buf, offset) offset += 2 rhttpssvc.decode.bytes = 4 record.name = name.decode(buf, offset) @@ -1891,7 +1908,7 @@ const renc = exports.record = function (type) { const answer = exports.answer = {} answer.encode = function (a, buf, offset) { - if (!buf) buf = Buffer.alloc(answer.encodingLength(a)) + if (!buf) buf = allocBuffer(answer.encodingLength(a)) if (!offset) offset = 0 const oldOffset = offset @@ -1899,16 +1916,16 @@ answer.encode = function (a, buf, offset) { name.encode(a.name, buf, offset) offset += name.encode.bytes - buf.writeUInt16BE(types.toType(a.type), offset) + writeUInt16BE(buf, types.toType(a.type), offset) if (a.type.toUpperCase() === 'OPT') { if (a.name !== '.') { throw new Error('OPT name must be root.') } - buf.writeUInt16BE(a.udpPayloadSize || 4096, offset + 2) - buf.writeUInt8(a.extendedRcode || 0, offset + 4) - buf.writeUInt8(a.ednsVersion || 0, offset + 5) - buf.writeUInt16BE(a.flags || 0, offset + 6) + writeUInt16BE(buf, a.udpPayloadSize || 4096, offset + 2) + writeUInt8(buf, a.extendedRcode || 0, offset + 4) + writeUInt8(buf, a.ednsVersion || 0, offset + 5) + writeUInt16BE(buf, a.flags || 0, offset + 6) offset += 8 ropt.encode(a.options || [], buf, offset) @@ -1916,8 +1933,8 @@ answer.encode = function (a, buf, offset) { } else { let klass = classes.toClass(a.class === undefined ? 'IN' : a.class) if (a.flush) klass |= FLUSH_MASK // the 1st bit of the class is the flush bit - buf.writeUInt16BE(klass, offset + 2) - buf.writeUInt32BE(a.ttl || 0, offset + 4) + writeUInt16BE(buf, klass, offset + 2) + writeUInt32BE(buf, a.ttl || 0, offset + 4) offset += 8 const enc = renc(a.type) @@ -1939,18 +1956,18 @@ answer.decode = function (buf, offset) { a.name = name.decode(buf, offset) offset += name.decode.bytes - a.type = types.toString(buf.readUInt16BE(offset)) + a.type = types.toString(readUInt16BE(buf, offset)) if (a.type === 'OPT') { - a.udpPayloadSize = buf.readUInt16BE(offset + 2) - a.extendedRcode = buf.readUInt8(offset + 4) - a.ednsVersion = buf.readUInt8(offset + 5) - a.flags = buf.readUInt16BE(offset + 6) + a.udpPayloadSize = readUInt16BE(buf, offset + 2) + a.extendedRcode = readUInt8(buf, offset + 4) + a.ednsVersion = readUInt8(buf, offset + 5) + a.flags = readUInt16BE(buf, offset + 6) a.flag_do = ((a.flags >> 15) & 0x1) === 1 a.options = ropt.decode(buf, offset + 8) offset += 8 + ropt.decode.bytes } else { - const klass = buf.readUInt16BE(offset + 2) - a.ttl = buf.readUInt32BE(offset + 4) + const klass = readUInt16BE(buf, offset + 2) + a.ttl = readUInt32BE(buf, offset + 4) a.class = classes.toString(klass & NOT_FLUSH_MASK) a.flush = !!(klass & FLUSH_MASK) @@ -1974,7 +1991,7 @@ answer.encodingLength = function (a) { const question = exports.question = {} question.encode = function (q, buf, offset) { - if (!buf) buf = Buffer.alloc(question.encodingLength(q)) + if (!buf) buf = allocBuffer(question.encodingLength(q)) if (!offset) offset = 0 const oldOffset = offset @@ -1982,10 +1999,10 @@ question.encode = function (q, buf, offset) { name.encode(q.name, buf, offset) offset += name.encode.bytes - buf.writeUInt16BE(types.toType(q.type), offset) + writeUInt16BE(buf, types.toType(q.type), offset) offset += 2 - buf.writeUInt16BE(((q.qu_bit === undefined || !q.qu_bit) ? 0 : QU_MASK) | classes.toClass(q.class === undefined ? 'IN' : q.class), offset) + writeUInt16BE(buf, ((q.qu_bit === undefined || !q.qu_bit) ? 0 : QU_MASK) | classes.toClass(q.class === undefined ? 'IN' : q.class), offset) offset += 2 question.encode.bytes = offset - oldOffset @@ -2003,11 +2020,11 @@ question.decode = function (buf, offset) { q.name = name.decode(buf, offset) offset += name.decode.bytes - q.type = types.toString(buf.readUInt16BE(offset)) + q.type = types.toString(readUInt16BE(buf, offset)) offset += 2 - q.qu_bit = (buf.readUInt16BE(offset) & QU_MASK) !== 0 - q.class = q.qu_bit ? classes.toString(buf.readUInt16BE(offset) - QU_MASK) : classes.toString(buf.readUInt16BE(offset)) + q.qu_bit = (readUInt16BE(buf, offset) & QU_MASK) !== 0 + q.class = q.qu_bit ? classes.toString(readUInt16BE(buf, offset) - QU_MASK) : classes.toString(readUInt16BE(buf, offset)) offset += 2 question.decode.bytes = offset - oldOffset @@ -2032,7 +2049,7 @@ exports.NXDOMAIN = 0x03 exports.encode = function (result, buf, offset) { const allocing = !buf - if (allocing) buf = Buffer.alloc(exports.encodingLength(result)) + if (allocing) buf = allocBuffer(exports.encodingLength(result)) if (!offset) offset = 0 const oldOffset = offset @@ -2091,9 +2108,9 @@ exports.encodingLength = function (result) { exports.streamEncode = function (result) { const buf = exports.encode(result) - const sbuf = Buffer.alloc(2) - sbuf.writeUInt16BE(buf.byteLength) - const combine = Buffer.concat([sbuf, buf]) + const sbuf = allocBuffer(2) + writeUInt16BE(sbuf, buf.byteLength) + const combine = bufferConcat([sbuf, buf]) exports.streamEncode.bytes = combine.byteLength return combine } @@ -2101,7 +2118,7 @@ exports.streamEncode = function (result) { exports.streamEncode.bytes = 0 exports.streamDecode = function (sbuf) { - const len = sbuf.readUInt16BE(0) + const len = readUInt16BE(sbuf, 0) if (sbuf.byteLength < len + 2) { // not enough data return null diff --git a/test.js b/test.js index 5206f9e..1d2ccf8 100644 --- a/test.js +++ b/test.js @@ -11,7 +11,7 @@ const ip = require('@leichtgewicht/ip-codec') const hexdump = require('./hexdump') tape('unknown', function (t) { - testEncoder(t, packet.unknown, Buffer.from('hello world')) + testEncoder(t, packet.unknown, new Uint8Array('hello world')) t.end() }) @@ -19,9 +19,9 @@ tape('txt', function (t) { testEncoder(t, packet.txt, []) testEncoder(t, packet.txt, ['hello world']) testEncoder(t, packet.txt, ['hello', 'world']) - testEncoder(t, packet.txt, [Buffer.from([0, 1, 2, 3, 4, 5])]) - testEncoder(t, packet.txt, ['a', 'b', Buffer.from([0, 1, 2, 3, 4, 5])]) - testEncoder(t, packet.txt, ['', Buffer.allocUnsafe(0)]) + testEncoder(t, packet.txt, [new Uint8Array([0, 1, 2, 3, 4, 5])]) + testEncoder(t, packet.txt, ['a', 'b', new Uint8Array([0, 1, 2, 3, 4, 5])]) + testEncoder(t, packet.txt, ['', new Uint8Array(0)]) t.end() }) @@ -29,16 +29,17 @@ tape('txt-scalar-string', function (t) { const buf = packet.txt.encode('hi') const val = packet.txt.decode(buf) t.ok(val.length === 1, 'array length') - t.ok(val[0].toString() === 'hi', 'data') + const decoded = val[0] instanceof Uint8Array ? new TextDecoder().decode(val[0]) : val[0].toString() + t.ok(decoded === 'hi', 'data') t.end() }) tape('txt-scalar-buffer', function (t) { - const data = Buffer.from([0, 1, 2, 3, 4, 5]) + const data = new Uint8Array([0, 1, 2, 3, 4, 5]) const buf = packet.txt.encode(data) const val = packet.txt.decode(buf) t.ok(val.length === 1, 'array length') - t.ok(val[0].equals(data), 'data') + t.ok(compare(t, val[0], data), 'data') t.end() }) @@ -50,7 +51,7 @@ tape('txt-invalid-data', function (t) { }) tape('null', function (t) { - testEncoder(t, packet.null, Buffer.from([0, 1, 2, 3, 4, 5])) + testEncoder(t, packet.null, new Uint8Array([0, 1, 2, 3, 4, 5])) t.end() }) @@ -259,7 +260,7 @@ tape('response', function (t) { answers: [{ type: 'NULL', name: 'hello.null.com', - data: Buffer.from([1, 2, 3, 4, 5]) + data: new Uint8Array([1, 2, 3, 4, 5]) }] }) @@ -315,7 +316,7 @@ tape('rcode', function (t) { tape('name_encoding', function (t) { let data = 'foo.example.com' - const buf = Buffer.allocUnsafe(255) + const buf = new Uint8Array(255) let offset = 0 packet.name.encode(data, buf, offset) t.ok(packet.name.encode.bytes === 17, 'name encoding length matches') @@ -384,40 +385,40 @@ tape('name_encoding', function (t) { tape('name_decoding', function (t) { // The two most significant bits of a valid label header must be either both zero or both one - t.throws(function () { packet.name.decode(Buffer.from([0x80])) }, /Cannot decode name \(bad label\)$/) - t.throws(function () { packet.name.decode(Buffer.from([0xb0])) }, /Cannot decode name \(bad label\)$/) + t.throws(function () { packet.name.decode(new Uint8Array([0x80])) }, /Cannot decode name \(bad label\)$/) + t.throws(function () { packet.name.decode(new Uint8Array([0xb0])) }, /Cannot decode name \(bad label\)$/) // Ensure there's enough buffer to read - t.throws(function () { packet.name.decode(Buffer.from([])) }, /Cannot decode name \(buffer overflow\)$/) - t.throws(function () { packet.name.decode(Buffer.from([0x01, 0x00])) }, /Cannot decode name \(buffer overflow\)$/) - t.throws(function () { packet.name.decode(Buffer.from([0x01])) }, /Cannot decode name \(buffer overflow\)$/) - t.throws(function () { packet.name.decode(Buffer.from([0xc0])) }, /Cannot decode name \(buffer overflow\)$/) + t.throws(function () { packet.name.decode(new Uint8Array([])) }, /Cannot decode name \(buffer overflow\)$/) + t.throws(function () { packet.name.decode(new Uint8Array([0x01, 0x00])) }, /Cannot decode name \(buffer overflow\)$/) + t.throws(function () { packet.name.decode(new Uint8Array([0x01])) }, /Cannot decode name \(buffer overflow\)$/) + t.throws(function () { packet.name.decode(new Uint8Array([0xc0])) }, /Cannot decode name \(buffer overflow\)$/) // Allow only pointers backwards - t.throws(function () { packet.name.decode(Buffer.from([0xc0, 0x00])) }, /Cannot decode name \(bad pointer\)$/) - t.throws(function () { packet.name.decode(Buffer.from([0xc0, 0x01])) }, /Cannot decode name \(bad pointer\)$/) + t.throws(function () { packet.name.decode(new Uint8Array([0xc0, 0x00])) }, /Cannot decode name \(bad pointer\)$/) + t.throws(function () { packet.name.decode(new Uint8Array([0xc0, 0x01])) }, /Cannot decode name \(bad pointer\)$/) // A name can be only 253 characters (when connected with dots) - const maxLength = Buffer.alloc(255) - maxLength.fill(Buffer.from([0x01, 0x61]), 0, 254) + const maxLength = new Uint8Array(255) + maxLength.fill(new Uint8Array([0x01, 0x61]), 0, 254) t.ok(packet.name.decode(maxLength) === new Array(127).fill('a').join('.')) - const tooLong = Buffer.alloc(256) - tooLong.fill(Buffer.from([0x01, 0x61])) + const tooLong = new Uint8Array(256) + tooLong.fill(new Uint8Array([0x01, 0x61])) t.throws(function () { packet.name.decode(tooLong) }, /Cannot decode name \(name too long\)$/) // Ensure jumps don't reset the total length counter - const tooLongWithJump = Buffer.alloc(403) - tooLongWithJump.fill(Buffer.from([0x01, 0x61]), 0, 200) - tooLongWithJump.fill(Buffer.from([0x01, 0x61]), 201, 401) + const tooLongWithJump = new Uint8Array(403) + tooLongWithJump.fill(new Uint8Array([0x01, 0x61]), 0, 200) + tooLongWithJump.fill(new Uint8Array([0x01, 0x61]), 201, 401) tooLongWithJump.set([0xc0, 0x00], 401) t.throws(function () { packet.name.decode(tooLongWithJump, 201) }, /Cannot decode name \(name too long\)$/) // Ensure a jump to a null byte doesn't add extra dots - t.ok(packet.name.decode(Buffer.from([0x00, 0x01, 0x61, 0xc0, 0x00]), 1) === 'a') + t.ok(packet.name.decode(new Uint8Array([0x00, 0x01, 0x61, 0xc0, 0x00]), 1) === 'a') // Ensure deeply nested pointers don't cause "Maximum call stack size exceeded" errors - const buf = Buffer.alloc(16386) + const buf = new Uint8Array(16386) for (let i = 0; i < 16384; i += 2) { buf.writeUInt16BE(0xc000 | i, i + 2) } @@ -519,7 +520,7 @@ tape('dnskey', function (t) { testEncoder(t, packet.dnskey, { flags: packet.dnskey.SECURE_ENTRYPOINT | packet.dnskey.ZONE_KEY, algorithm: 1, - key: Buffer.from([0, 1, 2, 3, 4, 5]) + key: new Uint8Array([0, 1, 2, 3, 4, 5]) }) t.end() }) @@ -534,12 +535,12 @@ tape('rrsig', function (t) { inception: 1233, keyTag: 2345, signersName: 'foo.com', - signature: Buffer.from([0, 1, 2, 3, 4, 5]) + signature: new Uint8Array([0, 1, 2, 3, 4, 5]) } testEncoder(t, packet.rrsig, testRRSIG) // Check the signature length is correct with extra junk at the end - const buf = Buffer.allocUnsafe(packet.rrsig.encodingLength(testRRSIG) + 4) + const buf = new Uint8Array(packet.rrsig.encodingLength(testRRSIG) + 4) packet.rrsig.encode(testRRSIG, buf) const val2 = packet.rrsig.decode(buf) t.ok(compare(t, testRRSIG, val2)) @@ -589,7 +590,7 @@ tape('nsec', function (t) { }) // Test with the sample NSEC from https://tools.ietf.org/html/rfc4034#section-4.3 - var sampleNSEC = Buffer.from('003704686f7374076578616d706c6503636f6d00' + + var sampleNSEC = new Uint8Array('003704686f7374076578616d706c6503636f6d00' + '0006400100000003041b000000000000000000000000000000000000000000000' + '000000020', 'hex') var decoded = packet.nsec.decode(sampleNSEC) @@ -608,8 +609,8 @@ tape('nsec3', function (t) { algorithm: 1, flags: 0, iterations: 257, - salt: Buffer.from([42, 42, 42]), - nextDomain: Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]), + salt: new Uint8Array([42, 42, 42]), + nextDomain: new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]), rrtypes: ['A', 'DNSKEY', 'CAA', 'DLV'] }) t.end() @@ -620,7 +621,7 @@ tape('ds', function (t) { keyTag: 1234, algorithm: 1, digestType: 1, - digest: Buffer.from([0, 1, 2, 3, 4, 5]) + digest: new Uint8Array([0, 1, 2, 3, 4, 5]) }) t.end() }) @@ -642,13 +643,13 @@ tape('tlsa', function (t) { usage: 3, selector: 1, matchingType: 1, - certificate: Buffer.from([0, 1, 2, 3, 4, 5]) + certificate: new Uint8Array([0, 1, 2, 3, 4, 5]) }) t.end() }) const unhexlify = (hex) => { - return Buffer.from(hex.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16) })) + return new Uint8Array(hex.match(/[\da-f]{2}/gi).map(function (h) { return parseInt(h, 16) })) } // @@ -1176,7 +1177,7 @@ tape('google resolver SVCB real world', function (t) { // tape('unpack', function (t) { - const buf = Buffer.from([ + const buf = new Uint8Array([ 0x00, 0x79, 0xde, 0xad, 0x85, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x02, 0x00, 0x02, 0x02, 0x6f, 0x6a, 0x05, @@ -1256,7 +1257,7 @@ function testEncoder (t, rpacket, val) { 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') - const bigger = Buffer.allocUnsafe(buf2.length + 10) + const bigger = new Uint8Array(buf2.length + 10) const buf3 = rpacket.encode(val, bigger, 10) const val4 = rpacket.decode(buf3, 10) @@ -1267,7 +1268,13 @@ function testEncoder (t, rpacket, val) { } function compare (t, a, b) { - if (Buffer.isBuffer(a)) return a.toString('hex') === b.toString('hex') + if (a instanceof Uint8Array && b instanceof Uint8Array) { + if (a.length !== b.length) return false + for (let i = 0; i < a.length; i++) { + if (a[i] !== b[i]) return false + } + return true + } if (typeof a === 'object' && a && b) { const keys = Object.keys(a) for (let i = 0; i < keys.length; i++) {