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.5.2 to 0.6.0

benchmark/instanceof.js

4

lib/decode.js

@@ -13,3 +13,3 @@ /**

decode.data = !( data instanceof Buffer )
decode.data = !( Buffer.isBuffer(data) )
? new Buffer( data )

@@ -97,3 +97,3 @@ : data

return parseInt( number, 10 )
}

@@ -100,0 +100,0 @@

@@ -17,3 +17,3 @@ /**

if( data instanceof Buffer ) {
if( Buffer.isBuffer(data) ) {
buffers.push(new Buffer(data.length + ':'))

@@ -20,0 +20,0 @@ buffers.push(data)

{
"name": "bencode",
"version": "0.5.2",
"version": "0.6.0",
"license": "MIT",
"description": "Bencode de/encoder",
"keywords": [ "torrent", "bittorrent", "bencode", "bdecode", "bencoding" ],
"keywords": [
"torrent",
"bittorrent",
"bencode",
"bdecode",
"bencoding"
],
"contributors": [

@@ -18,7 +23,4 @@ {

}
],
"main": "bencode.js",
"devDependencies": {

@@ -30,10 +32,8 @@ "matcha": "",

"dht.js": "",
"mocha": ""
"tape": "~2.12"
},
"scripts": {
"test": "node node_modules/mocha/bin/mocha",
"test": "node node_modules/tape/bin/tape test/*.test.js",
"bench": "node node_modules/matcha/bin/matcha benchmark/*.js"
},
"repository": {

@@ -43,7 +43,18 @@ "type": "git",

},
"bugs": {
"url": "https://github.com/themasch/node-bencode/issues"
},
"testling": {
"files": "test/*.test.js",
"browsers": [
"ie/6..latest",
"chrome/22..latest",
"firefox/16..latest",
"safari/latest",
"opera/11.0..latest",
"iphone/6..latest",
"ipad/6..latest",
"android-browser/latest"
]
}
}

@@ -1,2 +0,1 @@

# node-bencode [![build status](https://secure.travis-ci.org/themasch/node-bencode.png)](http://travis-ci.org/themasch/node-bencode) [![NPM version](https://badge.fury.io/js/bencode.png)](https://npmjs.org/package/bencode)

@@ -71,2 +70,6 @@

You can also use node-bencode with browserify to be able to use it in a lot of modern browsers.
[![testling results](https://ci.testling.com/themasch/node-bencode.png)](https://ci.testling.com/themasch/node-bencode)
### Encoding

@@ -73,0 +76,0 @@

module.exports = {
binKeyData: new Buffer("ZDU6ZmlsZXNkMjA6N7VVuuCjmp5LoM+n15a5iM/XJHdkODpjb21wbGV0ZWkwZTEwOmRvd25sb2FkZWRpMTBlMTA6aW5jb21wbGV0ZWkwZWVlZQ==", 'base64')
, binKeyName: (new Buffer("N++/vVXvv73go5rvv71L77+9z6fXlu+/ve+/ve+/ve+/vSR3", 'base64')).toString()
//binKeyData: new Buffer("ZDU6ZmlsZXNkMjA6N7VVuuCjmp5LoM+n15a5iM/XJHdkODpjb21wbGV0ZWkwZTEwOmRvd25sb2FkZWRpMTBlMTA6aW5jb21wbGV0ZWkwZWVlZQ==", 'base64')
binKeyData: new Buffer('ZDU6ZmlsZXNkMzY6N++/vVXvv73go5rvv71L77+9z6fXlu+/ve+/ve+/ve+/vSR3ZDg6Y29tcGxldGVpMGUxMDpkb3dubG9hZGVkaTEwZTEwOmluY29tcGxldGVpMGVlZWU=', 'base64')
, binKeyName: new Buffer("N++/vVXvv73go5rvv71L77+9z6fXlu+/ve+/ve+/ve+/vSR3", 'base64')
, binStringData: new Buffer('w7bCsXNkZg==', 'base64')
, binResultData: new Buffer('NzrDtsKxc2Rm', 'base64')
}

@@ -1,75 +0,85 @@

var assert = require('assert')
var test = require('tape').test
var bencode = require('./lib.js')
var data = require('./data.js')
describe("bencode", function() {
describe("#decode(x)", function() {
it('should be able to decode an integer', function() {
assert.deepEqual(bencode.decode('i123e'), 123);
assert.deepEqual(bencode.decode('i-123e'), -123);
});
it('should be able to decode a float (as int)', function() {
assert.deepEqual(bencode.decode('i12.3e'), 12);
assert.deepEqual(bencode.decode('i-12.3e'), -12);
});
it('should be able to decode a string', function() {
assert.deepEqual(bencode.decode('5:asdfe'), new Buffer('asdfe'));
assert.deepEqual(bencode.decode('4:öö'), new Buffer('öö'));
});
it('should be able to decode "binary keys"', function() {
assert.ok(bencode.decode(data.binKeyData).files.hasOwnProperty(data.binKeyName));
});
test("bencode#decode(x)", function(t) {
it('should be able to decode a dictionary', function() {
assert.deepEqual(
bencode.decode( 'd3:cow3:moo4:spam4:eggse' ),
{
cow: new Buffer('moo'),
spam: new Buffer('eggs')
}
)
assert.deepEqual(
bencode.decode( 'd4:spaml1:a1:bee' ),
{ spam: [
new Buffer('a'),
new Buffer('b')
] }
)
assert.deepEqual(
bencode.decode( 'd9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee'),
{
'publisher': new Buffer('bob'),
'publisher-webpage': new Buffer('www.example.com'),
'publisher.location': new Buffer('home')
}
)
});
t.test('should be able to decode an integer', function(t) {
t.plan(2)
t.equal(bencode.decode('i123e'), 123);
t.equal(bencode.decode('i-123e'), -123);
})
it('should be able to decode a list', function() {
assert.deepEqual(
bencode.decode( 'l4:spam4:eggse'),
[ new Buffer('spam'),
new Buffer('eggs') ]
)
});
it('should return the correct type', function() {
assert.ok(bencode.decode('4:öö') instanceof Buffer);
});
it('should be able to decode stuff in dicts (issue #12)', function() {
var someData = {
string: 'Hello World',
integer: 12345,
dict: {
key: 'This is a string within a dictionary'
},
list: [ 1, 2, 3, 4, 'string', 5, {} ]
t.test('should be able to decode a float (as int)', function(t) {
t.plan(2)
t.equal(bencode.decode('i12.3e'), 12);
t.equal(bencode.decode('i-12.3e'), -12);
})
t.test('should be able to decode a string', function(t) {
t.plan(2)
t.deepEqual(bencode.decode('5:asdfe'), new Buffer('asdfe'));
t.deepEqual(bencode.decode(data.binResultData.toString()), data.binStringData);
})
t.test('should be able to decode "binary keys"', function(t) {
t.plan(1)
t.ok(bencode.decode(data.binKeyData).files.hasOwnProperty(data.binKeyName));
})
t.test('should be able to decode a dictionary', function(t) {
t.plan(3)
t.deepEqual(
bencode.decode( 'd3:cow3:moo4:spam4:eggse' ),
{
cow: new Buffer('moo'),
spam: new Buffer('eggs')
}
var result = bencode.encode( someData )
var dat = bencode.decode ( result )
assert.equal(dat.integer, 12345)
assert.deepEqual(dat.string, new Buffer("Hello World"))
assert.deepEqual(dat.dict.key, new Buffer("This is a string within a dictionary"))
assert.deepEqual(dat.list, [1, 2, 3, 4, new Buffer('string'), 5, {}])
});
)
t.deepEqual(
bencode.decode( 'd4:spaml1:a1:bee' ),
{ spam: [
new Buffer('a'),
new Buffer('b')
] }
)
t.deepEqual(
bencode.decode( 'd9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee'),
{
'publisher': new Buffer('bob'),
'publisher-webpage': new Buffer('www.example.com'),
'publisher.location': new Buffer('home')
}
)
});
});
t.test('should be able to decode a list', function(t) {
t.plan(1)
t.deepEqual(
bencode.decode( 'l4:spam4:eggse'),
[ new Buffer('spam'),
new Buffer('eggs') ]
)
})
t.test('should return the correct type', function(t) {
t.plan(1)
t.ok(Buffer.isBuffer(bencode.decode('4:öö')));
})
t.test('should be able to decode stuff in dicts (issue #12)', function(t) {
t.plan(4)
var someData = {
string: 'Hello World',
integer: 12345,
dict: {
key: 'This is a string within a dictionary'
},
list: [ 1, 2, 3, 4, 'string', 5, {} ]
}
var result = bencode.encode( someData )
var dat = bencode.decode ( result )
t.equal(dat.integer, 12345)
t.deepEqual(dat.string, new Buffer("Hello World"))
t.deepEqual(dat.dict.key, new Buffer("This is a string within a dictionary"))
t.deepEqual(dat.list, [1, 2, 3, 4, new Buffer('string'), 5, {}])
})
})

@@ -1,72 +0,79 @@

var assert = require('assert')
var test = require('tape').test
var bencode = require('./lib.js')
var data = require('./data.js')
describe("bencode", function() {
describe("#decode(x, 'uft8')", function() {
it('should be able to decode an integer', function() {
assert.deepEqual(bencode.decode('i123e', 'utf8'), 123);
assert.deepEqual(bencode.decode('i-123e', 'utf8'), -123);
});
it('should be able to decode a float (as int)', function() {
assert.deepEqual(bencode.decode('i12.3e', 'utf8'), 12);
assert.deepEqual(bencode.decode('i-12.3e', 'utf8'), -12);
});
it('should be able to decode a string', function() {
assert.deepEqual(bencode.decode('5:asdfe', 'utf8'), 'asdfe');
assert.deepEqual(bencode.decode('4:öö', 'utf8'), 'öö');
});
it('should be able to decode "binary keys"', function() {
var decoded = bencode.decode(data.binKeyData, 'utf8')
assert.ok(decoded.files.hasOwnProperty(data.binKeyName));
});
test("bencode#decode(x, 'uft8')", function(t) {
t.test('should be able to decode an integer', function(t) {
t.plan(2)
t.equal(bencode.decode('i123e', 'utf8'), 123)
t.equal(bencode.decode('i-123e', 'utf8'), -123)
})
t.test('should be able to decode a float (as int)', function(t) {
t.plan(2)
t.equal(bencode.decode('i12.3e', 'utf8'), 12)
t.equal(bencode.decode('i-12.3e', 'utf8'), -12)
})
t.test('should be able to decode a string', function(t) {
t.plan(2)
t.equal(bencode.decode('5:asdfe', 'utf8'), 'asdfe')
t.deepEqual(bencode.decode(data.binResultData.toString(), 'utf8'), data.binStringData.toString());
})
t.test('should be able to decode "binary keys"', function(t) {
t.plan(1)
var decoded = bencode.decode(data.binKeyData, 'utf8')
t.ok(decoded.files.hasOwnProperty(data.binKeyName.toString('utf8')))
})
it('should be able to decode a dictionary', function() {
assert.deepEqual(
bencode.decode( 'd3:cow3:moo4:spam4:eggse', 'utf8' ),
{
cow: 'moo',
spam: 'eggs'
}
)
assert.deepEqual(
bencode.decode( 'd4:spaml1:a1:bee', 'utf8' ),
{ spam: [ 'a', 'b' ] }
)
assert.deepEqual(
bencode.decode( 'd9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee', 'utf8' ),
{
'publisher': 'bob',
'publisher-webpage': 'www.example.com',
'publisher.location': 'home'
}
)
});
t.test('should be able to decode a dictionary', function(t) {
t.plan(3)
t.deepEqual(
bencode.decode( 'd3:cow3:moo4:spam4:eggse', 'utf8' ),
{
cow: 'moo',
spam: 'eggs'
}
)
t.deepEqual(
bencode.decode( 'd4:spaml1:a1:bee', 'utf8' ),
{ spam: [ 'a', 'b' ] }
)
t.deepEqual(
bencode.decode( 'd9:publisher3:bob17:publisher-webpage15:www.example.com18:publisher.location4:homee', 'utf8' ),
{
'publisher': 'bob',
'publisher-webpage': 'www.example.com',
'publisher.location': 'home'
}
)
})
it('should be able to decode a list', function() {
assert.deepEqual(
bencode.decode( 'l4:spam4:eggse', 'utf8' ),
[ 'spam', 'eggs' ]
)
});
it('should return the correct type', function() {
assert.ok(typeof(bencode.decode('4:öö', 'utf8')) === 'string');
});
it('should be able to decode stuff in dicts (issue #12)', function() {
var someData = {
string: 'Hello World',
integer: 12345,
dict: {
key: 'This is a string within a dictionary'
},
list: [ 1, 2, 3, 4, 'string', 5, {} ]
}
var result = bencode.encode( someData )
var dat = bencode.decode ( result, 'utf8' )
assert.equal(dat.integer, 12345)
assert.deepEqual(dat.string, "Hello World")
assert.deepEqual(dat.dict.key, "This is a string within a dictionary")
assert.deepEqual(dat.list, [1, 2, 3, 4, 'string', 5, {}])
});
});
});
t.test('should be able to decode a list', function(t) {
t.plan(1)
t.deepEqual(
bencode.decode( 'l4:spam4:eggse', 'utf8' ),
[ 'spam', 'eggs' ]
)
})
t.test('should return the correct type', function(t) {
t.plan(1)
t.ok(typeof(bencode.decode('4:öö', 'utf8')) === 'string')
})
t.test('should be able to decode stuff in dicts (issue #12)', function(t) {
t.plan(4)
var someData = {
string: 'Hello World',
integer: 12345,
dict: {
key: 'This is a string within a dictionary'
},
list: [ 1, 2, 3, 4, 'string', 5, {} ]
}
var result = bencode.encode( someData )
var dat = bencode.decode ( result, 'utf8' )
t.equal(dat.integer, 12345)
t.deepEqual(dat.string, "Hello World")
t.deepEqual(dat.dict.key, "This is a string within a dictionary")
t.deepEqual(dat.list, [1, 2, 3, 4, 'string', 5, {}])
})
})

@@ -1,5 +0,6 @@

var assert = require("assert");
var bencode = require('./lib.js');
var test = require('tape').test
var bencode = require('./lib.js')
var data = require('./data.js')
describe("bencode", function() {
test('bencode#encode()', function(t) {

@@ -9,95 +10,111 @@ // prevent the warning showing up in the test

describe("#encode()", function() {
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");
});
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 force keys to be strings', function() {
var data = {
12: 'Hello World',
34: 12345,
};
assert.equal(bencode.encode(data).toString(), "d2:1211:Hello World2:34i12345ee")
})
it('should be able to encode a positive integer', function() {
assert.equal(bencode.encode(123), 'i123e');
})
it('should be able to encode a negative integer', function() {
assert.equal(bencode.encode(-123), 'i-123e');
})
it('should be able to encode a positive float (as int)', function() {
assert.equal(bencode.encode(123.5), 'i123e');
})
it('should be able to encode a negative float (as int)', function() {
assert.equal(bencode.encode(-123.5), 'i-123e');
})
it('should be able to safely encode numbers between -/+ 2 ^ 53 (as ints)', function() {
assert.equal(bencode.encode(0), 'i' + 0 + 'e');
t.test('should always return a Buffer', function(t) {
t.plan(5)
t.ok(Buffer.isBuffer(bencode.encode({})), "its a buffer for empty dicts")
t.ok(Buffer.isBuffer(bencode.encode("test")), "its a buffer for strings")
t.ok(Buffer.isBuffer(bencode.encode([3, 2])), "its a buffer for lists")
t.ok(Buffer.isBuffer(bencode.encode({"a": "b", 3: 6})), "its a buffer for big dicts")
t.ok(Buffer.isBuffer(bencode.encode(123)), "its a buffer for numbers")
})
var JAVASCRIPT_INT_BITS = 53;
var MAX_JAVASCRIPT_INT = Math.pow(2, JAVASCRIPT_INT_BITS);
t.test('should sort dictionaries', function(t) {
t.plan(1)
var data = { string: 'Hello World', integer: 12345 }
t.equal(bencode.encode(data).toString(), "d7:integeri12345e6:string11:Hello Worlde")
})
for (var exp = 1; exp < JAVASCRIPT_INT_BITS; ++exp) {
var val = Math.pow(2, exp);
// try the positive and negative
assert.equal(bencode.encode(val), 'i' + val + 'e');
assert.equal(bencode.encode(-val), 'i-' + val + 'e');
t.test('should force keys to be strings', function(t) {
t.plan(1)
var data = {
12: 'Hello World',
34: 12345,
}
t.equal(bencode.encode(data).toString(), "d2:1211:Hello World2:34i12345ee")
})
// try the value, one above and one below, both positive and negative
var above = val + 1;
var below = val - 1;
t.test('should be able to encode a positive integer', function(t) {
t.plan(1)
t.equal(bencode.encode(123).toString(), 'i123e')
})
t.test('should be able to encode a negative integer', function(t) {
t.plan(1)
t.equal(bencode.encode(-123).toString(), 'i-123e')
})
t.test('should be able to encode a positive float (as int)', function(t) {
t.plan(1)
t.equal(bencode.encode(123.5).toString(), 'i123e')
})
t.test('should be able to encode a negative float (as int)', function(t) {
t.plan(1)
t.equal(bencode.encode(-123.5).toString(), 'i-123e')
})
assert.equal(bencode.encode(above), 'i' + above + 'e');
assert.equal(bencode.encode(-above), 'i-' + above + 'e');
t.test('should be able to safely encode numbers between -/+ 2 ^ 53 (as ints)', function(t) {
var JAVASCRIPT_INT_BITS = 53
var MAX_JAVASCRIPT_INT = Math.pow(2, JAVASCRIPT_INT_BITS)
assert.equal(bencode.encode(below), 'i' + below + 'e');
assert.equal(bencode.encode(-below), 'i-' + below + 'e');
}
assert.equal(bencode.encode(MAX_JAVASCRIPT_INT), 'i' + MAX_JAVASCRIPT_INT + 'e');
assert.equal(bencode.encode(-MAX_JAVASCRIPT_INT), 'i-' + MAX_JAVASCRIPT_INT + 'e');
});
it('should be able to encode a previously problematice 64 bit int', function() {
assert.equal(bencode.encode(2433088826), 'i' + 2433088826 + 'e');
})
it('should be able to encode a negative 64 bit int', function() {
assert.equal(bencode.encode(-0xffffffff), 'i-' + 0xffffffff + 'e');
})
it('should be able to encode a positive 64 bit float (as int)', function() {
assert.equal(bencode.encode(0xffffffff + 0.5), 'i' + 0xffffffff + 'e');
})
it('should be able to encode a negative 64 bit float (as int)', function() {
assert.equal(bencode.encode(-0xffffffff - 0.5), 'i-' + 0xffffffff + 'e');
})
it('should be able to encode a string', function() {
assert.equal(bencode.encode("asdf"), '4:asdf');
assert.equal(bencode.encode(":asdf:"), '6::asdf:');
})
it('should be able to encode a unicode string', function() {
assert.equal(bencode.encode("ö±sdf"), '7:ö±sdf');
assert.equal(bencode.encode(new Buffer("ö±sdf")), '7:ö±sdf');
})
it('should be able to encode a buffer', function() {
assert.equal(bencode.encode(new Buffer("asdf")), '4:asdf');
assert.equal(bencode.encode(new Buffer(":asdf:")), '6::asdf:');
})
it('should be able to encode an array', function() {
assert.equal(bencode.encode([32, 12]), 'li32ei12ee');
assert.equal(bencode.encode([":asdf:"]), 'l6::asdf:e');
})
it('should be able to encode an object', function() {
assert.equal(bencode.encode({"a": "bc"}), 'd1:a2:bce')
assert.equal(bencode.encode({"a": "45", "b": 45}), 'd1:a2:451:bi45ee')
assert.equal(bencode.encode({"a": new Buffer("bc")}), 'd1:a2:bce')
})
t.plan((JAVASCRIPT_INT_BITS-1) * 6 + 3)
t.equal(bencode.encode(0).toString(), 'i' + 0 + 'e')
for (var exp = 1; exp < JAVASCRIPT_INT_BITS; ++exp) {
var val = Math.pow(2, exp)
// try the positive and negative
t.equal(bencode.encode(val).toString(), 'i' + val + 'e')
t.equal(bencode.encode(-val).toString(), 'i-' + val + 'e')
// try the value, one above and one below, both positive and negative
var above = val + 1
var below = val - 1
t.equal(bencode.encode(above).toString(), 'i' + above + 'e')
t.equal(bencode.encode(-above).toString(), 'i-' + above + 'e')
t.equal(bencode.encode(below).toString(), 'i' + below + 'e')
t.equal(bencode.encode(-below).toString(), 'i-' + below + 'e')
}
t.equal(bencode.encode(MAX_JAVASCRIPT_INT).toString(), 'i' + MAX_JAVASCRIPT_INT + 'e')
t.equal(bencode.encode(-MAX_JAVASCRIPT_INT).toString(), 'i-' + MAX_JAVASCRIPT_INT + 'e')
})
});
t.test('should be able to encode a previously problematice 64 bit int', function(t) {
t.plan(1)
t.equal(bencode.encode(2433088826).toString(), 'i' + 2433088826 + 'e')
})
t.test('should be able to encode a negative 64 bit int', function(t) {
t.plan(1)
t.equal(bencode.encode(-0xffffffff).toString(), 'i-' + 0xffffffff + 'e')
})
t.test('should be able to encode a positive 64 bit float (as int)', function(t) {
t.plan(1)
t.equal(bencode.encode(0xffffffff + 0.5).toString(), 'i' + 0xffffffff + 'e')
})
t.test('should be able to encode a negative 64 bit float (as int)', function(t) {
t.plan(1)
t.equal(bencode.encode(-0xffffffff - 0.5).toString(), 'i-' + 0xffffffff + 'e')
})
t.test('should be able to encode a string', function(t) {
t.plan(2)
t.equal(bencode.encode("asdf").toString(), '4:asdf')
t.equal(bencode.encode(":asdf:").toString(), '6::asdf:')
})
t.test('should be able to encode a unicode string', function(t) {
t.plan(2)
t.deepEqual(bencode.encode(data.binStringData.toString()), data.binResultData)
t.deepEqual(bencode.encode(data.binStringData.toString()), data.binResultData)
})
t.test('should be able to encode a buffer', function(t) {
t.plan(2)
t.equal(bencode.encode(new Buffer("asdf")).toString(), '4:asdf')
t.equal(bencode.encode(new Buffer(":asdf:")).toString(), '6::asdf:')
})
t.test('should be able to encode an array', function(t) {
t.plan(2)
t.equal(bencode.encode([32, 12]).toString(), 'li32ei12ee')
t.equal(bencode.encode([":asdf:"]).toString(), 'l6::asdf:e')
})
t.test('should be able to encode an object', function(t) {
t.plan(3)
t.equal(bencode.encode({"a": "bc"}).toString(), 'd1:a2:bce')
t.equal(bencode.encode({"a": "45", "b": 45}).toString(), 'd1:a2:451:bi45ee')
t.equal(bencode.encode({"a": new Buffer("bc")}).toString(), 'd1:a2:bce')
})
})

Sorry, the diff of this file is not supported yet

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