diff options
-rw-r--r-- | index.js | 47 | ||||
-rw-r--r-- | lib/concat-stream.js | 14 |
2 files changed, 47 insertions, 14 deletions
@@ -15,6 +15,7 @@ var Peer = require('simple-peer') var speedometer = require('speedometer') var zeroFill = require('zero-fill') +var concatStream = require('./lib/concat-stream') var Torrent = require('./lib/torrent') module.exports.WEBRTC_SUPPORT = Peer.WEBRTC_SUPPORT @@ -237,22 +238,31 @@ WebTorrent.prototype.seed = function (input, opts, onseed) { }) }) - createTorrent.parseInput(input, opts, function (err, files) { + if (!Array.isArray(input)) input = [ input ] + parallel(input.map(function (item) { + return function (cb) { + if (isReadable(item)) concatStream(item, cb) + else cb(null, item) + } + }), function (err, input) { if (err) return self.emit('error', err) - streams = files.map(function (file) { return file.getStream }) - - createTorrent(input, opts, function (err, torrentBuf) { + createTorrent.parseInput(input, opts, function (err, files) { if (err) return self.emit('error', err) - if (self.destroyed) return - - var existingTorrent = self.get(torrentBuf) - if (existingTorrent) { - torrent.destroy() - _onseed() - return - } else { - torrent._onTorrentId(torrentBuf) - } + streams = files.map(function (file) { return file.getStream }) + + createTorrent(input, opts, function (err, torrentBuf) { + if (err) return self.emit('error', err) + if (self.destroyed) return + + var existingTorrent = self.get(torrentBuf) + if (existingTorrent) { + torrent.destroy() + _onseed() + return + } else { + torrent._onTorrentId(torrentBuf) + } + }) }) }) @@ -303,3 +313,12 @@ WebTorrent.prototype.destroy = function (cb) { parallel(tasks, cb) } + +/** + * Check if `obj` is a node Readable stream + * @param {*} obj + * @return {boolean} + */ +function isReadable (obj) { + return typeof obj === 'object' && obj != null && typeof obj.pipe === 'function' +} diff --git a/lib/concat-stream.js b/lib/concat-stream.js new file mode 100644 index 0000000..c2d8860 --- /dev/null +++ b/lib/concat-stream.js @@ -0,0 +1,14 @@ +module.exports = function (stream, cb) { + var chunks = [] + stream.on('data', function (chunk) { + chunks.push(chunk) + }) + stream.once('end', function () { + if (cb) cb(null, Buffer.concat(chunks)) + cb = null + }) + stream.once('error', function (err) { + if (cb) cb(err) + cb = null + }) +} |