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>2015-08-22 15:46:16 +0300
committerFeross Aboukhadijeh <feross@feross.org>2015-08-22 15:46:16 +0300
commit958f30a0341ca323c7cef794b00fe38eed845297 (patch)
treee8a0248aa350c7d3638365b771f0975027c8ae21 /lib/file-stream.js
parent89645ebe81cf46d61e4d7be423cb8c7a32283b2b (diff)
BREAKING: Use abstract-chunk-store for storage
Part of an effort to share more code with `torrent-stream` by @mafintosh: https://github.com/mafintosh/torrent-stream/pull/133 https://github.com/mafintosh/torrent-stream/pull/132 Storage is now based on: https://www.npmjs.com/package/fs-chunk-store (in node) https://www.npmjs.com/package/memory-chunk-store (in the browser) and: https://www.npmjs.com/package/immediate-chunk-store https://www.npmjs.com/package/torrent-piece This is a BREAKING change, since users who pass in custom storage engine with `opts.storage` will need to update their storage backend to be `abstract-chunk-store` compliant: https://www.npmjs.com/package/abstract-chunk-store There are also likely other miscellaneous BREAKING API changes, since this was a huge refactor of the codebase.
Diffstat (limited to 'lib/file-stream.js')
-rw-r--r--lib/file-stream.js86
1 files changed, 37 insertions, 49 deletions
diff --git a/lib/file-stream.js b/lib/file-stream.js
index 5713aff..bd5f384 100644
--- a/lib/file-stream.js
+++ b/lib/file-stream.js
@@ -7,71 +7,58 @@ var stream = require('stream')
inherits(FileStream, stream.Readable)
/**
- * A readable stream of a torrent file.
+ * Readable stream of a torrent file
*
- * @param {Object} file
+ * @param {File} file
+ * @param {Object} opts
* @param {number} opts.start stream slice of file, starting from this byte (inclusive)
* @param {number} opts.end stream slice of file, ending with this byte (inclusive)
- * @param {number} opts.pieceLength length of an individual piece
*/
function FileStream (file, opts) {
- var self = this
- if (!(self instanceof FileStream)) return new FileStream(file, opts)
- stream.Readable.call(self, opts)
- debug('new filestream %s', JSON.stringify(opts))
-
- if (!opts) opts = {}
- if (!opts.start) opts.start = 0
- if (!opts.end) opts.end = file.length - 1
-
- self.destroyed = false
- self.length = opts.end - opts.start + 1
-
- var offset = opts.start + file.offset
- var pieceLength = opts.pieceLength
-
- self.startPiece = offset / pieceLength | 0
- self.endPiece = (opts.end + file.offset) / pieceLength | 0
-
- self._storage = file.storage
- self._piece = self.startPiece
- self._missing = self.length
- self._reading = false
- self._notifying = false
- self._criticalLength = Math.min((1024 * 1024 / pieceLength) | 0, 2)
- self._offset = offset - (self.startPiece * pieceLength)
+ stream.Readable.call(this, opts)
+
+ this.destroyed = false
+ this._torrent = file._torrent
+
+ var start = (opts && opts.start) || 0
+ var end = (opts && opts.end) || (file.length - 1)
+ var pieceLength = file._torrent.pieceLength
+
+ this._startPiece = (start + file.offset) / pieceLength | 0
+ this._endPiece = (end + file.offset) / pieceLength | 0
+
+ this._piece = this._startPiece
+ this._offset = (start + file.offset) - (this._startPiece * pieceLength)
+
+ this._missing = end - start + 1
+ this._reading = false
+ this._notifying = false
+ this._criticalLength = Math.min((1024 * 1024 / pieceLength) | 0, 2)
}
FileStream.prototype._read = function () {
- var self = this
- debug('_read')
- if (self._reading) return
- self._reading = true
- self.notify()
+ if (this._reading) return
+ this._reading = true
+ this._notify()
}
-FileStream.prototype.notify = function () {
+FileStream.prototype._notify = function () {
var self = this
- debug('notify')
if (!self._reading || self._missing === 0) return
- if (!self._storage.bitfield.get(self._piece)) {
- return self._storage.emit('critical', self._piece, self._piece + self._criticalLength)
+ if (!self._torrent.bitfield.get(self._piece)) {
+ return self._torrent.critical(self._piece, self._piece + self._criticalLength)
}
if (self._notifying) return
self._notifying = true
var p = self._piece
- self._storage.read(self._piece++, function (err, buffer) {
+ self._torrent.storage.get(p, function (err, buffer) {
+ console.log('GOT', p)
self._notifying = false
- if (self.destroyed) return
-
- if (err) {
- self._storage.emit('error', err)
- return self.destroy(err)
- }
-
+ if (self.destroyed) { console.log('destroyed'); return }
+ if (err) return self.destroy(err)
debug('read %s (length %s) (err %s)', p, buffer.length, err && err.message)
if (self._offset) {
@@ -83,6 +70,7 @@ FileStream.prototype.notify = function () {
buffer = buffer.slice(0, self._missing)
}
self._missing -= buffer.length
+ console.log('missing', self._missing)
debug('pushing buffer of length %s', buffer.length)
self._reading = false
@@ -90,11 +78,11 @@ FileStream.prototype.notify = function () {
if (self._missing === 0) self.push(null)
})
+ self._piece += 1
}
FileStream.prototype.destroy = function () {
- var self = this
- if (self.destroyed) return
- self.destroyed = true
- self._storage.emit('deselect', self.startPiece, self.endPiece, true)
+ if (this.destroyed) return
+ this.destroyed = true
+ this._torrent.deselect(this._startPiece, this._endPiece, true)
}