byteLength = function (value) { return bencode.encode(value).length }
...
> `String` __encoding__
If `encoding` is set, bytestrings are
automatically converted to strings.
Returns `Object` | `Array` | `Buffer` | `String` | `Number`
### bencode.byteLength( *value* ) or bencode.encodingLength( *value* )
> `Buffer` | `Array` | `String` | `Object` | `Number` | `Boolean` __value__
...
function decode(data, start, end, encoding) { if (data == null || data.length === 0) { return null } if (typeof start !== 'number' && encoding == null) { encoding = start start = undefined } if (typeof end !== 'number' && encoding == null) { encoding = end end = undefined } decode.position = 0 decode.encoding = encoding || null decode.data = !(Buffer.isBuffer(data)) ? new Buffer(data) : data.slice(start, end) decode.bytes = decode.data.length return decode.next() }
...
d4:dictd3:key36:This is a string within a dictionarye7:integeri12345e4:listli1ei2ei3ei4e6:stringi5edee6:string11:Hello Worlde
```
### Decoding
```javascript
var data = new Buffer( 'd6:string11:Hello World7:integeri12345e4:dictd3:key36:This is a string within a dictionarye4:listli1ei2ei3ei4e6
:stringi5edeee' )
var result = bencode.decode( data )
```
#### Output
```javascript
{
string: <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>,
...
function encode(data, buffer, offset) { var buffers = [] var result = null encode._encode(buffers, data) result = Buffer.concat(buffers) encode.bytes = result.length if (Buffer.isBuffer(buffer)) { result.copy(buffer, offset) return buffer } return result }
...
integer: 12345,
dict: {
key: 'This is a string within a dictionary'
},
list: [ 1, 2, 3, 4, 'string', 5, {} ]
}
var result = bencode.encode( data )
```
**NOTE** As of `bencode@0.8.0`, boolean values will be cast to integers (false -> 0, true -> 1).
#### Output
...
encodingLength = function (value) { return bencode.encode(value).length }
...
> `String` __encoding__
If `encoding` is set, bytestrings are
automatically converted to strings.
Returns `Object` | `Array` | `Buffer` | `String` | `Number`
### bencode.byteLength( *value* ) or bencode.encodingLength( *value* )
> `Buffer` | `Array` | `String` | `Object` | `Number` | `Boolean` __value__
...
function decode(data, start, end, encoding) { if (data == null || data.length === 0) { return null } if (typeof start !== 'number' && encoding == null) { encoding = start start = undefined } if (typeof end !== 'number' && encoding == null) { encoding = end end = undefined } decode.position = 0 decode.encoding = encoding || null decode.data = !(Buffer.isBuffer(data)) ? new Buffer(data) : data.slice(start, end) decode.bytes = decode.data.length return decode.next() }
...
d4:dictd3:key36:This is a string within a dictionarye7:integeri12345e4:listli1ei2ei3ei4e6:stringi5edee6:string11:Hello Worlde
```
### Decoding
```javascript
var data = new Buffer( 'd6:string11:Hello World7:integeri12345e4:dictd3:key36:This is a string within a dictionarye4:listli1ei2ei3ei4e6
:stringi5edeee' )
var result = bencode.decode( data )
```
#### Output
```javascript
{
string: <Buffer 48 65 6c 6c 6f 20 57 6f 72 6c 64>,
...
buffer = function () { var sep = decode.find(0x3A) var length = getIntFromBuffer(decode.data, decode.position, sep) var end = ++sep + length decode.position = end return decode.encoding ? decode.data.toString(decode.encoding, sep, end) : decode.data.slice(sep, end) }
n/a
dictionary = function () { decode.position++ var dict = {} while (decode.data[decode.position] !== 0x65) { dict[decode.buffer()] = decode.next() } decode.position++ return dict }
n/a
find = function (chr) { var i = decode.position var c = decode.data.length var d = decode.data while (i < c) { if (d[i] === chr) return i i++ } throw new Error( 'Invalid data: Missing delimiter "' + String.fromCharCode(chr) + '" [0x' + chr.toString(16) + ']' ) }
n/a
integer = function () { var end = decode.find(0x65) var number = getIntFromBuffer(decode.data, decode.position + 1, end) decode.position += end + 1 - decode.position return number }
n/a
list = function () { decode.position++ var lst = [] while (decode.data[decode.position] !== 0x65) { lst.push(decode.next()) } decode.position++ return lst }
n/a
next = function () { switch (decode.data[decode.position]) { case 0x64: return decode.dictionary() case 0x6C: return decode.list() case 0x69: return decode.integer() default: return decode.buffer() } }
n/a
function encode(data, buffer, offset) { var buffers = [] var result = null encode._encode(buffers, data) result = Buffer.concat(buffers) encode.bytes = result.length if (Buffer.isBuffer(buffer)) { result.copy(buffer, offset) return buffer } return result }
...
integer: 12345,
dict: {
key: 'This is a string within a dictionary'
},
list: [ 1, 2, 3, 4, 'string', 5, {} ]
}
var result = bencode.encode( data )
```
**NOTE** As of `bencode@0.8.0`, boolean values will be cast to integers (false -> 0, true -> 1).
#### Output
...
_encode = function (buffers, data) { if (Buffer.isBuffer(data)) { buffers.push(new Buffer(data.length + ':')) buffers.push(data) return } if (data == null) { return } switch (typeof data) { case 'string': encode.buffer(buffers, data) break case 'number': encode.number(buffers, data) break case 'object': data.constructor === Array ? encode.list(buffers, data) : encode.dict(buffers, data) break case 'boolean': encode.number(buffers, data ? 1 : 0) break } }
n/a
buffer = function (buffers, data) { buffers.push(new Buffer(Buffer.byteLength(data) + ':' + data)) }
n/a
dict = function (buffers, data) { buffers.push(buffD) var j = 0 var k // fix for issue #13 - sorted dicts var keys = Object.keys(data).sort() var kl = keys.length for (; j < kl; j++) { k = keys[j] if (data[k] == null) continue encode.buffer(buffers, k) encode._encode(buffers, data[k]) } buffers.push(buffE) }
n/a
list = function (buffers, data) { var i = 0 var c = data.length buffers.push(buffL) for (; i < c; i++) { if (data[i] == null) continue encode._encode(buffers, data[i]) } buffers.push(buffE) }
n/a
number = function (buffers, data) { var maxLo = 0x80000000 var hi = (data / maxLo) << 0 var lo = (data % maxLo) << 0 var val = hi * maxLo + lo buffers.push(new Buffer('i' + val + 'e')) if (val !== data && !encode._floatConversionDetected) { encode._floatConversionDetected = true console.warn( 'WARNING: Possible data corruption detected with value "' + data + '":', 'Bencoding only defines support for integers, value was converted to "' + val + '"' ) console.trace() } }
n/a