From f7c38067a322ff0f1fe95d23a5bdff0240b58764 Mon Sep 17 00:00:00 2001 From: Vincent Frentzel Date: Sat, 6 Feb 2016 19:02:36 +0100 Subject: [PATCH 01/11] Removed auto-close on download completion. Signed-off-by: Vincent Frentzel --- bin/cmd.js | 1 - 1 file changed, 1 deletion(-) diff --git a/bin/cmd.js b/bin/cmd.js index a4ae8ed1..2fd30f67 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -600,7 +600,6 @@ function drawTorrent (torrent) { function torrentDone () { if (argv['on-done']) cp.exec(argv['on-done']).unref() - if (!playerName && !serving && argv.out) gracefulExit() } function fatalError (err) { From 230a0f5e3d3e2e33b14fb2feb058434569d375be Mon Sep 17 00:00:00 2001 From: Vincent Frentzel Date: Sat, 6 Feb 2016 23:00:13 +0100 Subject: [PATCH 02/11] Revised output to accomodate remote operation over SSH. - Removed peer overview unless --verbose passed. - Redraw interval increased to 2s. Signed-off-by: Vincent Frentzel --- bin/cmd.js | 99 +++++++++++++++++++++++++++--------------------------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 2fd30f67..5452ce9f 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -492,7 +492,7 @@ var drawInterval function drawTorrent (torrent) { if (!argv.quiet) { process.stdout.write(new Buffer('G1tIG1sySg==', 'base64')) // clear for drawing - drawInterval = setInterval(draw, 500) + drawInterval = setInterval(draw, 2000) drawInterval.unref() } @@ -538,58 +538,59 @@ function drawTorrent (torrent) { '{green:Time remaining:} {bold:' + estimate + '} ' + '{green:Peers:} {bold:' + unchoked.length + '/' + torrent.numPeers + '}' ) - if (argv.verbose) { - line( - '{green:Queued peers:} {bold:' + torrent.swarm.numQueued + '} ' + - '{green:Blocked peers:} {bold:' + torrent.numBlockedPeers + '} ' + - '{green:Hotswaps:} {bold:' + hotswaps + '}' - ) - } + line( + '{green:Queued peers:} {bold:' + torrent.swarm.numQueued + '} ' + + '{green:Blocked peers:} {bold:' + torrent.numBlockedPeers + '} ' + + '{green:Hotswaps:} {bold:' + hotswaps + '}' + ) + line('') - - torrent.swarm.wires.every(function (wire) { - var progress = '?' - if (torrent.length) { - var bits = 0 - var piececount = Math.ceil(torrent.length / torrent.pieceLength) - for (var i = 0; i < piececount; i++) { - if (wire.peerPieces.get(i)) { - bits++ + + if (argv.verbose) { + torrent.swarm.wires.every(function (wire) { + var progress = '?' + if (torrent.length) { + var bits = 0 + var piececount = Math.ceil(torrent.length / torrent.pieceLength) + for (var i = 0; i < piececount; i++) { + if (wire.peerPieces.get(i)) { + bits++ + } + } + progress = bits === piececount ? 'S' : Math.floor(100 * bits / piececount) + '%' } + + var str = '{3:%s} {25+magenta:%s} {10:%s} {12+cyan:%s/s} {12+red:%s/s}' + var args = [ + progress, + wire.remoteAddress + ? (wire.remoteAddress + ':' + wire.remotePort) + : 'Unknown', + prettyBytes(wire.downloaded), + prettyBytes(wire.downloadSpeed()), + prettyBytes(wire.uploadSpeed()) + ] + + str += ' {15+grey:%s} {10+grey:%s}' + var tags = [] + if (wire.requests.length > 0) tags.push(wire.requests.length + ' reqs') + if (wire.peerChoking) tags.push('choked') + var reqStats = wire.requests.map(function (req) { return req.piece }) + args.push(tags.join(', '), reqStats.join(' ')) + + line.apply(undefined, [].concat(str, args)) + + peerslisted += 1 + return linesRemaining > 4 + }) + + if (torrent.numPeers > peerslisted) { + line('{60:}') + line('... and %s more', torrent.numPeers - peerslisted) } - progress = bits === piececount ? 'S' : Math.floor(100 * bits / piececount) + '%' - } - - var str = '{3:%s} {25+magenta:%s} {10:%s} {12+cyan:%s/s} {12+red:%s/s}' - var args = [ - progress, - wire.remoteAddress - ? (wire.remoteAddress + ':' + wire.remotePort) - : 'Unknown', - prettyBytes(wire.downloaded), - prettyBytes(wire.downloadSpeed()), - prettyBytes(wire.uploadSpeed()) - ] - if (argv.verbose) { - str += ' {15+grey:%s} {10+grey:%s}' - var tags = [] - if (wire.requests.length > 0) tags.push(wire.requests.length + ' reqs') - if (wire.peerChoking) tags.push('choked') - var reqStats = wire.requests.map(function (req) { return req.piece }) - args.push(tags.join(', '), reqStats.join(' ')) - } - line.apply(undefined, [].concat(str, args)) - - peerslisted += 1 - return linesRemaining > 4 - }) - - if (torrent.numPeers > peerslisted) { - line('{60:}') - line('... and %s more', torrent.numPeers - peerslisted) + + line('{60:}') } - - line('{60:}') clivas.flush(true) } From bf799f819f2627d0c0c28c2404c93c40ee8434fd Mon Sep 17 00:00:00 2001 From: Vincent Frentzel Date: Sun, 7 Feb 2016 18:38:04 +0100 Subject: [PATCH 03/11] Fixed style issues reported by Travis. Signed-off-by: Vincent Frentzel --- bin/cmd.js | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 5452ce9f..eaf57dcc 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -543,11 +543,11 @@ function drawTorrent (torrent) { '{green:Blocked peers:} {bold:' + torrent.numBlockedPeers + '} ' + '{green:Hotswaps:} {bold:' + hotswaps + '}' ) - + line('') - + if (argv.verbose) { - torrent.swarm.wires.every(function (wire) { + torrent.swarm.wires.every(function (wire) { var progress = '?' if (torrent.length) { var bits = 0 @@ -559,7 +559,7 @@ function drawTorrent (torrent) { } progress = bits === piececount ? 'S' : Math.floor(100 * bits / piececount) + '%' } - + var str = '{3:%s} {25+magenta:%s} {10:%s} {12+cyan:%s/s} {12+red:%s/s}' var args = [ progress, @@ -570,26 +570,26 @@ function drawTorrent (torrent) { prettyBytes(wire.downloadSpeed()), prettyBytes(wire.uploadSpeed()) ] - + str += ' {15+grey:%s} {10+grey:%s}' var tags = [] if (wire.requests.length > 0) tags.push(wire.requests.length + ' reqs') if (wire.peerChoking) tags.push('choked') var reqStats = wire.requests.map(function (req) { return req.piece }) args.push(tags.join(', '), reqStats.join(' ')) - + line.apply(undefined, [].concat(str, args)) - + peerslisted += 1 return linesRemaining > 4 }) - - if (torrent.numPeers > peerslisted) { + + if (torrent.numPeers > peerslisted) { line('{60:}') line('... and %s more', torrent.numPeers - peerslisted) - } - - line('{60:}') + } + + line('{60:}') } clivas.flush(true) } From e663bb021a080c1ad416d3d2ace541f3e4bcbc0f Mon Sep 17 00:00:00 2001 From: Vincent Frentzel Date: Sun, 7 Feb 2016 18:44:40 +0100 Subject: [PATCH 04/11] Added --client-port argument. - Use of 6881 as default value. - Alias -c defined.. Signed-off-by: Vincent Frentzel --- bin/cmd.js | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index eaf57dcc..80eb2e44 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -40,6 +40,7 @@ var argv = minimist(process.argv.slice(2), { alias: { p: 'port', b: 'blocklist', + c: 'client-port', t: 'subtitles', s: 'select', i: 'index', @@ -72,7 +73,8 @@ var argv = minimist(process.argv.slice(2), { 'on-exit' ], default: { - port: 8000 + port: 8000, + client-port: 6881 } }) @@ -203,14 +205,15 @@ Options (simple): -v, --version print the current version Options (advanced): - -p, --port [number] change the http server port [default: 8000] - -t, --subtitles [path] load subtitles file - -b, --blocklist [path] load blocklist file/http url - -a, --announce [url] tracker URL to announce to - -q, --quiet don't show UI on stdout - --on-done [script] run script after torrent download is done - --on-exit [script] run script before program exit - --verbose show torrent protocol details + -p, --port [number] change the http server port [default: 8000] + -c, --client-port [number] change the torrent client port [default: 6881] + -t, --subtitles [path] load subtitles file + -b, --blocklist [path] load blocklist file/http url + -a, --announce [url] tracker URL to announce to + -q, --quiet don't show UI on stdout + --on-done [script] run script after torrent download is done + --on-exit [script] run script before program exit + --verbose show torrent protocol details */ }.toString().split(/\n/).slice(2, -2).join('\n')) @@ -264,7 +267,9 @@ function runDownload (torrentId) { argv.out = process.cwd() } - client = new WebTorrent({ blocklist: argv.blocklist }) + client = new WebTorrent({ blocklist: argv.blocklist, + torrentPort: argv.client-port }) + client.on('error', fatalError) var torrent = client.add(torrentId, { path: argv.out, announce: argv.announce }) @@ -479,7 +484,9 @@ function runSeed (input) { return } - client = new WebTorrent({ blocklist: argv.blocklist }) + client = new WebTorrent({ blocklist: argv.blocklist, + torrentPort: argv.client-port }) + client.on('error', fatalError) client.seed(input, { announce: argv.announce }, function (torrent) { From 01ad416afc7671187b02d1c07be9c1f3994ec8c8 Mon Sep 17 00:00:00 2001 From: Vincent Frentzel Date: Sun, 7 Feb 2016 18:57:12 +0100 Subject: [PATCH 05/11] Fixed --client-port dictionary keys. Signed-off-by: Vincent Frentzel --- bin/cmd.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 80eb2e44..9319046e 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -74,7 +74,7 @@ var argv = minimist(process.argv.slice(2), { ], default: { port: 8000, - client-port: 6881 + 'client-port': 6881 } }) @@ -268,7 +268,7 @@ function runDownload (torrentId) { } client = new WebTorrent({ blocklist: argv.blocklist, - torrentPort: argv.client-port }) + torrentPort: argv.['client-port'] }) client.on('error', fatalError) @@ -485,7 +485,7 @@ function runSeed (input) { } client = new WebTorrent({ blocklist: argv.blocklist, - torrentPort: argv.client-port }) + torrentPort: argv.['client-port'] }) client.on('error', fatalError) From 995a7cb450e66bbd719f395bba2f27ddf8049fdd Mon Sep 17 00:00:00 2001 From: Vincent Frentzel Date: Sun, 7 Feb 2016 19:01:43 +0100 Subject: [PATCH 06/11] Removed extra . Signed-off-by: Vincent Frentzel --- bin/cmd.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 9319046e..f7c8f225 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -268,7 +268,7 @@ function runDownload (torrentId) { } client = new WebTorrent({ blocklist: argv.blocklist, - torrentPort: argv.['client-port'] }) + torrentPort: argv['client-port'] }) client.on('error', fatalError) @@ -485,7 +485,7 @@ function runSeed (input) { } client = new WebTorrent({ blocklist: argv.blocklist, - torrentPort: argv.['client-port'] }) + torrentPort: argv['client-port'] }) client.on('error', fatalError) From aaf7fb3abecba90cbc0b3a5f1fc02a2a7a2963d4 Mon Sep 17 00:00:00 2001 From: Vincent Frentzel Date: Sun, 7 Feb 2016 19:09:04 +0100 Subject: [PATCH 07/11] Style. Signed-off-by: Vincent Frentzel --- bin/cmd.js | 66 +++++++++++++++++++++++++++--------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index f7c8f225..2bc0c8f5 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -555,41 +555,41 @@ function drawTorrent (torrent) { if (argv.verbose) { torrent.swarm.wires.every(function (wire) { - var progress = '?' - if (torrent.length) { - var bits = 0 - var piececount = Math.ceil(torrent.length / torrent.pieceLength) - for (var i = 0; i < piececount; i++) { - if (wire.peerPieces.get(i)) { - bits++ - } + var progress = '?' + if (torrent.length) { + var bits = 0 + var piececount = Math.ceil(torrent.length / torrent.pieceLength) + for (var i = 0; i < piececount; i++) { + if (wire.peerPieces.get(i)) { + bits++ } - progress = bits === piececount ? 'S' : Math.floor(100 * bits / piececount) + '%' } - - var str = '{3:%s} {25+magenta:%s} {10:%s} {12+cyan:%s/s} {12+red:%s/s}' - var args = [ - progress, - wire.remoteAddress - ? (wire.remoteAddress + ':' + wire.remotePort) - : 'Unknown', - prettyBytes(wire.downloaded), - prettyBytes(wire.downloadSpeed()), - prettyBytes(wire.uploadSpeed()) - ] - - str += ' {15+grey:%s} {10+grey:%s}' - var tags = [] - if (wire.requests.length > 0) tags.push(wire.requests.length + ' reqs') - if (wire.peerChoking) tags.push('choked') - var reqStats = wire.requests.map(function (req) { return req.piece }) - args.push(tags.join(', '), reqStats.join(' ')) - - line.apply(undefined, [].concat(str, args)) - - peerslisted += 1 - return linesRemaining > 4 - }) + progress = bits === piececount ? 'S' : Math.floor(100 * bits / piececount) + '%' + } + + var str = '{3:%s} {25+magenta:%s} {10:%s} {12+cyan:%s/s} {12+red:%s/s}' + var args = [ + progress, + wire.remoteAddress + ? (wire.remoteAddress + ':' + wire.remotePort) + : 'Unknown', + prettyBytes(wire.downloaded), + prettyBytes(wire.downloadSpeed()), + prettyBytes(wire.uploadSpeed()) + ] + + str += ' {15+grey:%s} {10+grey:%s}' + var tags = [] + if (wire.requests.length > 0) tags.push(wire.requests.length + ' reqs') + if (wire.peerChoking) tags.push('choked') + var reqStats = wire.requests.map(function (req) { return req.piece }) + args.push(tags.join(', '), reqStats.join(' ')) + + line.apply(undefined, [].concat(str, args)) + + peerslisted += 1 + return linesRemaining > 4 + }) if (torrent.numPeers > peerslisted) { line('{60:}') From 96453691db35fcb9c6c6bb9a20c0c3800f213b7e Mon Sep 17 00:00:00 2001 From: Vincent Frentzel Date: Sun, 7 Feb 2016 19:11:08 +0100 Subject: [PATCH 08/11] Style. Signed-off-by: Vincent Frentzel --- bin/cmd.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 2bc0c8f5..b3193edf 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -592,8 +592,8 @@ function drawTorrent (torrent) { }) if (torrent.numPeers > peerslisted) { - line('{60:}') - line('... and %s more', torrent.numPeers - peerslisted) + line('{60:}') + line('... and %s more', torrent.numPeers - peerslisted) } line('{60:}') From f1581460701911951942ad0d36778c38f853cec0 Mon Sep 17 00:00:00 2001 From: Vincent Frentzel Date: Sun, 7 Feb 2016 19:16:07 +0100 Subject: [PATCH 09/11] Removed serving variable as auto-close was removed. Signed-off-by: Vincent Frentzel --- bin/cmd.js | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index b3193edf..749b0e2c 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -260,7 +260,7 @@ function runCreate (input) { }) } -var client, href, playerName, server, serving +var client, href, playerName, server function runDownload (torrentId) { if (!argv.out && !argv.stdout && !playerName) { @@ -339,10 +339,6 @@ function runDownload (torrentId) { } }) - server.once('connection', function () { - serving = true - }) - function onReady () { // if no index specified, use largest file var index = (typeof argv.index === 'number') From 4ba81351772ea593543ab2cb4e8bf7f3d382fa99 Mon Sep 17 00:00:00 2001 From: Vincent Frentzel Date: Sun, 7 Feb 2016 20:38:07 +0100 Subject: [PATCH 10/11] Added --exit, -e argument to exit webtorrent on completion. Signed-off-by: Vincent Frentzel --- bin/cmd.js | 25 +++++++------------------ test/node/cmd.js | 2 +- 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 749b0e2c..2ce9e698 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -48,7 +48,8 @@ var argv = minimist(process.argv.slice(2), { a: 'announce', q: 'quiet', h: 'help', - v: 'version' + v: 'version', + e: 'exit' }, boolean: [ // options that are always boolean 'airplay', @@ -62,7 +63,8 @@ var argv = minimist(process.argv.slice(2), { 'quiet', 'help', 'version', - 'verbose' + 'verbose', + 'exit' ], string: [ // options that are always strings 'out', @@ -211,6 +213,7 @@ Options (advanced): -b, --blocklist [path] load blocklist file/http url -a, --announce [url] tracker URL to announce to -q, --quiet don't show UI on stdout + -e, --exit exit webtorrent on completion --on-done [script] run script after torrent download is done --on-exit [script] run script before program exit --verbose show torrent protocol details @@ -303,22 +306,7 @@ function runDownload (torrentId) { ) }) - torrent.on('done', function () { - if (!argv.quiet) { - var numActiveWires = torrent.swarm.wires.reduce(function (num, wire) { - return num + (wire.downloaded > 0) - }, 0) - clivas.line('') - clivas.line( - 'torrent downloaded {green:successfully} from {bold:%s/%s} {green:peers} ' + - 'in {bold:%ss}!', - numActiveWires, - torrent.numPeers, - getRuntime() - ) - } - torrentDone() - }) + torrent.on('done', torrentDone) // Start http server server = torrent.createServer() @@ -604,6 +592,7 @@ function drawTorrent (torrent) { function torrentDone () { if (argv['on-done']) cp.exec(argv['on-done']).unref() + if (argv['exit']) gracefulExit() } function fatalError (err) { diff --git a/test/node/cmd.js b/test/node/cmd.js index 4c4f9e29..35cb3e25 100644 --- a/test/node/cmd.js +++ b/test/node/cmd.js @@ -100,7 +100,7 @@ test('Command line: webtorrent create /path/to/file', function (t) { test('Command line: webtorrent download (with local content)', function (t) { t.plan(2) - cp.exec(CMD + ' download ' + common.leaves.torrentPath + ' --out test/fixtures', function (err, data) { + cp.exec(CMD + ' download ' + common.leaves.torrentPath + ' --out test/fixtures' + ' --exit', function (err, data) { t.error(err) t.ok(data.indexOf('successfully') !== -1) }) From 785f7403c86a14eda747c3f6b19a6f486bf954bf Mon Sep 17 00:00:00 2001 From: Vincent Frentzel Date: Sun, 7 Feb 2016 20:48:45 +0100 Subject: [PATCH 11/11] Reverted f1581460 Signed-off-by: Vincent Frentzel --- bin/cmd.js | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/bin/cmd.js b/bin/cmd.js index 2ce9e698..e9a66ee3 100755 --- a/bin/cmd.js +++ b/bin/cmd.js @@ -263,7 +263,7 @@ function runCreate (input) { }) } -var client, href, playerName, server +var client, href, playerName, server, serving function runDownload (torrentId) { if (!argv.out && !argv.stdout && !playerName) { @@ -306,7 +306,22 @@ function runDownload (torrentId) { ) }) - torrent.on('done', torrentDone) + torrent.on('done', function () { + if (!argv.quiet) { + var numActiveWires = torrent.swarm.wires.reduce(function (num, wire) { + return num + (wire.downloaded > 0) + }, 0) + clivas.line('') + clivas.line( + 'torrent downloaded {green:successfully} from {bold:%s/%s} {green:peers} ' + + 'in {bold:%ss}!', + numActiveWires, + torrent.numPeers, + getRuntime() + ) + } + torrentDone() + }) // Start http server server = torrent.createServer() @@ -327,6 +342,10 @@ function runDownload (torrentId) { } }) + server.once('connection', function () { + serving = true + }) + function onReady () { // if no index specified, use largest file var index = (typeof argv.index === 'number') @@ -592,7 +611,7 @@ function drawTorrent (torrent) { function torrentDone () { if (argv['on-done']) cp.exec(argv['on-done']).unref() - if (argv['exit']) gracefulExit() + if (argv['exit'] && !playerName && !serving && argv.out) gracefulExit() } function fatalError (err) {