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

Web (HTTP) Seeding #331

Merged
merged 3 commits into from Jul 3, 2015
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -256,6 +256,10 @@ Torrent.prototype._onSwarmListening = function () {
// if full metadata was included in initial torrent id, use it
if (self.parsedTorrent.info) self._onMetadata(self.parsedTorrent)

if (self.parsedTorrent.urlList) {
self.parsedTorrent.urlList.forEach(self.addWebPeer.bind(self))
}

self.emit('listening', self.client.torrentPort)
}

@@ -402,6 +406,16 @@ Torrent.prototype.addPeer = function (peer) {
}
}

/**
* Add a web peer to the swarm
* @param {string} web peer url
*/
Torrent.prototype.addWebPeer = function (webPeer) {
var self = this
self.emit('webPeer', webPeer)
self.swarm.addWebPeer(webPeer, self.parsedTorrent)
}

/**
* Select a range of pieces to prioritize.
*
@@ -67,7 +67,9 @@
"bittorrent-tracker": "^4.0.0",
"brfs": "^1.2.0",
"browserify": "^10.0.0",
"finalhandler": "^0.3.6",
"run-auto": "^1.0.0",
"serve-static": "^1.9.3",
"standard": "^3.1.1",
"tape": "^4.0.0",
"uglify-js": "^2.4.15",
@@ -0,0 +1,78 @@
var auto = require('run-auto')
var fs = require('fs')
var parseTorrent = require('parse-torrent')
var test = require('tape')
var WebTorrent = require('../')

var http = require('http')
var serveStatic = require('serve-static')
var finalhandler = require('finalhandler')
var path = require('path')

var leavesPath = __dirname + '/content/Leaves of Grass by Walt Whitman.epub'
var leavesFilename = 'Leaves of Grass by Walt Whitman.epub'
var leavesFile = fs.readFileSync(leavesPath)
var leavesTorrent = fs.readFileSync(__dirname + '/torrents/leaves.torrent')
var leavesParsed = parseTorrent(leavesTorrent)

// remove trackers from .torrent file
leavesParsed.announce = []

test('Download using webseed (via .torrent file)', function (t) {
t.plan(5)

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

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

auto({
httpPort: function (cb) {
httpServer.listen(function () {
var port = httpServer.address().port
cb(null, port)
})
},
client1: ['httpPort', function (cb, r) {

leavesParsed.urlList.push('http://localhost:' + r.httpPort + '/' + leavesFilename)

var client1 = new WebTorrent({
tracker: false,
dht: { bootstrap: false }
})

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

client1.on('torrent', function (torrent) {
torrent.files.forEach(function (file) {
file.getBuffer(function (err, buf) {
if (err) throw err
t.deepEqual(buf, leavesFile, 'downloaded correct content')
})
})

torrent.once('done', function () {
t.pass('client1 downloaded torrent from webseed')
cb(null, client1)
})
})

client1.add(leavesParsed)

}]
}, function (err, r) {
t.error(err)
r.client1.destroy(function () {
t.pass('client1 destroyed')
})
httpServer.close(function () {
t.pass('http server closed')
})
})
})
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.