diff options
author | Feross Aboukhadijeh <feross@feross.org> | 2014-10-22 08:49:54 +0400 |
---|---|---|
committer | Feross Aboukhadijeh <feross@feross.org> | 2014-10-22 08:49:54 +0400 |
commit | be5f98e6fb7a153b54b8ba9afeed8d7b60f79646 (patch) | |
tree | 650507c14c4269d8ccf8c6e11a723024f7444251 /lib | |
parent | 2a30d436e9a598dbe202cb1ca5a589f31bc42d9f (diff) |
`client.seed` streams into storage
Diffstat (limited to 'lib')
-rw-r--r-- | lib/storage.js | 21 |
1 files changed, 9 insertions, 12 deletions
diff --git a/lib/storage.js b/lib/storage.js index 45839d4..69f4bd5 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -9,6 +9,7 @@ var EventEmitter = require('events').EventEmitter var extend = require('extend.js') var FileStream = require('./file-stream') var inherits = require('inherits') +var MultiStream = require('multistream') var sha1 = require('git-sha1') var stream = require('stream') @@ -288,7 +289,7 @@ function Storage (parsedTorrent, opts) { self.buffer = new Buffer(parsedTorrent.length) } - var pieceLength = parsedTorrent.pieceLength + var pieceLength = self.pieceLength = parsedTorrent.pieceLength var lastPieceLength = parsedTorrent.lastPieceLength var numPieces = parsedTorrent.pieces.length @@ -321,12 +322,12 @@ function Storage (parsedTorrent, opts) { Storage.BLOCK_LENGTH = BLOCK_LENGTH -Storage.writeToStorage = function (storage, buf, pieceLength, cb) { +Storage.prototype.load = function (streams, cb) { + var self = this + if (!Array.isArray(streams)) streams = [ streams ] var pieceIndex = 0 - var bufStream = new stream.Readable() - bufStream._read = function () {} - bufStream - .pipe(new BlockStream(pieceLength, { nopad: true })) + ;(new MultiStream(streams)) + .pipe(new BlockStream(self.pieceLength, { nopad: true })) .on('data', function (piece) { var index = pieceIndex pieceIndex += 1 @@ -337,10 +338,9 @@ Storage.writeToStorage = function (storage, buf, pieceLength, cb) { var offset = blockIndex * BLOCK_LENGTH blockIndex += 1 - storage.writeBlock(index, offset, block) + self.writeBlock(index, offset, block) }) - s.write(piece) - s.end() + s.end(piece) }) .on('end', function () { cb(null) @@ -348,9 +348,6 @@ Storage.writeToStorage = function (storage, buf, pieceLength, cb) { .on('error', function (err) { cb(err) }) - - bufStream.push(buf) - bufStream.push(null) } Object.defineProperty(Storage.prototype, 'downloaded', { |