@@ -34,42 +34,40 @@ const parseWebSocketRequest = require('./lib/server/parse-websocket')
3434class Server extends EventEmitter {
3535 constructor ( opts = { } ) {
3636 super ( )
37- const self = this
38-
3937 debug ( 'new server %s' , JSON . stringify ( opts ) )
4038
41- self . intervalMs = opts . interval
39+ this . intervalMs = opts . interval
4240 ? opts . interval
4341 : 10 * 60 * 1000 // 10 min
4442
45- self . _trustProxy = ! ! opts . trustProxy
46- if ( typeof opts . filter === 'function' ) self . _filter = opts . filter
43+ this . _trustProxy = ! ! opts . trustProxy
44+ if ( typeof opts . filter === 'function' ) this . _filter = opts . filter
4745
48- self . peersCacheLength = opts . peersCacheLength
49- self . peersCacheTtl = opts . peersCacheTtl
46+ this . peersCacheLength = opts . peersCacheLength
47+ this . peersCacheTtl = opts . peersCacheTtl
5048
51- self . _listenCalled = false
52- self . listening = false
53- self . destroyed = false
54- self . torrents = { }
49+ this . _listenCalled = false
50+ this . listening = false
51+ this . destroyed = false
52+ this . torrents = { }
5553
56- self . http = null
57- self . udp4 = null
58- self . udp6 = null
59- self . ws = null
54+ this . http = null
55+ this . udp4 = null
56+ this . udp6 = null
57+ this . ws = null
6058
6159 // start an http tracker unless the user explictly says no
6260 if ( opts . http !== false ) {
63- self . http = http . createServer ( )
64- self . http . on ( 'error' , err => { self . _onError ( err ) } )
65- self . http . on ( 'listening' , onListening )
61+ this . http = http . createServer ( )
62+ this . http . on ( 'error' , err => { this . _onError ( err ) } )
63+ this . http . on ( 'listening' , onListening )
6664
6765 // Add default http request handler on next tick to give user the chance to add
6866 // their own handler first. Handle requests untouched by user's handler.
6967 process . nextTick ( ( ) => {
70- self . http . on ( 'request' , ( req , res ) => {
68+ this . http . on ( 'request' , ( req , res ) => {
7169 if ( res . headersSent ) return
72- self . onHttpRequest ( req , res )
70+ this . onHttpRequest ( req , res )
7371 } )
7472 } )
7573 }
@@ -78,32 +76,32 @@ class Server extends EventEmitter {
7876 if ( opts . udp !== false ) {
7977 const isNode10 = / ^ v 0 .1 0 ./ . test ( process . version )
8078
81- self . udp4 = self . udp = dgram . createSocket (
79+ this . udp4 = this . udp = dgram . createSocket (
8280 isNode10 ? 'udp4' : { type : 'udp4' , reuseAddr : true }
8381 )
84- self . udp4 . on ( 'message' , ( msg , rinfo ) => { self . onUdpRequest ( msg , rinfo ) } )
85- self . udp4 . on ( 'error' , err => { self . _onError ( err ) } )
86- self . udp4 . on ( 'listening' , onListening )
82+ this . udp4 . on ( 'message' , ( msg , rinfo ) => { this . onUdpRequest ( msg , rinfo ) } )
83+ this . udp4 . on ( 'error' , err => { this . _onError ( err ) } )
84+ this . udp4 . on ( 'listening' , onListening )
8785
88- self . udp6 = dgram . createSocket (
86+ this . udp6 = dgram . createSocket (
8987 isNode10 ? 'udp6' : { type : 'udp6' , reuseAddr : true }
9088 )
91- self . udp6 . on ( 'message' , ( msg , rinfo ) => { self . onUdpRequest ( msg , rinfo ) } )
92- self . udp6 . on ( 'error' , err => { self . _onError ( err ) } )
93- self . udp6 . on ( 'listening' , onListening )
89+ this . udp6 . on ( 'message' , ( msg , rinfo ) => { this . onUdpRequest ( msg , rinfo ) } )
90+ this . udp6 . on ( 'error' , err => { this . _onError ( err ) } )
91+ this . udp6 . on ( 'listening' , onListening )
9492 }
9593
9694 // start a websocket tracker (for WebTorrent) unless the user explicitly says no
9795 if ( opts . ws !== false ) {
98- if ( ! self . http ) {
99- self . http = http . createServer ( )
100- self . http . on ( 'error' , err => { self . _onError ( err ) } )
101- self . http . on ( 'listening' , onListening )
96+ if ( ! this . http ) {
97+ this . http = http . createServer ( )
98+ this . http . on ( 'error' , err => { this . _onError ( err ) } )
99+ this . http . on ( 'listening' , onListening )
102100
103101 // Add default http request handler on next tick to give user the chance to add
104102 // their own handler first. Handle requests untouched by user's handler.
105103 process . nextTick ( ( ) => {
106- self . http . on ( 'request' , ( req , res ) => {
104+ this . http . on ( 'request' , ( req , res ) => {
107105 if ( res . headersSent ) return
108106 // For websocket trackers, we only need to handle the UPGRADE http method.
109107 // Return 404 for all other request types.
@@ -112,35 +110,35 @@ class Server extends EventEmitter {
112110 } )
113111 } )
114112 }
115- self . ws = new WebSocketServer ( {
116- server : self . http ,
113+ this . ws = new WebSocketServer ( {
114+ server : this . http ,
117115 perMessageDeflate : false ,
118116 clientTracking : false
119117 } )
120- self . ws . address = ( ) => {
121- return self . http . address ( )
118+ this . ws . address = ( ) => {
119+ return this . http . address ( )
122120 }
123- self . ws . on ( 'error' , err => { self . _onError ( err ) } )
124- self . ws . on ( 'connection' , ( socket , req ) => {
121+ this . ws . on ( 'error' , err => { this . _onError ( err ) } )
122+ this . ws . on ( 'connection' , ( socket , req ) => {
125123 // Note: socket.upgradeReq was removed in [email protected] , so re-add it. 126124 // https://github.com/websockets/ws/pull/1099
127125 socket . upgradeReq = req
128- self . onWebSocketConnection ( socket )
126+ this . onWebSocketConnection ( socket )
129127 } )
130128 }
131129
132130 if ( opts . stats !== false ) {
133- if ( ! self . http ) {
134- self . http = http . createServer ( )
135- self . http . on ( 'error' , err => { self . _onError ( err ) } )
136- self . http . on ( 'listening' , onListening )
131+ if ( ! this . http ) {
132+ this . http = http . createServer ( )
133+ this . http . on ( 'error' , err => { this . _onError ( err ) } )
134+ this . http . on ( 'listening' , onListening )
137135 }
138136
139137 // Http handler for '/stats' route
140- self . http . on ( 'request' , ( req , res ) => {
138+ this . http . on ( 'request' , ( req , res ) => {
141139 if ( res . headersSent ) return
142140
143- const infoHashes = Object . keys ( self . torrents )
141+ const infoHashes = Object . keys ( this . torrents )
144142 let activeTorrents = 0
145143 const allPeers = { }
146144
@@ -196,7 +194,7 @@ class Server extends EventEmitter {
196194
197195 if ( req . method === 'GET' && ( req . url === '/stats' || req . url === '/stats.json' ) ) {
198196 infoHashes . forEach ( infoHash => {
199- const peers = self . torrents [ infoHash ] . peers
197+ const peers = this . torrents [ infoHash ] . peers
200198 const keys = peers . keys
201199 if ( keys . length > 0 ) activeTorrents ++
202200
@@ -253,14 +251,24 @@ class Server extends EventEmitter {
253251 res . write ( JSON . stringify ( stats ) )
254252 res . end ( )
255253 } else if ( req . url === '/stats' ) {
256- res . end ( `<h1>${ stats . torrents } torrents (${ stats . activeTorrents } active)</h1>\n<h2>Connected Peers: ${ stats . peersAll } </h2>\n<h3>Peers Seeding Only: ${ stats . peersSeederOnly } </h3>\n<h3>Peers Leeching Only: ${ stats . peersLeecherOnly } </h3>\n<h3>Peers Seeding & Leeching: ${ stats . peersSeederAndLeecher } </h3>\n<h3>IPv4 Peers: ${ stats . peersIPv4 } </h3>\n<h3>IPv6 Peers: ${ stats . peersIPv6 } </h3>\n<h3>Clients:</h3>\n${ printClients ( stats . clients ) } `
257- )
254+ res . end ( `
255+ <h1>${ stats . torrents } torrents (${ stats . activeTorrents } active)</h1>
256+ <h2>Connected Peers: ${ stats . peersAll } </h2>
257+ <h3>Peers Seeding Only: ${ stats . peersSeederOnly } </h3>
258+ <h3>Peers Leeching Only: ${ stats . peersLeecherOnly } </h3>
259+ <h3>Peers Seeding & Leeching: ${ stats . peersSeederAndLeecher } </h3>
260+ <h3>IPv4 Peers: ${ stats . peersIPv4 } </h3>
261+ <h3>IPv6 Peers: ${ stats . peersIPv6 } </h3>
262+ <h3>Clients:</h3>
263+ ${ printClients ( stats . clients ) }
264+ ` . replace ( / ^ \s + / gm, '' ) ) // trim left
258265 }
259266 }
260267 } )
261268 }
262269
263- let num = ! ! self . http + ! ! self . udp4 + ! ! self . udp6
270+ let num = ! ! this . http + ! ! this . udp4 + ! ! this . udp6
271+ const self = this
264272 function onListening ( ) {
265273 num -= 1
266274 if ( num === 0 ) {
@@ -451,8 +459,6 @@ class Server extends EventEmitter {
451459 }
452460
453461 _onWebSocketRequest ( socket , opts , params ) {
454- const self = this
455-
456462 try {
457463 params = parseWebSocketRequest ( socket , opts , params )
458464 } catch ( err ) {
@@ -462,22 +468,22 @@ class Server extends EventEmitter {
462468
463469 // even though it's an error for the client, it's just a warning for the server.
464470 // don't crash the server because a client sent bad data :)
465- self . emit ( 'warning' , err )
471+ this . emit ( 'warning' , err )
466472 return
467473 }
468474
469475 if ( ! socket . peerId ) socket . peerId = params . peer_id // as hex
470476
471- self . _onRequest ( params , ( err , response ) => {
472- if ( self . destroyed || socket . destroyed ) return
477+ this . _onRequest ( params , ( err , response ) => {
478+ if ( this . destroyed || socket . destroyed ) return
473479 if ( err ) {
474480 socket . send ( JSON . stringify ( {
475481 action : params . action === common . ACTIONS . ANNOUNCE ? 'announce' : 'scrape' ,
476482 'failure reason' : err . message ,
477483 info_hash : common . hexToBinary ( params . info_hash )
478484 } ) , socket . onSend )
479485
480- self . emit ( 'warning' , err )
486+ this . emit ( 'warning' , err )
481487 return
482488 }
483489
@@ -495,7 +501,7 @@ class Server extends EventEmitter {
495501 response . info_hash = common . hexToBinary ( params . info_hash )
496502
497503 // WebSocket tracker should have a shorter interval – default: 2 minutes
498- response . interval = Math . ceil ( self . intervalMs / 1000 / 5 )
504+ response . interval = Math . ceil ( this . intervalMs / 1000 / 5 )
499505 }
500506
501507 // Skip sending update back for 'answer' announce messages – not needed
@@ -519,19 +525,26 @@ class Server extends EventEmitter {
519525 } )
520526 }
521527
528+ const done = ( ) => {
529+ // emit event once the announce is fully "processed"
530+ if ( params . action === common . ACTIONS . ANNOUNCE ) {
531+ this . emit ( common . EVENT_NAMES [ params . event ] , params . peer_id , params )
532+ }
533+ }
534+
522535 if ( params . answer ) {
523536 debug ( 'got answer %s from %s' , JSON . stringify ( params . answer ) , params . peer_id )
524537
525- self . getSwarm ( params . info_hash , ( err , swarm ) => {
526- if ( self . destroyed ) return
527- if ( err ) return self . emit ( 'warning' , err )
538+ this . getSwarm ( params . info_hash , ( err , swarm ) => {
539+ if ( this . destroyed ) return
540+ if ( err ) return this . emit ( 'warning' , err )
528541 if ( ! swarm ) {
529- return self . emit ( 'warning' , new Error ( 'no swarm with that `info_hash`' ) )
542+ return this . emit ( 'warning' , new Error ( 'no swarm with that `info_hash`' ) )
530543 }
531544 // Mark the destination peer as recently used in cache
532545 const toPeer = swarm . peers . get ( params . to_peer_id )
533546 if ( ! toPeer ) {
534- return self . emit ( 'warning' , new Error ( 'no peer with that `to_peer_id`' ) )
547+ return this . emit ( 'warning' , new Error ( 'no peer with that `to_peer_id`' ) )
535548 }
536549
537550 toPeer . socket . send ( JSON . stringify ( {
@@ -548,13 +561,6 @@ class Server extends EventEmitter {
548561 } else {
549562 done ( )
550563 }
551-
552- function done ( ) {
553- // emit event once the announce is fully "processed"
554- if ( params . action === common . ACTIONS . ANNOUNCE ) {
555- self . emit ( common . EVENT_NAMES [ params . event ] , params . peer_id , params )
556- }
557- }
558564 } )
559565 }
560566
@@ -624,8 +630,8 @@ class Server extends EventEmitter {
624630 _onAnnounce ( params , cb ) {
625631 const self = this
626632
627- if ( self . _filter ) {
628- self . _filter ( params . info_hash , params , err => {
633+ if ( this . _filter ) {
634+ this . _filter ( params . info_hash , params , err => {
629635 // Presence of `err` means that this announce request is disallowed
630636 if ( err ) return cb ( err )
631637
@@ -693,17 +699,15 @@ class Server extends EventEmitter {
693699 }
694700
695701 _onScrape ( params , cb ) {
696- const self = this
697-
698702 if ( params . info_hash == null ) {
699703 // if info_hash param is omitted, stats for all torrents are returned
700704 // TODO: make this configurable!
701- params . info_hash = Object . keys ( self . torrents )
705+ params . info_hash = Object . keys ( this . torrents )
702706 }
703707
704708 series ( params . info_hash . map ( infoHash => {
705709 return cb => {
706- self . getSwarm ( infoHash , ( err , swarm ) => {
710+ this . getSwarm ( infoHash , ( err , swarm ) => {
707711 if ( err ) return cb ( err )
708712 if ( swarm ) {
709713 swarm . scrape ( params , ( err , scrapeInfo ) => {
@@ -725,7 +729,7 @@ class Server extends EventEmitter {
725729 const response = {
726730 action : common . ACTIONS . SCRAPE ,
727731 files : { } ,
728- flags : { min_request_interval : Math . ceil ( self . intervalMs / 1000 ) }
732+ flags : { min_request_interval : Math . ceil ( this . intervalMs / 1000 ) }
729733 }
730734
731735 results . forEach ( result => {
0 commit comments