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 tests for appendTo

  • Loading branch information
whitef0x0 committed Dec 4, 2015
commit 53ea5f1811d571a4c8adc3864c1b47c58aa901c1
@@ -101,14 +101,6 @@ 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
})
@@ -134,14 +126,6 @@ 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()
})
})
}

@@ -181,4 +165,4 @@ function nextTick (cb, err, val) {
process.nextTick(function () {
if (cb) cb(err, val)
})
}
}
@@ -73,7 +73,9 @@
"brfs": "^1.2.0",
"browserify": "^12.0.1",
"finalhandler": "^0.4.0",
"istanbul-combine": "^0.3.0",
"istanbul-coveralls": "^1.0.3",
"jsdom": "^7.1.1",
"run-auto": "^1.0.0",
"serve-static": "^1.9.3",
"simple-get": "^1.0.0",
@@ -112,11 +114,11 @@
"build-debug": "browserify -s WebTorrent -e ./ > webtorrent.debug.js",
"size": "npm run build && cat webtorrent.min.js | gzip | wc -c",
"test-local": "standard && node ./bin/test.js",
"test": "standard && node ./bin/test.js && istanbul cover -- test/*.js && istanbul-coveralls",
"coverage": "istanbul cover -- tape test/*.js && istanbul-coveralls",
"test": "standard && node ./bin/test.js && coverage",
"test-browser": "zuul -- test/basic.js",
"test-browser-local": "zuul --local -- test/basic.js",
"test-node": "tape test/*.js",
"test-node-resume": "tape test/resume-torrent-scenarios.js",
"covearge": "istanbul cover test.js && istanbul-coveralls"
"test-append": "zuul --local -- test/browser/append-to.js"
}
}
@@ -0,0 +1,33 @@
var fs = require('fs')
var test = require('tape')
var WebTorrent = require('../../')
var AppendTo = require('../../lib/append-to')

var bigBuckFile = fs.readFileSync(__dirname + '/big-buck-bunny.mp4')

test('AppendTo should append and stream if file is video', function (t) {
t.plan(2)

//Start Seeding file
var client = new WebTorrent()
client.seed(bigBuckFile, { name: 'big-buck-bunny.mp4' }, function(_seedTorrent) {

client.add(_seedTorrent, function (torrent) {
torrent.files.forEach(function (file) {

console.log(file.name)
AppendTo(file, window.document.getElementsByTagName('body')[0], function (err, currElem) {
if(err) t.fail(err)

console.log(currElem)

t.pass('appended video to html element')
t.equal(window.document.getElementsByName('video')[0], currElem)
t.equal(window.document.getElementsByName('video').length, 1)

t.equal(currElem.nodeName, "VIDEO")
})
})
})
})
})
Binary file not shown.
@@ -0,0 +1,8 @@
{
"name": "test",
"version": "0.0.0",
"browserify": {
"transform": ["brfs"]
}
}

@@ -14,33 +14,36 @@ var leavesParsed = parseTorrent(leavesTorrent)
test('Torrent.progress shoud be 100% when torrent is done', function (t) {
t.plan(2)

var torrent_client = new WebTorrent({ dht: false, tracker: false })
var currTorrent = torrent_client.add(leavesPath)
var torrent_client = new WebTorrent({ tracker: false, dht: false })
var currTorrent = torrent_client.add(leavesTorrent, function(_torrent){

currTorrent.once('done', function () {
t.equal(currTorrent.progress, 1)
t.equal(currTorrent.downloaded, currTorrent.length)
torrent_client.destroy()
t.end()
})

})

test('Torrent.progress shoud be 0% when torrent has not started', function (t) {
t.plan(2)

var torrent_client = new WebTorrent()
var currTorrent = new Torrent(leavesPath, {client: torrent_client})
var torrent_client = new WebTorrent({ tracker: false, dht: false })
var currTorrent = torrent_client.add(leavesTorrent)

currTorrent.once('infoHash', function () {
t.equal(currTorrent.progress, 0)
t.equal(currTorrent.downloaded, 0)
torrent_client.destroy()
t.end()
})
})

test('Torrent._processParsedTorrent should update torrent with announce, urlList and new magnetURI and torrentFile', function (t) {
t.plan(5)

var torrent_client = new WebTorrent()
var currTorrent = new Torrent(null, {
client: torrent_client,
var torrent_client = new WebTorrent({ tracker: false, dht: false })
var currTorrent = torrent_client.add(null, {
announce: [ 'udp://tracker.openbittorrent.com:80', 'udp://tracker.openbittorrent.com:80' ],
urlList: [ 'http://instant.io/mytorrent.torrent' ]
})
@@ -51,14 +54,15 @@ test('Torrent._processParsedTorrent should update torrent with announce, urlList
t.notEqual(currTorrent.announce.indexOf('udp://tracker.openbittorrent.com:80') > -1)
t.ok(currTorrent.announce.indexOf('udp://tracker.openbittorrent.com:80') > -1)
t.equal(currTorrent.urlList.slice(-1)[0], 'http://instant.io/mytorrent.torrent')
torrent_client.destroy()
t.end()
})

test('Torrent._onMetadata should do nothing if torrent has metadata and is not being resumed', function (t) {
t.plan(3)

var torrent_client = new WebTorrent()
var currTorrent = new Torrent(leavesParsed, {
client: torrent_client,
var torrent_client = new WebTorrent({ tracker: false, dht: false })
var currTorrent = torrent_client.add(leavesParsed, {
announce: [ 'udp://tracker.openbittorrent.com:80', 'udp://tracker.openbittorrent.com:80' ],
urlList: [ 'http://instant.io/mytorrent.torrent' ]
})
@@ -69,19 +73,20 @@ test('Torrent._onMetadata should do nothing if torrent has metadata and is not b
t.notOk(currTorrent.resumed)

currTorrent._onMetadata(currTorrent)
t.notOk(onStoreSpy.called, '_onStore should not have been called')
t.notOk(onStoreSpy.called, '_onStore should not have been called')
torrent_client.destroy()
t.end()
})

})

test('Torrent._onMetadata should reinitialize torrent if torrent was paused and resumed', function (t) {
t.plan(6)

var torrent_client = new WebTorrent()
var currTorrent = new Torrent(leavesParsed, {
client: torrent_client,
var torrent_client = new WebTorrent({ tracker: false, dht: false })
var currTorrent = torrent_client.add(leavesParsed, {
announce: [ 'udp://tracker.openbittorrent.com:80', 'udp://tracker.openbittorrent.com:80' ],
urlList: [ 'http://instant.io/mytorrent.torrent']
urlList: [ 'http://instant.io/mytorrent.torrent' ]
})

currTorrent.once('ready', function (_torrent) {
@@ -99,22 +104,46 @@ test('Torrent._onMetadata should reinitialize torrent if torrent was paused and
t.ok(onStoreSpy.called, 'torrent._onStore should have been called')
t.ok(checkDoneSpy.called, 'torrent._checkDone should have been called')
t.notOk(onErrorSpy.called, 'torrent._onError should have not been called')
t.ok(currTorrent.pieces.length)
t.ok(currTorrent.pieces.length > 0)
torrent_client.destroy()
t.end()
})
})
})

test('Torrent._onMetadata should reinitialize torrent if metadata is deleted', function (t) {
test('Torrent.pause should destroy swarm and stop discovery', function (t) {
t.plan(6)

var torrent_client = new WebTorrent()
var currTorrent = new Torrent(leavesParsed, {
client: torrent_client,
var torrent_client = new WebTorrent({ tracker: false, dht: false })
var currTorrent = torrent_client.add(leavesParsed, {
announce: [ 'udp://tracker.openbittorrent.com:80', 'udp://tracker.openbittorrent.com:80' ],
urlList: [ 'http://instant.io/mytorrent.torrent']
urlList: [ 'http://instant.io/mytorrent.torrent' ]
})

currTorrent.once('ready', function (_torrent) {
currTorrent.once('ready', function () {
t.ok(currTorrent.swarm, 'torrent.swarm should exist before pause')
t.ok(currTorrent.discovery, 'torrent.discovery should exist before pause')
currTorrent.pause(function () {
t.ok(currTorrent.paused, 'torrent.paused should be true')
t.equal(currTorrent._rechokeIntervalId, null)
t.ok(currTorrent.swarm.destroyed, 'torrent.swarm should be destroyed')
t.notOk(!!currTorrent.discovery.tracker, 'torrent.discovery should be stopped')
torrent_client.destroy()
t.end()
})
})
})

test('Torrent._onMetadata should reinitialize torrent if metadata is deleted', function (t) {
t.plan(5)

var torrent_client = new WebTorrent({ tracker: false, dht: false })
var currTorrent = torrent_client.add(leavesParsed, {
announce: [ 'udp://tracker.openbittorrent.com:80', 'udp://tracker.openbittorrent.com:80' ],
urlList: [ 'http://instant.io/mytorrent.torrent' ]
})

currTorrent.once('ready', function () {
var onStoreSpy = sinon.spy(currTorrent, "_onStore")
var onErrorSpy = sinon.spy(currTorrent, "_onError")

@@ -127,28 +156,8 @@ test('Torrent._onMetadata should reinitialize torrent if metadata is deleted', f
t.ok(onStoreSpy.called, 'torrent._onStore should have been called')
t.notOk(onErrorSpy.called, 'torrent._onError should have not been called')
t.ok(currTorrent.pieces.length > 0)
})
})
})

test('Torrent.pause should destroy swarm and stop discovery', function (t) {
t.plan(6)

var torrent_client = new WebTorrent()
var currTorrent = new Torrent(leavesParsed, {
client: torrent_client,
announce: [ 'udp://tracker.openbittorrent.com:80', 'udp://tracker.openbittorrent.com:80' ],
urlList: [ 'http://instant.io/mytorrent.torrent']
})

currTorrent.once('ready', function () {
t.ok(currTorrent.swarm, 'torrent.swarm should exist before pause')
t.ok(currTorrent.discovery, 'torrent.discovery should exist before pause')
currTorrent.pause(function () {
t.ok(currTorrent.paused, 'torrent.paused should be true')
t.equal(currTorrent._rechokeIntervalId, null)
t.ok(currTorrent.swarm.destroyed, 'torrent.swarm should be destroyed')
t.ok(currTorrent.discovery.tracker.destroyed, 'torrent.discovery should be stopped')
torrent_client.destroy()
t.end()
})
})
})

Large diffs are not rendered by default.

You are viewing a condensed version of this merge commit. You can view the full changes here.
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.