Compare commits
	
		
			No commits in common. "master" and "v3.2.0" have entirely different histories.
		
	
	
		
	
		
@ -1,2 +0,0 @@
 | 
				
			|||||||
.travis.yml
 | 
					 | 
				
			||||||
test/
 | 
					 | 
				
			||||||
@ -1,3 +1,7 @@
 | 
				
			|||||||
language: node_js
 | 
					language: node_js
 | 
				
			||||||
node_js:
 | 
					node_js:
 | 
				
			||||||
- lts/*
 | 
					- lts/*
 | 
				
			||||||
 | 
					env:
 | 
				
			||||||
 | 
					  global:
 | 
				
			||||||
 | 
					  - secure: Htsm8eeR11ph6lbAOLPbFfJGou8eDPQMfLXUBx6c9+QgrbBKHM1RKscJNvSi1AM1rOOKSk1pCZwqVR2n+9kQs30MxZppyIngiy5Jcz7/b5FDvnm/JkNzq+7sEeGZzRK11XmFkWuYRK9TvUaDuuo/NOc1NJc0ics/gzrjNLcT1tw=
 | 
				
			||||||
 | 
					  - secure: FufpUBE/caYoQ5MfsIWFYVM0C4gj+4OSaqu6W8exucFDB68zTmO0te11tOwg98PhFnSaDbJ7h3gWXYGTpbFUErkeMBRMTsNjYYoQhcWWjzD4yo5iIfh215TJ5vhE4sIeCCi0TxHgbJkF3KthsUyaOdy/Q3sFISZ/VQSPFBDdmbU=
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										12
									
								
								.zuul.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								.zuul.yml
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					ui: tape
 | 
				
			||||||
 | 
					browsers:
 | 
				
			||||||
 | 
					  - name: chrome
 | 
				
			||||||
 | 
					    version: latest
 | 
				
			||||||
 | 
					  - name: firefox
 | 
				
			||||||
 | 
					    version: latest
 | 
				
			||||||
 | 
					  - name: safari
 | 
				
			||||||
 | 
					    version: latest
 | 
				
			||||||
 | 
					  - name: microsoftedge
 | 
				
			||||||
 | 
					    version: latest
 | 
				
			||||||
 | 
					  - name: iphone
 | 
				
			||||||
 | 
					    version: latest
 | 
				
			||||||
							
								
								
									
										32
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								README.md
									
									
									
									
									
								
							@ -11,6 +11,8 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
### BitTorrent Extension for Peers to Send Metadata Files (BEP 9)
 | 
					### BitTorrent Extension for Peers to Send Metadata Files (BEP 9)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[](https://saucelabs.com/u/ut_metadata)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
JavaScript implementation of the [Extension for Peers to Send Metadata Files (BEP 9)](http://www.bittorrent.org/beps/bep_0009.html). Use with [bittorrent-protocol](https://www.npmjs.com/package/bittorrent-protocol).
 | 
					JavaScript implementation of the [Extension for Peers to Send Metadata Files (BEP 9)](http://www.bittorrent.org/beps/bep_0009.html). Use with [bittorrent-protocol](https://www.npmjs.com/package/bittorrent-protocol).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The purpose of this extension is to allow clients to join a swarm and complete a download without the need of downloading a .torrent file first. This extension instead allows clients to download the metadata from peers. It makes it possible to support magnet links, a link on a web page only containing enough information to join the swarm (the info hash).
 | 
					The purpose of this extension is to allow clients to join a swarm and complete a download without the need of downloading a .torrent file first. This extension instead allows clients to download the metadata from peers. It makes it possible to support magnet links, a link on a web page only containing enough information to join the swarm (the info hash).
 | 
				
			||||||
@ -30,15 +32,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
 | 
				
			||||||
const Protocol = require('bittorrent-protocol')
 | 
					var Protocol = require('bittorrent-protocol')
 | 
				
			||||||
const net = require('net')
 | 
					var net = require('net')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
net.createServer(socket => {
 | 
					net.createServer(function (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', (infoHash, peerId) => {
 | 
					  wire.on('handshake', function (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 +50,12 @@ net.createServer(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
 | 
				
			||||||
const Protocol = require('bittorrent-protocol')
 | 
					var Protocol = require('bittorrent-protocol')
 | 
				
			||||||
const net = require('net')
 | 
					var net = require('net')
 | 
				
			||||||
const ut_metadata = require('ut_metadata')
 | 
					var ut_metadata = require('ut_metadata')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
net.createServer(socket => {
 | 
					net.createServer(function (socket) {
 | 
				
			||||||
  const wire = new Protocol()
 | 
					  var wire = new Protocol()
 | 
				
			||||||
  socket.pipe(wire).pipe(socket)
 | 
					  socket.pipe(wire).pipe(socket)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // initialize the extension
 | 
					  // initialize the extension
 | 
				
			||||||
@ -65,7 +67,7 @@ net.createServer(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', metadata => {
 | 
					  wire.ut_metadata.on('metadata', function (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 +77,12 @@ net.createServer(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', err => {
 | 
					  wire.ut_metadata.on('warning', function (err) {
 | 
				
			||||||
    console.log(err.message)
 | 
					    console.log(err.message)
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // handle handshake
 | 
					  // handle handshake
 | 
				
			||||||
  wire.on('handshake', (infoHash, peerId) => {
 | 
					  wire.on('handshake', function (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 +97,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
 | 
				
			||||||
const metadata = fs.readFileSync(__dirname + '/file.torrent')
 | 
					var metadata = fs.readFileSync(__dirname + '/file.torrent')
 | 
				
			||||||
wire.use(ut_metadata(metadata))
 | 
					wire.use(ut_metadata(metadata))
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -119,7 +121,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', metadata => {
 | 
					wire.ut_metadata.on('metadata', function (metadata) {
 | 
				
			||||||
  console.log(Buffer.isBuffer(metadata)) // true
 | 
					  console.log(Buffer.isBuffer(metadata)) // true
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
@ -136,7 +138,7 @@ Fired if:
 | 
				
			|||||||
 - the peer repeatedly sent invalid data
 | 
					 - the peer repeatedly sent invalid data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
```js
 | 
					```js
 | 
				
			||||||
wire.ut_metadata.on('warning', err => {
 | 
					wire.ut_metadata.on('warning', function (err) {
 | 
				
			||||||
  console.log(err.message)
 | 
					  console.log(err.message)
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										121
									
								
								index.js
									
									
									
									
									
								
							
							
						
						
									
										121
									
								
								index.js
									
									
									
									
									
								
							@ -1,30 +1,31 @@
 | 
				
			|||||||
/*! ut_metadata. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */
 | 
					var bencode = require('bencode')
 | 
				
			||||||
const { EventEmitter } = require('events')
 | 
					var BitField = require('bitfield')
 | 
				
			||||||
const bencode = require('bencode')
 | 
					var Buffer = require('safe-buffer').Buffer
 | 
				
			||||||
const BitField = require('bitfield').default
 | 
					var debug = require('debug')('ut_metadata')
 | 
				
			||||||
const debug = require('debug')('ut_metadata')
 | 
					var EventEmitter = require('events').EventEmitter
 | 
				
			||||||
const sha1 = require('simple-sha1')
 | 
					var inherits = require('inherits')
 | 
				
			||||||
 | 
					var sha1 = require('simple-sha1')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const MAX_METADATA_SIZE = 1E7 // 10 MB
 | 
					var MAX_METADATA_SIZE = 10000000 // 10MB
 | 
				
			||||||
const BITFIELD_GROW = 1E3
 | 
					var BITFIELD_GROW = 1000
 | 
				
			||||||
const PIECE_LENGTH = 1 << 14 // 16 KiB
 | 
					var PIECE_LENGTH = 16 * 1024
 | 
				
			||||||
 | 
					
 | 
				
			||||||
module.exports = metadata => {
 | 
					module.exports = function (metadata) {
 | 
				
			||||||
  class utMetadata extends EventEmitter {
 | 
					  inherits(utMetadata, EventEmitter)
 | 
				
			||||||
    constructor (wire) {
 | 
					
 | 
				
			||||||
      super()
 | 
					  function utMetadata (wire) {
 | 
				
			||||||
 | 
					    EventEmitter.call(this)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    this._wire = wire
 | 
					    this._wire = wire
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      this._fetching = false
 | 
					 | 
				
			||||||
    this._metadataComplete = false
 | 
					    this._metadataComplete = false
 | 
				
			||||||
    this._metadataSize = null
 | 
					    this._metadataSize = null
 | 
				
			||||||
      // how many reject messages to tolerate before quitting
 | 
					    this._remainingRejects = null // how many reject messages to tolerate before quitting
 | 
				
			||||||
      this._remainingRejects = null
 | 
					    this._fetching = false
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      // The largest torrent file that I know of is ~1-2MB, which is ~100
 | 
					    // The largest .torrent file that I know of is ~1-2MB, which is ~100 pieces.
 | 
				
			||||||
      // pieces. Therefore, cap the bitfield to 10x that (1000 pieces) so a
 | 
					    // Therefore, cap the bitfield to 10x that (1000 pieces) so a malicious peer can't
 | 
				
			||||||
      // malicious peer can't make it grow to fill all memory.
 | 
					    // 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,11 +33,14 @@ module.exports = metadata => {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    onHandshake (infoHash, peerId, extensions) {
 | 
					  // Name of the bittorrent-protocol extension
 | 
				
			||||||
 | 
					  utMetadata.prototype.name = 'ut_metadata'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  utMetadata.prototype.onHandshake = function (infoHash, peerId, extensions) {
 | 
				
			||||||
    this._infoHash = infoHash
 | 
					    this._infoHash = infoHash
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    onExtendedHandshake (handshake) {
 | 
					  utMetadata.prototype.onExtendedHandshake = function (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'))
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -53,15 +57,16 @@ module.exports = metadata => {
 | 
				
			|||||||
    this._numPieces = Math.ceil(this._metadataSize / PIECE_LENGTH)
 | 
					    this._numPieces = Math.ceil(this._metadataSize / PIECE_LENGTH)
 | 
				
			||||||
    this._remainingRejects = this._numPieces * 2
 | 
					    this._remainingRejects = this._numPieces * 2
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (this._fetching) {
 | 
				
			||||||
      this._requestPieces()
 | 
					      this._requestPieces()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    onMessage (buf) {
 | 
					  utMetadata.prototype.onMessage = function (buf) {
 | 
				
			||||||
      let dict
 | 
					    var dict, trailer
 | 
				
			||||||
      let trailer
 | 
					 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
        const str = buf.toString()
 | 
					      var str = buf.toString()
 | 
				
			||||||
        const trailerIndex = str.indexOf('ee') + 2
 | 
					      var 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) {
 | 
				
			||||||
@ -92,7 +97,7 @@ module.exports = metadata => {
 | 
				
			|||||||
   * Ask the peer to send metadata.
 | 
					   * Ask the peer to send metadata.
 | 
				
			||||||
   * @public
 | 
					   * @public
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
    fetch () {
 | 
					  utMetadata.prototype.fetch = function () {
 | 
				
			||||||
    if (this._metadataComplete) {
 | 
					    if (this._metadataComplete) {
 | 
				
			||||||
      return
 | 
					      return
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -106,17 +111,17 @@ module.exports = metadata => {
 | 
				
			|||||||
   * Stop asking the peer to send metadata.
 | 
					   * Stop asking the peer to send metadata.
 | 
				
			||||||
   * @public
 | 
					   * @public
 | 
				
			||||||
   */
 | 
					   */
 | 
				
			||||||
    cancel () {
 | 
					  utMetadata.prototype.cancel = function () {
 | 
				
			||||||
    this._fetching = false
 | 
					    this._fetching = false
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    setMetadata (metadata) {
 | 
					  utMetadata.prototype.setMetadata = function (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 {
 | 
				
			||||||
        const info = bencode.decode(metadata).info
 | 
					      var info = bencode.decode(metadata).info
 | 
				
			||||||
      if (info) {
 | 
					      if (info) {
 | 
				
			||||||
        metadata = bencode.encode(info)
 | 
					        metadata = bencode.encode(info)
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
@ -134,53 +139,51 @@ module.exports = 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({
 | 
					    this.emit('metadata', bencode.encode({ info: bencode.decode(this.metadata) }))
 | 
				
			||||||
        info: bencode.decode(this.metadata)
 | 
					 | 
				
			||||||
      }))
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return true
 | 
					    return true
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _send (dict, trailer) {
 | 
					  utMetadata.prototype._send = function (dict, trailer) {
 | 
				
			||||||
      let buf = bencode.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])
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    this._wire.extended('ut_metadata', buf)
 | 
					    this._wire.extended('ut_metadata', buf)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _request (piece) {
 | 
					  utMetadata.prototype._request = function (piece) {
 | 
				
			||||||
      this._send({ msg_type: 0, piece })
 | 
					    this._send({ msg_type: 0, piece: piece })
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _data (piece, buf, totalSize) {
 | 
					  utMetadata.prototype._data = function (piece, buf, totalSize) {
 | 
				
			||||||
      const msg = { msg_type: 1, piece }
 | 
					    var msg = { msg_type: 1, piece: 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)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _reject (piece) {
 | 
					  utMetadata.prototype._reject = function (piece) {
 | 
				
			||||||
      this._send({ msg_type: 2, piece })
 | 
					    this._send({ msg_type: 2, piece: piece })
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _onRequest (piece) {
 | 
					  utMetadata.prototype._onRequest = function (piece) {
 | 
				
			||||||
    if (!this._metadataComplete) {
 | 
					    if (!this._metadataComplete) {
 | 
				
			||||||
      this._reject(piece)
 | 
					      this._reject(piece)
 | 
				
			||||||
      return
 | 
					      return
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
      const start = piece * PIECE_LENGTH
 | 
					    var start = piece * PIECE_LENGTH
 | 
				
			||||||
      let end = start + PIECE_LENGTH
 | 
					    var end = start + PIECE_LENGTH
 | 
				
			||||||
    if (end > this._metadataSize) {
 | 
					    if (end > this._metadataSize) {
 | 
				
			||||||
      end = this._metadataSize
 | 
					      end = this._metadataSize
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
      const buf = this.metadata.slice(start, end)
 | 
					    var buf = this.metadata.slice(start, end)
 | 
				
			||||||
    this._data(piece, buf, this._metadataSize)
 | 
					    this._data(piece, buf, this._metadataSize)
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _onData (piece, buf, totalSize) {
 | 
					  utMetadata.prototype._onData = function (piece, buf, totalSize) {
 | 
				
			||||||
      if (buf.length > PIECE_LENGTH || !this._fetching) {
 | 
					    if (buf.length > PIECE_LENGTH) {
 | 
				
			||||||
      return
 | 
					      return
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    buf.copy(this.metadata, piece * PIECE_LENGTH)
 | 
					    buf.copy(this.metadata, piece * PIECE_LENGTH)
 | 
				
			||||||
@ -188,10 +191,9 @@ module.exports = metadata => {
 | 
				
			|||||||
    this._checkDone()
 | 
					    this._checkDone()
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _onReject (piece) {
 | 
					  utMetadata.prototype._onReject = function (piece) {
 | 
				
			||||||
    if (this._remainingRejects > 0 && this._fetching) {
 | 
					    if (this._remainingRejects > 0 && this._fetching) {
 | 
				
			||||||
        // If we haven't been rejected too much,
 | 
					      // If we haven't been rejected too much, then try to request the piece again
 | 
				
			||||||
        // then try to request the piece again
 | 
					 | 
				
			||||||
      this._request(piece)
 | 
					      this._request(piece)
 | 
				
			||||||
      this._remainingRejects -= 1
 | 
					      this._remainingRejects -= 1
 | 
				
			||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
@ -199,17 +201,16 @@ module.exports = metadata => {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _requestPieces () {
 | 
					  utMetadata.prototype._requestPieces = function () {
 | 
				
			||||||
      if (!this._fetching) return
 | 
					 | 
				
			||||||
    this.metadata = Buffer.alloc(this._metadataSize)
 | 
					    this.metadata = Buffer.alloc(this._metadataSize)
 | 
				
			||||||
      for (let piece = 0; piece < this._numPieces; piece++) {
 | 
					    for (var piece = 0; piece < this._numPieces; piece++) {
 | 
				
			||||||
      this._request(piece)
 | 
					      this._request(piece)
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _checkDone () {
 | 
					  utMetadata.prototype._checkDone = function () {
 | 
				
			||||||
      let done = true
 | 
					    var done = true
 | 
				
			||||||
      for (let piece = 0; piece < this._numPieces; piece++) {
 | 
					    for (var piece = 0; piece < this._numPieces; piece++) {
 | 
				
			||||||
      if (!this._bitfield.get(piece)) {
 | 
					      if (!this._bitfield.get(piece)) {
 | 
				
			||||||
        done = false
 | 
					        done = false
 | 
				
			||||||
        break
 | 
					        break
 | 
				
			||||||
@ -218,14 +219,14 @@ module.exports = metadata => {
 | 
				
			|||||||
    if (!done) return
 | 
					    if (!done) return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // attempt to set metadata -- may fail sha1 check
 | 
					    // attempt to set metadata -- may fail sha1 check
 | 
				
			||||||
      const success = this.setMetadata(this.metadata)
 | 
					    var success = this.setMetadata(this.metadata)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!success) {
 | 
					    if (!success) {
 | 
				
			||||||
      this._failedMetadata()
 | 
					      this._failedMetadata()
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _failedMetadata () {
 | 
					  utMetadata.prototype._failedMetadata = function () {
 | 
				
			||||||
    // 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,10 +236,6 @@ module.exports = 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
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										42
									
								
								package.json
									
									
									
									
									
								
							
							
						
						
									
										42
									
								
								package.json
									
									
									
									
									
								
							@ -1,9 +1,9 @@
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
  "name": "ut_metadata",
 | 
					  "name": "ut_metadata",
 | 
				
			||||||
  "description": "Extension for Peers to Send Metadata Files (BEP 9)",
 | 
					  "description": "Extension for Peers to Send Metadata Files (BEP 9)",
 | 
				
			||||||
  "version": "3.5.2",
 | 
					  "version": "3.2.0",
 | 
				
			||||||
  "author": {
 | 
					  "author": {
 | 
				
			||||||
    "name": "WebTorrent LLC",
 | 
					    "name": "WebTorrent, LLC",
 | 
				
			||||||
    "email": "feross@webtorrent.io",
 | 
					    "email": "feross@webtorrent.io",
 | 
				
			||||||
    "url": "https://webtorrent.io"
 | 
					    "url": "https://webtorrent.io"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
@ -11,17 +11,20 @@
 | 
				
			|||||||
    "url": "https://github.com/webtorrent/ut_metadata/issues"
 | 
					    "url": "https://github.com/webtorrent/ut_metadata/issues"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "dependencies": {
 | 
					  "dependencies": {
 | 
				
			||||||
    "bencode": "^2.0.1",
 | 
					    "bencode": "^2.0.0",
 | 
				
			||||||
    "bitfield": "^4.0.0",
 | 
					    "bitfield": "^2.0.0",
 | 
				
			||||||
    "debug": "^4.2.0",
 | 
					    "debug": "^3.1.0",
 | 
				
			||||||
    "simple-sha1": "^3.0.1"
 | 
					    "inherits": "^2.0.1",
 | 
				
			||||||
 | 
					    "safe-buffer": "^5.0.1",
 | 
				
			||||||
 | 
					    "simple-sha1": "^2.0.0"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "devDependencies": {
 | 
					  "devDependencies": {
 | 
				
			||||||
    "bittorrent-protocol": "^3.1.2",
 | 
					    "bittorrent-protocol": "^2.0.0",
 | 
				
			||||||
    "brfs": "^2.0.2",
 | 
					    "brfs": "^1.2.0",
 | 
				
			||||||
    "standard": "*",
 | 
					    "standard": "*",
 | 
				
			||||||
    "tape": "^5.0.1",
 | 
					    "tape": "^4.0.0",
 | 
				
			||||||
    "webtorrent-fixtures": "^1.7.3"
 | 
					    "webtorrent-fixtures": "^1.3.0",
 | 
				
			||||||
 | 
					    "zuul": "^3.8.0"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "keywords": [
 | 
					  "keywords": [
 | 
				
			||||||
    "Extension for Peers to Send Metadata Files",
 | 
					    "Extension for Peers to Send Metadata Files",
 | 
				
			||||||
@ -41,20 +44,9 @@
 | 
				
			|||||||
    "url": "git://github.com/webtorrent/ut_metadata.git"
 | 
					    "url": "git://github.com/webtorrent/ut_metadata.git"
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "scripts": {
 | 
					  "scripts": {
 | 
				
			||||||
    "test": "standard && tape test/*.js"
 | 
					    "test": "standard && npm run test-node && npm run test-browser",
 | 
				
			||||||
  },
 | 
					    "test-browser": "zuul -- test/*.js",
 | 
				
			||||||
  "funding": [
 | 
					    "test-browser-local": "zuul --local -- test/*.js",
 | 
				
			||||||
    {
 | 
					    "test-node": "tape test/*.js"
 | 
				
			||||||
      "type": "github",
 | 
					 | 
				
			||||||
      "url": "https://github.com/sponsors/feross"
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
      "type": "patreon",
 | 
					 | 
				
			||||||
      "url": "https://www.patreon.com/feross"
 | 
					 | 
				
			||||||
    },
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
      "type": "consulting",
 | 
					 | 
				
			||||||
      "url": "https://feross.org/support"
 | 
					 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  ]
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,11 +1,11 @@
 | 
				
			|||||||
const { leavesMetadata } = require('webtorrent-fixtures')
 | 
					var bencode = require('bencode')
 | 
				
			||||||
const bencode = require('bencode')
 | 
					var fixtures = require('webtorrent-fixtures')
 | 
				
			||||||
const Protocol = require('bittorrent-protocol')
 | 
					var Protocol = require('bittorrent-protocol')
 | 
				
			||||||
const test = require('tape')
 | 
					var test = require('tape')
 | 
				
			||||||
const utMetadata = require('../')
 | 
					var utMetadata = require('../')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test('wire.use(utMetadata())', t => {
 | 
					test('wire.use(utMetadata())', function (t) {
 | 
				
			||||||
  const wire = new Protocol()
 | 
					  var wire = new Protocol()
 | 
				
			||||||
  wire.pipe(wire)
 | 
					  wire.pipe(wire)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire.use(utMetadata())
 | 
					  wire.use(utMetadata())
 | 
				
			||||||
@ -17,18 +17,15 @@ test('wire.use(utMetadata())', t => {
 | 
				
			|||||||
  t.end()
 | 
					  t.end()
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test('wire.use(utMetadata(metadata))', t => {
 | 
					test('wire.use(utMetadata(metadata))', function (t) {
 | 
				
			||||||
  const wire = new Protocol()
 | 
					  var wire = new Protocol()
 | 
				
			||||||
  wire.pipe(wire)
 | 
					  wire.pipe(wire)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire.use(utMetadata(leavesMetadata.torrent))
 | 
					  wire.use(utMetadata(fixtures.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(
 | 
					  t.equal(wire.ut_metadata.metadata.toString('hex'), bencode.encode(bencode.decode(fixtures.leavesMetadata.torrent).info).toString('hex'))
 | 
				
			||||||
    wire.ut_metadata.metadata.toString('hex'),
 | 
					 | 
				
			||||||
    bencode.encode(bencode.decode(leavesMetadata.torrent).info).toString('hex')
 | 
					 | 
				
			||||||
  )
 | 
					 | 
				
			||||||
  t.end()
 | 
					  t.end()
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										175
									
								
								test/fetch.js
									
									
									
									
									
								
							
							
						
						
									
										175
									
								
								test/fetch.js
									
									
									
									
									
								
							@ -1,39 +1,35 @@
 | 
				
			|||||||
const { leavesMetadata, sintel } = require('webtorrent-fixtures')
 | 
					var bencode = require('bencode')
 | 
				
			||||||
const bencode = require('bencode')
 | 
					var Buffer = require('safe-buffer').Buffer
 | 
				
			||||||
const Protocol = require('bittorrent-protocol')
 | 
					var fixtures = require('webtorrent-fixtures')
 | 
				
			||||||
const test = require('tape')
 | 
					var Protocol = require('bittorrent-protocol')
 | 
				
			||||||
const utMetadata = require('../')
 | 
					var test = require('tape')
 | 
				
			||||||
 | 
					var utMetadata = require('../')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const id1 = Buffer.from('01234567890123456789')
 | 
					var id1 = Buffer.from('01234567890123456789')
 | 
				
			||||||
const id2 = Buffer.from('12345678901234567890')
 | 
					var id2 = Buffer.from('12345678901234567890')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test('fetch()', t => {
 | 
					test('fetch()', function (t) {
 | 
				
			||||||
  t.plan(3)
 | 
					  t.plan(3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const wire1 = new Protocol()
 | 
					  var wire1 = new Protocol()
 | 
				
			||||||
  const wire2 = new Protocol()
 | 
					  var wire2 = new Protocol()
 | 
				
			||||||
  wire1.pipe(wire2).pipe(wire1)
 | 
					  wire1.pipe(wire2).pipe(wire1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire1.use(utMetadata(leavesMetadata.torrent)) // wire1 already has metadata
 | 
					  wire1.use(utMetadata(fixtures.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', _metadata => {
 | 
					  wire2.ut_metadata.on('metadata', function (_metadata) {
 | 
				
			||||||
    // got metadata!
 | 
					    // got metadata!
 | 
				
			||||||
    t.equal(
 | 
					    t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(fixtures.leavesMetadata.torrent).info }).toString('hex'))
 | 
				
			||||||
      _metadata.toString('hex'),
 | 
					 | 
				
			||||||
      bencode.encode({
 | 
					 | 
				
			||||||
        info: bencode.decode(leavesMetadata.torrent).info
 | 
					 | 
				
			||||||
      }).toString('hex')
 | 
					 | 
				
			||||||
    )
 | 
					 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire2.on('handshake', (infoHash, peerId, extensions) => {
 | 
					  wire2.on('handshake', function (infoHash, peerId, extensions) {
 | 
				
			||||||
    wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
 | 
					    wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2)
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire2.on('extended', ext => {
 | 
					  wire2.on('extended', function (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') {
 | 
				
			||||||
@ -46,14 +42,14 @@ test('fetch()', t => {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
 | 
					  wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1)
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test('fetch() from peer without metadata', t => {
 | 
					test('fetch() from peer without metadata', function (t) {
 | 
				
			||||||
  t.plan(2)
 | 
					  t.plan(2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const wire1 = new Protocol()
 | 
					  var wire1 = new Protocol()
 | 
				
			||||||
  const wire2 = new Protocol()
 | 
					  var 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
 | 
				
			||||||
@ -61,25 +57,25 @@ test('fetch() from peer without metadata', t => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  wire2.ut_metadata.fetch()
 | 
					  wire2.ut_metadata.fetch()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire2.ut_metadata.on('metadata', () => {
 | 
					  wire2.ut_metadata.on('metadata', function () {
 | 
				
			||||||
    t.fail('No "metadata" event should fire')
 | 
					    t.fail('No "metadata" event should fire')
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire1.ut_metadata.onMessage = () => {
 | 
					  wire1.ut_metadata.onMessage = function () {
 | 
				
			||||||
    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
 | 
					    // No messages should be sent because wire1 never sent metadata_size in the
 | 
				
			||||||
    // in the extended handshake, so he doesn't have metadata
 | 
					    // extended handshake, so he doesn't have metadata
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire2.ut_metadata.on('warning', () => {
 | 
					  wire2.ut_metadata.on('warning', function () {
 | 
				
			||||||
    t.pass('got warning about peer missing metadata')
 | 
					    t.pass('got warning about peer missing metadata')
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire2.on('handshake', (infoHash, peerId, extensions) => {
 | 
					  wire2.on('handshake', function (infoHash, peerId, extensions) {
 | 
				
			||||||
    wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
 | 
					    wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2)
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire2.on('extended', ext => {
 | 
					  wire2.on('extended', function (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') {
 | 
				
			||||||
@ -89,44 +85,36 @@ test('fetch() from peer without metadata', t => {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
 | 
					  wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1)
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test('fetch when peer gets metadata later (setMetadata)', t => {
 | 
					test('fetch when peer gets metadata later (setMetadata)', function (t) {
 | 
				
			||||||
  t.plan(3)
 | 
					  t.plan(3)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const wire1 = new Protocol()
 | 
					  var wire1 = new Protocol()
 | 
				
			||||||
  const wire2 = new Protocol()
 | 
					  var 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(() => {
 | 
					  process.nextTick(function () {
 | 
				
			||||||
    // wire1 gets metadata later
 | 
					    wire1.ut_metadata.setMetadata(fixtures.leavesMetadata.torrent) // wire1 gets metadata later
 | 
				
			||||||
    wire1.ut_metadata.setMetadata(leavesMetadata.torrent)
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    process.nextTick(() => {
 | 
					    process.nextTick(function () {
 | 
				
			||||||
      // wire2 does not start with metadata,
 | 
					      // wire2 does not start with metadata, but connects to wire1 after it gets 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', _metadata => {
 | 
					      wire2.ut_metadata.on('metadata', function (_metadata) {
 | 
				
			||||||
        // got metadata!
 | 
					        // got metadata!
 | 
				
			||||||
        t.equal(
 | 
					        t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(fixtures.leavesMetadata.torrent).info }).toString('hex'))
 | 
				
			||||||
          _metadata.toString('hex'),
 | 
					 | 
				
			||||||
          bencode.encode({
 | 
					 | 
				
			||||||
            info: bencode.decode(leavesMetadata.torrent).info
 | 
					 | 
				
			||||||
          }).toString('hex')
 | 
					 | 
				
			||||||
        )
 | 
					 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      wire2.on('handshake', (infoHash, peerId, extensions) => {
 | 
					      wire2.on('handshake', function (infoHash, peerId, extensions) {
 | 
				
			||||||
        wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
 | 
					        wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2)
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      wire2.on('extended', ext => {
 | 
					      wire2.on('extended', function (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') {
 | 
				
			||||||
@ -139,43 +127,37 @@ test('fetch when peer gets metadata later (setMetadata)', t => {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
      })
 | 
					      })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
      wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
 | 
					      wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1)
 | 
				
			||||||
    })
 | 
					    })
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test('fetch() large torrent', t => {
 | 
					test('fetch() large torrent', function (t) {
 | 
				
			||||||
  t.plan(4)
 | 
					  t.plan(4)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const wire1 = new Protocol()
 | 
					  var wire1 = new Protocol()
 | 
				
			||||||
  const wire2 = new Protocol()
 | 
					  var wire2 = new Protocol()
 | 
				
			||||||
  wire1.pipe(wire2).pipe(wire1)
 | 
					  wire1.pipe(wire2).pipe(wire1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire1.use(utMetadata(sintel.torrent)) // wire1 already has metadata
 | 
					  wire1.use(utMetadata(fixtures.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', _metadata => {
 | 
					  wire2.ut_metadata.on('metadata', function (_metadata) {
 | 
				
			||||||
    // got metadata!
 | 
					    // got metadata!
 | 
				
			||||||
    t.equal(
 | 
					    t.equal(_metadata.toString('hex'), bencode.encode({ info: bencode.decode(fixtures.sintel.torrent).info }).toString('hex'))
 | 
				
			||||||
      _metadata.toString('hex'),
 | 
					 | 
				
			||||||
      bencode.encode({
 | 
					 | 
				
			||||||
        info: bencode.decode(sintel.torrent).info
 | 
					 | 
				
			||||||
      }).toString('hex')
 | 
					 | 
				
			||||||
    )
 | 
					 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire2.on('handshake', (infoHash, peerId, extensions) => {
 | 
					  wire2.on('handshake', function (infoHash, peerId, extensions) {
 | 
				
			||||||
    wire2.handshake(sintel.parsedTorrent.infoHash, id2)
 | 
					    wire2.handshake(fixtures.sintel.parsedTorrent.infoHash, id2)
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire2.on('extended', ext => {
 | 
					  wire2.on('extended', function (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,
 | 
					      // note: this should get called twice, once for each block of the sintel metadata
 | 
				
			||||||
      // 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
 | 
				
			||||||
@ -186,17 +168,17 @@ test('fetch() large torrent', t => {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire1.handshake(sintel.parsedTorrent.infoHash, id1)
 | 
					  wire1.handshake(fixtures.sintel.parsedTorrent.infoHash, id1)
 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test('discard invalid metadata', t => {
 | 
					test('discard invalid metadata', function (t) {
 | 
				
			||||||
  t.plan(1)
 | 
					  t.plan(1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const wire1 = new Protocol()
 | 
					  var wire1 = new Protocol()
 | 
				
			||||||
  const wire2 = new Protocol()
 | 
					  var wire2 = new Protocol()
 | 
				
			||||||
  wire1.pipe(wire2).pipe(wire1)
 | 
					  wire1.pipe(wire2).pipe(wire1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  const invalidMetadata = leavesMetadata.torrent.slice(0)
 | 
					  var invalidMetadata = fixtures.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))
 | 
				
			||||||
@ -204,48 +186,17 @@ test('discard invalid metadata', t => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  wire2.ut_metadata.fetch()
 | 
					  wire2.ut_metadata.fetch()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire2.ut_metadata.on('metadata', () => {
 | 
					  wire2.ut_metadata.on('metadata', function () {
 | 
				
			||||||
    t.fail('No "metadata" event should fire')
 | 
					    t.fail('No "metadata" event should fire')
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire2.ut_metadata.on('warning', () => {
 | 
					  wire2.ut_metadata.on('warning', function () {
 | 
				
			||||||
    t.pass('got warning because peer sent reject too much')
 | 
					    t.pass('got warning because peer sent reject too much')
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire2.on('handshake', (infoHash, peerId, extensions) => {
 | 
					  wire2.on('handshake', function (infoHash, peerId, extensions) {
 | 
				
			||||||
    wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
 | 
					    wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2)
 | 
				
			||||||
  })
 | 
					  })
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  wire1.handshake(leavesMetadata.parsedTorrent.infoHash, id1)
 | 
					  wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1)
 | 
				
			||||||
})
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
test('stop receiving data after cancel', t => {
 | 
					 | 
				
			||||||
  t.plan(1)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  const wire1 = new Protocol()
 | 
					 | 
				
			||||||
  const wire2 = new Protocol()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  wire1.pipe(wire2).pipe(wire1)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  wire1.use(utMetadata(sintel.torrent))
 | 
					 | 
				
			||||||
  wire2.use(utMetadata())
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  wire2.ut_metadata.once('metadata', () => {
 | 
					 | 
				
			||||||
    t.fail('No "metadata" event should fire')
 | 
					 | 
				
			||||||
  })
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  wire2.once('handshake', (infoHash, peerId, extensions) => {
 | 
					 | 
				
			||||||
    wire2.handshake(sintel.parsedTorrent.infoHash, id2)
 | 
					 | 
				
			||||||
    wire2.ut_metadata.fetch()
 | 
					 | 
				
			||||||
  })
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  wire2.on('extended', ext => {
 | 
					 | 
				
			||||||
    if (ext === 'ut_metadata') {
 | 
					 | 
				
			||||||
      wire2.ut_metadata.cancel()
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  })
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  wire1.handshake(sintel.parsedTorrent.infoHash, id1)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  process.nextTick(() => t.pass('no metadata received'))
 | 
					 | 
				
			||||||
})
 | 
					})
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user