Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize lib/peer.js #1495

Merged
merged 2 commits into from Aug 30, 2018
Merged
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

Prev

Fix standard errors

  • Loading branch information
DiegoRBaquero committed Aug 30, 2018
commit ccf2f8daf235b4bf2dab61b553cb8f6b4626c705
@@ -1,20 +1,20 @@
const arrayRemove = require('unordered-array-remove');
const debug = require('debug')('webtorrent:peer');
const Wire = require('bittorrent-protocol');
const arrayRemove = require('unordered-array-remove')
const debug = require('debug')('webtorrent:peer')
const Wire = require('bittorrent-protocol')

const WebConn = require('./webconn');
const WebConn = require('./webconn')

const CONNECT_TIMEOUT_TCP = 5000;
const CONNECT_TIMEOUT_WEBRTC = 25000;
const HANDSHAKE_TIMEOUT = 25000;
const CONNECT_TIMEOUT_TCP = 5000
const CONNECT_TIMEOUT_WEBRTC = 25000
const HANDSHAKE_TIMEOUT = 25000

/**
* WebRTC peer connections start out connected, because WebRTC peers require an
* "introduction" (i.e. WebRTC signaling), and there's no equivalent to an IP address
* that lets you refer to a WebRTC endpoint.
*/
exports.createWebRTCPeer = (conn, swarm) => {
const peer = new Peer(conn.id, 'webrtc');
const peer = new Peer(conn.id, 'webrtc')
peer.conn = conn
peer.swarm = swarm

@@ -35,8 +35,8 @@ exports.createWebRTCPeer = (conn, swarm) => {
* know what swarm the connection is intended for.
*/
exports.createTCPIncomingPeer = conn => {
const addr = `${conn.remoteAddress}:${conn.remotePort}`;
const peer = new Peer(addr, 'tcpIncoming');
const addr = `${conn.remoteAddress}:${conn.remotePort}`
const peer = new Peer(addr, 'tcpIncoming')
peer.conn = conn
peer.addr = addr

@@ -50,7 +50,7 @@ exports.createTCPIncomingPeer = conn => {
* available connection), the client can attempt to connect to the address.
*/
exports.createTCPOutgoingPeer = (addr, swarm) => {
const peer = new Peer(addr, 'tcpOutgoing');
const peer = new Peer(addr, 'tcpOutgoing')
peer.addr = addr
peer.swarm = swarm

@@ -61,7 +61,7 @@ exports.createTCPOutgoingPeer = (addr, swarm) => {
* Peer that represents a Web Seed (BEP17 / BEP19).
*/
exports.createWebSeedPeer = (url, swarm) => {
const peer = new Peer(url, 'webSeed');
const peer = new Peer(url, 'webSeed')
peer.swarm = swarm
peer.conn = new WebConn(url, swarm)

@@ -77,7 +77,7 @@ exports.createWebSeedPeer = (url, swarm) => {
* @param {string} type the type of the peer
*/
class Peer {
constructor(id, type) {
constructor (id, type) {
this.id = id
this.type = type

@@ -100,15 +100,15 @@ class Peer {
* Called once the peer is connected (i.e. fired 'connect' event)
* @param {Socket} conn
*/
onConnect() {
onConnect () {
if (this.destroyed) return
this.connected = true

debug('Peer %s connected', this.id)

clearTimeout(this.connectTimeout)

const conn = this.conn;
const conn = this.conn
conn.once('end', () => {
this.destroy()
})
@@ -122,7 +122,7 @@ class Peer {
this.destroy(err)
})

const wire = this.wire = new Wire();
const wire = this.wire = new Wire()
wire.type = this.type
wire.once('end', () => {
this.destroy()
@@ -151,7 +151,7 @@ class Peer {
* @param {string} infoHash
* @param {string} peerId
*/
onHandshake(infoHash, peerId) {
onHandshake (infoHash, peerId) {
if (!this.swarm) return // `this.swarm` not set yet, so do nothing
if (this.destroyed) return

@@ -171,7 +171,7 @@ class Peer {

this.retries = 0

let addr = this.addr;
let addr = this.addr
if (!addr && this.conn.remoteAddress && this.conn.remotePort) {
addr = `${this.conn.remoteAddress}:${this.conn.remotePort}`
}
@@ -183,31 +183,31 @@ class Peer {
if (!this.sentHandshake) this.handshake()
}

handshake() {
handshake () {
const opts = {
dht: this.swarm.private ? false : !!this.swarm.client.dht
};
}
this.wire.handshake(this.swarm.infoHash, this.swarm.client.peerId, opts)
this.sentHandshake = true
}

startConnectTimeout() {
startConnectTimeout () {
clearTimeout(this.connectTimeout)
this.connectTimeout = setTimeout(() => {
this.destroy(new Error('connect timeout'))
}, this.type === 'webrtc' ? CONNECT_TIMEOUT_WEBRTC : CONNECT_TIMEOUT_TCP)
if (this.connectTimeout.unref) this.connectTimeout.unref()
}

startHandshakeTimeout() {
startHandshakeTimeout () {
clearTimeout(this.handshakeTimeout)
this.handshakeTimeout = setTimeout(() => {
this.destroy(new Error('handshake timeout'))
}, HANDSHAKE_TIMEOUT)
if (this.handshakeTimeout.unref) this.handshakeTimeout.unref()
}

destroy(err) {
destroy (err) {
if (this.destroyed) return
this.destroyed = true
this.connected = false
@@ -217,9 +217,9 @@ class Peer {
clearTimeout(this.connectTimeout)
clearTimeout(this.handshakeTimeout)

const swarm = this.swarm;
const conn = this.conn;
const wire = this.wire;
const swarm = this.swarm
const conn = this.conn
const wire = this.wire

this.swarm = null
this.conn = null
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.