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-10-22 08:49:54 +0400
committerFeross Aboukhadijeh <feross@feross.org>2014-10-22 08:49:54 +0400
commitbe5f98e6fb7a153b54b8ba9afeed8d7b60f79646 (patch)
tree650507c14c4269d8ccf8c6e11a723024f7444251 /index.js
parent2a30d436e9a598dbe202cb1ca5a589f31bc42d9f (diff)
`client.seed` streams into storage
Diffstat (limited to 'index.js')
-rw-r--r--index.js76
1 files changed, 41 insertions, 35 deletions
diff --git a/index.js b/index.js
index 37cc4d7..37540b4 100644
--- a/index.js
+++ b/index.js
@@ -3,7 +3,6 @@
module.exports = WebTorrent
-var blobToBuffer = require('blob-to-buffer')
var createTorrent = require('create-torrent')
var debug = require('debug')('webtorrent')
var DHT = require('bittorrent-dht/client') // browser exclude
@@ -190,7 +189,7 @@ WebTorrent.prototype.download = function (torrentId, opts, ontorrent) {
* - W3C FileList object (basically an array of `File` objects)
* - Array of `File` objects
*
- * @param {string|File|FileList|Array.<File>|Blob|Array.<Blob>} input
+ * @param {string|File|FileList|Blob|Buffer|Array.<File|Blob|Buffer>} input
* @param {Object} opts
* @param {function} onseed
*/
@@ -201,48 +200,46 @@ WebTorrent.prototype.seed = function (input, opts, onseed) {
opts = {}
}
- // TODO: support `input` as filesystem path string
+ // TODO: support `input` as string, or array of strings
if (typeof FileList !== 'undefined' && input instanceof FileList)
input = Array.prototype.slice.call(input)
- if (typeof Blob !== 'undefined' && input instanceof Blob)
+ if (isBlob(input) || Buffer.isBuffer(input)) {
input = [ input ]
+ }
- parallel(input.map(function (file) {
- return function (cb) {
- if (Buffer.isBuffer(file)) cb(null, file)
- else blobToBuffer(file, cb)
- }
- }), function (err, buffers) {
+ 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 torrent
+ createTorrent(input, opts, function (err, torrentBuf) {
if (err) return self.emit('error', err)
- var buffer = Buffer.concat(buffers)
-
- var torrent
- function clientOnSeed (_torrent) {
- if (torrent.infoHash === _torrent.infoHash) {
- onseed(torrent)
- self.removeListener('seed', clientOnSeed)
- }
- }
- if (onseed) self.on('seed', clientOnSeed)
-
- createTorrent(input, opts, function (err, torrentBuf) {
- if (err) return self.emit('error', err)
- var parsedTorrent = parseTorrent(torrentBuf)
- self.add(torrentBuf, opts, function (_torrent) {
- torrent = _torrent
- Storage.writeToStorage(
- torrent.storage,
- buffer,
- parsedTorrent.pieceLength,
- function (err) {
- if (err) return self.emit('error', err)
- self.emit('seed', torrent)
- })
- })
+ var parsedTorrent = parseTorrent(torrentBuf)
+ 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)
+ })
})
})
+
+ function clientOnSeed (_torrent) {
+ if (torrent.infoHash === _torrent.infoHash) {
+ onseed(torrent)
+ self.removeListener('seed', clientOnSeed)
+ }
+ }
+ if (onseed) self.on('seed', clientOnSeed)
}
/**
@@ -291,3 +288,12 @@ WebTorrent.prototype.destroy = function (cb) {
parallel(tasks, cb)
}
+
+/**
+ * Check if `obj` is a W3C Blob object (which is the superclass of W3C File)
+ * @param {*} obj
+ * @return {boolean}
+ */
+function isBlob (obj) {
+ return typeof Blob !== 'undefined' && obj instanceof Blob
+}