Skip to content

Commit d90e0be

Browse files
committed
Expose HTTPTracker to browser env.
1 parent 58943a6 commit d90e0be

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

lib/client/http-tracker.js

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,23 @@ const bencode = require('bencode')
33
const compact2string = require('compact2string')
44
const debug = require('debug')('bittorrent-tracker:http-tracker')
55
const get = require('simple-get')
6+
const querystring = require('querystring-browser')
67

78
const common = require('../common')
89
const Tracker = require('./tracker')
910

1011
const HTTP_SCRAPE_SUPPORT = /\/(announce)[^/]*$/
1112

13+
const REQUEST_TIMEOUT = 15000
14+
const DESTROY_TIMEOUT = 1000
15+
16+
const querystringStringify = obj => {
17+
let ret = querystring.stringify(obj, null, null, { encodeURIComponent: escape })
18+
ret = ret.replace(/[@*/+]/g, char => // `escape` doesn't encode the characters @*/+ so we do it manually
19+
`%${char.charCodeAt(0).toString(16).toUpperCase()}`)
20+
return ret
21+
}
22+
1223
/**
1324
* HTTP torrent tracker client (for an individual tracker)
1425
*
@@ -86,7 +97,7 @@ class HTTPTracker extends Tracker {
8697

8798
// Otherwise, wait a short time for pending requests to complete, then force
8899
// destroy them.
89-
timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT)
100+
timeout = setTimeout(destroyCleanup, DESTROY_TIMEOUT)
90101

91102
// But, if all pending requests complete before the timeout fires, do cleanup
92103
// right away.
@@ -111,13 +122,13 @@ class HTTPTracker extends Tracker {
111122
_request (requestUrl, params, cb) {
112123
const self = this
113124
const u = requestUrl + (!requestUrl.includes('?') ? '?' : '&') +
114-
common.querystringStringify(params)
125+
querystringStringify(params)
115126

116127
this.cleanupFns.push(cleanup)
117128

118129
let request = get.concat({
119130
url: u,
120-
timeout: common.REQUEST_TIMEOUT,
131+
timeout: REQUEST_TIMEOUT,
121132
headers: {
122133
'user-agent': this.client._userAgent || ''
123134
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
"devDependencies": {
4949
"@webtorrent/semantic-release-config": "1.0.5",
5050
"magnet-uri": "6.2.0",
51+
"querystring-browser": "^1.0.4",
5152
"semantic-release": "17.4.3",
5253
"standard": "*",
5354
"tape": "5.2.2",

server.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ class Server extends EventEmitter {
6868
process.nextTick(() => {
6969
this.http.on('request', (req, res) => {
7070
if (res.headersSent) return
71+
res.setHeader('Access-Control-Allow-Origin', '*')
72+
res.setHeader('Access-Control-Allow-Headers', '*')
73+
res.setHeader('Access-Control-Allow-Credentials', 'true')
74+
res.setHeader('Access-Control-Allow-Methods', '*')
7175
this.onHttpRequest(req, res)
7276
})
7377
})

0 commit comments

Comments
 (0)