Skip to content

Commit

Permalink
fix: modernize code (#2134)
Browse files Browse the repository at this point in the history
* fix: modernize code

* standard fix
  • Loading branch information
DiegoRBaquero committed Jul 11, 2021
1 parent feb719d commit 46033ae
Show file tree
Hide file tree
Showing 35 changed files with 848 additions and 857 deletions.
16 changes: 4 additions & 12 deletions lib/peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,34 +45,26 @@ exports.createWebRTCPeer = (conn, swarm) => {
* listening port of the TCP server. Until the remote peer sends a handshake, we don't
* know what swarm the connection is intended for.
*/
exports.createTCPIncomingPeer = conn => {
return _createIncomingPeer(conn, 'tcpIncoming')
}
exports.createTCPIncomingPeer = conn => _createIncomingPeer(conn, 'tcpIncoming')

/**
* Incoming uTP peers start out connected, because the remote peer connected to the
* listening port of the uTP server. Until the remote peer sends a handshake, we don't
* know what swarm the connection is intended for.
*/
exports.createUTPIncomingPeer = conn => {
return _createIncomingPeer(conn, 'utpIncoming')
}
exports.createUTPIncomingPeer = conn => _createIncomingPeer(conn, 'utpIncoming')

/**
* Outgoing TCP peers start out with just an IP address. At some point (when there is an
* available connection), the client can attempt to connect to the address.
*/
exports.createTCPOutgoingPeer = (addr, swarm) => {
return _createOutgoingPeer(addr, swarm, 'tcpOutgoing')
}
exports.createTCPOutgoingPeer = (addr, swarm) => _createOutgoingPeer(addr, swarm, 'tcpOutgoing')

/**
* Outgoing uTP peers start out with just an IP address. At some point (when there is an
* available connection), the client can attempt to connect to the address.
*/
exports.createUTPOutgoingPeer = (addr, swarm) => {
return _createOutgoingPeer(addr, swarm, 'utpOutgoing')
}
exports.createUTPOutgoingPeer = (addr, swarm) => _createOutgoingPeer(addr, swarm, 'utpOutgoing')

const _createIncomingPeer = (conn, type) => {
const addr = `${conn.remoteAddress}:${conn.remotePort}`
Expand Down
7 changes: 4 additions & 3 deletions lib/torrent.js
Original file line number Diff line number Diff line change
Expand Up @@ -1269,7 +1269,7 @@ class Torrent extends EventEmitter {
const self = this

if (typeof window !== 'undefined' && typeof window.requestIdleCallback === 'function') {
window.requestIdleCallback(function () { self._updateWire(wire) }, { timeout: 250 })
window.requestIdleCallback(() => { self._updateWire(wire) }, { timeout: 250 })
} else {
self._updateWire(wire)
}
Expand Down Expand Up @@ -1677,8 +1677,8 @@ class Torrent extends EventEmitter {
// is the torrent done? (if all current selections are satisfied, or there are
// no selections, then torrent is done)
let done = true
for (let i = 0; i < this._selections.length; i++) {
const selection = this._selections[i]

for (const selection of this._selections) {
for (let piece = selection.from; piece <= selection.to; piece++) {
if (!this.bitfield.get(piece)) {
done = false
Expand All @@ -1687,6 +1687,7 @@ class Torrent extends EventEmitter {
}
if (!done) break
}

if (!this.done && done) {
this.done = true
this._debug(`torrent done: ${this.infoHash}`)
Expand Down
4 changes: 1 addition & 3 deletions lib/webconn.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ class WebConn extends Wire {
end: rangeEnd
}]
} else {
const requestedFiles = files.filter(file => {
return file.offset <= rangeEnd && (file.offset + file.length) > rangeStart
})
const requestedFiles = files.filter(file => file.offset <= rangeEnd && (file.offset + file.length) > rangeStart)
if (requestedFiles.length < 1) {
return cb(new Error('Could not find file corresponding to web seed range request'))
}
Expand Down
58 changes: 29 additions & 29 deletions test/browser/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ img.name = 'img.png'
function verifyImage (t, err, elem) {
t.error(err)
t.ok(typeof elem.src === 'string')
t.ok(elem.src.indexOf('blob') !== -1)
t.ok(elem.src.includes('blob'))
t.equal(elem.parentElement.nodeName, 'BODY')
t.ok(elem.alt, 'file.name')
elem.remove()
Expand All @@ -17,97 +17,97 @@ function verifyImage (t, err, elem) {
// TODO get these working
// logic taken from https://github.com/atom/electron/issues/2288#issuecomment-123147993
if (!(global && global.process && global.process.versions && global.process.versions.electron)) {
test('image append w/ query selector', function (t) {
test('image append w/ query selector', t => {
t.plan(6)

const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })
client.on('error', err => { t.fail(err) })
client.on('warning', err => { t.fail(err) })

client.seed(img, function (torrent) {
torrent.files[0].appendTo('body', function (err, elem) {
client.seed(img, torrent => {
torrent.files[0].appendTo('body', (err, elem) => {
verifyImage(t, err, elem)
client.destroy(function (err) {
client.destroy(err => {
t.error(err, 'client destroyed')
})
})
})
})

test('image append w/ element', function (t) {
test('image append w/ element', t => {
t.plan(6)

const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })
client.on('error', err => { t.fail(err) })
client.on('warning', err => { t.fail(err) })

client.seed(img, function (torrent) {
torrent.files[0].appendTo(document.body, function (err, elem) {
client.seed(img, torrent => {
torrent.files[0].appendTo(document.body, (err, elem) => {
verifyImage(t, err, elem)
client.destroy(function (err) {
client.destroy(err => {
t.error(err, 'client destroyed')
})
})
})
})

test('image render w/ query selector', function (t) {
test('image render w/ query selector', t => {
t.plan(6)

const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })
client.on('error', err => { t.fail(err) })
client.on('warning', err => { t.fail(err) })

const tag = document.createElement('img')
tag.className = 'tag'
document.body.appendChild(tag)

client.seed(img, function (torrent) {
torrent.files[0].renderTo('img.tag', function (err, elem) {
client.seed(img, torrent => {
torrent.files[0].renderTo('img.tag', (err, elem) => {
verifyImage(t, err, elem)
client.destroy(function (err) {
client.destroy(err => {
t.error(err, 'client destroyed')
})
})
})
})

test('image render w/ element', function (t) {
test('image render w/ element', t => {
t.plan(6)

const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })
client.on('error', err => { t.fail(err) })
client.on('warning', err => { t.fail(err) })

const tag = document.createElement('img')
document.body.appendChild(tag)

client.seed(img, function (torrent) {
torrent.files[0].renderTo(tag, function (err, elem) {
client.seed(img, torrent => {
torrent.files[0].renderTo(tag, (err, elem) => {
verifyImage(t, err, elem)
client.destroy(function (err) {
client.destroy(err => {
t.error(err, 'client destroyed')
})
})
})
})
}

test('WebTorrent.WEBRTC_SUPPORT', function (t) {
test('WebTorrent.WEBRTC_SUPPORT', t => {
t.plan(2)

const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })
client.on('error', err => { t.fail(err) })
client.on('warning', err => { t.fail(err) })

t.equal(WebTorrent.WEBRTC_SUPPORT, true)

client.destroy(function (err) {
client.destroy(err => {
t.error(err, 'client destroyed')
})
})
54 changes: 27 additions & 27 deletions test/client-add-duplicate-trackers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@ const fixtures = require('webtorrent-fixtures')
const test = require('tape')
const WebTorrent = require('../')

test('client.add: duplicate trackers', function (t) {
test('client.add: duplicate trackers', t => {
t.plan(3)

const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })
client.on('error', err => { t.fail(err) })
client.on('warning', err => { t.fail(err) })

const torrent = client.add(fixtures.leaves.torrent, {
announce: ['wss://example.com', 'wss://example.com', 'wss://example.com']
})

torrent.on('ready', function () {
t.equal(torrent.magnetURI, fixtures.leaves.magnetURI + '&tr=' + encodeURIComponent('wss://example.com'))
client.remove(fixtures.leaves.magnetURI, function (err) { t.error(err, 'torrent destroyed') })
client.destroy(function (err) { t.error(err, 'client destroyed') })
torrent.on('ready', () => {
t.equal(torrent.magnetURI, `${fixtures.leaves.magnetURI}&tr=${encodeURIComponent('wss://example.com')}`)
client.remove(fixtures.leaves.magnetURI, err => { t.error(err, 'torrent destroyed') })
client.destroy(err => { t.error(err, 'client destroyed') })
})
})

test('client.add: duplicate trackers, with multiple torrents', function (t) {
test('client.add: duplicate trackers, with multiple torrents', t => {
t.plan(5)

// Re-use this object, in case webtorrent is changing it
Expand All @@ -31,27 +31,27 @@ test('client.add: duplicate trackers, with multiple torrents', function (t) {

const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })
client.on('error', err => { t.fail(err) })
client.on('warning', err => { t.fail(err) })

const torrent1 = client.add(fixtures.leaves.torrent, opts)

torrent1.on('ready', function () {
t.equal(torrent1.magnetURI, fixtures.leaves.magnetURI + '&tr=' + encodeURIComponent('wss://example.com'))
torrent1.on('ready', () => {
t.equal(torrent1.magnetURI, `${fixtures.leaves.magnetURI}&tr=${encodeURIComponent('wss://example.com')}`)

const torrent2 = client.add(fixtures.alice.torrent, opts)

torrent2.on('ready', function () {
t.equal(torrent2.magnetURI, fixtures.alice.magnetURI + '&tr=' + encodeURIComponent('wss://example.com'))
torrent2.on('ready', () => {
t.equal(torrent2.magnetURI, `${fixtures.alice.magnetURI}&tr=${encodeURIComponent('wss://example.com')}`)

torrent1.destroy(function (err) { t.error(err, 'torrent1 destroyed') })
torrent2.destroy(function (err) { t.error(err, 'torrent2 destroyed') })
client.destroy(function (err) { t.error(err, 'client destroyed') })
torrent1.destroy(err => { t.error(err, 'torrent1 destroyed') })
torrent2.destroy(err => { t.error(err, 'torrent2 destroyed') })
client.destroy(err => { t.error(err, 'client destroyed') })
})
})
})

test('client.add: duplicate trackers (including in .torrent file), multiple torrents', function (t) {
test('client.add: duplicate trackers (including in .torrent file), multiple torrents', t => {
t.plan(5)

// Re-use this object, in case webtorrent is changing it
Expand All @@ -68,22 +68,22 @@ test('client.add: duplicate trackers (including in .torrent file), multiple torr

const client = new WebTorrent({ dht: false, tracker: false, lsd: false })

client.on('error', function (err) { t.fail(err) })
client.on('warning', function (err) { t.fail(err) })
client.on('error', err => { t.fail(err) })
client.on('warning', err => { t.fail(err) })

const torrent1 = client.add(parsedTorrentLeaves, opts)

torrent1.on('ready', function () {
t.equal(torrent1.magnetURI, fixtures.leaves.magnetURI + '&tr=' + encodeURIComponent('wss://example.com'))
torrent1.on('ready', () => {
t.equal(torrent1.magnetURI, `${fixtures.leaves.magnetURI}&tr=${encodeURIComponent('wss://example.com')}`)

const torrent2 = client.add(parsedTorrentAlice, opts)

torrent2.on('ready', function () {
t.equal(torrent2.magnetURI, fixtures.alice.magnetURI + '&tr=' + encodeURIComponent('wss://example.com'))
torrent2.on('ready', () => {
t.equal(torrent2.magnetURI, `${fixtures.alice.magnetURI}&tr=${encodeURIComponent('wss://example.com')}`)

torrent1.destroy(function (err) { t.error(err, 'torrent1 destroyed') })
torrent2.destroy(function (err) { t.error(err, 'torrent2 destroyed') })
client.destroy(function (err) { t.error(err, 'client destroyed') })
torrent1.destroy(err => { t.error(err, 'torrent1 destroyed') })
torrent2.destroy(err => { t.error(err, 'torrent2 destroyed') })
client.destroy(err => { t.error(err, 'client destroyed') })
})
})
})
Loading

0 comments on commit 46033ae

Please sign in to comment.