bncode -> bencode
This commit is contained in:
parent
fe95752c37
commit
b1e28a04a5
12
index.js
12
index.js
|
@ -1,5 +1,5 @@
|
|||
var BitField = require('bitfield')
|
||||
var bncode = require('bncode')
|
||||
var bencode = require('bencode')
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
var inherits = require('inherits')
|
||||
var Rusha = require('rusha-browserify') // Fast SHA1 (works in browser)
|
||||
|
@ -64,7 +64,7 @@ module.exports = function (metadata) {
|
|||
try {
|
||||
var str = buf.toString()
|
||||
var trailerIndex = str.indexOf('ee') + 2
|
||||
dict = bncode.decode(str.substring(0, trailerIndex))
|
||||
dict = bencode.decode(str.substring(0, trailerIndex))
|
||||
trailer = buf.slice(trailerIndex)
|
||||
} catch (err) {
|
||||
// drop invalid messages
|
||||
|
@ -117,9 +117,9 @@ module.exports = function (metadata) {
|
|||
|
||||
// if full torrent dictionary was passed in, pull out just `info` key
|
||||
try {
|
||||
var info = bncode.decode(metadata).info
|
||||
var info = bencode.decode(metadata).info
|
||||
if (info) {
|
||||
metadata = bncode.encode(info)
|
||||
metadata = bencode.encode(info)
|
||||
}
|
||||
} catch (err) {}
|
||||
|
||||
|
@ -135,13 +135,13 @@ module.exports = function (metadata) {
|
|||
this._metadataSize = this.metadata.length
|
||||
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
|
||||
}
|
||||
|
||||
ut_metadata.prototype._send = function (dict, trailer) {
|
||||
var buf = bncode.encode(dict)
|
||||
var buf = bencode.encode(dict)
|
||||
if (Buffer.isBuffer(trailer)) {
|
||||
buf = Buffer.concat([buf, trailer])
|
||||
}
|
||||
|
|
|
@ -11,8 +11,8 @@
|
|||
"url": "https://github.com/feross/ut_metadata/issues"
|
||||
},
|
||||
"dependencies": {
|
||||
"bencode": "^0.6.0",
|
||||
"bitfield": "^1.0.1",
|
||||
"bncode": "^0.5.0",
|
||||
"inherits": "^2.0.1",
|
||||
"rusha-browserify": "^0.7.3"
|
||||
},
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
var fs = require('fs')
|
||||
var Protocol = require('bittorrent-protocol')
|
||||
var ut_metadata = require('../')
|
||||
var bncode = require('bncode')
|
||||
var bencode = require('bencode')
|
||||
var test = require('tape')
|
||||
|
||||
// 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.fetch)
|
||||
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()
|
||||
})
|
||||
|
|
|
@ -2,7 +2,7 @@ var fs = require('fs')
|
|||
var parseTorrent = require('parse-torrent')
|
||||
var Protocol = require('bittorrent-protocol')
|
||||
var ut_metadata = require('../')
|
||||
var bncode = require('bncode')
|
||||
var bencode = require('bencode')
|
||||
var test = require('tape')
|
||||
|
||||
// Used in multiple tests
|
||||
|
@ -29,7 +29,7 @@ test('fetch()', function (t) {
|
|||
|
||||
wire2.ut_metadata.on('metadata', function (_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) {
|
||||
|
@ -114,7 +114,7 @@ test('fetch when peer gets metadata later (setMetadata)', function (t) {
|
|||
|
||||
wire2.ut_metadata.on('metadata', function (_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) {
|
||||
|
@ -153,7 +153,7 @@ test('fetch() large torrent', function (t) {
|
|||
|
||||
wire2.ut_metadata.on('metadata', function (_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) {
|
||||
|
|
Loading…
Reference in New Issue