Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added pause/resume, refactored code, improved command line interface, etc #514

Closed
wants to merge 21 commits into from
Closed
Changes from 1 commit
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

added resume/pause support for audio/video streaming

  • Loading branch information
whitef0x0 committed Nov 27, 2015
commit 61d1afbf33303865206b6295cd82dca1419703c3
@@ -489,6 +489,8 @@ function runSeed (input) {

client.on('torrent', function (torrent) {
if (argv.quiet) console.log(torrent.magnetURI)
process.stdin.setRawMode(true)
process.stdin.resume()
drawTorrent(torrent)
})
}
@@ -551,9 +553,7 @@ function drawTorrent (torrent) {
if (!argv.quiet) {

process.stdout.write(new Buffer('G1tIG1sySg==', 'base64')) // clear for drawing
process.stdin.setRawMode(true)
process.stdin.resume()
process.stdin.setEncoding( 'utf8' );
process.stdin.setEncoding('utf8');
drawInterval = setInterval(draw, 500)
drawInterval.unref()

@@ -101,6 +101,14 @@ module.exports = function appendTo (file, rootElem, cb) {
elem.autoplay = true // for chrome
elem.play() // for firefox

file.on('paused', function(){
elem.pause()
})

file.on('resume', function(){
if(elem.paused) elem.play()
})

elem.addEventListener('progress', function () {
currentTime = elem.currentTime
})
@@ -126,6 +134,14 @@ module.exports = function appendTo (file, rootElem, cb) {
elem.addEventListener('playing', onPlaying)
elem.src = url
elem.play()

file.on('paused', function(){
elem.pause()
})

file.on('resume', function(){
if(elem.paused) elem.play()
})
})
}

@@ -249,6 +249,10 @@ Torrent.prototype._onPausedTorrent = function (parsedTorrent) {
self.paused = true
self.emit('paused')

self.files.forEach(function(file){
file.emit('paused')
}

self.on('resume', function(stream) { self._onResume(parsedTorrent); });
}

@@ -333,6 +337,10 @@ Torrent.prototype._onResume = function (parsedTorrent) {

debug('got resumed')

self.files.forEach(function(file){
file.emit('resumed')
})

self.paused = false
self.resumed = true
self._onParsedTorrent(parsedTorrent)
@@ -10,149 +10,112 @@ var serveStatic = require('serve-static')
var CMD_PATH = path.resolve(__dirname, '..', 'bin', 'cmd.js')
var CMD = 'node ' + CMD_PATH

// test('Command line: webtorrent help', function (t) {
// t.plan(6)
test('Command line: webtorrent help', function (t) {
t.plan(6)

// cp.exec(CMD + ' help', function (err, data) {
// t.error(err) // no error, exit code 0
// t.ok(data.toLowerCase().indexOf('usage') !== -1)
// })

// cp.exec(CMD + ' --help', function (err, data) {
// t.error(err) // no error, exit code 0
// t.ok(data.toLowerCase().indexOf('usage') !== -1)
// })

// cp.exec(CMD, function (err, data) {
// t.error(err) // no error, exit code 0
// t.ok(data.toLowerCase().indexOf('usage') !== -1)
// })
// })
cp.exec(CMD + ' help', function (err, data) {
t.error(err) // no error, exit code 0
t.ok(data.toLowerCase().indexOf('usage') !== -1)
})

// test('Command line: webtorrent version', function (t) {
// t.plan(6)
// var expectedVersion = require(path.resolve(__dirname, '..', 'package.json')).version + '\n'
cp.exec(CMD + ' --help', function (err, data) {
t.error(err) // no error, exit code 0
t.ok(data.toLowerCase().indexOf('usage') !== -1)
})

// cp.exec(CMD + ' version', function (err, data) {
// t.error(err)
// t.equal(data, expectedVersion)
// })
cp.exec(CMD, function (err, data) {
t.error(err) // no error, exit code 0
t.ok(data.toLowerCase().indexOf('usage') !== -1)
})
})

// cp.exec(CMD + ' --version', function (err, data) {
// t.error(err)
// t.equal(data, expectedVersion)
// })
test('Command line: webtorrent version', function (t) {
t.plan(6)
var expectedVersion = require(path.resolve(__dirname, '..', 'package.json')).version + '\n'

// cp.exec(CMD + ' -v', function (err, data) {
// t.error(err)
// t.equal(data, expectedVersion)
// })
// })
cp.exec(CMD + ' version', function (err, data) {
t.error(err)
t.equal(data, expectedVersion)
})

// test('Command line: webtorrent info /path/to/file.torrent', function (t) {
// t.plan(3)
cp.exec(CMD + ' --version', function (err, data) {
t.error(err)
t.equal(data, expectedVersion)
})

// var leavesPath = path.resolve(__dirname, 'torrents', 'leaves.torrent')
// var leaves = fs.readFileSync(leavesPath)
cp.exec(CMD + ' -v', function (err, data) {
t.error(err)
t.equal(data, expectedVersion)
})
})

// cp.exec(CMD + ' info ' + leavesPath, function (err, data) {
// t.error(err)
// data = JSON.parse(data)
// var parsedTorrent = parseTorrent(leaves)
// delete parsedTorrent.info
// delete parsedTorrent.infoBuffer
// t.deepEqual(data, JSON.parse(JSON.stringify(parsedTorrent, undefined, 2)))
// })
test('Command line: webtorrent info /path/to/file.torrent', function (t) {
t.plan(3)

// cp.exec(CMD + ' info /bad/path', function (err) {
// t.ok(err instanceof Error)
// })
// })
var leavesPath = path.resolve(__dirname, 'torrents', 'leaves.torrent')
var leaves = fs.readFileSync(leavesPath)

// test('Command line: webtorrent info magnet_uri', function (t) {
// t.plan(2)
cp.exec(CMD + ' info ' + leavesPath, function (err, data) {
t.error(err)
data = JSON.parse(data)
var parsedTorrent = parseTorrent(leaves)
delete parsedTorrent.info
delete parsedTorrent.infoBuffer
t.deepEqual(data, JSON.parse(JSON.stringify(parsedTorrent, undefined, 2)))
})

// var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce&tr=http%3A%2F%2Ftracker.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80'
cp.exec(CMD + ' info /bad/path', function (err) {
t.ok(err instanceof Error)
})
})

// cp.exec(CMD + ' info "' + leavesMagnetURI + '"', function (err, data) {
// t.error(err)
// data = JSON.parse(data)
// var parsedTorrent = parseTorrent(leavesMagnetURI)
// t.deepEqual(data, JSON.parse(JSON.stringify(parsedTorrent, undefined, 2)))
// })
// })
test('Command line: webtorrent info magnet_uri', function (t) {
t.plan(2)

// test('Command line: webtorrent create /path/to/file', function (t) {
// t.plan(1)
var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce&tr=http%3A%2F%2Ftracker.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80'

// var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub')
cp.exec(CMD + ' info "' + leavesMagnetURI + '"', function (err, data) {
t.error(err)
data = JSON.parse(data)
var parsedTorrent = parseTorrent(leavesMagnetURI)
t.deepEqual(data, JSON.parse(JSON.stringify(parsedTorrent, undefined, 2)))
})
})

// var child = spawn('node', [ CMD_PATH, 'create', leavesPath ])
// child.on('error', function (err) { t.fail(err) })
test('Command line: webtorrent create /path/to/file', function (t) {
t.plan(1)

// var chunks = []
// child.stdout.on('data', function (chunk) {
// chunks.push(chunk)
// })
// child.stdout.on('end', function () {
// var buf = Buffer.concat(chunks)
// var parsedTorrent = parseTorrent(new Buffer(buf, 'binary'))
// t.deepEqual(parsedTorrent.infoHash, 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36')
// })
// })
var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub')

// test('Command line: webtorrent download /path/to/file --port 80', function (t) {
// t.plan(2)
var child = spawn('node', [ CMD_PATH, 'create', leavesPath ])
child.on('error', function (err) { t.fail(err) })

// 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)
// })
// })
var spawn = cp.spawn;
function shspawn(command) {
return spawn('sh', ['-c', command]);
}
var chunks = []
child.stdout.on('data', function (chunk) {
chunks.push(chunk)
})
child.stdout.on('end', function () {
var buf = Buffer.concat(chunks)
var parsedTorrent = parseTorrent(new Buffer(buf, 'binary'))
t.deepEqual(parsedTorrent.infoHash, 'd2474e86c95b19b8bcfdb92bc12c9d44667cfa36')
})
})

test('Command line: webtorrent seed --port 80', function (t) {
test('Command line: webtorrent download /path/to/file --port 80', function (t) {
t.plan(2)

//var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub')

var seed = shspawn(CMD + " --port 80 seed test/content/Leaves\\ of\\ Grass\\ by\\ Walt\\ Whitman.epub")

seed.stdout.on('data', function (data) {
t.ok(data.indexOf('seeding') !== -1)
seed.unref();
cp.exec(CMD + ' --port 80 --out content download torrents/leaves.torrent', function (err, data) {
t.error(err)
t.ok(data.indexOf('successfully') !== -1)
})
})

// test('Command line: webtorrent download magnet_uri --port 80', function (t) {
// t.plan(2)
// var leavesMagnetURI = 'magnet:?xt=urn:btih:d2474e86c95b19b8bcfdb92bc12c9d44667cfa36&dn=Leaves+of+Grass+by+Walt+Whitman.epub&tr=http%3A%2F%2Ftracker.bittorrent.am%2Fannounce&tr=http%3A%2F%2Ftracker.thepiratebay.org%2Fannounce&tr=udp%3A%2F%2Ffr33domtracker.h33t.com%3A3310%2Fannounce&tr=udp%3A%2F%2Ftracker.ccc.de%3A80&tr=udp%3A%2F%2Ftracker.openbittorrent.com%3A80&tr=udp%3A%2F%2Ftracker.publicbt.com%3A80'

// var serve = serveStatic(path.join(__dirname, 'content'))
// var httpServer = http.createServer(function (req, res) {
// var done = finalhandler(req, res)
// serve(req, res, done)
// })
// var magnetUri

// httpServer.on('error', function (err) { t.fail(err) })

// var leavesTorrent = fs.readFileSync(path.resolve(__dirname, 'torrents', 'leaves.torrent'))
// var leavesParsed = parseTorrent(leavesTorrent)
// var leavesFilename = 'Leaves of Grass by Walt Whitman.epub'

// httpServer.listen(function(){
// var webSeedUrl = 'http://localhost:' + httpServer.address().port + '/' + leavesFilename
// var magnetUri = 'magnet:?xt=urn:btih:' + leavesParsed.infoHash +
// '&ws=' + encodeURIComponent(webSeedUrl)

// var leavesPath = path.resolve(__dirname, 'content', 'Leaves of Grass by Walt Whitman.epub')

// cp.exec(CMD + ' --port 80 download '+magnetUri, function (err, data) {
// t.error(err)
// t.ok(data.indexOf('seeding') !== -1)
// })
// cp.exec(CMD + ' --port 80 --out content download "'+leavesMagnetURI+'"', function (err, data) {
// t.error(err)
// t.ok(data.indexOf('seeding') !== -1)
// })
// })
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.