Compare commits

..

55 Commits

Author SHA1 Message Date
Feross Aboukhadijeh
d7613c3ac1 3.5.2 2020-11-05 11:17:57 -10:00
Feross Aboukhadijeh
953df40533
Merge pull request #47 from webtorrent/bitfield 2020-11-05 22:16:35 +01:00
Feross Aboukhadijeh
adf80f9e31 bitfield@4 2020-11-03 09:36:31 -10:00
Feross Aboukhadijeh
a19b83ad6d bump deps 2020-11-03 09:31:23 -10:00
Feross Aboukhadijeh
18c33fa5f7 package metadata 2020-07-16 21:49:31 -07:00
Feross Aboukhadijeh
eeeebdda22 3.5.1 2020-07-16 21:49:30 -07:00
Feross Aboukhadijeh
4f99851a9e deps 2020-07-16 21:49:16 -07:00
Feross Aboukhadijeh
c89df72e4e
Merge pull request #46 from webtorrent/greenkeeper/tape-5.0.0
Update tape to the latest version 🚀
2020-07-17 06:47:15 +02:00
greenkeeper[bot]
5bdc9df4f7
chore(package): update tape to version 5.0.0 2020-04-24 22:04:03 +00:00
Feross Aboukhadijeh
f2eb816fff 3.5.0 2019-09-07 09:24:05 -07:00
Feross Aboukhadijeh
8cfba16e12
Merge pull request #44 from bjornstar/nothing-after-cancel
Stop receiving data after cancel
2019-09-07 09:23:40 -07:00
Feross Aboukhadijeh
1013a093c7 3.4.1 2019-09-05 16:47:55 -07:00
Feross
cbd3f0d8b5
Update simple-sha1 to the latest version 🚀 (#45)
Update simple-sha1 to the latest version 🚀

Co-authored-by: null <23040076+greenkeeper[bot]@users.noreply.github.com>
2019-09-05 16:47:17 -07:00
greenkeeper[bot]
4a049f18cd
fix(package): update simple-sha1 to version 3.0.0 2019-09-05 23:06:14 +00:00
Feross Aboukhadijeh
d409e4bf2a 3.4.0 2019-07-20 11:43:33 -07:00
Feross Aboukhadijeh
96cbab00ce bitfield@3 2019-07-20 11:42:42 -07:00
Bjorn Stromberg
50ab830a75 Stop receiving data after cancel 2019-05-28 23:14:41 +09:00
Diego Rodríguez Baquero
60f328d8ba
Merge pull request #42 from webtorrent/greenkeeper/debug-4.0.0
Update debug to the latest version 🚀
2018-12-26 09:40:03 -05:00
greenkeeper[bot]
ea9fd78040 fix(package): update debug to version 4.0.0 2018-09-11 13:56:04 +00:00
Feross Aboukhadijeh
106acca7c8 3.3.0 2018-08-29 12:32:17 -07:00
Feross Aboukhadijeh
a9826a9612
Merge pull request #41 from jimmywarting/feature/modernize
modernize
2018-08-29 12:31:49 -07:00
Jimmy Wärting
4539a41dad modernize test files 2018-08-28 19:34:08 +02:00
Jimmy Wärting
6a0f5aecbe updated examples in readme 2018-08-28 13:27:03 +02:00
Jimmy Wärting
2034e9ed2c removed inherits dep 2018-08-28 11:42:05 +02:00
Jimmy Wärting
f84070f89a modernize index.js 2018-08-28 11:41:44 +02:00
Feross Aboukhadijeh
aa3f1459de remove safe-buffer 2018-07-26 21:26:04 -07:00
Feross Aboukhadijeh
3405e9a94e remove airtap 2018-07-26 21:20:20 -07:00
Diego Rodríguez Baquero
449dd19356
Merge pull request #39 from webtorrent/greenkeeper/bittorrent-protocol-3.0.0
Update bittorrent-protocol to the latest version 🚀
2018-07-26 20:10:53 -07:00
Diego Rodríguez Baquero
95abc05086
Merge branch 'master' into greenkeeper/bittorrent-protocol-3.0.0 2018-07-26 20:10:46 -07:00
Diego Rodríguez Baquero
7b1713ef96
Merge pull request #35 from webtorrent/greenkeeper/brfs-2.0.0
Update brfs to the latest version 🚀
2018-07-26 20:09:48 -07:00
Diego Rodríguez Baquero
1505eae11a
Merge pull request #40 from webtorrent/greenkeeper/airtap-0.1.0
chore(package): update airtap to version 0.1.0
2018-07-26 20:09:35 -07:00
greenkeeper[bot]
b45145bde5 chore(package): update bittorrent-protocol to version 3.0.0 2018-07-27 00:51:03 +00:00
greenkeeper[bot]
67b7fac40f chore(package): update airtap to version 0.1.0
Closes #36
2018-07-07 09:31:12 +00:00
greenkeeper[bot]
92fb4d380c chore(package): update brfs to version 2.0.0 2018-06-22 11:20:28 +00:00
Feross Aboukhadijeh
03050eca54 3.2.2 2018-06-09 16:19:42 -07:00
Feross Aboukhadijeh
6b8a93a9af npmignore 2018-06-09 16:16:01 -07:00
Feross Aboukhadijeh
d2f8239760
Merge pull request #34 from webtorrent/greenkeeper/airtap-0.0.7
chore(package): update airtap to version 0.0.7
2018-06-09 16:14:42 -07:00
greenkeeper[bot]
a2fa6e1212 chore(package): update airtap to version 0.0.7
Closes #32
2018-05-25 11:33:22 +00:00
Feross Aboukhadijeh
58b00eab05
Merge pull request #31 from webtorrent/greenkeeper/airtap-0.0.5
Update airtap to the latest version 🚀
2018-05-08 20:01:37 -07:00
greenkeeper[bot]
e19f8abcf6 chore(package): update airtap to version 0.0.5 2018-05-06 11:37:40 +00:00
Feross Aboukhadijeh
b584a2b93c 3.2.1 2018-04-12 23:50:58 -07:00
Feross Aboukhadijeh
04b2b55be4 zuul -> airtap 2018-04-12 23:46:02 -07:00
Feross Aboukhadijeh
e85020983e 3.2.0 2018-02-27 20:09:08 -08:00
Diego Rodríguez Baquero
084403d245
Merge pull request #29 from webtorrent/greenkeeper/bencode-2.0.0
Update bencode to the latest version 🚀
2018-02-26 15:02:06 -05:00
greenkeeper[bot]
7929a169e5 fix(package): update bencode to version 2.0.0 2018-02-26 18:18:51 +00:00
Feross Aboukhadijeh
ef43f5370f sauce: drop android 2018-02-20 15:24:46 -08:00
Feross Aboukhadijeh
bed0e361cc travis: node lts 2018-02-20 15:24:38 -08:00
Feross Aboukhadijeh
79ef8e4470 3.1.0 2018-02-20 15:23:40 -08:00
Diego Rodríguez Baquero
8bc4d72237
Merge pull request #28 from webtorrent/greenkeeper/bitfield-2.0.0
Update bitfield to the latest version 🚀
2018-02-20 09:43:46 -05:00
greenkeeper[bot]
635577950a fix(package): update bitfield to version 2.0.0 2018-02-20 05:52:47 +00:00
Feross Aboukhadijeh
2a39a8fe1c
remove greenkeeper badge 2018-01-29 19:09:40 +08:00
Diego Rodríguez Baquero
5aeaca0dd8 Merge pull request #25 from webtorrent/greenkeeper/initial
Update dependencies to enable Greenkeeper 🌴
2017-09-29 08:50:22 -05:00
Diego Rodríguez Baquero
9f78fc76ae Update README.md 2017-09-29 08:50:12 -05:00
greenkeeper[bot]
76362ff9ca docs(readme): add Greenkeeper badge 2017-09-29 05:23:28 +00:00
greenkeeper[bot]
ebfcb7df5f chore(package): update dependencies 2017-09-29 05:21:01 +00:00
8 changed files with 399 additions and 354 deletions

2
.npmignore Normal file
View File

@ -0,0 +1,2 @@
.travis.yml
test/

View File

@ -1,7 +1,3 @@
language: node_js
node_js:
- node
env:
global:
- secure: Htsm8eeR11ph6lbAOLPbFfJGou8eDPQMfLXUBx6c9+QgrbBKHM1RKscJNvSi1AM1rOOKSk1pCZwqVR2n+9kQs30MxZppyIngiy5Jcz7/b5FDvnm/JkNzq+7sEeGZzRK11XmFkWuYRK9TvUaDuuo/NOc1NJc0ics/gzrjNLcT1tw=
- secure: FufpUBE/caYoQ5MfsIWFYVM0C4gj+4OSaqu6W8exucFDB68zTmO0te11tOwg98PhFnSaDbJ7h3gWXYGTpbFUErkeMBRMTsNjYYoQhcWWjzD4yo5iIfh215TJ5vhE4sIeCCi0TxHgbJkF3KthsUyaOdy/Q3sFISZ/VQSPFBDdmbU=
- lts/*

View File

@ -1,14 +0,0 @@
ui: tape
browsers:
- name: chrome
version: latest
- name: firefox
version: latest
- name: safari
version: latest
- name: microsoftedge
version: latest
- name: android
version: latest
- name: iphone
version: latest

View File

@ -11,8 +11,6 @@
### BitTorrent Extension for Peers to Send Metadata Files (BEP 9)
[![Sauce Test Status](https://saucelabs.com/browser-matrix/ut_metadata.svg)](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).
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).
@ -32,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:
```js
var Protocol = require('bittorrent-protocol')
var net = require('net')
const Protocol = require('bittorrent-protocol')
const net = require('net')
net.createServer(function (socket) {
net.createServer(socket => {
var wire = new Protocol()
socket.pipe(wire).pipe(socket)
// 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'))
})
@ -50,12 +48,12 @@ net.createServer(function (socket) {
To add support for BEP 9, simply modify your code like this:
```js
var Protocol = require('bittorrent-protocol')
var net = require('net')
var ut_metadata = require('ut_metadata')
const Protocol = require('bittorrent-protocol')
const net = require('net')
const ut_metadata = require('ut_metadata')
net.createServer(function (socket) {
var wire = new Protocol()
net.createServer(socket => {
const wire = new Protocol()
socket.pipe(wire).pipe(socket)
// initialize the extension
@ -67,7 +65,7 @@ net.createServer(function (socket) {
wire.ut_metadata.fetch()
// '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!
// Note: the event will not fire if the peer does not support ut_metadata, if they
@ -77,12 +75,12 @@ net.createServer(function (socket) {
// 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.
wire.ut_metadata.on('warning', function (err) {
wire.ut_metadata.on('warning', err => {
console.log(err.message)
})
// 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'))
})
@ -97,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.
```js
var metadata = fs.readFileSync(__dirname + '/file.torrent')
const metadata = fs.readFileSync(__dirname + '/file.torrent')
wire.use(ut_metadata(metadata))
```
@ -121,7 +119,7 @@ Fired when metadata is available and verified to be correct. Called with a singl
parameter of type Buffer.
```js
wire.ut_metadata.on('metadata', function (metadata) {
wire.ut_metadata.on('metadata', metadata => {
console.log(Buffer.isBuffer(metadata)) // true
})
```
@ -138,7 +136,7 @@ Fired if:
- the peer repeatedly sent invalid data
```js
wire.ut_metadata.on('warning', function (err) {
wire.ut_metadata.on('warning', err => {
console.log(err.message)
})
```

455
index.js
View File

@ -1,241 +1,244 @@
var bencode = require('bencode')
var BitField = require('bitfield')
var Buffer = require('safe-buffer').Buffer
var debug = require('debug')('ut_metadata')
var EventEmitter = require('events').EventEmitter
var inherits = require('inherits')
var sha1 = require('simple-sha1')
/*! ut_metadata. MIT License. WebTorrent LLC <https://webtorrent.io/opensource> */
const { EventEmitter } = require('events')
const bencode = require('bencode')
const BitField = require('bitfield').default
const debug = require('debug')('ut_metadata')
const sha1 = require('simple-sha1')
var MAX_METADATA_SIZE = 10000000 // 10MB
var BITFIELD_GROW = 1000
var PIECE_LENGTH = 16 * 1024
const MAX_METADATA_SIZE = 1E7 // 10 MB
const BITFIELD_GROW = 1E3
const PIECE_LENGTH = 1 << 14 // 16 KiB
module.exports = function (metadata) {
inherits(utMetadata, EventEmitter)
module.exports = metadata => {
class utMetadata extends EventEmitter {
constructor (wire) {
super()
function utMetadata (wire) {
EventEmitter.call(this)
this._wire = wire
this._wire = wire
this._fetching = false
this._metadataComplete = false
this._metadataSize = null
// how many reject messages to tolerate before quitting
this._remainingRejects = null
this._metadataComplete = false
this._metadataSize = null
this._remainingRejects = null // how many reject messages to tolerate before quitting
this._fetching = false
// The largest torrent file that I know of is ~1-2MB, which is ~100
// pieces. Therefore, cap the bitfield to 10x that (1000 pieces) so a
// malicious peer can't make it grow to fill all memory.
this._bitfield = new BitField(0, { grow: BITFIELD_GROW })
// The largest .torrent file that I know of is ~1-2MB, which is ~100 pieces.
// Therefore, cap the bitfield to 10x that (1000 pieces) so a malicious peer can't
// make it grow to fill all memory.
this._bitfield = new BitField(0, { grow: BITFIELD_GROW })
if (Buffer.isBuffer(metadata)) {
this.setMetadata(metadata)
}
}
if (Buffer.isBuffer(metadata)) {
this.setMetadata(metadata)
onHandshake (infoHash, peerId, extensions) {
this._infoHash = infoHash
}
onExtendedHandshake (handshake) {
if (!handshake.m || !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'))
}
if (typeof handshake.metadata_size !== 'number' ||
MAX_METADATA_SIZE < handshake.metadata_size ||
handshake.metadata_size <= 0) {
return this.emit('warning', new Error('Peer gave invalid metadata size'))
}
this._metadataSize = handshake.metadata_size
this._numPieces = Math.ceil(this._metadataSize / PIECE_LENGTH)
this._remainingRejects = this._numPieces * 2
this._requestPieces()
}
onMessage (buf) {
let dict
let trailer
try {
const str = buf.toString()
const trailerIndex = str.indexOf('ee') + 2
dict = bencode.decode(str.substring(0, trailerIndex))
trailer = buf.slice(trailerIndex)
} catch (err) {
// drop invalid messages
return
}
switch (dict.msg_type) {
case 0:
// ut_metadata request (from peer)
// example: { 'msg_type': 0, 'piece': 0 }
this._onRequest(dict.piece)
break
case 1:
// ut_metadata data (in response to our request)
// example: { 'msg_type': 1, 'piece': 0, 'total_size': 3425 }
this._onData(dict.piece, trailer, dict.total_size)
break
case 2:
// ut_metadata reject (peer doesn't have piece we requested)
// { 'msg_type': 2, 'piece': 0 }
this._onReject(dict.piece)
break
}
}
/**
* Ask the peer to send metadata.
* @public
*/
fetch () {
if (this._metadataComplete) {
return
}
this._fetching = true
if (this._metadataSize) {
this._requestPieces()
}
}
/**
* Stop asking the peer to send metadata.
* @public
*/
cancel () {
this._fetching = false
}
setMetadata (metadata) {
if (this._metadataComplete) return true
debug('set metadata')
// if full torrent dictionary was passed in, pull out just `info` key
try {
const info = bencode.decode(metadata).info
if (info) {
metadata = bencode.encode(info)
}
} catch (err) {}
// check hash
if (this._infoHash && this._infoHash !== sha1.sync(metadata)) {
return false
}
this.cancel()
this.metadata = metadata
this._metadataComplete = true
this._metadataSize = this.metadata.length
this._wire.extendedHandshake.metadata_size = this._metadataSize
this.emit('metadata', bencode.encode({
info: bencode.decode(this.metadata)
}))
return true
}
_send (dict, trailer) {
let buf = bencode.encode(dict)
if (Buffer.isBuffer(trailer)) {
buf = Buffer.concat([buf, trailer])
}
this._wire.extended('ut_metadata', buf)
}
_request (piece) {
this._send({ msg_type: 0, piece })
}
_data (piece, buf, totalSize) {
const msg = { msg_type: 1, piece }
if (typeof totalSize === 'number') {
msg.total_size = totalSize
}
this._send(msg, buf)
}
_reject (piece) {
this._send({ msg_type: 2, piece })
}
_onRequest (piece) {
if (!this._metadataComplete) {
this._reject(piece)
return
}
const start = piece * PIECE_LENGTH
let end = start + PIECE_LENGTH
if (end > this._metadataSize) {
end = this._metadataSize
}
const buf = this.metadata.slice(start, end)
this._data(piece, buf, this._metadataSize)
}
_onData (piece, buf, totalSize) {
if (buf.length > PIECE_LENGTH || !this._fetching) {
return
}
buf.copy(this.metadata, piece * PIECE_LENGTH)
this._bitfield.set(piece)
this._checkDone()
}
_onReject (piece) {
if (this._remainingRejects > 0 && this._fetching) {
// If we haven't been rejected too much,
// then try to request the piece again
this._request(piece)
this._remainingRejects -= 1
} else {
this.emit('warning', new Error('Peer sent "reject" too much'))
}
}
_requestPieces () {
if (!this._fetching) return
this.metadata = Buffer.alloc(this._metadataSize)
for (let piece = 0; piece < this._numPieces; piece++) {
this._request(piece)
}
}
_checkDone () {
let done = true
for (let piece = 0; piece < this._numPieces; piece++) {
if (!this._bitfield.get(piece)) {
done = false
break
}
}
if (!done) return
// attempt to set metadata -- may fail sha1 check
const success = this.setMetadata(this.metadata)
if (!success) {
this._failedMetadata()
}
}
_failedMetadata () {
// reset bitfield & try again
this._bitfield = new BitField(0, { grow: BITFIELD_GROW })
this._remainingRejects -= this._numPieces
if (this._remainingRejects > 0) {
this._requestPieces()
} else {
this.emit('warning', new Error('Peer sent invalid metadata'))
}
}
}
// Name of the bittorrent-protocol extension
utMetadata.prototype.name = 'ut_metadata'
utMetadata.prototype.onHandshake = function (infoHash, peerId, extensions) {
this._infoHash = infoHash
}
utMetadata.prototype.onExtendedHandshake = function (handshake) {
if (!handshake.m || !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'))
}
if (typeof handshake.metadata_size !== 'number' ||
MAX_METADATA_SIZE < handshake.metadata_size ||
handshake.metadata_size <= 0) {
return this.emit('warning', new Error('Peer gave invalid metadata size'))
}
this._metadataSize = handshake.metadata_size
this._numPieces = Math.ceil(this._metadataSize / PIECE_LENGTH)
this._remainingRejects = this._numPieces * 2
if (this._fetching) {
this._requestPieces()
}
}
utMetadata.prototype.onMessage = function (buf) {
var dict, trailer
try {
var str = buf.toString()
var trailerIndex = str.indexOf('ee') + 2
dict = bencode.decode(str.substring(0, trailerIndex))
trailer = buf.slice(trailerIndex)
} catch (err) {
// drop invalid messages
return
}
switch (dict.msg_type) {
case 0:
// ut_metadata request (from peer)
// example: { 'msg_type': 0, 'piece': 0 }
this._onRequest(dict.piece)
break
case 1:
// ut_metadata data (in response to our request)
// example: { 'msg_type': 1, 'piece': 0, 'total_size': 3425 }
this._onData(dict.piece, trailer, dict.total_size)
break
case 2:
// ut_metadata reject (peer doesn't have piece we requested)
// { 'msg_type': 2, 'piece': 0 }
this._onReject(dict.piece)
break
}
}
/**
* Ask the peer to send metadata.
* @public
*/
utMetadata.prototype.fetch = function () {
if (this._metadataComplete) {
return
}
this._fetching = true
if (this._metadataSize) {
this._requestPieces()
}
}
/**
* Stop asking the peer to send metadata.
* @public
*/
utMetadata.prototype.cancel = function () {
this._fetching = false
}
utMetadata.prototype.setMetadata = function (metadata) {
if (this._metadataComplete) return true
debug('set metadata')
// if full torrent dictionary was passed in, pull out just `info` key
try {
var info = bencode.decode(metadata).info
if (info) {
metadata = bencode.encode(info)
}
} catch (err) {}
// check hash
if (this._infoHash && this._infoHash !== sha1.sync(metadata)) {
return false
}
this.cancel()
this.metadata = metadata
this._metadataComplete = true
this._metadataSize = this.metadata.length
this._wire.extendedHandshake.metadata_size = this._metadataSize
this.emit('metadata', bencode.encode({ info: bencode.decode(this.metadata) }))
return true
}
utMetadata.prototype._send = function (dict, trailer) {
var buf = bencode.encode(dict)
if (Buffer.isBuffer(trailer)) {
buf = Buffer.concat([buf, trailer])
}
this._wire.extended('ut_metadata', buf)
}
utMetadata.prototype._request = function (piece) {
this._send({ msg_type: 0, piece: piece })
}
utMetadata.prototype._data = function (piece, buf, totalSize) {
var msg = { msg_type: 1, piece: piece }
if (typeof totalSize === 'number') {
msg.total_size = totalSize
}
this._send(msg, buf)
}
utMetadata.prototype._reject = function (piece) {
this._send({ msg_type: 2, piece: piece })
}
utMetadata.prototype._onRequest = function (piece) {
if (!this._metadataComplete) {
this._reject(piece)
return
}
var start = piece * PIECE_LENGTH
var end = start + PIECE_LENGTH
if (end > this._metadataSize) {
end = this._metadataSize
}
var buf = this.metadata.slice(start, end)
this._data(piece, buf, this._metadataSize)
}
utMetadata.prototype._onData = function (piece, buf, totalSize) {
if (buf.length > PIECE_LENGTH) {
return
}
buf.copy(this.metadata, piece * PIECE_LENGTH)
this._bitfield.set(piece)
this._checkDone()
}
utMetadata.prototype._onReject = function (piece) {
if (this._remainingRejects > 0 && this._fetching) {
// If we haven't been rejected too much, then try to request the piece again
this._request(piece)
this._remainingRejects -= 1
} else {
this.emit('warning', new Error('Peer sent "reject" too much'))
}
}
utMetadata.prototype._requestPieces = function () {
this.metadata = Buffer.alloc(this._metadataSize)
for (var piece = 0; piece < this._numPieces; piece++) {
this._request(piece)
}
}
utMetadata.prototype._checkDone = function () {
var done = true
for (var piece = 0; piece < this._numPieces; piece++) {
if (!this._bitfield.get(piece)) {
done = false
break
}
}
if (!done) return
// attempt to set metadata -- may fail sha1 check
var success = this.setMetadata(this.metadata)
if (!success) {
this._failedMetadata()
}
}
utMetadata.prototype._failedMetadata = function () {
// reset bitfield & try again
this._bitfield = new BitField(0, { grow: BITFIELD_GROW })
this._remainingRejects -= this._numPieces
if (this._remainingRejects > 0) {
this._requestPieces()
} else {
this.emit('warning', new Error('Peer sent invalid metadata'))
}
}
return utMetadata
}

View File

@ -1,9 +1,9 @@
{
"name": "ut_metadata",
"description": "Extension for Peers to Send Metadata Files (BEP 9)",
"version": "3.0.11",
"version": "3.5.2",
"author": {
"name": "WebTorrent, LLC",
"name": "WebTorrent LLC",
"email": "feross@webtorrent.io",
"url": "https://webtorrent.io"
},
@ -11,20 +11,17 @@
"url": "https://github.com/webtorrent/ut_metadata/issues"
},
"dependencies": {
"bencode": "^1.0.0",
"bitfield": "^1.0.1",
"debug": "^2.2.0",
"inherits": "^2.0.1",
"safe-buffer": "^5.0.1",
"simple-sha1": "^2.0.0"
"bencode": "^2.0.1",
"bitfield": "^4.0.0",
"debug": "^4.2.0",
"simple-sha1": "^3.0.1"
},
"devDependencies": {
"bittorrent-protocol": "^2.0.0",
"brfs": "^1.2.0",
"bittorrent-protocol": "^3.1.2",
"brfs": "^2.0.2",
"standard": "*",
"tape": "^4.0.0",
"webtorrent-fixtures": "^1.3.0",
"zuul": "^3.8.0"
"tape": "^5.0.1",
"webtorrent-fixtures": "^1.7.3"
},
"keywords": [
"Extension for Peers to Send Metadata Files",
@ -44,9 +41,20 @@
"url": "git://github.com/webtorrent/ut_metadata.git"
},
"scripts": {
"test": "standard && npm run test-node && npm run test-browser",
"test-browser": "zuul -- test/*.js",
"test-browser-local": "zuul --local -- test/*.js",
"test-node": "tape test/*.js"
}
"test": "standard && tape test/*.js"
},
"funding": [
{
"type": "github",
"url": "https://github.com/sponsors/feross"
},
{
"type": "patreon",
"url": "https://www.patreon.com/feross"
},
{
"type": "consulting",
"url": "https://feross.org/support"
}
]
}

View File

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

View File

@ -1,35 +1,39 @@
var bencode = require('bencode')
var Buffer = require('safe-buffer').Buffer
var fixtures = require('webtorrent-fixtures')
var Protocol = require('bittorrent-protocol')
var test = require('tape')
var utMetadata = require('../')
const { leavesMetadata, sintel } = require('webtorrent-fixtures')
const bencode = require('bencode')
const Protocol = require('bittorrent-protocol')
const test = require('tape')
const utMetadata = require('../')
var id1 = Buffer.from('01234567890123456789')
var id2 = Buffer.from('12345678901234567890')
const id1 = Buffer.from('01234567890123456789')
const id2 = Buffer.from('12345678901234567890')
test('fetch()', function (t) {
test('fetch()', t => {
t.plan(3)
var wire1 = new Protocol()
var wire2 = new Protocol()
const wire1 = new Protocol()
const wire2 = new Protocol()
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.ut_metadata.fetch()
wire2.ut_metadata.on('metadata', function (_metadata) {
wire2.ut_metadata.on('metadata', _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.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2)
wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
})
wire2.on('extended', function (ext) {
wire2.on('extended', ext => {
if (ext === 'handshake') {
t.pass('got extended handshake')
} else if (ext === 'ut_metadata') {
@ -42,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)
var wire1 = new Protocol()
var wire2 = new Protocol()
const wire1 = new Protocol()
const wire2 = new Protocol()
wire1.pipe(wire2).pipe(wire1)
wire1.use(utMetadata()) // neither wire has metadata
@ -57,25 +61,25 @@ test('fetch() from peer without metadata', function (t) {
wire2.ut_metadata.fetch()
wire2.ut_metadata.on('metadata', function () {
wire2.ut_metadata.on('metadata', () => {
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')
// No messages should be sent because wire1 never sent metadata_size in the
// extended handshake, so he doesn't have metadata
// No messages should be sent because wire1 never sent metadata_size
// 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')
})
wire2.on('handshake', function (infoHash, peerId, extensions) {
wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2)
wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
})
wire2.on('extended', function (ext) {
wire2.on('extended', ext => {
if (ext === 'handshake') {
t.pass('got extended handshake')
} else if (ext === 'ut_metadata') {
@ -85,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)
var wire1 = new Protocol()
var wire2 = new Protocol()
const wire1 = new Protocol()
const wire2 = new Protocol()
wire1.pipe(wire2).pipe(wire1)
wire1.use(utMetadata()) // wire1 starts without metadata
process.nextTick(function () {
wire1.ut_metadata.setMetadata(fixtures.leavesMetadata.torrent) // wire1 gets metadata later
process.nextTick(() => {
// wire1 gets metadata later
wire1.ut_metadata.setMetadata(leavesMetadata.torrent)
process.nextTick(function () {
// wire2 does not start with metadata, but connects to wire1 after it gets metadata
process.nextTick(() => {
// wire2 does not start with metadata,
// but connects to wire1 after it gets metadata
wire2.use(utMetadata())
wire2.ut_metadata.fetch()
wire2.ut_metadata.on('metadata', function (_metadata) {
wire2.ut_metadata.on('metadata', _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.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2)
wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
})
wire2.on('extended', function (ext) {
wire2.on('extended', ext => {
if (ext === 'handshake') {
t.pass('got extended handshake')
} else if (ext === 'ut_metadata') {
@ -127,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)
var wire1 = new Protocol()
var wire2 = new Protocol()
const wire1 = new Protocol()
const wire2 = new Protocol()
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.ut_metadata.fetch()
wire2.ut_metadata.on('metadata', function (_metadata) {
wire2.ut_metadata.on('metadata', _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.handshake(fixtures.sintel.parsedTorrent.infoHash, id2)
wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(sintel.parsedTorrent.infoHash, id2)
})
wire2.on('extended', function (ext) {
wire2.on('extended', ext => {
if (ext === 'handshake') {
t.pass('got extended handshake')
} 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')
// this is emitted for consistency's sake, but it's ignored
@ -168,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)
var wire1 = new Protocol()
var wire2 = new Protocol()
const wire1 = new Protocol()
const wire2 = new Protocol()
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
wire1.use(utMetadata(invalidMetadata))
@ -186,17 +204,48 @@ test('discard invalid metadata', function (t) {
wire2.ut_metadata.fetch()
wire2.ut_metadata.on('metadata', function () {
wire2.ut_metadata.on('metadata', () => {
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')
})
wire2.on('handshake', function (infoHash, peerId, extensions) {
wire2.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id2)
wire2.on('handshake', (infoHash, peerId, extensions) => {
wire2.handshake(leavesMetadata.parsedTorrent.infoHash, id2)
})
wire1.handshake(fixtures.leavesMetadata.parsedTorrent.infoHash, id1)
wire1.handshake(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'))
})