From acebfcf38ea2d7a17286522d9ed71d751f88bee5 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Tue, 9 Feb 2016 22:05:01 -0800 Subject: client.seed: fix stream input type convert streams to buffer, since we need to consume the stream twice. --- index.js | 47 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 33 insertions(+), 14 deletions(-) (limited to 'index.js') diff --git a/index.js b/index.js index 3b8694a..8a02678 100644 --- a/index.js +++ b/index.js @@ -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' +} -- cgit v1.2.3