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:
authorFeross Aboukhadijeh <feross@feross.org>2014-12-17 02:30:39 +0300
committerFeross Aboukhadijeh <feross@feross.org>2014-12-17 02:43:06 +0300
commit58c1f4cb260c6d163e368a157e1a675a43cd38d4 (patch)
tree5900f9a7911d62ab6bbe42888dd567d04a4cd088 /index.js
parent9e55cc7eec04e07060f32d1695b44f30a7c25187 (diff)
support seeding by string path to file
For #197
Diffstat (limited to 'index.js')
-rw-r--r--index.js66
1 files changed, 36 insertions, 30 deletions
diff --git a/index.js b/index.js
index 406d65a..a76ab97 100644
--- a/index.js
+++ b/index.js
@@ -9,15 +9,17 @@ var DHT = require('bittorrent-dht/client') // browser exclude
var EventEmitter = require('events').EventEmitter
var extend = require('extend.js')
var FileReadStream = require('filestream/read')
-var FSStorage = require('./lib/fs-storage') // browser exclude
+var fs = require('fs')
var hat = require('hat')
var inherits = require('inherits')
var loadIPSet = require('load-ip-set') // browser exclude
var parallel = require('run-parallel')
var parseTorrent = require('parse-torrent')
var speedometer = require('speedometer')
-var Storage = require('./lib/storage')
var stream = require('stream')
+
+var FSStorage = require('./lib/fs-storage') // browser exclude
+var Storage = require('./lib/storage')
var Torrent = require('./lib/torrent')
inherits(WebTorrent, EventEmitter)
@@ -196,45 +198,49 @@ WebTorrent.prototype.seed = function (input, opts, onseed) {
}
if (!opts) opts = {}
- // TODO: support `input` as string, or array of strings
+ // TODO: support an array of paths
+ // TODO: support path to folder (currently, only path to file supported)
if (typeof FileList !== 'undefined' && input instanceof FileList)
input = Array.prototype.slice.call(input)
- if (isBlob(input) || Buffer.isBuffer(input)) {
+ if (isBlob(input) || Buffer.isBuffer(input))
input = [ input ]
- }
- var streams = input.map(function (item) {
- if (isBlob(item)) return new FileReadStream(item)
- else if (Buffer.isBuffer(item)) {
- var s = new stream.PassThrough()
- s.end(item)
- return s
- } else throw new Error('unsupported input type to `seed`')
- })
+ var streams
+ if (Array.isArray(input) && input.length > 0) {
+ streams = input.map(function (item) {
+ if (isBlob(item)) return new FileReadStream(item)
+ else if (Buffer.isBuffer(item)) {
+ var s = new stream.PassThrough()
+ s.end(item)
+ return s
+ } else {
+ throw new Error('Array must contain only File|Blob|Buffer objects')
+ }
+ })
+ } else if (typeof input === 'string') {
+ streams = [ fs.createReadStream(input) ]
+ } else {
+ throw new Error('invalid input type')
+ }
- var torrent
createTorrent(input, opts, function (err, torrentBuf) {
if (err) return self.emit('error', err)
- self.add(torrentBuf, opts, function (_torrent) {
- torrent = _torrent
- torrent.storage.load(
- streams,
- function (err) {
- if (err) return self.emit('error', err)
- self.emit('seed', torrent)
- })
+ self.add(torrentBuf, opts, function (torrent) {
+ var tasks = [function (cb) {
+ torrent.storage.load(streams, cb)
+ }]
+ if (self.dht) tasks.push(function (cb) {
+ torrent.on('dhtAnnounce', cb)
+ })
+ parallel(tasks, function (err) {
+ if (err) return self.emit('error', err)
+ if (onseed) onseed(torrent)
+ self.emit('seed', torrent)
+ })
})
})
-
- function clientOnSeed (_torrent) {
- if (torrent.infoHash === _torrent.infoHash) {
- onseed(torrent)
- self.removeListener('seed', clientOnSeed)
- }
- }
- if (onseed) self.on('seed', clientOnSeed)
}
/**