Skip to content

Commit

Permalink
Set bytes to position for compliance with abstract-encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
ckcr4lyf committed Apr 1, 2024
1 parent e32b5d0 commit a1135e6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,10 @@ function decode (data, start, end, encoding) {
? text2arr(data)
: new Uint8Array(data.slice(start, end))

decode.bytes = decode.data.length
const decodeResult = decode.next()
decode.bytes = decode.position

return decode.next()
return decodeResult
}

decode.bytes = 0
Expand Down
24 changes: 24 additions & 0 deletions test/decode.length.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import test from 'tape'
import bencode from '../index.js'

test('bencode#decode#bytes', function (t) {
t.test('bytes should be set to correct value for two separate bencoded elements', function (t) {
t.plan(2)
t.equal(bencode.decode('i123ei123e'), 123)
t.equal(bencode.decode.bytes, 5)
})
t.test('bytes should be set to correct value when followed by random junk data', function (t) {
t.plan(1)
const someData = {
string: 'Hello World',
integer: 12345,
dict: {
key: 'This is a string within a dictionary'
},
list: [1, 2, 3, 4, 'string', 5, {}]
}
const result = bencode.encode(someData)
bencode.decode(Buffer.from(result).toString() + 'RANDOM_JUNK_DATA')
t.equal(bencode.decode.bytes, result.length)
})
})

0 comments on commit a1135e6

Please sign in to comment.