Merge pull request #41 from jimmywarting/feature/modernize

modernize
This commit is contained in:
Feross Aboukhadijeh 2018-08-29 12:31:49 -07:00 committed by GitHub
commit a9826a9612
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 339 additions and 314 deletions

View File

@ -30,15 +30,15 @@ This package should be used with [bittorrent-protocol](https://www.npmjs.com/pac
Say you're already using `bittorrent-protocol`. Your code might look something like this: Say you're already using `bittorrent-protocol`. Your code might look something like this:
```js ```js
var Protocol = require('bittorrent-protocol') const Protocol = require('bittorrent-protocol')
var net = require('net') const net = require('net')
net.createServer(function (socket) { net.createServer(socket => {
var wire = new Protocol() var wire = new Protocol()
socket.pipe(wire).pipe(socket) socket.pipe(wire).pipe(socket)
// handle handshake // handle handshake
wire.on('handshake', function (infoHash, peerId) { wire.on('handshake', (infoHash, peerId) => {
wire.handshake(new Buffer('my info hash'), new Buffer('my peer id')) wire.handshake(new Buffer('my info hash'), new Buffer('my peer id'))
}) })
@ -48,12 +48,12 @@ net.createServer(function (socket) {
To add support for BEP 9, simply modify your code like this: To add support for BEP 9, simply modify your code like this:
```js ```js
var Protocol = require('bittorrent-protocol') const Protocol = require('bittorrent-protocol')
var net = require('net') const net = require('net')
var ut_metadata = require('ut_metadata') const ut_metadata = require('ut_metadata')
net.createServer(function (socket) { net.createServer(socket => {
var wire = new Protocol() const wire = new Protocol()
socket.pipe(wire).pipe(socket) socket.pipe(wire).pipe(socket)
// initialize the extension // initialize the extension
@ -65,7 +65,7 @@ net.createServer(function (socket) {
wire.ut_metadata.fetch() wire.ut_metadata.fetch()
// 'metadata' event will fire when the metadata arrives and is verified to be correct! // 'metadata' event will fire when the metadata arrives and is verified to be correct!
wire.ut_metadata.on('metadata', function (metadata) { wire.ut_metadata.on('metadata', metadata => {
// got metadata! // got metadata!
// Note: the event will not fire if the peer does not support ut_metadata, if they // Note: the event will not fire if the peer does not support ut_metadata, if they
@ -75,12 +75,12 @@ net.createServer(function (socket) {
// optionally, listen to the 'warning' event if you want to know that metadata is // optionally, listen to the 'warning' event if you want to know that metadata is
// probably not going to arrive for one of the above reasons. // probably not going to arrive for one of the above reasons.
wire.ut_metadata.on('warning', function (err) { wire.ut_metadata.on('warning', err => {
console.log(err.message) console.log(err.message)
}) })
// handle handshake // handle handshake
wire.on('handshake', function (infoHash, peerId) { wire.on('handshake', (infoHash, peerId) => {
wire.handshake(new Buffer('my info hash'), new Buffer('my peer id')) wire.handshake(new Buffer('my info hash'), new Buffer('my peer id'))
}) })
@ -95,7 +95,7 @@ Initialize the extension. If you have the torrent metadata (Buffer), pass it int
`ut_metadata` constructor so it's made available to the peer. `ut_metadata` constructor so it's made available to the peer.
```js ```js
var metadata = fs.readFileSync(__dirname + '/file.torrent') const metadata = fs.readFileSync(__dirname + '/file.torrent')
wire.use(ut_metadata(metadata)) wire.use(ut_metadata(metadata))
``` ```
@ -119,7 +119,7 @@ Fired when metadata is available and verified to be correct. Called with a singl
parameter of type Buffer. parameter of type Buffer.
```js ```js
wire.ut_metadata.on('metadata', function (metadata) { wire.ut_metadata.on('metadata', metadata => {
console.log(Buffer.isBuffer(metadata)) // true console.log(Buffer.isBuffer(metadata)) // true
}) })
``` ```
@ -136,7 +136,7 @@ Fired if:
- the peer repeatedly sent invalid data - the peer repeatedly sent invalid data
```js ```js
wire.ut_metadata.on('warning', function (err) { wire.ut_metadata.on('warning', err => {
console.log(err.message) console.log(err.message)
}) })
``` ```

114
index.js
View File

@ -1,30 +1,29 @@
var bencode = require('bencode') const { EventEmitter } = require('events')
var BitField = require('bitfield') const bencode = require('bencode')
var debug = require('debug')('ut_metadata') const BitField = require('bitfield')
var EventEmitter = require('events').EventEmitter const debug = require('debug')('ut_metadata')
var inherits = require('inherits') const sha1 = require('simple-sha1')
var sha1 = require('simple-sha1')
var MAX_METADATA_SIZE = 10000000 // 10MB const MAX_METADATA_SIZE = 1E7 // 10 MB
var BITFIELD_GROW = 1000 const BITFIELD_GROW = 1E3
var PIECE_LENGTH = 16 * 1024 const PIECE_LENGTH = 1 << 14 // 16 KiB
module.exports = function (metadata) { module.exports = metadata => {
inherits(utMetadata, EventEmitter) class utMetadata extends EventEmitter {
constructor (wire) {
function utMetadata (wire) { super()
EventEmitter.call(this)
this._wire = wire this._wire = wire
this._fetching = false
this._metadataComplete = false this._metadataComplete = false
this._metadataSize = null this._metadataSize = null
this._remainingRejects = null // how many reject messages to tolerate before quitting // how many reject messages to tolerate before quitting
this._fetching = false this._remainingRejects = null
// The largest .torrent file that I know of is ~1-2MB, which is ~100 pieces. // The largest torrent file that I know of is ~1-2MB, which is ~100
// Therefore, cap the bitfield to 10x that (1000 pieces) so a malicious peer can't // pieces. Therefore, cap the bitfield to 10x that (1000 pieces) so a
// make it grow to fill all memory. // malicious peer can't make it grow to fill all memory.
this._bitfield = new BitField(0, { grow: BITFIELD_GROW }) this._bitfield = new BitField(0, { grow: BITFIELD_GROW })
if (Buffer.isBuffer(metadata)) { if (Buffer.isBuffer(metadata)) {
@ -32,14 +31,11 @@ module.exports = function (metadata) {
} }
} }
// Name of the bittorrent-protocol extension onHandshake (infoHash, peerId, extensions) {
utMetadata.prototype.name = 'ut_metadata'
utMetadata.prototype.onHandshake = function (infoHash, peerId, extensions) {
this._infoHash = infoHash this._infoHash = infoHash
} }
utMetadata.prototype.onExtendedHandshake = function (handshake) { onExtendedHandshake (handshake) {
if (!handshake.m || !handshake.m.ut_metadata) { if (!handshake.m || !handshake.m.ut_metadata) {
return this.emit('warning', new Error('Peer does not support ut_metadata')) return this.emit('warning', new Error('Peer does not support ut_metadata'))
} }
@ -61,11 +57,12 @@ module.exports = function (metadata) {
} }
} }
utMetadata.prototype.onMessage = function (buf) { onMessage (buf) {
var dict, trailer let dict
let trailer
try { try {
var str = buf.toString() const str = buf.toString()
var trailerIndex = str.indexOf('ee') + 2 const trailerIndex = str.indexOf('ee') + 2
dict = bencode.decode(str.substring(0, trailerIndex)) dict = bencode.decode(str.substring(0, trailerIndex))
trailer = buf.slice(trailerIndex) trailer = buf.slice(trailerIndex)
} catch (err) { } catch (err) {
@ -96,7 +93,7 @@ module.exports = function (metadata) {
* Ask the peer to send metadata. * Ask the peer to send metadata.
* @public * @public
*/ */
utMetadata.prototype.fetch = function () { fetch () {
if (this._metadataComplete) { if (this._metadataComplete) {
return return
} }
@ -110,17 +107,17 @@ module.exports = function (metadata) {
* Stop asking the peer to send metadata. * Stop asking the peer to send metadata.
* @public * @public
*/ */
utMetadata.prototype.cancel = function () { cancel () {
this._fetching = false this._fetching = false
} }
utMetadata.prototype.setMetadata = function (metadata) { setMetadata (metadata) {
if (this._metadataComplete) return true if (this._metadataComplete) return true
debug('set metadata') debug('set 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 = bencode.decode(metadata).info const info = bencode.decode(metadata).info
if (info) { if (info) {
metadata = bencode.encode(info) metadata = bencode.encode(info)
} }
@ -138,50 +135,52 @@ 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', bencode.encode({ info: bencode.decode(this.metadata) })) this.emit('metadata', bencode.encode({
info: bencode.decode(this.metadata)
}))
return true return true
} }
utMetadata.prototype._send = function (dict, trailer) { _send (dict, trailer) {
var buf = bencode.encode(dict) let buf = bencode.encode(dict)
if (Buffer.isBuffer(trailer)) { if (Buffer.isBuffer(trailer)) {
buf = Buffer.concat([buf, trailer]) buf = Buffer.concat([buf, trailer])
} }
this._wire.extended('ut_metadata', buf) this._wire.extended('ut_metadata', buf)
} }
utMetadata.prototype._request = function (piece) { _request (piece) {
this._send({ msg_type: 0, piece: piece }) this._send({ msg_type: 0, piece })
} }
utMetadata.prototype._data = function (piece, buf, totalSize) { _data (piece, buf, totalSize) {
var msg = { msg_type: 1, piece: piece } const msg = { msg_type: 1, piece }
if (typeof totalSize === 'number') { if (typeof totalSize === 'number') {
msg.total_size = totalSize msg.total_size = totalSize
} }
this._send(msg, buf) this._send(msg, buf)
} }
utMetadata.prototype._reject = function (piece) { _reject (piece) {
this._send({ msg_type: 2, piece: piece }) this._send({ msg_type: 2, piece })
} }
utMetadata.prototype._onRequest = function (piece) { _onRequest (piece) {
if (!this._metadataComplete) { if (!this._metadataComplete) {
this._reject(piece) this._reject(piece)
return return
} }
var start = piece * PIECE_LENGTH const start = piece * PIECE_LENGTH
var end = start + PIECE_LENGTH let end = start + PIECE_LENGTH
if (end > this._metadataSize) { if (end > this._metadataSize) {
end = this._metadataSize end = this._metadataSize
} }
var buf = this.metadata.slice(start, end) const buf = this.metadata.slice(start, end)
this._data(piece, buf, this._metadataSize) this._data(piece, buf, this._metadataSize)
} }
utMetadata.prototype._onData = function (piece, buf, totalSize) { _onData (piece, buf, totalSize) {
if (buf.length > PIECE_LENGTH) { if (buf.length > PIECE_LENGTH) {
return return
} }
@ -190,9 +189,10 @@ module.exports = function (metadata) {
this._checkDone() this._checkDone()
} }
utMetadata.prototype._onReject = function (piece) { _onReject (piece) {
if (this._remainingRejects > 0 && this._fetching) { if (this._remainingRejects > 0 && this._fetching) {
// If we haven't been rejected too much, then try to request the piece again // If we haven't been rejected too much,
// then try to request the piece again
this._request(piece) this._request(piece)
this._remainingRejects -= 1 this._remainingRejects -= 1
} else { } else {
@ -200,16 +200,16 @@ module.exports = function (metadata) {
} }
} }
utMetadata.prototype._requestPieces = function () { _requestPieces () {
this.metadata = Buffer.alloc(this._metadataSize) this.metadata = Buffer.alloc(this._metadataSize)
for (var piece = 0; piece < this._numPieces; piece++) { for (let piece = 0; piece < this._numPieces; piece++) {
this._request(piece) this._request(piece)
} }
} }
utMetadata.prototype._checkDone = function () { _checkDone () {
var done = true let done = true
for (var piece = 0; piece < this._numPieces; piece++) { for (let piece = 0; piece < this._numPieces; piece++) {
if (!this._bitfield.get(piece)) { if (!this._bitfield.get(piece)) {
done = false done = false
break break
@ -218,14 +218,14 @@ module.exports = function (metadata) {
if (!done) return if (!done) return
// attempt to set metadata -- may fail sha1 check // attempt to set metadata -- may fail sha1 check
var success = this.setMetadata(this.metadata) const success = this.setMetadata(this.metadata)
if (!success) { if (!success) {
this._failedMetadata() this._failedMetadata()
} }
} }
utMetadata.prototype._failedMetadata = function () { _failedMetadata () {
// reset bitfield & try again // reset bitfield & try again
this._bitfield = new BitField(0, { grow: BITFIELD_GROW }) this._bitfield = new BitField(0, { grow: BITFIELD_GROW })
this._remainingRejects -= this._numPieces this._remainingRejects -= this._numPieces
@ -235,6 +235,10 @@ module.exports = function (metadata) {
this.emit('warning', new Error('Peer sent invalid metadata')) this.emit('warning', new Error('Peer sent invalid metadata'))
} }
} }
}
// Name of the bittorrent-protocol extension
utMetadata.prototype.name = 'ut_metadata'
return utMetadata return utMetadata
} }

View File

@ -14,7 +14,6 @@
"bencode": "^2.0.0", "bencode": "^2.0.0",
"bitfield": "^2.0.0", "bitfield": "^2.0.0",
"debug": "^3.1.0", "debug": "^3.1.0",
"inherits": "^2.0.1",
"simple-sha1": "^2.0.0" "simple-sha1": "^2.0.0"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1,11 +1,11 @@
var bencode = require('bencode') const { leavesMetadata } = require('webtorrent-fixtures')
var fixtures = require('webtorrent-fixtures') const bencode = require('bencode')
var Protocol = require('bittorrent-protocol') const Protocol = require('bittorrent-protocol')
var test = require('tape') const test = require('tape')
var utMetadata = require('../') const utMetadata = require('../')
test('wire.use(utMetadata())', function (t) { test('wire.use(utMetadata())', t => {
var wire = new Protocol() const wire = new Protocol()
wire.pipe(wire) wire.pipe(wire)
wire.use(utMetadata()) wire.use(utMetadata())
@ -17,15 +17,18 @@ test('wire.use(utMetadata())', function (t) {
t.end() t.end()
}) })
test('wire.use(utMetadata(metadata))', function (t) { test('wire.use(utMetadata(metadata))', t => {
var wire = new Protocol() const wire = new Protocol()
wire.pipe(wire) wire.pipe(wire)
wire.use(utMetadata(fixtures.leavesMetadata.torrent)) wire.use(utMetadata(leavesMetadata.torrent))
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'), bencode.encode(bencode.decode(fixtures.leavesMetadata.torrent).info).toString('hex')) t.equal(
wire.ut_metadata.metadata.toString('hex'),
bencode.encode(bencode.decode(leavesMetadata.torrent).info).toString('hex')
)
t.end() t.end()
}) })

View File

@ -1,34 +1,39 @@
var bencode = require('bencode') const { leavesMetadata, sintel } = require('webtorrent-fixtures')
var fixtures = require('webtorrent-fixtures') const bencode = require('bencode')
var Protocol = require('bittorrent-protocol') const Protocol = require('bittorrent-protocol')
var test = require('tape') const test = require('tape')
var utMetadata = require('../') const utMetadata = require('../')
var id1 = Buffer.from('01234567890123456789') const id1 = Buffer.from('01234567890123456789')
var id2 = Buffer.from('12345678901234567890') const id2 = Buffer.from('12345678901234567890')
test('fetch()', function (t) { test('fetch()', t => {
t.plan(3) t.plan(3)
var wire1 = new Protocol() const wire1 = new Protocol()
var wire2 = new Protocol() const wire2 = new Protocol()
wire1.pipe(wire2).pipe(wire1) wire1.pipe(wire2).pipe(wire1)
wire1.use(utMetadata(fixtures.leavesMetadata.torrent)) // wire1 already has metadata wire1.use(utMetadata(leavesMetadata.torrent)) // wire1 already has metadata
wire2.use(utMetadata()) // wire2 does not wire2.use(utMetadata()) // wire2 does not
wire2.ut_metadata.fetch() wire2.ut_metadata.fetch()
wire2.ut_metadata.on('metadata', function (_metadata) { wire2.ut_metadata.on('metadata', _metadata => {
// got metadata! // got metadata!
t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(fixtures.leavesMetadata.torrent).info }).toString('hex')) t.equal(
_metadata.toString('hex'),
bencode.encode({
info: bencode.decode(leavesMetadata.torrent).info
}).toString('hex')
)
}) })
wire2.on('handshake', function (infoHash, peerId, extensions) { wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2) wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
}) })
wire2.on('extended', function (ext) { wire2.on('extended', ext => {
if (ext === 'handshake') { if (ext === 'handshake') {
t.pass('got extended handshake') t.pass('got extended handshake')
} else if (ext === 'ut_metadata') { } else if (ext === 'ut_metadata') {
@ -41,14 +46,14 @@ test('fetch()', function (t) {
} }
}) })
wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1) wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
}) })
test('fetch() from peer without metadata', function (t) { test('fetch() from peer without metadata', t => {
t.plan(2) t.plan(2)
var wire1 = new Protocol() const wire1 = new Protocol()
var wire2 = new Protocol() const wire2 = new Protocol()
wire1.pipe(wire2).pipe(wire1) wire1.pipe(wire2).pipe(wire1)
wire1.use(utMetadata()) // neither wire has metadata wire1.use(utMetadata()) // neither wire has metadata
@ -56,25 +61,25 @@ test('fetch() from peer without metadata', function (t) {
wire2.ut_metadata.fetch() wire2.ut_metadata.fetch()
wire2.ut_metadata.on('metadata', function () { wire2.ut_metadata.on('metadata', () => {
t.fail('No "metadata" event should fire') t.fail('No "metadata" event should fire')
}) })
wire1.ut_metadata.onMessage = function () { wire1.ut_metadata.onMessage = () => {
t.fail('No messages should be sent to wire1') t.fail('No messages should be sent to wire1')
// No messages should be sent because wire1 never sent metadata_size in the // No messages should be sent because wire1 never sent metadata_size
// extended handshake, so he doesn't have metadata // in the extended handshake, so he doesn't have metadata
} }
wire2.ut_metadata.on('warning', function () { wire2.ut_metadata.on('warning', () => {
t.pass('got warning about peer missing metadata') t.pass('got warning about peer missing metadata')
}) })
wire2.on('handshake', function (infoHash, peerId, extensions) { wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2) wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
}) })
wire2.on('extended', function (ext) { wire2.on('extended', ext => {
if (ext === 'handshake') { if (ext === 'handshake') {
t.pass('got extended handshake') t.pass('got extended handshake')
} else if (ext === 'ut_metadata') { } else if (ext === 'ut_metadata') {
@ -84,36 +89,44 @@ test('fetch() from peer without metadata', function (t) {
} }
}) })
wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1) wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
}) })
test('fetch when peer gets metadata later (setMetadata)', function (t) { test('fetch when peer gets metadata later (setMetadata)', t => {
t.plan(3) t.plan(3)
var wire1 = new Protocol() const wire1 = new Protocol()
var wire2 = new Protocol() const wire2 = new Protocol()
wire1.pipe(wire2).pipe(wire1) wire1.pipe(wire2).pipe(wire1)
wire1.use(utMetadata()) // wire1 starts without metadata wire1.use(utMetadata()) // wire1 starts without metadata
process.nextTick(function () { process.nextTick(() => {
wire1.ut_metadata.setMetadata(fixtures.leavesMetadata.torrent) // wire1 gets metadata later // wire1 gets metadata later
wire1.ut_metadata.setMetadata(leavesMetadata.torrent)
process.nextTick(function () { process.nextTick(() => {
// wire2 does not start with metadata, but connects to wire1 after it gets metadata // wire2 does not start with metadata,
// but connects to wire1 after it gets metadata
wire2.use(utMetadata()) wire2.use(utMetadata())
wire2.ut_metadata.fetch() wire2.ut_metadata.fetch()
wire2.ut_metadata.on('metadata', function (_metadata) { wire2.ut_metadata.on('metadata', _metadata => {
// got metadata! // got metadata!
t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(fixtures.leavesMetadata.torrent).info }).toString('hex')) t.equal(
_metadata.toString('hex'),
bencode.encode({
info: bencode.decode(leavesMetadata.torrent).info
}).toString('hex')
)
}) })
wire2.on('handshake', function (infoHash, peerId, extensions) { wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2) wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
}) })
wire2.on('extended', function (ext) { wire2.on('extended', ext => {
if (ext === 'handshake') { if (ext === 'handshake') {
t.pass('got extended handshake') t.pass('got extended handshake')
} else if (ext === 'ut_metadata') { } else if (ext === 'ut_metadata') {
@ -126,37 +139,43 @@ test('fetch when peer gets metadata later (setMetadata)', function (t) {
} }
}) })
wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1) wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
}) })
}) })
}) })
test('fetch() large torrent', function (t) { test('fetch() large torrent', t => {
t.plan(4) t.plan(4)
var wire1 = new Protocol() const wire1 = new Protocol()
var wire2 = new Protocol() const wire2 = new Protocol()
wire1.pipe(wire2).pipe(wire1) wire1.pipe(wire2).pipe(wire1)
wire1.use(utMetadata(fixtures.sintel.torrent)) // wire1 already has metadata wire1.use(utMetadata(sintel.torrent)) // wire1 already has metadata
wire2.use(utMetadata()) // wire2 does not wire2.use(utMetadata()) // wire2 does not
wire2.ut_metadata.fetch() wire2.ut_metadata.fetch()
wire2.ut_metadata.on('metadata', function (_metadata) { wire2.ut_metadata.on('metadata', _metadata => {
// got metadata! // got metadata!
t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(fixtures.sintel.torrent).info }).toString('hex')) t.equal(
_metadata.toString('hex'),
bencode.encode({
info: bencode.decode(sintel.torrent).info
}).toString('hex')
)
}) })
wire2.on('handshake', function (infoHash, peerId, extensions) { wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(fixtures.sintel.parsedTorrent.infoHash, id2) wire2.handshake(sintel.parsedTorrent.infoHash, id2)
}) })
wire2.on('extended', function (ext) { wire2.on('extended', ext => {
if (ext === 'handshake') { if (ext === 'handshake') {
t.pass('got extended handshake') t.pass('got extended handshake')
} else if (ext === 'ut_metadata') { } else if (ext === 'ut_metadata') {
// note: this should get called twice, once for each block of the sintel metadata // note: this should get called twice,
// once for each block of the sintel metadata
t.pass('got extended ut_metadata message') t.pass('got extended ut_metadata message')
// this is emitted for consistency's sake, but it's ignored // this is emitted for consistency's sake, but it's ignored
@ -167,17 +186,17 @@ test('fetch() large torrent', function (t) {
} }
}) })
wire1.handshake(fixtures.sintel.parsedTorrent.infoHash, id1) wire1.handshake(sintel.parsedTorrent.infoHash, id1)
}) })
test('discard invalid metadata', function (t) { test('discard invalid metadata', t => {
t.plan(1) t.plan(1)
var wire1 = new Protocol() const wire1 = new Protocol()
var wire2 = new Protocol() const wire2 = new Protocol()
wire1.pipe(wire2).pipe(wire1) wire1.pipe(wire2).pipe(wire1)
var invalidMetadata = fixtures.leavesMetadata.torrent.slice(0) const invalidMetadata = leavesMetadata.torrent.slice(0)
invalidMetadata[55] = 65 // mess up a byte in the info block invalidMetadata[55] = 65 // mess up a byte in the info block
wire1.use(utMetadata(invalidMetadata)) wire1.use(utMetadata(invalidMetadata))
@ -185,17 +204,17 @@ test('discard invalid metadata', function (t) {
wire2.ut_metadata.fetch() wire2.ut_metadata.fetch()
wire2.ut_metadata.on('metadata', function () { wire2.ut_metadata.on('metadata', () => {
t.fail('No "metadata" event should fire') t.fail('No "metadata" event should fire')
}) })
wire2.ut_metadata.on('warning', function () { wire2.ut_metadata.on('warning', () => {
t.pass('got warning because peer sent reject too much') t.pass('got warning because peer sent reject too much')
}) })
wire2.on('handshake', function (infoHash, peerId, extensions) { wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2) wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
}) })
wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1) wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
}) })