From 2b696a734fc031ecc375826b5c263079370c8a03 Mon Sep 17 00:00:00 2001 From: Joseph Frazier Date: Sun, 8 Nov 2015 21:01:35 -0500 Subject: [PATCH 1/3] Test automatically picking an available port see https://github.com/feross/webtorrent/issues/411 --- test/cmd.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/cmd.js b/test/cmd.js index ee8a87b9..5699a0e6 100644 --- a/test/cmd.js +++ b/test/cmd.js @@ -99,6 +99,15 @@ test('Command line: webtorrent create /path/to/file', function (t) { }) }) +test('Command line: webtorrent download --port 80', function (t) { + t.plan(2) + + cp.exec(CMD + ' --port 80 --out test/content download test/torrents/leaves.torrent', function (err, data) { + t.error(err) + t.ok(data.indexOf('successfully') !== -1) + }) +}) + // TODO: test 'webtorrent download /path/to/torrent' // TODO: test 'webtorrent download magnet_uri' // TODO: test 'webtorrent seed /path/to/file' From 8c14314beea9b3dd2c5dc92a095600724850cbb2 Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Mon, 28 Sep 2015 01:12:40 +0200 Subject: [PATCH 2/3] Pick an available port instead of crashing with "listen EADDRINUSE" or "listen EACCES" Fixes: #411 --- bin/cmd.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 1e393009..77ed9191 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -310,10 +310,23 @@ function runDownload (torrentId) { // Start http server server = torrent.createServer() - server.listen(argv.port, function () { + + function initServer () { if (torrent.ready) onReady() else torrent.once('ready', onReady) - }) + } + + server.listen(argv.port, initServer) + + .on('error', function (err) { + // In case the port is unusable + if (err.code === 'EADDRINUSE') { + // Let the OS choose one for us + server.listen(0, initServer) + } + else throw err + }) + server.once('connection', function () { serving = true }) @@ -366,8 +379,8 @@ function runDownload (torrentId) { function onSelection (index) { href = (argv.airplay || argv.chromecast || argv.xbmc) - ? 'http://' + networkAddress() + ':' + argv.port + '/' + index - : 'http://localhost:' + argv.port + '/' + index + ? 'http://' + networkAddress() + ':' + server.address().port + '/' + index + : 'http://localhost:' + server.address().port + '/' + index if (playerName) torrent.files[index].select() if (argv.stdout) torrent.files[index].createReadStream().pipe(process.stdout) From 6e4c7ffe7ee003734c07c780c15cf757fce38145 Mon Sep 17 00:00:00 2001 From: Yoann Ciabaud Date: Mon, 28 Sep 2015 09:57:08 +0200 Subject: [PATCH 3/3] Pick a new port on EACCES --- bin/cmd.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/cmd.js b/bin/cmd.js index 77ed9191..c342be33 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -320,7 +320,7 @@ function runDownload (torrentId) { .on('error', function (err) { // In case the port is unusable - if (err.code === 'EADDRINUSE') { + if (err.code === 'EADDRINUSE' || err.code === 'EACCES') { // Let the OS choose one for us server.listen(0, initServer) }