Skip to content

Commit

Permalink
Drop buffer (#139)
Browse files Browse the repository at this point in the history
* perf: deprecate buffer

* fix: types
  • Loading branch information
ThaUnknown authored Jan 31, 2023
1 parent dfe560a commit 26669b9
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 95 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import byteLength from './lib/encoding-length.js'
/**
* Determines the amount of bytes
* needed to encode the given value
* @param {Object|Array|Buffer|String|Number|Boolean} value
* @param {Object|Array|Uint8Array|String|Number|Boolean} value
* @return {Number} byteCount
*/
const encodingLength = byteLength
Expand Down
18 changes: 10 additions & 8 deletions lib/decode.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { arr2text, text2arr } from 'uint8-util'

const INTEGER_START = 0x69 // 'i'
const STRING_DELIM = 0x3A // ':'
const DICTIONARY_START = 0x64 // 'd'
Expand All @@ -8,7 +10,7 @@ const END_OF_TYPE = 0x65 // 'e'
* replaces parseInt(buffer.toString('ascii', start, end)).
* For strings with less then ~30 charachters, this is actually a lot faster.
*
* @param {Buffer} data
* @param {Uint8Array} data
* @param {Number} start
* @param {Number} end
* @return {Number} calculated number
Expand Down Expand Up @@ -48,11 +50,11 @@ function getIntFromBuffer (buffer, start, end) {
/**
* Decodes bencoded data.
*
* @param {Buffer} data
* @param {Uint8Array} data
* @param {Number} start (optional)
* @param {Number} end (optional)
* @param {String} encoding (optional)
* @return {Object|Array|Buffer|String|Number}
* @return {Object|Array|Uint8Array|String|Number}
*/
function decode (data, start, end, encoding) {
if (data == null || data.length === 0) {
Expand All @@ -72,9 +74,9 @@ function decode (data, start, end, encoding) {
decode.position = 0
decode.encoding = encoding || null

decode.data = !(Buffer.isBuffer(data))
? Buffer.from(data)
: data.slice(start, end)
decode.data = !(ArrayBuffer.isView(data))
? text2arr(data)
: new Uint8Array(data.slice(start, end))

decode.bytes = decode.data.length

Expand Down Expand Up @@ -122,7 +124,7 @@ decode.dictionary = function () {
const dict = {}

while (decode.data[decode.position] !== END_OF_TYPE) {
dict[decode.buffer()] = decode.next()
dict[arr2text(decode.buffer())] = decode.next()
}

decode.position++
Expand Down Expand Up @@ -161,7 +163,7 @@ decode.buffer = function () {
decode.position = end

return decode.encoding
? decode.data.toString(decode.encoding, sep, end)
? arr2text(decode.data.slice(sep, end))
: decode.data.slice(sep, end)
}

Expand Down
30 changes: 15 additions & 15 deletions lib/encode.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { concat, text2arr } from 'uint8-util'
import { getType } from './util.js'

/**
* Encodes data in bencode.
*
* @param {Buffer|Array|String|Object|Number|Boolean} data
* @return {Buffer}
* @param {Uint8Array|Array|String|Object|Number|Boolean} data
* @return {Uint8Array}
*/
function encode (data, buffer, offset) {
const buffers = []
let result = null

encode._encode(buffers, data)
result = Buffer.concat(buffers)
result = concat(buffers)
encode.bytes = result.length

if (Buffer.isBuffer(buffer)) {
result.copy(buffer, offset)
if (ArrayBuffer.isView(buffer)) {
buffer.set(result, offset)
return buffer
}

Expand All @@ -29,29 +30,28 @@ encode._encode = function (buffers, data) {
if (data == null) { return }

switch (getType(data)) {
case 'buffer': encode.buffer(buffers, data); break
case 'object': encode.dict(buffers, data); break
case 'map': encode.dictMap(buffers, data); break
case 'array': encode.list(buffers, data); break
case 'set': encode.listSet(buffers, data); break
case 'string': encode.string(buffers, data); break
case 'number': encode.number(buffers, data); break
case 'boolean': encode.number(buffers, data); break
case 'arraybufferview': encode.buffer(buffers, Buffer.from(data.buffer, data.byteOffset, data.byteLength)); break
case 'arraybuffer': encode.buffer(buffers, Buffer.from(data)); break
case 'arraybufferview': encode.buffer(buffers, new Uint8Array(data.buffer, data.byteOffset, data.byteLength)); break
case 'arraybuffer': encode.buffer(buffers, new Uint8Array(data)); break
}
}

const buffE = Buffer.from('e')
const buffD = Buffer.from('d')
const buffL = Buffer.from('l')
const buffE = new Uint8Array([0x65])
const buffD = new Uint8Array([0x64])
const buffL = new Uint8Array([0x6C])

encode.buffer = function (buffers, data) {
buffers.push(Buffer.from(data.length + ':'), data)
buffers.push(text2arr(data.length + ':'), data)
}

encode.string = function (buffers, data) {
buffers.push(Buffer.from(Buffer.byteLength(data) + ':' + data))
buffers.push(text2arr(text2arr(data).byteLength + ':' + data))
}

encode.number = function (buffers, data) {
Expand All @@ -60,7 +60,7 @@ encode.number = function (buffers, data) {
const lo = (data % maxLo) << 0
const val = hi * maxLo + lo

buffers.push(Buffer.from('i' + val + 'e'))
buffers.push(text2arr('i' + val + 'e'))

if (val !== data && !encode._floatConversionDetected) {
encode._floatConversionDetected = true
Expand Down Expand Up @@ -98,7 +98,7 @@ encode.dictMap = function (buffers, data) {

for (const key of keys) {
if (data.get(key) == null) continue
Buffer.isBuffer(key)
ArrayBuffer.isView(key)
? encode._encode(buffers, key)
: encode.string(buffers, String(key))
encode._encode(buffers, data.get(key))
Expand Down
8 changes: 4 additions & 4 deletions lib/encoding-length.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { text2arr } from 'uint8-util'
import { digitCount, getType } from './util.js'

function listLength (list) {
Expand All @@ -14,7 +15,7 @@ function mapLength (map) {
let length = 1 + 1 // type marker + end-of-type marker

for (const [key, value] of map) {
const keyLength = Buffer.byteLength(key)
const keyLength = text2arr(key).byteLength
length += digitCount(keyLength) + 1 + keyLength
length += encodingLength(value)
}
Expand All @@ -27,7 +28,7 @@ function objectLength (value) {
const keys = Object.keys(value)

for (let i = 0; i < keys.length; i++) {
const keyLength = Buffer.byteLength(keys[i])
const keyLength = text2arr(keys[i]).byteLength
length += digitCount(keyLength) + 1 + keyLength
length += encodingLength(value[keys[i]])
}
Expand All @@ -36,7 +37,7 @@ function objectLength (value) {
}

function stringLength (value) {
const length = Buffer.byteLength(value)
const length = text2arr(value).byteLength
return digitCount(length) + 1 + length
}

Expand All @@ -53,7 +54,6 @@ function encodingLength (value) {
const type = getType(value)

switch (type) {
case 'buffer': return digitCount(value.length) + 1 + value.length
case 'arraybufferview': return arrayBufferLength(value)
case 'string': return stringLength(value)
case 'array': case 'set': return listLength(value)
Expand Down
1 change: 0 additions & 1 deletion lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export function digitCount (value) {
}

export function getType (value) {
if (Buffer.isBuffer(value)) return 'buffer'
if (ArrayBuffer.isView(value)) return 'arraybufferview'
if (Array.isArray(value)) return 'array'
if (value instanceof Number) return 'number'
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,8 @@
},
"release": {
"extends": "@webtorrent/semantic-release-config"
},
"dependencies": {
"uint8-util": "^2.1.6"
}
}
6 changes: 1 addition & 5 deletions test/BEP-0023.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ test('BEP 0023', function (t) {
const announce = fs.readFileSync(filename)
const data = bencode.decode(announce)

console.log(data)

t.plan(1)
t.deepEqual(data, {
complete: 4,
incomplete: 3,
interval: 1800,
'min interval': 1800,
peers: Buffer.from('2ebd1b641a1f51d54c0546cc342190401a1f626ee9c6c8d5cb0d92131a1fac4e689a3c6b180f3d5746db', 'hex')
peers: new Uint8Array(Buffer.from('2ebd1b641a1f51d54c0546cc342190401a1f626ee9c6c8d5cb0d92131a1fac4e689a3c6b180f3d5746db', 'hex'))
})
})

Expand All @@ -31,8 +29,6 @@ test('BEP 0023', function (t) {
const announce = fs.readFileSync(filename)
const data = bencode.decode(announce, 'utf8')

console.log(data)

t.plan(1)
t.deepEqual(data, {
complete: 4,
Expand Down
30 changes: 15 additions & 15 deletions test/decode.buffer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ test('bencode#decode(x)', function (t) {

t.test('should be able to decode a string', function (t) {
t.plan(2)
t.deepEqual(bencode.decode('5:asdfe'), Buffer.from('asdfe'))
t.deepEqual(bencode.decode(data.binResultData.toString()), data.binStringData)
t.deepEqual(bencode.decode('5:asdfe'), new Uint8Array(Buffer.from('asdfe')))
t.deepEqual(bencode.decode(data.binResultData.toString()), new Uint8Array(data.binStringData))
})

t.test('should be able to decode "binary keys"', function (t) {
Expand All @@ -48,25 +48,25 @@ test('bencode#decode(x)', function (t) {
t.deepEqual(
bencode.decode('d3:cow3:moo4:spam4:eggse'),
{
cow: Buffer.from('moo'),
spam: Buffer.from('eggs')
cow: new Uint8Array(Buffer.from('moo')),
spam: new Uint8Array(Buffer.from('eggs'))
}
)
t.deepEqual(
bencode.decode('d4:spaml1:a1:bee'),
{
spam: [
Buffer.from('a'),
Buffer.from('b')
new Uint8Array(Buffer.from('a')),
new Uint8Array(Buffer.from('b'))
]
}
)
t.deepEqual(
bencode.decode('d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee'),
{
publisher: Buffer.from('bob'),
'publisher-webpage': Buffer.from('www.example.com'),
'publisher.location': Buffer.from('home')
publisher: new Uint8Array(Buffer.from('bob')),
'publisher-webpage': new Uint8Array(Buffer.from('www.example.com')),
'publisher.location': new Uint8Array(Buffer.from('home'))
}
)
})
Expand All @@ -75,13 +75,13 @@ test('bencode#decode(x)', function (t) {
t.plan(1)
t.deepEqual(
bencode.decode('l4:spam4:eggse'),
[Buffer.from('spam'),
Buffer.from('eggs')]
[new Uint8Array(Buffer.from('spam')),
new Uint8Array(Buffer.from('eggs'))]
)
})
t.test('should return the correct type', function (t) {
t.plan(1)
t.ok(Buffer.isBuffer(bencode.decode('4:öö')))
t.ok(ArrayBuffer.isView(bencode.decode('4:öö')))
})
t.test('should be able to decode stuff in dicts (issue #12)', function (t) {
t.plan(4)
Expand All @@ -96,8 +96,8 @@ test('bencode#decode(x)', function (t) {
const result = bencode.encode(someData)
const dat = bencode.decode(result)
t.equal(dat.integer, 12345)
t.deepEqual(dat.string, Buffer.from('Hello World'))
t.deepEqual(dat.dict.key, Buffer.from('This is a string within a dictionary'))
t.deepEqual(dat.list, [1, 2, 3, 4, Buffer.from('string'), 5, {}])
t.deepEqual(dat.string, new Uint8Array(Buffer.from('Hello World')))
t.deepEqual(dat.dict.key, new Uint8Array(Buffer.from('This is a string within a dictionary')))
t.deepEqual(dat.list, [1, 2, 3, 4, new Uint8Array(Buffer.from('string')), 5, {}])
})
})
Loading

0 comments on commit 26669b9

Please sign in to comment.