Socket
Socket
Sign inDemoInstall

bencode

Package Overview
Dependencies
Maintainers
2
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bencode - npm Package Compare versions

Comparing version 0.4.2 to 0.4.3

.npmignore

23

benchmark/index.js
var Benchmark = require( 'benchmark' )
var Benchmark = require( '../lib/benchmark' )
var fs = require( 'fs' )

@@ -13,8 +13,13 @@

var object = bencode.decode( buffer, 'ascii' )
var doEncode = true,
doDecode = true;
if(process.argv.length > 2) {
doEncode = (process.argv.indexOf('encode') !== -1);
doDecode = (process.argv.indexOf('decode') !== -1);
}
// ////////////////////////////////////////////////////////////////////////////
if(doEncode) {
console.log( 'ENCODING\n' )
var encoding = new Benchmark.Suite()
.add( 'bencode', function () {

@@ -35,3 +40,3 @@ bencode.encode( object )

})
.on( 'cycle', function ( event ) {

@@ -46,8 +51,9 @@ console.log( event.target.toString() )

.run()
}
// ////////////////////////////////////////////////////////////////////////////
if(doDecode) {
console.log( 'DECODING\n' )
var decoding = new Benchmark.Suite()
.add( 'bencode', function () {

@@ -68,3 +74,3 @@ bencode.decode( buffer )

})
.on( 'cycle', function ( event, bench ) {

@@ -79,1 +85,2 @@ console.log( event.target.toString() )

.run()
}

@@ -1,195 +0,4 @@

/**
* Encodes data in bencode.
*
* @param {Buffer|Array|String|Object|Number} data
* @return {Buffer}
*/
function encode( data ) {
if( data instanceof Buffer ) {
return Buffer.concat([
new Buffer(data.length+':'),
data
])
}
switch( typeof data ) {
case 'string':
return encode.bytes( data )
break
case 'number':
return encode.number( data )
break
case 'object':
return data.constructor === Array
? encode.list( data )
: encode.dict( data )
break
}
module.exports = {
encode: require( './lib/encode' ),
decode: require( './lib/decode' )
}
var buff_e = new Buffer('e')
, buff_d = new Buffer('d')
, buff_l = new Buffer('l')
encode.bytes = function( data ) {
return Buffer.concat([
new Buffer(Buffer.byteLength( data )+":"),
new Buffer(data)
])
}
encode.number = function( data ) {
return new Buffer('i' + data + 'e')
}
encode.dict = function( data ) {
var dict = [ buff_d ]
var i = 1;
// fix for issue #13 - sorted dicts
var keys = []
for( var k in data) {
keys.push(k)
}
keys = keys.sort()
for(var j=0;j<keys.length;j++) {
dict[i++] = encode( keys[j] )
dict[i++] = encode( data[keys[j]] )
}
dict[i++] = buff_e
return Buffer.concat(dict)
}
encode.list = function( data ) {
var i = 0, j = 1
var c = data.length
var lst = [ buff_l ]
for( ; i < c; i++ ) {
lst[j++] = encode( data[i] )
}
lst[j++] = buff_e
return Buffer.concat(lst)
}
/**
* Decodes bencoded data.
*
* @param {Buffer} data
* @param {String} encoding
* @return {Object|Array|Buffer|String|Number}
*/
function decode( data, encoding ) {
decode.position = 0
decode.encoding = encoding || null
decode.data = !( data instanceof Buffer )
? new Buffer( data )
: data
return decode.next()
}
decode.position = 0
decode.data = null
decode.encoding = null
decode.next = function() {
switch( decode.data[decode.position] ) {
case 0x64: return decode.dictionary(); break
case 0x6C: return decode.list(); break
case 0x69: return decode.integer(); break
default: return decode.bytes(); break
}
}
decode.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++
}
return -1
}
decode.dictionary = function() {
decode.position++
var dict = {}
while( decode.data[decode.position] !== 0x65 ) {
dict[ decode.next() ] = decode.next()
}
decode.position++
return dict
}
decode.list = function() {
decode.position++
var lst = []
while( decode.data[decode.position] !== 0x65 ) {
lst.push( decode.next() )
}
decode.position++
return lst
}
decode.integer = function() {
var end = decode.find( 0x65 )
var number = decode.data.slice( decode.position+1, end )
decode.position += end + 1 - decode.position
return +number
}
decode.bytes = function() {
var sep = decode.find( 0x3A )
var length = +decode.data.slice( decode.position, sep ).toString()
var sepl = ++sep + length
var bytes = decode.data.slice( sep, sepl )
decode.position += sepl - decode.position
return decode.encoding
? bytes.toString( decode.encoding )
: bytes
}
// Expose methods
exports.encode = encode
exports.decode = decode
{
"name": "bencode",
"version": "0.4.2",
"version": "0.4.3",
"license": "MIT",

@@ -8,8 +8,15 @@ "description": "Bencode de/encoder",

"author": {
"name": "Mark Schmale",
"email": "ma.schmale@googlemail.com",
"url": "http://masch.it/"
},
"contributors": [
{
"name": "Mark Schmale",
"email": "masch@masch.it",
"url": "http://masch.it/"
},
{
"name": "Jonas Hermsmeier",
"email": "jonas@hojoki.com"
}
],
"main": "bencode.js",

@@ -26,3 +33,3 @@

"scripts": {
"test": "./node_modules/.bin/mocha tests/*.test.js"
"test": "./node_modules/.bin/mocha"
},

@@ -29,0 +36,0 @@

# node-bencode [![build status](https://secure.travis-ci.org/themasch/node-bencode.png)](http://travis-ci.org/themasch/node-bencode)
A node library for encoding and decoding bencoded data,
A node library for encoding and decoding bencoded data,
according to the [BitTorrent specification](http://www.bittorrent.org/beps/bep_0003.html).

@@ -39,18 +39,22 @@

### encode
```
bencode x 1,143 ops/sec ±2.15% (94 runs sampled)
bencoding x 1,362 ops/sec ±1.07% (92 runs sampled)
dht-bencode x 1,502 ops/sec ±1.48% (97 runs sampled)
bncode x 1,424 ops/sec ±1.89% (95 runs sampled)
dht.js x 1,277 ops/sec ±1.37% (96 runs sampled)
bencode x 7,939 ops/sec ±2.22% (74 runs sampled)
bencoding x 5,632 ops/sec ±1.47% (86 runs sampled)
dht-bencode x 6,403 ops/sec ±3.45% (87 runs sampled)
bncode x 5,106 ops/sec ±3.63% (82 runs sampled)
dht.js x 7,972 ops/sec ±2.22% (84 runs sampled)
```
### decode
```
bencode x 29,588 ops/sec ±0.75% (99 runs sampled)
bencoding x 27,844 ops/sec ±1.97% (95 runs sampled)
dht-bencode x 22,975 ops/sec ±1.84% (90 runs sampled)
bncode x 990 ops/sec ±2.71% (88 runs sampled)
dht.js x 19,103 ops/sec ±2.14% (89 runs sampled)
```
bencode x 23,162 ops/sec ±0.86% (100 runs sampled)
bencoding x 30,022 ops/sec ±0.53% (98 runs sampled)
dht-bencode x 26,607 ops/sec ±0.15% (102 runs sampled)
bncode x 883 ops/sec ±1.26% (97 runs sampled)
dht.js x 20,978 ops/sec ±1.05% (99 runs sampled)
```
*all benchmarks from an Intel Core2Duo@2.6GHz with node v0.10.3.*
## Usage

@@ -82,3 +86,3 @@

```
d6:string11:Hello World7:integeri12345e4:dictd3:key36:This is a string within a dictionarye4:litli1ei2ei3ei4e6:stringi5edeee
d4:dictd3:key36:This is a string within a dictionarye7:integeri12345e4:listli1ei2ei3ei4e6:stringi5edee6:string11:Hello Worlde
```

@@ -135,3 +139,3 @@

> `Buffer` __data__
> `Buffer` __data__
> `String` __encoding__

@@ -138,0 +142,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc