From fa6ade18cdcb68dd71badb146e5c196f67deebf5 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 9 Jul 2020 18:09:45 +0200 Subject: [PATCH 1/3] Refactor `announce` and `announceList` --- index.js | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/index.js b/index.js index 6b91ff40..73d05108 100644 --- a/index.js +++ b/index.js @@ -23,7 +23,7 @@ function flat (arr1) { ) } -const announceList = [ +const ANNOUNCE_LIST = [ ['udp://tracker.leechers-paradise.org:6969'], ['udp://tracker.coppersurfer.tk:6969'], ['udp://tracker.opentrackr.org:1337'], @@ -273,20 +273,29 @@ function getPieceList (files, pieceLength, cb) { } function onFiles (files, opts, cb) { - let announceList = opts.announceList + let announce = null + let announceList = [] - if (!announceList) { - if (typeof opts.announce === 'string') announceList = [[opts.announce]] - else if (Array.isArray(opts.announce)) { - announceList = opts.announce.map(u => [u]) - } + if (opts.announce) { + let trackerList = opts.announce + if (!Array.isArray(trackerList)) trackerList = [trackerList] + + const [first, ...rest] = trackerList.filter(u => typeof u === 'string') + if (first) announce = first + announceList = announceList.concat([[first], ...rest.map(u => [u])]) } - if (!announceList) announceList = [] + if (opts.announceList && Array.isArray(opts.announceList)) { + opts.announceList.forEach(l => { + if (!Array.isArray(l)) return + const trackerList = l.filter(u => typeof u === 'string') + if (trackerList.length > 0) announceList.push(trackerList) + }) + } if (global.WEBTORRENT_ANNOUNCE) { if (typeof global.WEBTORRENT_ANNOUNCE === 'string') { - announceList.push([[global.WEBTORRENT_ANNOUNCE]]) + announceList.push([global.WEBTORRENT_ANNOUNCE]) } else if (Array.isArray(global.WEBTORRENT_ANNOUNCE)) { announceList = announceList.concat(global.WEBTORRENT_ANNOUNCE.map(u => [u])) } @@ -294,9 +303,11 @@ function onFiles (files, opts, cb) { // When no trackers specified, use some reasonable defaults if (opts.announce === undefined && opts.announceList === undefined) { - announceList = announceList.concat(module.exports.announceList) + announceList = announceList.concat(ANNOUNCE_LIST) } + if (!announce && announceList.length > 0) announce = announceList[0][0] + if (typeof opts.urlList === 'string') opts.urlList = [opts.urlList] const torrent = { @@ -307,10 +318,9 @@ function onFiles (files, opts, cb) { encoding: 'UTF-8' } - if (announceList.length !== 0) { - torrent.announce = announceList[0][0] - torrent['announce-list'] = announceList - } + if (announce) torrent.announce = announce + + if (announceList.length !== 0) torrent['announce-list'] = announceList if (opts.comment !== undefined) torrent.comment = opts.comment @@ -431,4 +441,4 @@ function getStreamStream (readable, file) { module.exports = createTorrent module.exports.parseInput = parseInput -module.exports.announceList = announceList +module.exports.announceList = ANNOUNCE_LIST From 9c27704827e9f44c2514bf418587220aa8c71bd5 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 9 Jul 2020 18:39:50 +0200 Subject: [PATCH 2/3] Add tests for `announce` and `announceList` params --- test/announce.js | 102 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 test/announce.js diff --git a/test/announce.js b/test/announce.js new file mode 100644 index 00000000..8cc8f8b0 --- /dev/null +++ b/test/announce.js @@ -0,0 +1,102 @@ +const fixtures = require('webtorrent-fixtures') +const parseTorrent = require('parse-torrent') +const test = require('tape') +const createTorrent = require('../') + +test('announce is added to torrent', t => { + t.plan(4) + + createTorrent(fixtures.leaves.contentPath, { + announce: 'wss://example1.com', + }, (err, torrent) => { + t.error(err) + + const parsedTorrent = parseTorrent(torrent) + + t.ok(Array.isArray(parsedTorrent.announce)) + + t.equals(parsedTorrent.announce.length, 1) + + t.ok(parsedTorrent.announce.includes('wss://example1.com')) + }) +}) + +test('invalid announceList as string', t => { + t.plan(3) + + createTorrent(fixtures.leaves.contentPath, { + announceList: 'wss://example1.com' + }, (err, torrent) => { + t.error(err) + + const parsedTorrent = parseTorrent(torrent) + + t.ok(Array.isArray(parsedTorrent.announce)) + + t.equals(parsedTorrent.announce.length, 0) + }) +}) + +test('announceList is a list of lists of strings', t => { + t.plan(6) + + createTorrent(fixtures.leaves.contentPath, { + announceList: [['wss://example1.com', 'wss://example2.com'], ['wss://example3.com']] + }, (err, torrent) => { + t.error(err) + + const parsedTorrent = parseTorrent(torrent) + + t.ok(Array.isArray(parsedTorrent.announce)) + + t.equals(parsedTorrent.announce.length, 3) + + t.ok(parsedTorrent.announce.includes('wss://example1.com')) + + t.ok(parsedTorrent.announce.includes('wss://example2.com')) + + t.ok(parsedTorrent.announce.includes('wss://example3.com')) + }) +}) + +test('verify that announce and announceList can be used together', t => { + t.plan(5) + + createTorrent(fixtures.leaves.contentPath, { + announce: 'wss://example1.com', + announceList: [['wss://example2.com']] + }, (err, torrent) => { + t.error(err) + + const parsedTorrent = parseTorrent(torrent) + + t.ok(Array.isArray(parsedTorrent.announce)) + + t.equals(parsedTorrent.announce.length, 2) + + t.ok(parsedTorrent.announce.includes('wss://example1.com')) + + t.ok(parsedTorrent.announce.includes('wss://example2.com')) + }) +}) + +test('invalid trackers are discarded', t => { + t.plan(5) + + createTorrent(fixtures.leaves.contentPath, { + announce: 'wss://example1.com', + announceList: [['wss://example2.com'], [1234, ['wss://thisisinsidealist.com']]] + }, (err, torrent) => { + t.error(err) + + const parsedTorrent = parseTorrent(torrent) + + t.ok(Array.isArray(parsedTorrent.announce)) + + t.equals(parsedTorrent.announce.length, 2) + + t.ok(parsedTorrent.announce.includes('wss://example1.com')) + + t.ok(parsedTorrent.announce.includes('wss://example2.com')) + }) +}) From 635cd66e7eae8053c55336f40761466483738446 Mon Sep 17 00:00:00 2001 From: Alex Date: Thu, 9 Jul 2020 19:12:58 +0200 Subject: [PATCH 3/3] Remove trailing comma --- test/announce.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/announce.js b/test/announce.js index 8cc8f8b0..4f34ec2a 100644 --- a/test/announce.js +++ b/test/announce.js @@ -7,7 +7,7 @@ test('announce is added to torrent', t => { t.plan(4) createTorrent(fixtures.leaves.contentPath, { - announce: 'wss://example1.com', + announce: 'wss://example1.com' }, (err, torrent) => { t.error(err)