lib/storage.js
/**
 * Storage for a torrent download. Handles the complexities of reading and writing
 * to pieces and files.
 *
 * @param {Object} parsedTorrent
 * @param {Object} opts
 */
Storage for a torrent download. Handles the complexities of reading and writing
to pieces and files.

@param {Object} parsedTorrent
@param {Object} opts
Storage for a torrent download . Handles the complexities of reading and writing to pieces and files . @param { Object } parsedTorrent @param { Object } opts
function Storage (parsedTorrent, opts) {
  var self = this
  EventEmitter.call(self)
  if (!debug.enabled) self.setMaxListeners(0)
  opts = opts || {}
  self.bitfield = new BitField(parsedTorrent.pieces.length)
  self.done = false
  self.closed = false
  self.readonly = true
  if (!opts.nobuffer) {
    self.buffer = new Buffer(parsedTorrent.length)
  }
  var pieceLength = self.pieceLength = parsedTorrent.pieceLength
  var lastPieceLength = parsedTorrent.lastPieceLength
  var numPieces = parsedTorrent.pieces.length
  self.pieces = parsedTorrent.pieces.map(function (hash, index) {
    var start = index * pieceLength
    var end = start + (index === numPieces - 1 ? lastPieceLength : pieceLength)
    var buffer = (self.buffer ? self.buffer.slice(start, end) : end - start)
    var piece = new Piece(index, hash, buffer, !!opts.noVerify)
    piece.on('done', self._onPieceDone.bind(self, piece))
    return piece
  })
  self.files = parsedTorrent.files.map(function (fileObj) {
    var start = fileObj.offset
    var end = start + fileObj.length - 1
    var startPiece = start / pieceLength | 0
    var endPiece = end / pieceLength | 0
    var pieces = self.pieces.slice(startPiece, endPiece + 1)
    var file = new File(self, fileObj, pieces, pieceLength)
    file.on('done', self._onFileDone.bind(self, file))
    return file
  })
}
function Storage ( parsedTorrent , opts ) { var self = this EventEmitter . call ( self ) if ( ! debug . enabled ) self . setMaxListeners ( 0 ) opts = opts || { } self . bitfield = new BitField ( parsedTorrent . pieces . length ) self . done = false self . closed = false self . readonly = true if ( ! opts . nobuffer ) { self . buffer = new Buffer ( parsedTorrent . length ) } var pieceLength = self . pieceLength = parsedTorrent . pieceLength var lastPieceLength = parsedTorrent . lastPieceLength var numPieces = parsedTorrent . pieces . length self . pieces = parsedTorrent . pieces . map ( function ( hash , index ) { var start = index * pieceLength var end = start + ( index === numPieces - 1 ? lastPieceLength : pieceLength ) var buffer = ( self . buffer ? self . buffer . slice ( start , end ) : end - start ) var piece = new Piece ( index , hash , buffer , ! ! opts . noVerify ) piece . on ( 'done' , self . _onPieceDone . bind ( self , piece ) ) return piece } ) self . files = parsedTorrent . files . map ( function ( fileObj ) { var start = fileObj . offset var end = start + fileObj . length - 1 var startPiece = start / pieceLength | 0 var endPiece = end / pieceLength | 0 var pieces = self . pieces . slice ( startPiece , endPiece + 1 ) var file = new File ( self , fileObj , pieces , pieceLength ) file . on ( 'done' , self . _onFileDone . bind ( self , file ) ) return file } ) }
Storage for a torrent download.
Storage for a torrent download .
// if we're backed by a buffer, the piece's buffer will reference the same memory. // otherwise, the piece's buffer will be lazily created on demand