Comparing version 0.4.0 to 0.4.1
/** | ||
* Encodes data in bencode. | ||
* | ||
* | ||
* @param {Buffer|Array|String|Object|Number} data | ||
* @return {String} | ||
* @return {Buffer} | ||
*/ | ||
function encode( data ) { | ||
if( data instanceof Buffer ) | ||
return data.length + ':' + data | ||
if( data instanceof Buffer ) { | ||
return Buffer.concat([ | ||
new Buffer(data.length+':'), | ||
data | ||
]) | ||
} | ||
switch( typeof data ) { | ||
case 'string': | ||
case 'string': | ||
return encode.bytes( data ) | ||
break | ||
case 'number': | ||
case 'number': | ||
return encode.number( data ) | ||
@@ -29,8 +33,15 @@ break | ||
var buff_e = new Buffer('e') | ||
, buff_d = new Buffer('d') | ||
, buff_l = new Buffer('l') | ||
encode.bytes = function( data ) { | ||
return Buffer.byteLength( data ) + ':' + data | ||
return Buffer.concat([ | ||
new Buffer(Buffer.byteLength( data )+":"), | ||
new Buffer(data) | ||
]) | ||
} | ||
encode.number = function( data ) { | ||
return 'i' + data + 'e' | ||
return new Buffer('i' + data + 'e') | ||
} | ||
@@ -40,10 +51,12 @@ | ||
var dict = 'd' | ||
var dict = [ buff_d ] | ||
var i = 1; | ||
for( var k in data ) { | ||
dict += encode( k ) + encode( data[k] ) | ||
dict[i++] = encode( k ) | ||
dict[i++] = encode( data[k] ) | ||
} | ||
dict[i++] = buff_e | ||
return Buffer.concat(dict) | ||
return dict + 'e' | ||
} | ||
@@ -53,11 +66,12 @@ | ||
var i = 0 | ||
var i = 0, j = 1 | ||
var c = data.length | ||
var lst = 'l' | ||
var lst = [ buff_l ] | ||
for( ; i < c; i++ ) { | ||
lst += encode( data[i] ) | ||
lst[j++] = encode( data[i] ) | ||
} | ||
return lst + 'e' | ||
lst[j++] = buff_e | ||
return Buffer.concat(lst) | ||
@@ -68,3 +82,3 @@ } | ||
* Decodes bencoded data. | ||
* | ||
* | ||
* @param {Buffer} data | ||
@@ -153,3 +167,3 @@ * @param {String} encoding | ||
var end = decode.find( 0x65 ) | ||
var number = decode.data.slice( 1, end ) | ||
var number = decode.data.slice( decode.position+1, end ) | ||
@@ -156,0 +170,0 @@ decode.position += end + 1 - decode.position |
{ | ||
"name": "bencode", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"license": "MIT", | ||
"description": "Bencode de/encoder", | ||
@@ -5,0 +6,0 @@ "keywords": [ "torrent", "bittorrent", "bencode", "bdecode", "bencoding" ], |
@@ -1,4 +0,3 @@ | ||
[![build status](https://secure.travis-ci.org/themasch/node-bencode.png)](http://travis-ci.org/themasch/node-bencode) | ||
# node-bencode | ||
# node-bencode [![build status](https://secure.travis-ci.org/themasch/node-bencode.png)](http://travis-ci.org/themasch/node-bencode) | ||
@@ -41,14 +40,15 @@ A node library for encoding and decoding bencoded data, | ||
``` | ||
bencode x 3,638 ops/sec ±0.54% (101 runs sampled) | ||
bencoding x 844 ops/sec ±3.18% (93 runs sampled) | ||
dht-bencode x 845 ops/sec ±3.73% (89 runs sampled) | ||
bncode x 845 ops/sec ±2.66% (92 runs sampled) | ||
dht.js x 768 ops/sec ±2.70% (92 runs sampled) | ||
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 16,337 ops/sec ±2.47% (96 runs sampled) | ||
bencoding x 21,101 ops/sec ±0.84% (95 runs sampled) | ||
dht-bencode x 18,477 ops/sec ±0.77% (98 runs sampled) | ||
bncode x 760 ops/sec ±1.63% (93 runs sampled) | ||
dht.js x 14,624 ops/sec ±2.15% (95 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) | ||
``` | ||
@@ -130,3 +130,3 @@ | ||
Returns `String` | ||
Returns `Buffer` | ||
@@ -133,0 +133,0 @@ ### bencode.decode( *data*, *encoding* ) |
var assert = require("assert"); | ||
var bencode = require('../bencode.js'); | ||
describe("bencode", function() { | ||
describe("#encode()", function() { | ||
it('should always return a string', function() { | ||
assert.equal(typeof(bencode.encode({})), "string"); | ||
assert.equal(typeof(bencode.encode("test")), "string"); | ||
assert.equal(typeof(bencode.encode([3, 2])), "string"); | ||
assert.equal(typeof(bencode.encode({"a": "b", 3: 6})), "string"); | ||
assert.equal(typeof(bencode.encode(123)), "string"); | ||
it('should always return a Buffer', function() { | ||
assert.ok(Buffer.isBuffer(bencode.encode({})), "its not a buffer for empty dicts"); | ||
assert.ok(Buffer.isBuffer(bencode.encode("test")), "its not a buffer for strings"); | ||
assert.ok(Buffer.isBuffer(bencode.encode([3, 2])), "its not a buffer for lists"); | ||
assert.ok(Buffer.isBuffer(bencode.encode({"a": "b", 3: 6})), "its not a buffer for big dicts"); | ||
assert.ok(Buffer.isBuffer(bencode.encode(123)), "its not a buffer for numbers"); | ||
}); | ||
@@ -12,0 +13,0 @@ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Non-existent author
Supply chain riskThe package was published by an npm account that no longer exists.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
42260
11
0
299
0