Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
var randomIterate = require('random-iterate')
var Rusha = require('rusha')
var speedometer = require('speedometer')
var uniq = require('uniq')
var utMetadata = require('ut_metadata')
var utPex = require('ut_pex') // browser exclude
var File = require('./file')
var Peer = require('./peer')
var RarityMap = require('./rarity-map')
var Server = require('./server') // browser exclude
var MAX_BLOCK_LENGTH = 128 * 1024
var PIECE_TIMEOUT = 30000
var CHOKE_TIMEOUT = 5000
var SPEED_THRESHOLD = 3 * Piece.BLOCK_LENGTH
var PIPELINE_MIN_DURATION = 0.5
var PIPELINE_MAX_DURATION = 1
var RECHOKE_INTERVAL = 10000 // 10 seconds
var RECHOKE_OPTIMISTIC_DURATION = 2 // 30 seconds
var FILESYSTEM_CONCURRENCY = 2
var RECONNECT_WAIT = [ 1000, 5000, 15000 ]
var VERSION = require('../package.json').version
var USER_AGENT = 'WebTorrent/' + VERSION + ' (https://webtorrent.io)'
var TMP
try {
const sha1 = require('simple-sha1')
const speedometer = require('speedometer')
const uniq = require('uniq')
const utMetadata = require('ut_metadata')
const utPex = require('ut_pex') // browser exclude
const parseRange = require('parse-numeric-range')
const File = require('./file')
const Peer = require('./peer')
const RarityMap = require('./rarity-map')
const Server = require('./server') // browser exclude
const MAX_BLOCK_LENGTH = 128 * 1024
const PIECE_TIMEOUT = 30000
const CHOKE_TIMEOUT = 5000
const SPEED_THRESHOLD = 3 * Piece.BLOCK_LENGTH
const PIPELINE_MIN_DURATION = 0.5
const PIPELINE_MAX_DURATION = 1
const RECHOKE_INTERVAL = 10000 // 10 seconds
const RECHOKE_OPTIMISTIC_DURATION = 2 // 30 seconds
// IndexedDB chunk stores used in the browser benefit from maximum concurrency
const FILESYSTEM_CONCURRENCY = process.browser ? Infinity : 2
const RECONNECT_WAIT = [ 1000, 5000, 15000 ]
const VERSION = require('../package.json').version
const USER_AGENT = `WebTorrent/${VERSION} (https://webtorrent.io)`
let TMP
var eos = require('end-of-stream')
var piece = require('torrent-piece')
var rimraf = require('rimraf')
var FSChunkStore = require('fs-chunk-store')
var ImmediateChunkStore = require('immediate-chunk-store')
var peerDiscovery = require('torrent-discovery')
var bufferFrom = require('buffer-from')
var blocklist = require('ip-set')
var exchangeMetadata = require('./lib/exchange-metadata')
var fileStream = require('./lib/file-stream')
var MAX_REQUESTS = 5
var CHOKE_TIMEOUT = 5000
var REQUEST_TIMEOUT = 30000
var SPEED_THRESHOLD = 3 * piece.BLOCK_LENGTH
var DEFAULT_PORT = 6881
var BAD_PIECE_STRIKES_MAX = 3
var BAD_PIECE_STRIKES_DURATION = 120000 // 2 minutes
var RECHOKE_INTERVAL = 10000
var RECHOKE_OPTIMISTIC_DURATION = 2
var TMP = fs.existsSync('/tmp') ? '/tmp' : os.tmpDir()
var noop = function () {}
var sha1 = function (data) {
return crypto.createHash('sha1').update(data).digest('hex')
}
if (2 * otherSpeed > speed || otherSpeed > minSpeed) continue
min = other
minSpeed = otherSpeed
}
if (!min) return false
for (i = 0; i < r.length; i++) {
if (r[i] === min) r[i] = null
}
for (i = 0; i < min.requests.length; i++) {
var req = min.requests[i]
if (req.piece !== index) continue
pieces[index].cancel((req.offset / piece.BLOCK_SIZE) | 0)
}
engine.emit('hotswap', min, wire, index)
return true
}
function speedRanker () {
var speed = wire.downloadSpeed() || 1
if (speed > SPEED_THRESHOLD) return function () { return true }
var secs = Math.max(1, wire.requests.length) * Piece.BLOCK_LENGTH / speed
var tries = 10
var ptr = 0
return function (index) {
if (!tries || self.bitfield.get(index)) return true
var missing = self.pieces[index].missing
for (; ptr < self.wires.length; ptr++) {
var otherWire = self.wires[ptr]
var otherSpeed = otherWire.downloadSpeed()
if (otherSpeed < SPEED_THRESHOLD) continue
if (otherSpeed <= speed) continue
if (!otherWire.peerPieces.get(index)) continue
if ((missing -= otherSpeed * secs) > 0) continue
var onhotswap = opts.hotswap === false ? falsy : function (wire, index) {
var speed = wire.downloadSpeed()
if (speed < piece.BLOCK_LENGTH) return
if (!reservations[index] || !pieces[index]) return
var r = reservations[index]
var minSpeed = Infinity
var min
for (var i = 0; i < r.length; i++) {
var other = r[i]
if (!other || other === wire) continue
var otherSpeed = other.downloadSpeed()
if (otherSpeed >= SPEED_THRESHOLD) continue
if (2 * otherSpeed > speed || otherSpeed > minSpeed) continue
min = other
minSpeed = otherSpeed
function getBlockPipelineLength (wire, duration) {
return 2 + Math.ceil(duration * wire.downloadSpeed() / Piece.BLOCK_LENGTH)
}
_hotswap (wire, index) {
const speed = wire.downloadSpeed()
if (speed < Piece.BLOCK_LENGTH) return false
if (!this._reservations[index]) return false
const r = this._reservations[index]
if (!r) {
return false
}
let minSpeed = Infinity
let minWire
let i
for (i = 0; i < r.length; i++) {
const otherWire = r[i]
if (!otherWire || otherWire === wire) continue
const otherSpeed = otherWire.downloadSpeed()