emit more warnings

This commit is contained in:
Feross Aboukhadijeh 2014-03-22 21:10:23 -07:00
parent 289b7c3e06
commit 3ab3dd5efd
2 changed files with 14 additions and 3 deletions

View File

@ -37,8 +37,15 @@ module.exports = function (metadata) {
}
ut_metadata.prototype.onExtendedHandshake = function (handshake) {
if (!handshake.m.ut_metadata) {
return this.emit('warning', new Error('Peer does not support ut_metadata'))
}
if (!handshake.metadata_size) {
return this.emit('warning', new Error('Peer does not have metadata'))
}
this._metadataSize = handshake.metadata_size
if (this._metadataSize && this._fetching) {
if (this._fetching) {
this._requestPieces()
}
}
@ -51,7 +58,7 @@ module.exports = function (metadata) {
dict = bncode.decode(str.substring(0, trailerIndex))
trailer = buf.slice(trailerIndex)
} catch (err) {
this.emit('warning', new Error('Could not decode ut_metadata message'))
// drop invalid messages
return
}

View File

@ -48,7 +48,7 @@ test('fetch()', function (t) {
})
test('fetch() from peer without metadata', function (t) {
t.plan(1)
t.plan(2)
var wire1 = new Protocol()
var wire2 = new Protocol()
@ -69,6 +69,10 @@ test('fetch() from peer without metadata', function (t) {
// extended handshake, so he doesn't have metadata
}
wire2.ext('ut_metadata').on('warning', function (err) {
t.pass('got warning about peer missing metadata')
})
wire2.on('handshake', function (infoHash, peerId, extensions) {
wire2.handshake(parsedTorrent.infoHash, id2)
})