Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/webtorrent/webtorrent.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorfisch0920 <fisch0920@gmail.com>2014-05-16 07:37:39 +0400
committerfisch0920 <fisch0920@gmail.com>2014-05-16 07:37:39 +0400
commit1529dcff555bba3abe9a67e93d030548fdb8863f (patch)
tree769588721381d9d1f40648d71eefc7c95c4abac0 /index.js
parentf98a3fdeb9282aaa5b40e8de42d21ebf50b757c4 (diff)
stream to VLC is now working
Diffstat (limited to 'index.js')
-rw-r--r--index.js74
1 files changed, 69 insertions, 5 deletions
diff --git a/index.js b/index.js
index a0022f8..3a6b7ef 100644
--- a/index.js
+++ b/index.js
@@ -4,8 +4,12 @@ module.exports = WebTorrent
var Client = require('bittorrent-client')
var fs = require('fs')
+var url = require('url')
var http = require('http')
var inherits = require('inherits')
+var pump = require('pump')
+var mime = require('mime')
+var rangeParser = require('range-parser')
inherits(WebTorrent, Client)
@@ -18,6 +22,8 @@ function WebTorrent (opts) {
return
}
+ self._startServer()
+
self.on('torrent', function (torrent) {
self._onTorrent(torrent)
})
@@ -38,8 +44,7 @@ WebTorrent.prototype.add = function (torrentId, opts, cb) {
}
if (typeof cb !== 'function') cb = function () {}
- // TODO: support passing in an index to file to download
- // self.index = opts.index
+ self.index = opts.index
// Called once we have a torrentId that bittorrent-client can handle
function onTorrentId (torrentId) {
@@ -79,13 +84,72 @@ WebTorrent.prototype._onTorrent = function (torrent) {
// if no index specified, use largest file
// TODO: support torrent index selection correctly -- this doesn't work yet
- /*if (typeof torrent.index !== 'number') {
+ if (typeof torrent.index !== 'number') {
var largestFile = torrent.files.reduce(function (a, b) {
return a.length > b.length ? a : b
})
torrent.index = torrent.files.indexOf(largestFile)
}
- // TODO
- torrent.files[torrent.index].select()*/
+ torrent.files[torrent.index].select()
+ self.index = torrent.index
+ self.torrent = torrent
+}
+
+WebTorrent.prototype._startServer = function () {
+ var self = this
+ self.server = http.createServer()
+ self.server.on('request', self._onRequest.bind(self))
+}
+
+WebTorrent.prototype._onRequest = function (req, res) {
+ var self = this
+
+ if (!self.ready) {
+ return self.once('ready', self._onRequest.bind(self, req, res))
+ }
+
+ var u = url.parse(req.url)
+
+ if (u.pathname === '/favicon.ico') {
+ return res.end()
+ }
+ if (u.pathname === '/') {
+ u.pathname = '/' + self.index
+ }
+
+ var i = Number(u.pathname.slice(1))
+
+ if (isNaN(i) || i >= self.torrent.files.length) {
+ res.statusCode = 404
+ return res.end()
+ }
+
+ var file = self.torrent.files[i]
+ var range = req.headers.range
+
+ res.setHeader('Accept-Ranges', 'bytes')
+ res.setHeader('Content-Type', mime.lookup(file.name))
+
+ if (!range) {
+ res.statusCode = 206
+ res.setHeader('Content-Length', file.length)
+ if (req.method === 'HEAD') {
+ return res.end()
+ }
+ pump(file.createReadStream(), res)
+ return
+ }
+
+ range = rangeParser(file.length, range)[0] // don't support multi-range reqs
+ res.statusCode = 206
+
+ var rangeStr = 'bytes ' + range.start + '-' + range.end + '/' + file.length
+ res.setHeader('Content-Range', rangeStr)
+ res.setHeader('Content-Length', range.end - range.start + 1)
+
+ if (req.method === 'HEAD') {
+ return res.end()
+ }
+ pump(file.createReadStream(range), res)
}