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 unit tests for torrent.js

  • Loading branch information
whitef0x0 committed Dec 3, 2015
commit 10f9cc26da5c859ad266b05c6026871831f4118d
@@ -1,2 +1,7 @@
node_modules
webtorrent.debug.js
coverage
content
examples/npm-debug.log
npm-debug.log
webtorrent.debug.js
@@ -0,0 +1,50 @@
verbose: false
instrumentation:
root: .
extensions:
- .js
default-excludes: true
excludes: []
embed-source: false
variable: __coverage__
compact: false
preserve-comments: false
complete-copy: false
save-baseline: false
baseline-file: ./coverage/coverage-baseline.json
include-all-sources: false
include-pid: false
reporting:
print: summary
reports:
- lcov
dir: ./coverage
watermarks:
statements: [50, 80]
lines: [50, 80]
functions: [50, 80]
branches: [50, 80]
report-config:
json: {file: coverage-final.json}
json-summary: {file: coverage-summary.json}
lcovonly: {file: lcov.info}
text: {file: coverage-final.txt}
text-lcov: {file: lcov.info}
text-summary: {file: coverage-summary.txt}
hooks:
hook-run-in-context: false
post-require-hook: null
handle-sigint: false
check:
global:
statements: 80
lines: 80
branches: 0
functions: 0
excludes: []
each:
statements: 80
lines: 80
branches: 0
functions: 0
excludes: []
@@ -66,7 +66,7 @@ function Torrent (torrentId, opts) {
self.path = opts.path
self._store = opts.store || FSChunkStore

self.disableSeeding = opts.disableSeeding || false
self.shouldSeed = opts.shouldSeed || true

self.strategy = opts.strategy || 'sequential'

@@ -245,7 +245,7 @@ Torrent.prototype._processParsedTorrent = function (parsedTorrent) {
})
}

if (this.urlList && !this.disableSeeding) {
if (this.urlList) {
// Allow specifying web seeds via `opts` parameter
parsedTorrent.urlList = parsedTorrent.urlList.concat(this.urlList)
}
@@ -316,19 +316,18 @@ Torrent.prototype._onMetadata = function (metadata) {
}
debug('got metadata')

var parsedTorrent
if (metadata && metadata.infoHash) {
// `metadata` is a parsed torrent (from parse-torrent module)
parsedTorrent = metadata
} else {
try {
parsedTorrent = parseTorrent(metadata)
} catch (err) {
return self._onError(err)
}
}

if (!self.resumed) {
var parsedTorrent
if (metadata && metadata.infoHash) {
// `metadata` is a parsed torrent (from parse-torrent module)
parsedTorrent = metadata
} else {
try {
parsedTorrent = parseTorrent(metadata)
} catch (err) {
return self._onError(err)
}
}
self._processParsedTorrent(parsedTorrent)
self.metadata = self.torrentFile
}
@@ -337,7 +336,7 @@ Torrent.prototype._onMetadata = function (metadata) {
self.discovery.setTorrent(self)

// add web seed urls (BEP19)
if (self.urlList && !self.disableSeeding) self.urlList.forEach(self.addWebSeed.bind(self))
if (self.urlList) self.urlList.forEach(self.addWebSeed.bind(self))

if (!self.resumed) {
self._hashes = self.pieces
@@ -450,22 +449,15 @@ Torrent.prototype.pause = function (cb) {
self.paused = true
self.emit('paused')

// Emit paused event to files for streaming controls
if (self.files) {
self.files.forEach(function (file) {
file.emit('paused')
})
}

if (self._rechokeIntervalId) {
clearInterval(self._rechokeIntervalId)
self._rechokeIntervalId = null
}

var tasks = []

if (typeof cb !== 'function') {
cb = function () { return }
if (typeof cb !== 'function' || !cb) {
cb = noop
}

if (self.swarm) tasks.push(function (cb) { self.swarm.destroy(cb) })
@@ -485,13 +477,6 @@ Torrent.prototype.resume = function () {

self.emit('resume')

// Emit 'resumed' event to files (for streaming controls)
if (self.files) {
self.files.forEach(function (file) {
file.emit('resume')
})
}

self._onResume(self.parsedTorrent)
}

@@ -500,7 +485,7 @@ Torrent.prototype.resume = function () {
*/
Torrent.prototype.destroy = function (cb) {
var self = this
if (self.destroyed || self.paused) return
if (self.destroyed) return
self.destroyed = true
debug('destroy')

@@ -711,6 +696,32 @@ Torrent.prototype._onWire = function (wire, addr) {
}
}

Torrent.prototype.disableSeeding = function () {
if(self.paused || self.destroyed || !self.swarm) return

self.shouldSeed = false
self.swarm.wires.forEach(function (wire) {
// If we didn't have the metadata at the time ut_metadata was initialized for this
// wire, we still want to make it available to the peer in case they request it.
if (wire.ut_metadata) wire.ut_metadata.setMetadata(self.metadata)

self._onWireWithMetadata(wire)
})
}

Torrent.prototype.enableSeeding = function () {
if(self.paused || self.destroyed || !self.swarm) return

self.shouldSeed = true
self.swarm.wires.forEach(function (wire) {
// If we didn't have the metadata at the time ut_metadata was initialized for this
// wire, we still want to make it available to the peer in case they request it.
if (wire.ut_metadata) wire.ut_metadata.setMetadata(self.metadata)

self._onWireWithMetadata(wire)
})
}

Torrent.prototype._onWireWithMetadata = function (wire) {
var self = this
var timeoutId = null
@@ -733,7 +744,7 @@ Torrent.prototype._onWireWithMetadata = function (wire) {
for (; i < self.pieces.length; ++i) {
if (!wire.peerPieces.get(i)) return
}
wire.isSeeder = true
wire.isSeeder = self.shouldSeed
wire.choke() // always choke seeders
}

@@ -1224,7 +1235,7 @@ Torrent.prototype._request = function (wire, index, hotswap) {

Torrent.prototype._checkDone = function () {
var self = this
if (self.destroyed || self.paused) return
if (self.destroyed) return

// are any new files done?
self.files.forEach(function (file) {
@@ -73,9 +73,11 @@
"brfs": "^1.2.0",
"browserify": "^11.0.0",
"finalhandler": "^0.4.0",
"istanbul-coveralls": "^1.0.3",
"run-auto": "^1.0.0",
"serve-static": "^1.9.3",
"simple-get": "^1.0.0",
"sinon": "^1.17.2",
"standard": "^5.1.0",
"tape": "^4.0.0",
"uglify-js": "^2.4.15",
@@ -109,10 +111,12 @@
"build": "browserify -s WebTorrent -e ./ | uglifyjs -m > webtorrent.min.js",
"build-debug": "browserify -s WebTorrent -e ./ > webtorrent.debug.js",
"size": "npm run build && cat webtorrent.min.js | gzip | wc -c",
"test": "standard && node ./bin/test.js",
"test-local": "standard && node ./bin/test.js",
"test": "standard && node ./bin/test.js && istanbul cover -- test/*.js && istanbul-coveralls",
"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"
"test-node-resume": "tape test/resume-torrent-scenarios.js",
"covearge": "istanbul cover test.js && istanbul-coveralls"
}
}

This file was deleted.

@@ -147,7 +147,7 @@ function scenario1_1Test (t, serverType) {

var currentTorrent = client2.add(leavesParsed)

client2.once('torrent', function (torrent) {
client2.once('infoHash', function () {
// Pause the torrent
client2.pause(currentTorrent)
t.ok(currentTorrent.paused, 'Torrent should be paused')
@@ -161,7 +161,7 @@ function scenario1_1Test (t, serverType) {
}]
}, function (err, r) {
t.error(err)
t.equal(trackerStartCount, 2, 'trackerStartCount should be 2')
t.equal(trackerStartCount, 1, 'trackerStartCount should be 1')

r.tracker.close(function () {
t.pass('tracker closed')
@@ -419,7 +419,7 @@ function scenario1_4Test (t, serverType) {

var currentTorrent = client2.add(leavesParsed)

client2.once('torrent', function (torrent) {
client2.once('infoHash', function () {
client2.pause(currentTorrent)
t.ok(currentTorrent.paused, 'Torrent should be paused')

@@ -437,7 +437,7 @@ function scenario1_4Test (t, serverType) {
}]
}, function (err, r) {
t.error(err)
t.equal(trackerStartCount, 2, 'trackerStartCount should be 2')
t.equal(trackerStartCount, 1, 'trackerStartCount should be 1')

r.tracker.close(function () {
t.pass('tracker closed')
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.