Comparing version 0.4.1 to 0.4.2
@@ -9,3 +9,3 @@ | ||
function encode( data ) { | ||
if( data instanceof Buffer ) { | ||
@@ -17,3 +17,3 @@ return Buffer.concat([ | ||
} | ||
switch( typeof data ) { | ||
@@ -32,3 +32,3 @@ case 'string': | ||
} | ||
} | ||
@@ -52,17 +52,24 @@ | ||
encode.dict = function( data ) { | ||
var dict = [ buff_d ] | ||
var i = 1; | ||
for( var k in data ) { | ||
dict[i++] = encode( k ) | ||
dict[i++] = encode( data[k] ) | ||
// 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 | ||
@@ -75,6 +82,6 @@ var c = data.length | ||
} | ||
lst[j++] = buff_e | ||
return Buffer.concat(lst) | ||
} | ||
@@ -90,12 +97,12 @@ | ||
function decode( data, encoding ) { | ||
decode.position = 0 | ||
decode.encoding = encoding || null | ||
decode.data = !( data instanceof Buffer ) | ||
? new Buffer( data ) | ||
: data | ||
return decode.next() | ||
} | ||
@@ -108,3 +115,3 @@ | ||
decode.next = function() { | ||
switch( decode.data[decode.position] ) { | ||
@@ -116,11 +123,11 @@ case 0x64: return decode.dictionary(); break | ||
} | ||
} | ||
decode.find = function( chr ) { | ||
var i = decode.position | ||
var c = decode.data.length | ||
var d = decode.data | ||
while( i < c ) { | ||
@@ -131,52 +138,52 @@ if( d[i] === chr ) | ||
} | ||
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 ) | ||
@@ -186,9 +193,9 @@ var length = +decode.data.slice( decode.position, sep ).toString() | ||
var bytes = decode.data.slice( sep, sepl ) | ||
decode.position += sepl - decode.position | ||
return decode.encoding | ||
? bytes.toString( decode.encoding ) | ||
: bytes | ||
} | ||
@@ -195,0 +202,0 @@ |
{ | ||
"name": "bencode", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"license": "MIT", | ||
"description": "Bencode de/encoder", | ||
"keywords": [ "torrent", "bittorrent", "bencode", "bdecode", "bencoding" ], | ||
"author": { | ||
@@ -13,5 +13,5 @@ "name": "Mark Schmale", | ||
}, | ||
"main": "bencode.js", | ||
"devDependencies": { | ||
@@ -22,3 +22,3 @@ "benchmark": "", | ||
"bncode": "", | ||
"dht.js": "", | ||
"dht.js": "", | ||
"mocha": "" | ||
@@ -29,3 +29,3 @@ }, | ||
}, | ||
"repository": { | ||
@@ -35,7 +35,7 @@ "type": "git", | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/themasch/node-bencode/issues" | ||
} | ||
} |
@@ -13,3 +13,9 @@ var assert = require("assert"); | ||
}); | ||
it('should sort dictionories', function() { | ||
var data = { | ||
string: 'Hello World', | ||
integer: 12345, | ||
}; | ||
assert.equal(bencode.encode(data).toString(), "d7:integeri12345e6:string11:Hello Worlde"); | ||
}) | ||
it('should be able to encode a number', function() { | ||
@@ -45,2 +51,2 @@ assert.equal(bencode.encode(123), 'i123e'); | ||
}) | ||
}); | ||
}); |
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
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
42528
313
10