bncode -> bencode

This commit is contained in:
Feross Aboukhadijeh 2014-06-11 17:56:20 -07:00
parent fe95752c37
commit b1e28a04a5
4 changed files with 13 additions and 13 deletions

View File

@ -1,5 +1,5 @@
var BitField = require('bitfield') var BitField = require('bitfield')
var bncode = require('bncode') var bencode = require('bencode')
var EventEmitter = require('events').EventEmitter var EventEmitter = require('events').EventEmitter
var inherits = require('inherits') var inherits = require('inherits')
var Rusha = require('rusha-browserify') // Fast SHA1 (works in browser) var Rusha = require('rusha-browserify') // Fast SHA1 (works in browser)
@ -64,7 +64,7 @@ module.exports = function (metadata) {
try { try {
var str = buf.toString() var str = buf.toString()
var trailerIndex = str.indexOf('ee') + 2 var trailerIndex = str.indexOf('ee') + 2
dict = bncode.decode(str.substring(0, trailerIndex)) dict = bencode.decode(str.substring(0, trailerIndex))
trailer = buf.slice(trailerIndex) trailer = buf.slice(trailerIndex)
} catch (err) { } catch (err) {
// drop invalid messages // drop invalid messages
@ -117,9 +117,9 @@ module.exports = function (metadata) {
// if full torrent dictionary was passed in, pull out just `info` key // if full torrent dictionary was passed in, pull out just `info` key
try { try {
var info = bncode.decode(metadata).info var info = bencode.decode(metadata).info
if (info) { if (info) {
metadata = bncode.encode(info) metadata = bencode.encode(info)
} }
} catch (err) {} } catch (err) {}
@ -135,13 +135,13 @@ module.exports = function (metadata) {
this._metadataSize = this.metadata.length this._metadataSize = this.metadata.length
this._wire.extendedHandshake.metadata_size = this._metadataSize this._wire.extendedHandshake.metadata_size = this._metadataSize
this.emit('metadata', bncode.encode({ info: bncode.decode(this.metadata) })) this.emit('metadata', bencode.encode({ info: bencode.decode(this.metadata) }))
return true return true
} }
ut_metadata.prototype._send = function (dict, trailer) { ut_metadata.prototype._send = function (dict, trailer) {
var buf = bncode.encode(dict) var buf = bencode.encode(dict)
if (Buffer.isBuffer(trailer)) { if (Buffer.isBuffer(trailer)) {
buf = Buffer.concat([buf, trailer]) buf = Buffer.concat([buf, trailer])
} }

View File

@ -11,8 +11,8 @@
"url": "https://github.com/feross/ut_metadata/issues" "url": "https://github.com/feross/ut_metadata/issues"
}, },
"dependencies": { "dependencies": {
"bencode": "^0.6.0",
"bitfield": "^1.0.1", "bitfield": "^1.0.1",
"bncode": "^0.5.0",
"inherits": "^2.0.1", "inherits": "^2.0.1",
"rusha-browserify": "^0.7.3" "rusha-browserify": "^0.7.3"
}, },

View File

@ -1,7 +1,7 @@
var fs = require('fs') var fs = require('fs')
var Protocol = require('bittorrent-protocol') var Protocol = require('bittorrent-protocol')
var ut_metadata = require('../') var ut_metadata = require('../')
var bncode = require('bncode') var bencode = require('bencode')
var test = require('tape') var test = require('tape')
// Used in multiple tests // Used in multiple tests
@ -29,6 +29,6 @@ test('wire.use(ut_metadata(metadata))', function (t) {
t.ok(wire.ut_metadata) t.ok(wire.ut_metadata)
t.ok(wire.ut_metadata.fetch) t.ok(wire.ut_metadata.fetch)
t.ok(wire.ut_metadata.cancel) t.ok(wire.ut_metadata.cancel)
t.equal(wire.ut_metadata.metadata.toString('hex'), bncode.encode(bncode.decode(metadata).info).toString('hex')) t.equal(wire.ut_metadata.metadata.toString('hex'), bencode.encode(bencode.decode(metadata).info).toString('hex'))
t.end() t.end()
}) })

View File

@ -2,7 +2,7 @@ var fs = require('fs')
var parseTorrent = require('parse-torrent') var parseTorrent = require('parse-torrent')
var Protocol = require('bittorrent-protocol') var Protocol = require('bittorrent-protocol')
var ut_metadata = require('../') var ut_metadata = require('../')
var bncode = require('bncode') var bencode = require('bencode')
var test = require('tape') var test = require('tape')
// Used in multiple tests // Used in multiple tests
@ -29,7 +29,7 @@ test('fetch()', function (t) {
wire2.ut_metadata.on('metadata', function (_metadata) { wire2.ut_metadata.on('metadata', function (_metadata) {
// got metadata! // got metadata!
t.equal(_metadata.toString('hex'), bncode.encode({ info: bncode.decode(metadata).info }).toString('hex')) t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(metadata).info }).toString('hex'))
}) })
wire2.on('handshake', function (infoHash, peerId, extensions) { wire2.on('handshake', function (infoHash, peerId, extensions) {
@ -114,7 +114,7 @@ test('fetch when peer gets metadata later (setMetadata)', function (t) {
wire2.ut_metadata.on('metadata', function (_metadata) { wire2.ut_metadata.on('metadata', function (_metadata) {
// got metadata! // got metadata!
t.equal(_metadata.toString('hex'), bncode.encode({ info: bncode.decode(metadata).info }).toString('hex')) t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(metadata).info }).toString('hex'))
}) })
wire2.on('handshake', function (infoHash, peerId, extensions) { wire2.on('handshake', function (infoHash, peerId, extensions) {
@ -153,7 +153,7 @@ test('fetch() large torrent', function (t) {
wire2.ut_metadata.on('metadata', function (_metadata) { wire2.ut_metadata.on('metadata', function (_metadata) {
// got metadata! // got metadata!
t.equal(_metadata.toString('hex'), bncode.encode({ info: bncode.decode(largeMetadata).info }).toString('hex')) t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(largeMetadata).info }).toString('hex'))
}) })
wire2.on('handshake', function (infoHash, peerId, extensions) { wire2.on('handshake', function (infoHash, peerId, extensions) {