modernize index.js
This commit is contained in:
parent
aa3f1459de
commit
f84070f89a
96
index.js
96
index.js
@ -1,19 +1,17 @@
|
|||||||
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 = 10000000 // 10MB
|
||||||
var BITFIELD_GROW = 1000
|
const BITFIELD_GROW = 1000
|
||||||
var PIECE_LENGTH = 16 * 1024
|
const PIECE_LENGTH = 16 * 1024
|
||||||
|
|
||||||
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
|
||||||
|
|
||||||
@ -32,14 +30,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 +56,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 +92,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 +106,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)
|
||||||
}
|
}
|
||||||
@ -143,45 +139,45 @@ module.exports = function (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,7 +186,7 @@ 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)
|
||||||
@ -200,16 +196,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 +214,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 +231,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
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user