Skip to content

Commit 26669b9

Browse files
authored
Drop buffer (#139)
* perf: deprecate buffer * fix: types
1 parent dfe560a commit 26669b9

File tree

10 files changed

+95
-95
lines changed

10 files changed

+95
-95
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import byteLength from './lib/encoding-length.js'
44
/**
55
* Determines the amount of bytes
66
* needed to encode the given value
7-
* @param {Object|Array|Buffer|String|Number|Boolean} value
7+
* @param {Object|Array|Uint8Array|String|Number|Boolean} value
88
* @return {Number} byteCount
99
*/
1010
const encodingLength = byteLength

lib/decode.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { arr2text, text2arr } from 'uint8-util'
2+
13
const INTEGER_START = 0x69 // 'i'
24
const STRING_DELIM = 0x3A // ':'
35
const DICTIONARY_START = 0x64 // 'd'
@@ -8,7 +10,7 @@ const END_OF_TYPE = 0x65 // 'e'
810
* replaces parseInt(buffer.toString('ascii', start, end)).
911
* For strings with less then ~30 charachters, this is actually a lot faster.
1012
*
11-
* @param {Buffer} data
13+
* @param {Uint8Array} data
1214
* @param {Number} start
1315
* @param {Number} end
1416
* @return {Number} calculated number
@@ -48,11 +50,11 @@ function getIntFromBuffer (buffer, start, end) {
4850
/**
4951
* Decodes bencoded data.
5052
*
51-
* @param {Buffer} data
53+
* @param {Uint8Array} data
5254
* @param {Number} start (optional)
5355
* @param {Number} end (optional)
5456
* @param {String} encoding (optional)
55-
* @return {Object|Array|Buffer|String|Number}
57+
* @return {Object|Array|Uint8Array|String|Number}
5658
*/
5759
function decode (data, start, end, encoding) {
5860
if (data == null || data.length === 0) {
@@ -72,9 +74,9 @@ function decode (data, start, end, encoding) {
7274
decode.position = 0
7375
decode.encoding = encoding || null
7476

75-
decode.data = !(Buffer.isBuffer(data))
76-
? Buffer.from(data)
77-
: data.slice(start, end)
77+
decode.data = !(ArrayBuffer.isView(data))
78+
? text2arr(data)
79+
: new Uint8Array(data.slice(start, end))
7880

7981
decode.bytes = decode.data.length
8082

@@ -122,7 +124,7 @@ decode.dictionary = function () {
122124
const dict = {}
123125

124126
while (decode.data[decode.position] !== END_OF_TYPE) {
125-
dict[decode.buffer()] = decode.next()
127+
dict[arr2text(decode.buffer())] = decode.next()
126128
}
127129

128130
decode.position++
@@ -161,7 +163,7 @@ decode.buffer = function () {
161163
decode.position = end
162164

163165
return decode.encoding
164-
? decode.data.toString(decode.encoding, sep, end)
166+
? arr2text(decode.data.slice(sep, end))
165167
: decode.data.slice(sep, end)
166168
}
167169

lib/encode.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
1+
import { concat, text2arr } from 'uint8-util'
12
import { getType } from './util.js'
23

34
/**
45
* Encodes data in bencode.
56
*
6-
* @param {Buffer|Array|String|Object|Number|Boolean} data
7-
* @return {Buffer}
7+
* @param {Uint8Array|Array|String|Object|Number|Boolean} data
8+
* @return {Uint8Array}
89
*/
910
function encode (data, buffer, offset) {
1011
const buffers = []
1112
let result = null
1213

1314
encode._encode(buffers, data)
14-
result = Buffer.concat(buffers)
15+
result = concat(buffers)
1516
encode.bytes = result.length
1617

17-
if (Buffer.isBuffer(buffer)) {
18-
result.copy(buffer, offset)
18+
if (ArrayBuffer.isView(buffer)) {
19+
buffer.set(result, offset)
1920
return buffer
2021
}
2122

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

3132
switch (getType(data)) {
32-
case 'buffer': encode.buffer(buffers, data); break
3333
case 'object': encode.dict(buffers, data); break
3434
case 'map': encode.dictMap(buffers, data); break
3535
case 'array': encode.list(buffers, data); break
3636
case 'set': encode.listSet(buffers, data); break
3737
case 'string': encode.string(buffers, data); break
3838
case 'number': encode.number(buffers, data); break
3939
case 'boolean': encode.number(buffers, data); break
40-
case 'arraybufferview': encode.buffer(buffers, Buffer.from(data.buffer, data.byteOffset, data.byteLength)); break
41-
case 'arraybuffer': encode.buffer(buffers, Buffer.from(data)); break
40+
case 'arraybufferview': encode.buffer(buffers, new Uint8Array(data.buffer, data.byteOffset, data.byteLength)); break
41+
case 'arraybuffer': encode.buffer(buffers, new Uint8Array(data)); break
4242
}
4343
}
4444

45-
const buffE = Buffer.from('e')
46-
const buffD = Buffer.from('d')
47-
const buffL = Buffer.from('l')
45+
const buffE = new Uint8Array([0x65])
46+
const buffD = new Uint8Array([0x64])
47+
const buffL = new Uint8Array([0x6C])
4848

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

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

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

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

6565
if (val !== data && !encode._floatConversionDetected) {
6666
encode._floatConversionDetected = true
@@ -98,7 +98,7 @@ encode.dictMap = function (buffers, data) {
9898

9999
for (const key of keys) {
100100
if (data.get(key) == null) continue
101-
Buffer.isBuffer(key)
101+
ArrayBuffer.isView(key)
102102
? encode._encode(buffers, key)
103103
: encode.string(buffers, String(key))
104104
encode._encode(buffers, data.get(key))

lib/encoding-length.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { text2arr } from 'uint8-util'
12
import { digitCount, getType } from './util.js'
23

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

1617
for (const [key, value] of map) {
17-
const keyLength = Buffer.byteLength(key)
18+
const keyLength = text2arr(key).byteLength
1819
length += digitCount(keyLength) + 1 + keyLength
1920
length += encodingLength(value)
2021
}
@@ -27,7 +28,7 @@ function objectLength (value) {
2728
const keys = Object.keys(value)
2829

2930
for (let i = 0; i < keys.length; i++) {
30-
const keyLength = Buffer.byteLength(keys[i])
31+
const keyLength = text2arr(keys[i]).byteLength
3132
length += digitCount(keyLength) + 1 + keyLength
3233
length += encodingLength(value[keys[i]])
3334
}
@@ -36,7 +37,7 @@ function objectLength (value) {
3637
}
3738

3839
function stringLength (value) {
39-
const length = Buffer.byteLength(value)
40+
const length = text2arr(value).byteLength
4041
return digitCount(length) + 1 + length
4142
}
4243

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

5556
switch (type) {
56-
case 'buffer': return digitCount(value.length) + 1 + value.length
5757
case 'arraybufferview': return arrayBufferLength(value)
5858
case 'string': return stringLength(value)
5959
case 'array': case 'set': return listLength(value)

lib/util.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ export function digitCount (value) {
88
}
99

1010
export function getType (value) {
11-
if (Buffer.isBuffer(value)) return 'buffer'
1211
if (ArrayBuffer.isView(value)) return 'arraybufferview'
1312
if (Array.isArray(value)) return 'array'
1413
if (value instanceof Number) return 'number'

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,8 @@
6161
},
6262
"release": {
6363
"extends": "@webtorrent/semantic-release-config"
64+
},
65+
"dependencies": {
66+
"uint8-util": "^2.1.6"
6467
}
6568
}

test/BEP-0023.test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@ test('BEP 0023', function (t) {
1414
const announce = fs.readFileSync(filename)
1515
const data = bencode.decode(announce)
1616

17-
console.log(data)
18-
1917
t.plan(1)
2018
t.deepEqual(data, {
2119
complete: 4,
2220
incomplete: 3,
2321
interval: 1800,
2422
'min interval': 1800,
25-
peers: Buffer.from('2ebd1b641a1f51d54c0546cc342190401a1f626ee9c6c8d5cb0d92131a1fac4e689a3c6b180f3d5746db', 'hex')
23+
peers: new Uint8Array(Buffer.from('2ebd1b641a1f51d54c0546cc342190401a1f626ee9c6c8d5cb0d92131a1fac4e689a3c6b180f3d5746db', 'hex'))
2624
})
2725
})
2826

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

34-
console.log(data)
35-
3632
t.plan(1)
3733
t.deepEqual(data, {
3834
complete: 4,

test/decode.buffer.test.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ test('bencode#decode(x)', function (t) {
3434

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

4141
t.test('should be able to decode "binary keys"', function (t) {
@@ -48,25 +48,25 @@ test('bencode#decode(x)', function (t) {
4848
t.deepEqual(
4949
bencode.decode('d3:cow3:moo4:spam4:eggse'),
5050
{
51-
cow: Buffer.from('moo'),
52-
spam: Buffer.from('eggs')
51+
cow: new Uint8Array(Buffer.from('moo')),
52+
spam: new Uint8Array(Buffer.from('eggs'))
5353
}
5454
)
5555
t.deepEqual(
5656
bencode.decode('d4:spaml1:a1:bee'),
5757
{
5858
spam: [
59-
Buffer.from('a'),
60-
Buffer.from('b')
59+
new Uint8Array(Buffer.from('a')),
60+
new Uint8Array(Buffer.from('b'))
6161
]
6262
}
6363
)
6464
t.deepEqual(
6565
bencode.decode('d9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee'),
6666
{
67-
publisher: Buffer.from('bob'),
68-
'publisher-webpage': Buffer.from('www.example.com'),
69-
'publisher.location': Buffer.from('home')
67+
publisher: new Uint8Array(Buffer.from('bob')),
68+
'publisher-webpage': new Uint8Array(Buffer.from('www.example.com')),
69+
'publisher.location': new Uint8Array(Buffer.from('home'))
7070
}
7171
)
7272
})
@@ -75,13 +75,13 @@ test('bencode#decode(x)', function (t) {
7575
t.plan(1)
7676
t.deepEqual(
7777
bencode.decode('l4:spam4:eggse'),
78-
[Buffer.from('spam'),
79-
Buffer.from('eggs')]
78+
[new Uint8Array(Buffer.from('spam')),
79+
new Uint8Array(Buffer.from('eggs'))]
8080
)
8181
})
8282
t.test('should return the correct type', function (t) {
8383
t.plan(1)
84-
t.ok(Buffer.isBuffer(bencode.decode('4:öö')))
84+
t.ok(ArrayBuffer.isView(bencode.decode('4:öö')))
8585
})
8686
t.test('should be able to decode stuff in dicts (issue #12)', function (t) {
8787
t.plan(4)
@@ -96,8 +96,8 @@ test('bencode#decode(x)', function (t) {
9696
const result = bencode.encode(someData)
9797
const dat = bencode.decode(result)
9898
t.equal(dat.integer, 12345)
99-
t.deepEqual(dat.string, Buffer.from('Hello World'))
100-
t.deepEqual(dat.dict.key, Buffer.from('This is a string within a dictionary'))
101-
t.deepEqual(dat.list, [1, 2, 3, 4, Buffer.from('string'), 5, {}])
99+
t.deepEqual(dat.string, new Uint8Array(Buffer.from('Hello World')))
100+
t.deepEqual(dat.dict.key, new Uint8Array(Buffer.from('This is a string within a dictionary')))
101+
t.deepEqual(dat.list, [1, 2, 3, 4, new Uint8Array(Buffer.from('string')), 5, {}])
102102
})
103103
})

0 commit comments

Comments
 (0)