Skip to content

Commit 5bfe1ce

Browse files
committed
feat: ✨use @fkn/lib & @webvpn/libs polyfills
1 parent b4557a7 commit 5bfe1ce

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import queueMicrotask from 'queue-microtask'
77
import { hex2arr, hex2bin, text2arr, arr2hex, arr2text } from 'uint8-util'
88

99
import common from './lib/common.js'
10-
import HTTPTracker from './lib/client/http-tracker.js' // empty object in browser
11-
import UDPTracker from './lib/client/udp-tracker.js' // empty object in browser
10+
import HTTPTracker from './lib/client/http-tracker.js'
11+
import UDPTracker from './lib/client/udp-tracker.js'
1212
import WebSocketTracker from './lib/client/websocket-tracker.js'
1313

1414
const debug = Debug('bittorrent-tracker:client')

lib/client/http-tracker.js

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import arrayRemove from 'unordered-array-remove'
22
import bencode from 'bencode'
33
import Debug from 'debug'
4-
import fetch from 'cross-fetch-ponyfill'
4+
import { serverProxyFetch as fetch } from '@fkn/lib'
55
import { bin2hex, hex2bin, arr2text, text2arr, arr2hex } from 'uint8-util'
66

77
import common from '../common.js'
@@ -121,7 +121,36 @@ class HTTPTracker extends Tracker {
121121
}
122122

123123
async _request (requestUrl, params, cb) {
124-
const parsedUrl = new URL(requestUrl + (requestUrl.indexOf('?') === -1 ? '?' : '&') + common.querystringStringify(params))
124+
const parsedUrl = new URL(
125+
requestUrl +
126+
(requestUrl.indexOf('?') === -1 ? '?' : '&') +
127+
common.querystringStringify(
128+
Object.fromEntries(
129+
Object
130+
.entries(params)
131+
.filter(([_, value]) =>
132+
!(
133+
value === undefined
134+
|| value === null
135+
|| value === ''
136+
|| (
137+
typeof value === 'number'
138+
&& isNaN(value)
139+
)
140+
)
141+
)
142+
)
143+
)
144+
)
145+
146+
parsedUrl.searchParams.set(
147+
'info_hash',
148+
new URLSearchParams({ info_hash: escape(params.info_hash) })
149+
.toString()
150+
.replaceAll('%25', '%')
151+
.replace('info_hash=', '')
152+
)
153+
125154
let agent
126155
if (this.client._proxyOpts) {
127156
agent = parsedUrl.protocol === 'https:' ? this.client._proxyOpts.httpsAgent : this.client._proxyOpts.httpAgent
@@ -144,7 +173,7 @@ class HTTPTracker extends Tracker {
144173
let res
145174
let controller = abortTimeout(common.REQUEST_TIMEOUT)
146175
try {
147-
res = await fetch(parsedUrl.toString(), {
176+
res = await fetch(parsedUrl.toString().replaceAll('%25', '%'), {
148177
agent,
149178
signal: controller.signal,
150179
dispatcher: agent,

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
"bittorrent-tracker": "./bin/cmd.js"
1212
},
1313
"browser": {
14-
"./lib/common-node.js": false,
15-
"./lib/client/http-tracker.js": false,
16-
"./lib/client/udp-tracker.js": false,
1714
"./server.js": false,
15+
"dgram": false,
1816
"socks": false
1917
},
2018
"chromeapp": {
@@ -27,8 +25,11 @@
2725
},
2826
"type": "module",
2927
"dependencies": {
28+
"@fkn/lib": "^0.2.1",
3029
"@thaunknown/simple-peer": "^10.0.8",
3130
"@thaunknown/simple-websocket": "^9.1.3",
31+
"@webvpn/dgram": "^0.2.0",
32+
"@webvpn/net": "^0.2.0",
3233
"bencode": "^4.0.0",
3334
"bittorrent-peerid": "^1.3.6",
3435
"chrome-dgram": "^3.0.6",

0 commit comments

Comments
 (0)