@@ -21,37 +21,42 @@ inherits(Client, EventEmitter)
2121 *
2222 * Find torrent peers, to help a torrent client participate in a torrent swarm.
2323 *
24- * @param {string|Buffer } peerId peer id
25- * @param {Number } port torrent client listening port
26- * @param {Object } torrent parsed torrent
27- * @param {Object } opts options object
28- * @param {Number } opts.rtcConfig RTCPeerConnection configuration object
29- * @param {Number } opts.wrtc custom webrtc impl (useful in node.js)
30- * @param {function } opts.getAnnounceOpts callback to provide data to tracker
24+ * @param {Object } opts options object
25+ * @param {string|Buffer } opts.infoHash torrent info hash
26+ * @param {string|Buffer } opts.peerId peer id
27+ * @param {string|Array.<string> } opts.announce announce
28+ * @param {number } opts.port torrent client listening port
29+ * @param {function } opts.getAnnounceOpts callback to provide data to tracker
30+ * @param {number } opts.rtcConfig RTCPeerConnection configuration object
31+ * @param {number } opts.wrtc custom webrtc impl (useful in node.js)
3132 */
32- function Client ( peerId , port , torrent , opts ) {
33+ function Client ( opts ) {
3334 var self = this
34- if ( ! ( self instanceof Client ) ) return new Client ( peerId , port , torrent , opts )
35+ if ( ! ( self instanceof Client ) ) return new Client ( opts )
3536 EventEmitter . call ( self )
3637 if ( ! opts ) opts = { }
3738
39+ if ( ! opts . peerId ) throw new Error ( 'Option `peerId` is required' )
40+ if ( ! opts . infoHash ) throw new Error ( 'Option `infoHash` is required' )
41+ if ( ! opts . announce ) throw new Error ( 'Option `announce` is required' )
42+ if ( ! process . browser && ! opts . port ) throw new Error ( 'Option `port` is required' )
43+
3844 // required
39- self . peerId = typeof peerId === 'string'
40- ? peerId
41- : peerId . toString ( 'hex' )
42- self . peerIdBuffer = new Buffer ( self . peerId , 'hex' )
43- self . _peerIdBinary = self . peerIdBuffer . toString ( 'binary' )
44-
45- self . infoHash = typeof torrent . infoHash === 'string'
46- ? torrent . infoHash
47- : torrent . infoHash . toString ( 'hex' )
48- self . infoHashBuffer = new Buffer ( self . infoHash , 'hex' )
49- self . _infoHashBinary = self . infoHashBuffer . toString ( 'binary' )
50-
51- self . torrentLength = torrent . length
52- self . destroyed = false
45+ self . peerId = typeof opts . peerId === 'string'
46+ ? opts . peerId
47+ : opts . peerId . toString ( 'hex' )
48+ self . _peerIdBuffer = new Buffer ( self . peerId , 'hex' )
49+ self . _peerIdBinary = self . _peerIdBuffer . toString ( 'binary' )
50+
51+ self . infoHash = typeof opts . infoHash === 'string'
52+ ? opts . infoHash
53+ : opts . infoHash . toString ( 'hex' )
54+ self . _infoHashBuffer = new Buffer ( self . infoHash , 'hex' )
55+ self . _infoHashBinary = self . _infoHashBuffer . toString ( 'binary' )
5356
54- self . _port = port
57+ self . _port = opts . port
58+
59+ self . destroyed = false
5560
5661 self . _rtcConfig = opts . rtcConfig
5762 self . _wrtc = opts . wrtc
@@ -61,11 +66,11 @@ function Client (peerId, port, torrent, opts) {
6166
6267 var webrtcSupport = ! ! self . _wrtc || typeof window !== 'undefined'
6368
64- var announce = ( typeof torrent . announce === 'string' )
65- ? [ torrent . announce ]
66- : torrent . announce == null
69+ var announce = ( typeof opts . announce === 'string' )
70+ ? [ opts . announce ]
71+ : opts . announce == null
6772 ? [ ]
68- : torrent . announce
73+ : opts . announce
6974
7075 announce = announce . map ( function ( announceUrl ) {
7176 announceUrl = announceUrl . toString ( )
@@ -112,23 +117,27 @@ function Client (peerId, port, torrent, opts) {
112117 * Simple convenience function to scrape a tracker for an info hash without needing to
113118 * create a Client, pass it a parsed torrent, etc. Support scraping a tracker for multiple
114119 * torrents at the same time.
115- * @param {string } announceUrl
116- * @param {string|Array.<string> } infoHash
120+ * @params {Object} opts
121+ * @param {string|Array.<string> } opts.infoHash
122+ * @param {string } opts.announce
117123 * @param {function } cb
118124 */
119- Client . scrape = function ( announceUrl , infoHash , cb ) {
125+ Client . scrape = function ( opts , cb ) {
120126 cb = once ( cb )
121127
122- var peerId = new Buffer ( '01234567890123456789' ) // dummy value
123- var port = 6881 // dummy value
124- var torrent = {
125- infoHash : Array . isArray ( infoHash ) ? infoHash [ 0 ] : infoHash ,
126- announce : [ announceUrl ]
127- }
128- var client = new Client ( peerId , port , torrent )
128+ if ( ! opts . infoHash ) throw new Error ( 'Option `infoHash` is required' )
129+ if ( ! opts . announce ) throw new Error ( 'Option `announce` is required' )
130+
131+ var clientOpts = extend ( opts , {
132+ infoHash : Array . isArray ( opts . infoHash ) ? opts . infoHash [ 0 ] : opts . infoHash ,
133+ peerId : new Buffer ( '01234567890123456789' ) , // dummy value
134+ port : 6881 // dummy value
135+ } )
136+
137+ var client = new Client ( clientOpts )
129138 client . once ( 'error' , cb )
130139
131- var len = Array . isArray ( infoHash ) ? infoHash . length : 1
140+ var len = Array . isArray ( opts . infoHash ) ? opts . infoHash . length : 1
132141 var results = { }
133142 client . on ( 'scrape' , function ( data ) {
134143 len -= 1
@@ -144,10 +153,11 @@ Client.scrape = function (announceUrl, infoHash, cb) {
144153 }
145154 } )
146155
147- infoHash = Array . isArray ( infoHash )
148- ? infoHash . map ( function ( infoHash ) { return new Buffer ( infoHash , 'hex' ) } )
149- : new Buffer ( infoHash , 'hex' )
150- client . scrape ( { infoHash : infoHash } )
156+ opts . infoHash = Array . isArray ( opts . infoHash )
157+ ? opts . infoHash . map ( function ( infoHash ) { return new Buffer ( infoHash , 'hex' ) } )
158+ : new Buffer ( opts . infoHash , 'hex' )
159+ client . scrape ( { infoHash : opts . infoHash } )
160+ return client
151161}
152162
153163/**
@@ -198,9 +208,6 @@ Client.prototype.complete = function (opts) {
198208 var self = this
199209 debug ( 'send `complete`' )
200210 if ( ! opts ) opts = { }
201- if ( opts . downloaded == null && self . torrentLength != null ) {
202- opts . downloaded = self . torrentLength
203- }
204211 opts = self . _defaultAnnounceOpts ( opts )
205212 opts . event = 'completed'
206213 self . _announce ( opts )
@@ -277,10 +284,6 @@ Client.prototype._defaultAnnounceOpts = function (opts) {
277284 if ( opts . uploaded == null ) opts . uploaded = 0
278285 if ( opts . downloaded == null ) opts . downloaded = 0
279286
280- if ( opts . left == null && self . torrentLength != null ) {
281- opts . left = self . torrentLength - opts . downloaded
282- }
283-
284287 if ( self . _getAnnounceOpts ) opts = extend ( opts , self . _getAnnounceOpts ( ) )
285288 return opts
286289}
0 commit comments