diff --git a/client.js b/client.js index c37345f1..5eb7e630 100644 --- a/client.js +++ b/client.js @@ -285,6 +285,18 @@ Client.scrape = (opts, cb) => { } }) + client.once('done', success => { + if (len !== 0) { + client.destroy() + const keys = Object.keys(results) + if (keys.length === 1) { + cb(null, results[keys[0]]) + } else { + cb(null, results) + } + } + }) + client.scrape({ infoHash: opts.infoHash }) return client } diff --git a/index.js b/index.js index e812c41c..bce36f39 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,7 @@ /*! bittorrent-tracker. MIT License. WebTorrent LLC */ import Client from './client.js' import Server from './server.js' +import common from './lib/common.js' export default Client -export { Client, Server } +export { Client, Server, common } diff --git a/lib/client/http-tracker.js b/lib/client/http-tracker.js index 6297d2e2..7916eb83 100644 --- a/lib/client/http-tracker.js +++ b/lib/client/http-tracker.js @@ -9,7 +9,7 @@ import Tracker from './tracker.js' import compact2string from 'compact2string' const debug = Debug('bittorrent-tracker:http-tracker') -const HTTP_SCRAPE_SUPPORT = /\/(announce)[^/]*$/ +const HTTP_SCRAPE_SUPPORT = /(\/ann(?:ounce)?)[^/]*$/ function abortTimeout (ms) { const controller = new AbortController() @@ -38,7 +38,7 @@ class HTTPTracker extends Tracker { const match = this.announceUrl.match(HTTP_SCRAPE_SUPPORT) if (match) { const pre = this.announceUrl.slice(0, match.index) - const post = this.announceUrl.slice(match.index + 9) + const post = this.announceUrl.slice(match.index + match[1].length) this.scrapeUrl = `${pre}/scrape${post}` } @@ -80,7 +80,7 @@ class HTTPTracker extends Tracker { this._request(this.scrapeUrl, params, (err, data) => { if (err) return this.client.emit('warning', err) this._onScrapeResponse(data) - }) + }).catch((err) => this.client.emit('warning', err)) } destroy (cb) { @@ -255,7 +255,7 @@ class HTTPTracker extends Tracker { keys.forEach(_infoHash => { // TODO: optionally handle data.flags.min_request_interval // (separate from announce interval) - const infoHash = _infoHash.length !== 20 ? arr2hex(text2arr(_infoHash)) : bin2hex(_infoHash) + const infoHash = _infoHash.length === 40 ? _infoHash : _infoHash.length !== 20 ? arr2hex(text2arr(_infoHash)) : bin2hex(_infoHash) const response = Object.assign(data[_infoHash], { announce: this.announceUrl, @@ -263,6 +263,8 @@ class HTTPTracker extends Tracker { }) this.client.emit('scrape', response) }) + + this.client.emit('done', true) } } diff --git a/lib/common-node.js b/lib/common-node.js index 9fefa5c4..af63eb10 100644 --- a/lib/common-node.js +++ b/lib/common-node.js @@ -32,13 +32,13 @@ export const EVENT_NAMES = { * Client request timeout. How long to wait before considering a request to a * tracker server to have timed out. */ -export const REQUEST_TIMEOUT = 15000 +export let REQUEST_TIMEOUT = 15000 /** * Client destroy timeout. How long to wait before forcibly cleaning up all * pending requests, open sockets, etc. */ -export const DESTROY_TIMEOUT = 1000 +export let DESTROY_TIMEOUT = 1000 export function toUInt32 (n) { const buf = new Uint8Array(4) diff --git a/lib/common.js b/lib/common.js index ef888579..f82619f5 100644 --- a/lib/common.js +++ b/lib/common.js @@ -4,8 +4,8 @@ import * as common from './common-node.js' export * from './common-node.js' -export const DEFAULT_ANNOUNCE_PEERS = 50 -export const MAX_ANNOUNCE_PEERS = 82 +export let DEFAULT_ANNOUNCE_PEERS = 50 +export let MAX_ANNOUNCE_PEERS = 82 // HACK: Fix for WHATWG URL object not parsing non-standard URL schemes like // 'udp:'. Just replace it with 'http:' since we only need a few properties.