Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
110 commits
Select commit Hold shift + click to select a range
023afb9
fix: typo in ws example (#417)
rfinnie Mar 6, 2022
048bc45
chore(release): 9.18.4
semantic-release-bot Mar 6, 2022
e077218
chore(deps): update actions/setup-node action to v3 (#414)
renovate[bot] Mar 6, 2022
aa2dc81
chore(deps): update actions/stale action to v5 (#416)
renovate[bot] Mar 6, 2022
330301a
chore(deps): update actions/checkout action to v3 (#415)
renovate[bot] Mar 6, 2022
f7928cf
fix: connection leaks (#420)
lookis Mar 25, 2022
2a79101
chore(release): 9.18.5
semantic-release-bot Mar 25, 2022
4b041ca
chore(deps): update actions/cache action to v3 (#419)
renovate[bot] Mar 27, 2022
38c4fdb
chore(deps): update dependency tape to v5.5.3
renovate-bot Apr 8, 2022
8d54938
fix: revert #420
DiegoRBaquero May 11, 2022
0120218
ci: rollback to node 14
DiegoRBaquero May 11, 2022
a048097
chore(release): 9.18.6
semantic-release-bot May 11, 2022
ef76b3f
feat(events): Support of `paused` client event (#411)
pavloniym Jun 1, 2022
4b5299b
chore(release): 9.19.0
semantic-release-bot Jun 1, 2022
e6d3189
feat: esm (#431)
ThaUnknown Dec 5, 2022
de947a7
chore(release): 10.0.0
semantic-release-bot Dec 5, 2022
15bcf89
chore(deps): update dependency @webtorrent/semantic-release-config to…
renovate[bot] Dec 5, 2022
bf74ecf
chore(deps): update dependency tape to v5.6.1
renovate[bot] Dec 6, 2022
926ceee
fix(deps): update dependency bencode to v3 (#434) [skip ci]
renovate[bot] Dec 7, 2022
35ede9c
chore(deps): update dependency webtorrent-fixtures to v2 (#436) [skip…
renovate[bot] Dec 7, 2022
40c2567
chore(deps): update actions/stale action to v6 (#428) [skip ci]
renovate[bot] Dec 7, 2022
ac84fbd
chore(deps): update dependency semantic-release to v19 [security] (#4…
renovate[bot] Dec 7, 2022
9be843c
fix(deps): update dependency string2compact to v2 (#437)
renovate[bot] Dec 7, 2022
cb0bdfc
chore(deps): update dependency semantic-release to v19.0.5 (#438)
renovate[bot] Dec 7, 2022
df39fed
chore(release): 10.0.1
semantic-release-bot Dec 7, 2022
0d297e4
chore(deps): update dependency tape to v5.6.3
renovate[bot] Jan 17, 2023
dda9935
ci: upgrade node
DiegoRBaquero Jan 26, 2023
b38acbd
ci: add node-pre-gyp
DiegoRBaquero Jan 30, 2023
90ee337
chore(deps): update actions/stale action to v7 (#439)
renovate[bot] Jan 30, 2023
4bf8cb5
chore(deps): update dependency semantic-release to v20 (#441)
renovate[bot] Jan 30, 2023
138c6e7
chore(deps): update dependency magnet-uri to v7 (#444)
renovate[bot] Jan 30, 2023
b72d226
chore(deps): update webtorrent (#445)
renovate[bot] Feb 1, 2023
7c4578f
ci: update release node version (#447)
ThaUnknown Feb 1, 2023
2209d4f
fix(deps): update dependency ws to v8 (#448)
renovate[bot] Feb 1, 2023
7356e34
chore(release): 10.0.2
semantic-release-bot Feb 1, 2023
bf4481c
chore(deps): update dependency semantic-release to v20.1.1
renovate[bot] Mar 1, 2023
bf8831c
adds handling for x-forwarded-for comma-separated syntax (#452)
tdjsnelling Mar 17, 2023
76a2c7a
chore(deps): update dependency semantic-release to v20.1.3
renovate[bot] Mar 18, 2023
d31e8b6
chore(deps): update dependency magnet-uri to v7.0.3
renovate[bot] Apr 2, 2023
fc7c232
chore(deps): update dependency @webtorrent/semantic-release-config to…
renovate[bot] May 8, 2023
3f01c29
perf: replace simple websocket with maintained one (#464)
ThaUnknown May 25, 2023
6864ef9
chore(release): 10.0.3
semantic-release-bot May 25, 2023
c99eb89
fix: drop buffer (#465)
ThaUnknown May 26, 2023
51a6b6d
chore(release): 10.0.4
semantic-release-bot May 26, 2023
52f5502
fix: only stringify views (#467)
ThaUnknown May 27, 2023
c5f70dd
chore(release): 10.0.5
semantic-release-bot May 27, 2023
3b2dedb
fix: replace simple-peer with maintained one (#466)
ThaUnknown May 27, 2023
17d4c66
chore(release): 10.0.6
semantic-release-bot May 27, 2023
c338104
chore(deps): update dependency magnet-uri to v7.0.4 (#468)
renovate[bot] May 27, 2023
ac3ea82
chore(deps): update dependency magnet-uri to v7.0.5
renovate[bot] May 30, 2023
a12022a
fix: imports (#471)
ThaUnknown Jun 5, 2023
a6bb919
chore(release): 10.0.7
semantic-release-bot Jun 5, 2023
d7061f7
fix: bigInt (#472)
ThaUnknown Jun 7, 2023
f9e8700
chore(deps): update dependency semantic-release to v21 (#459)
renovate[bot] Jun 7, 2023
ff778f4
chore(release): 10.0.8
semantic-release-bot Jun 7, 2023
0ddb991
chore(deps): update dependency semantic-release to v21.0.5
renovate[bot] Jun 11, 2023
7c845f0
perf: use peer/lite (#474)
ThaUnknown Jun 16, 2023
e3dd9b9
chore(release): 10.0.9
semantic-release-bot Jun 16, 2023
5b8db06
perf: use simple-peer/lite (#475)
ThaUnknown Jun 16, 2023
fcafbe5
chore(release): 10.0.10
semantic-release-bot Jun 16, 2023
9840d4c
chore(deps): update dependency semantic-release to v21.0.6
renovate[bot] Jun 29, 2023
5c97a2e
chore(deps): update dependency tape to v5.6.4
renovate[bot] Jul 3, 2023
e4aa137
chore(deps): update dependency semantic-release to v21.0.7
renovate[bot] Jul 6, 2023
9ca7468
chore(deps): update dependency tape to v5.6.5
renovate[bot] Jul 13, 2023
d2d361f
chore(deps): update dependency tape to v5.6.6
renovate[bot] Jul 19, 2023
63e5144
chore(deps): update dependency @mapbox/node-pre-gyp to v1.0.11 (#482)
renovate[bot] Jul 23, 2023
bd1acfb
chore(deps): update actions/stale action to v8 (#484)
renovate[bot] Jul 23, 2023
b487809
chore(deps): update dependency @webtorrent/semantic-release-config to…
renovate[bot] Jul 24, 2023
11cce83
fix: mangled scrape infohashes (#486)
ThaUnknown Aug 1, 2023
ae54372
chore(release): 10.0.11
semantic-release-bot Aug 1, 2023
aeccf9c
fix(deps): update dependency bencode to v4 (#487)
renovate[bot] Aug 9, 2023
ad00e13
chore(release): 10.0.12
semantic-release-bot Aug 9, 2023
6c85f1e
chore(deps): update dependency semantic-release to v21.0.8
renovate[bot] Aug 21, 2023
0f61147
chore(deps): update dependency semantic-release to v21.0.9
renovate[bot] Aug 21, 2023
3268ae5
chore(deps): update dependency semantic-release to v21.1.0
renovate[bot] Aug 25, 2023
a6922f7
chore(deps): update dependency semantic-release to v21.1.1
renovate[bot] Aug 25, 2023
7ddd003
chore(deps): update dependency semantic-release to v21.1.2
renovate[bot] Sep 17, 2023
de4a34b
chore(deps): update dependency tape to v5.7.0
renovate[bot] Sep 22, 2023
5e86736
chore(deps): update dependency tape to v5.7.1
renovate[bot] Oct 12, 2023
e14738b
chore(deps): update dependency tape to v5.7.2
renovate[bot] Oct 22, 2023
bce64e1
feat(major): drop simple-get (#443)
ThaUnknown Oct 31, 2023
e44bfe8
chore(release): 11.0.0
semantic-release-bot Oct 31, 2023
d9dacc8
chore(deps): update dependency tape to v5.7.3
renovate[bot] Jan 14, 2024
3f59b58
fix: update build badge url (#506)
GridexX Jan 16, 2024
a96ea91
chore(release): 11.0.1
semantic-release-bot Jan 16, 2024
5e2f59b
chore(deps): update dependency tape to v5.7.4
renovate[bot] Jan 26, 2024
ea1e78e
chore(deps): update dependency tape to v5.7.5
renovate[bot] Feb 15, 2024
fe75272
fix(parse-http): ignore announcements from peers with invalid announc…
SilentBot1 Mar 12, 2024
a4f956e
chore(release): 11.0.2
semantic-release-bot Mar 12, 2024
f6a7993
Add event timestamp (#516)
krazak Apr 11, 2024
633d68a
feat: updated webrtc implementation (#519)
ThaUnknown May 22, 2024
428fb22
fix: semantic release (#520)
ThaUnknown May 22, 2024
683fca8
chore(release): 11.1.0
semantic-release-bot May 22, 2024
ed57d0c
chore(deps): update dependency tape to v5.8.0
renovate[bot] Jun 16, 2024
b21a6a5
chore(deps): update dependency tape to v5.8.1
renovate[bot] Jun 17, 2024
83a24ce
perf: drop clone (#523)
ThaUnknown Jul 1, 2024
6a6280a
chore(release): 11.1.1
semantic-release-bot Jul 1, 2024
e9d8f8c
fix: statuscode (#526)
ThaUnknown Aug 13, 2024
fd5ea54
chore(release): 11.1.2
semantic-release-bot Aug 13, 2024
8b02864
chore(deps): update dependency tape to v5.9.0
renovate[bot] Sep 17, 2024
f2f4990
Add subpath import for client and server (#535)
Chocobozzz Nov 7, 2024
e45516d
Bugfix - when using AbortController, errors on resulting stream must …
uriva Nov 29, 2024
7c963a6
chore(deps): update actions/cache action to v4 (#543)
renovate[bot] Dec 28, 2024
e7de90c
feat: release #539 and #535 (#544)
SilentBot1 Dec 28, 2024
9809195
chore(release): 11.2.0
semantic-release-bot Dec 28, 2024
934dc1b
chore(deps): update dependency magnet-uri to v7.0.7 (#545)
renovate[bot] Jan 4, 2025
3cd77f3
fix: http announce no left (#548)
ThaUnknown Jan 19, 2025
b4557a7
chore(release): 11.2.1
semantic-release-bot Jan 19, 2025
1571551
fix: export WebSocketTracker (#558)
subins2000 Sep 6, 2025
295c69a
chore(release): 11.2.2
semantic-release-bot Sep 6, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: drop buffer (webtorrent#465)
  • Loading branch information
ThaUnknown authored May 26, 2023
commit c99eb892088ef3c67ea5bf014dfdd86799251a7e
24 changes: 11 additions & 13 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import once from 'once'
import parallel from 'run-parallel'
import Peer from 'simple-peer'
import queueMicrotask from 'queue-microtask'
import { hex2arr, hex2bin, text2arr, arr2hex, arr2text } from 'uint8-util'

import common from './lib/common.js'
import HTTPTracker from './lib/client/http-tracker.js' // empty object in browser
Expand All @@ -18,8 +19,8 @@ const debug = Debug('bittorrent-tracker:client')
* Find torrent peers, to help a torrent client participate in a torrent swarm.
*
* @param {Object} opts options object
* @param {string|Buffer} opts.infoHash torrent info hash
* @param {string|Buffer} opts.peerId peer id
* @param {string|Uint8Array} opts.infoHash torrent info hash
* @param {string|Uint8Array} opts.peerId peer id
* @param {string|Array.<string>} opts.announce announce
* @param {number} opts.port torrent client listening port
* @param {function} opts.getAnnounceOpts callback to provide data to tracker
Expand All @@ -39,15 +40,15 @@ class Client extends EventEmitter {

this.peerId = typeof opts.peerId === 'string'
? opts.peerId
: opts.peerId.toString('hex')
this._peerIdBuffer = Buffer.from(this.peerId, 'hex')
this._peerIdBinary = this._peerIdBuffer.toString('binary')
: arr2hex(opts.peerId)
this._peerIdBuffer = hex2arr(this.peerId)
this._peerIdBinary = hex2bin(this.peerId)

this.infoHash = typeof opts.infoHash === 'string'
? opts.infoHash.toLowerCase()
: opts.infoHash.toString('hex')
this._infoHashBuffer = Buffer.from(this.infoHash, 'hex')
this._infoHashBinary = this._infoHashBuffer.toString('binary')
: arr2hex(opts.infoHash)
this._infoHashBuffer = hex2arr(this.infoHash)
this._infoHashBinary = hex2bin(this.infoHash)

debug('new client %s', this.infoHash)

Expand All @@ -69,7 +70,7 @@ class Client extends EventEmitter {

// Remove trailing slash from trackers to catch duplicates
announce = announce.map(announceUrl => {
announceUrl = announceUrl.toString()
announceUrl = arr2text(announceUrl)
if (announceUrl[announceUrl.length - 1] === '/') {
announceUrl = announceUrl.substring(0, announceUrl.length - 1)
}
Expand Down Expand Up @@ -260,7 +261,7 @@ Client.scrape = (opts, cb) => {

const clientOpts = Object.assign({}, opts, {
infoHash: Array.isArray(opts.infoHash) ? opts.infoHash[0] : opts.infoHash,
peerId: Buffer.from('01234567890123456789'), // dummy value
peerId: text2arr('01234567890123456789'), // dummy value
port: 6881 // dummy value
})

Expand All @@ -284,9 +285,6 @@ Client.scrape = (opts, cb) => {
}
})

opts.infoHash = Array.isArray(opts.infoHash)
? opts.infoHash.map(infoHash => Buffer.from(infoHash, 'hex'))
: Buffer.from(opts.infoHash, 'hex')
client.scrape({ infoHash: opts.infoHash })
return client
}
Expand Down
13 changes: 7 additions & 6 deletions lib/client/http-tracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import clone from 'clone'
import Debug from 'debug'
import get from 'simple-get'
import Socks from 'socks'
import { bin2hex, hex2bin, arr2text } from 'uint8-util'

import common from '../common.js'
import Tracker from './tracker.js'
Expand Down Expand Up @@ -65,8 +66,8 @@ class HTTPTracker extends Tracker {
}

const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
? opts.infoHash.map(infoHash => infoHash.toString('binary'))
: (opts.infoHash && opts.infoHash.toString('binary')) || this.client._infoHashBinary
? opts.infoHash.map(infoHash => hex2bin(infoHash))
: (opts.infoHash && hex2bin(opts.infoHash)) || this.client._infoHashBinary
const params = {
info_hash: infoHashes
}
Expand Down Expand Up @@ -159,13 +160,13 @@ class HTTPTracker extends Tracker {
} catch (err) {
return cb(new Error(`Error decoding tracker response: ${err.message}`))
}
const failure = data['failure reason'] && Buffer.from(data['failure reason']).toString()
const failure = data['failure reason'] && arr2text(data['failure reason'])
if (failure) {
debug(`failure from ${requestUrl} (${failure})`)
return cb(new Error(failure))
}

const warning = data['warning message'] && Buffer.from(data['warning message']).toString()
const warning = data['warning message'] && arr2text(data['warning message'])
if (warning) {
debug(`warning from ${requestUrl} (${warning})`)
self.client.emit('warning', new Error(warning))
Expand All @@ -189,7 +190,7 @@ class HTTPTracker extends Tracker {

const response = Object.assign({}, data, {
announce: this.announceUrl,
infoHash: common.binaryToHex(data.info_hash)
infoHash: bin2hex(data.info_hash || String(data.info_hash))
})
this.client.emit('update', response)

Expand Down Expand Up @@ -248,7 +249,7 @@ class HTTPTracker extends Tracker {
// (separate from announce interval)
const response = Object.assign(data[infoHash], {
announce: this.announceUrl,
infoHash: common.binaryToHex(infoHash)
infoHash: bin2hex(infoHash)
})
this.client.emit('scrape', response)
})
Expand Down
34 changes: 17 additions & 17 deletions lib/client/udp-tracker.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import arrayRemove from 'unordered-array-remove'
import BN from 'bn.js'
import clone from 'clone'
import Debug from 'debug'
import dgram from 'dgram'
import randombytes from 'randombytes'
import Socks from 'socks'
import { concat, hex2arr, randomBytes } from 'uint8-util'

import common from '../common.js'
import Tracker from './tracker.js'
Expand Down Expand Up @@ -131,7 +130,7 @@ class UDPTracker extends Tracker {
}, common.REQUEST_TIMEOUT)
if (timeout.unref) timeout.unref()

send(Buffer.concat([
send(concat([
common.CONNECTION_ID,
common.toUInt32(common.ACTIONS.CONNECT),
transactionId
Expand Down Expand Up @@ -175,7 +174,8 @@ class UDPTracker extends Tracker {

function onSocketMessage (msg) {
if (proxySocket) msg = msg.slice(10)
if (msg.length < 8 || msg.readUInt32BE(4) !== transactionId.readUInt32BE(0)) {
const view = new DataView(transactionId.buffer)
if (msg.length < 8 || msg.readUInt32BE(4) !== view.getUint32(0)) {
return onError(new Error('tracker sent invalid transaction id'))
}

Expand Down Expand Up @@ -270,14 +270,14 @@ class UDPTracker extends Tracker {
function announce (connectionId, opts) {
transactionId = genTransactionId()

send(Buffer.concat([
send(concat([
connectionId,
common.toUInt32(common.ACTIONS.ANNOUNCE),
transactionId,
self.client._infoHashBuffer,
self.client._peerIdBuffer,
toUInt64(opts.downloaded),
opts.left != null ? toUInt64(opts.left) : Buffer.from('FFFFFFFFFFFFFFFF', 'hex'),
opts.left != null ? toUInt64(opts.left) : hex2arr('ffffffffffffffff'),
toUInt64(opts.uploaded),
common.toUInt32(common.EVENTS[opts.event] || 0),
common.toUInt32(0), // ip address (optional)
Expand All @@ -291,10 +291,10 @@ class UDPTracker extends Tracker {
transactionId = genTransactionId()

const infoHash = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
? Buffer.concat(opts.infoHash)
? concat(opts.infoHash)
: (opts.infoHash || self.client._infoHashBuffer)

send(Buffer.concat([
send(concat([
connectionId,
common.toUInt32(common.ACTIONS.SCRAPE),
transactionId,
Expand All @@ -307,26 +307,26 @@ class UDPTracker extends Tracker {
UDPTracker.prototype.DEFAULT_ANNOUNCE_INTERVAL = 30 * 60 * 1000 // 30 minutes

function genTransactionId () {
return randombytes(4)
return randomBytes(4)
}

function toUInt16 (n) {
const buf = Buffer.allocUnsafe(2)
buf.writeUInt16BE(n, 0)
const buf = new Uint8Array(2)
const view = new DataView(buf.buffer)
view.setUint16(0, n)
return buf
}

const MAX_UINT = 4294967295

function toUInt64 (n) {
if (n > MAX_UINT || typeof n === 'string') {
const bytes = new BN(n).toArray()
while (bytes.length < 8) {
bytes.unshift(0)
}
return Buffer.from(bytes)
const buf = new Uint8Array(8)
const view = new DataView(buf.buffer)
view.setBigUint64(0, n)
return buf
}
return Buffer.concat([common.toUInt32(0), common.toUInt32(n)])
return concat([new Uint8Array(4), common.toUInt32(n)])
}

function noop () {}
Expand Down
28 changes: 14 additions & 14 deletions lib/client/websocket-tracker.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import clone from 'clone'
import Debug from 'debug'
import Peer from 'simple-peer'
import randombytes from 'randombytes'
import Socket from '@thaunknown/simple-websocket'
import Socks from 'socks'
import { arr2text, arr2hex, hex2bin, bin2hex, randomBytes } from 'uint8-util'

import common from '../common.js'
import { DESTROY_TIMEOUT } from '../common.js'
import Tracker from './tracker.js'

const debug = Debug('bittorrent-tracker:websocket-tracker')
Expand Down Expand Up @@ -80,8 +80,8 @@ class WebSocketTracker extends Tracker {
}

const infoHashes = (Array.isArray(opts.infoHash) && opts.infoHash.length > 0)
? opts.infoHash.map(infoHash => infoHash.toString('binary'))
: (opts.infoHash && opts.infoHash.toString('binary')) || this.client._infoHashBinary
? opts.infoHash.map(infoHash => hex2bin(infoHash))
: (opts.infoHash && hex2bin(opts.infoHash)) || this.client._infoHashBinary
const params = {
action: 'scrape',
info_hash: infoHashes
Expand Down Expand Up @@ -138,7 +138,7 @@ class WebSocketTracker extends Tracker {

// Otherwise, wait a short time for potential responses to come in from the
// server, then force close the socket.
timeout = setTimeout(destroyCleanup, common.DESTROY_TIMEOUT)
timeout = setTimeout(destroyCleanup, DESTROY_TIMEOUT)

// But, if a response comes from the server before the timeout fires, do cleanup
// right away.
Expand Down Expand Up @@ -214,7 +214,7 @@ class WebSocketTracker extends Tracker {
this.expectingResponse = false

try {
data = JSON.parse(Buffer.from(data))
data = JSON.parse(arr2text(data))
} catch (err) {
this.client.emit('warning', new Error('Invalid tracker response'))
return
Expand All @@ -233,7 +233,7 @@ class WebSocketTracker extends Tracker {
if (data.info_hash !== this.client._infoHashBinary) {
debug(
'ignoring websocket data from %s for %s (looking for %s: reused socket)',
this.announceUrl, common.binaryToHex(data.info_hash), this.client.infoHash
this.announceUrl, bin2hex(data.info_hash), this.client.infoHash
)
return
}
Expand Down Expand Up @@ -266,7 +266,7 @@ class WebSocketTracker extends Tracker {
if (data.complete != null) {
const response = Object.assign({}, data, {
announce: this.announceUrl,
infoHash: common.binaryToHex(data.info_hash)
infoHash: bin2hex(data.info_hash)
})
this.client.emit('update', response)
}
Expand All @@ -275,7 +275,7 @@ class WebSocketTracker extends Tracker {
if (data.offer && data.peer_id) {
debug('creating peer (from remote offer)')
peer = this._createPeer()
peer.id = common.binaryToHex(data.peer_id)
peer.id = bin2hex(data.peer_id)
peer.once('signal', answer => {
const params = {
action: 'announce',
Expand All @@ -293,10 +293,10 @@ class WebSocketTracker extends Tracker {
}

if (data.answer && data.peer_id) {
const offerId = common.binaryToHex(data.offer_id)
const offerId = bin2hex(data.offer_id)
peer = this.peers[offerId]
if (peer) {
peer.id = common.binaryToHex(data.peer_id)
peer.id = bin2hex(data.peer_id)
this.client.emit('peer', peer)
peer.signal(data.answer)

Expand All @@ -323,7 +323,7 @@ class WebSocketTracker extends Tracker {
// (separate from announce interval)
const response = Object.assign(data[infoHash], {
announce: this.announceUrl,
infoHash: common.binaryToHex(infoHash)
infoHash: bin2hex(infoHash)
})
this.client.emit('scrape', response)
})
Expand Down Expand Up @@ -376,13 +376,13 @@ class WebSocketTracker extends Tracker {
checkDone()

function generateOffer () {
const offerId = randombytes(20).toString('hex')
const offerId = arr2hex(randomBytes(20))
debug('creating peer (from _generateOffers)')
const peer = self.peers[offerId] = self._createPeer({ initiator: true })
peer.once('signal', offer => {
offers.push({
offer,
offer_id: common.hexToBinary(offerId)
offer_id: hex2bin(offerId)
})
checkDone()
})
Expand Down
8 changes: 5 additions & 3 deletions lib/common-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
*/

import querystring from 'querystring'
import { concat } from 'uint8-util'

export const IPV4_RE = /^[\d.]+$/
export const IPV6_RE = /^[\da-fA-F:]+$/
export const REMOVE_IPV4_MAPPED_IPV6_RE = /^::ffff:/

export const CONNECTION_ID = Buffer.concat([toUInt32(0x417), toUInt32(0x27101980)])
export const CONNECTION_ID = concat([toUInt32(0x417), toUInt32(0x27101980)])
export const ACTIONS = { CONNECT: 0, ANNOUNCE: 1, SCRAPE: 2, ERROR: 3 }
export const EVENTS = { update: 0, completed: 1, started: 2, stopped: 3, paused: 4 }
export const EVENT_IDS = {
Expand Down Expand Up @@ -40,8 +41,9 @@ export const REQUEST_TIMEOUT = 15000
export const DESTROY_TIMEOUT = 1000

export function toUInt32 (n) {
const buf = Buffer.allocUnsafe(4)
buf.writeUInt32BE(n, 0)
const buf = new Uint8Array(4)
const view = new DataView(buf.buffer)
view.setUint32(0, n)
return buf
}

Expand Down
17 changes: 1 addition & 16 deletions lib/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,11 @@
* Functions/constants needed by both the client and server.
*/
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 const binaryToHex = str => {
if (typeof str !== 'string') {
str = String(str)
}
return Buffer.from(str, 'binary').toString('hex')
}

export const hexToBinary = str => {
if (typeof str !== 'string') {
str = String(str)
}
return Buffer.from(str, 'hex').toString('binary')
}

// 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.
//
Expand Down Expand Up @@ -49,8 +36,6 @@ export const parseUrl = str => {
export default {
DEFAULT_ANNOUNCE_PEERS,
MAX_ANNOUNCE_PEERS,
binaryToHex,
hexToBinary,
parseUrl,
...common
}
Loading