From 36d4e076bb050f21a58873d1abe5f8788fed11cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20W=C3=A4rting?= Date: Sat, 18 Aug 2018 11:09:19 +0200 Subject: [PATCH 1/4] modernize lib/file.js --- lib/file.js | 198 +++++++++++++++++++++++++--------------------------- 1 file changed, 97 insertions(+), 101 deletions(-) diff --git a/lib/file.js b/lib/file.js index eb2fc211..f9adb063 100644 --- a/lib/file.js +++ b/lib/file.js @@ -1,127 +1,123 @@ -module.exports = File - -var eos = require('end-of-stream') -var EventEmitter = require('events').EventEmitter -var FileStream = require('./file-stream') -var inherits = require('inherits') -var path = require('path') -var render = require('render-media') -var stream = require('readable-stream') -var streamToBlob = require('stream-to-blob') -var streamToBlobURL = require('stream-to-blob-url') -var streamToBuffer = require('stream-with-known-length-to-buffer') - -inherits(File, EventEmitter) - -function File (torrent, file) { - EventEmitter.call(this) - - this._torrent = torrent - this._destroyed = false - - this.name = file.name - this.path = file.path - this.length = file.length - this.offset = file.offset - - this.done = false - - var start = file.offset - var end = start + file.length - 1 - - this._startPiece = start / this._torrent.pieceLength | 0 - this._endPiece = end / this._torrent.pieceLength | 0 - - if (this.length === 0) { - this.done = true - this.emit('done') +const eos = require('end-of-stream') +const {EventEmitter} = require('events') +const FileStream = require('./file-stream') +const path = require('path') +const render = require('render-media') +const stream = require('readable-stream') +const streamToBlob = require('stream-to-blob') +const streamToBlobURL = require('stream-to-blob-url') +const streamToBuffer = require('stream-with-known-length-to-buffer') + +class File extends EventEmitter { + constructor (torrent, file) { + super() + + this._torrent = torrent + this._destroyed = false + + this.name = file.name + this.path = file.path + this.length = file.length + this.offset = file.offset + + this.done = false + + const start = file.offset + const end = start + file.length - 1 + + this._startPiece = start / this._torrent.pieceLength | 0 + this._endPiece = end / this._torrent.pieceLength | 0 + + if (this.length === 0) { + this.done = true + this.emit('done') + } } -} -Object.defineProperty(File.prototype, 'downloaded', { - get: function () { + get downloaded () { if (!this._torrent.bitfield) return 0 - var downloaded = 0 - for (var index = this._startPiece; index <= this._endPiece; ++index) { + let downloaded = 0 + for (let index = this._startPiece; index <= this._endPiece; ++index) { if (this._torrent.bitfield.get(index)) { // verified data - downloaded += (index === this._endPiece) ? this._torrent.lastPieceLength : this._torrent.pieceLength + downloaded += index === this._endPiece ? this._torrent.lastPieceLength : this._torrent.pieceLength } else { // "in progress" data - var piece = this._torrent.pieces[index] - downloaded += (piece.length - piece.missing) + const piece = this._torrent.pieces[index] + downloaded += piece.length - piece.missing } } return downloaded } -}) -Object.defineProperty(File.prototype, 'progress', { - get: function () { return this.length ? this.downloaded / this.length : 0 } -}) + get progress () { + return this.length ? this.downloaded / this.length : 0 + } -File.prototype.select = function (priority) { - if (this.length === 0) return - this._torrent.select(this._startPiece, this._endPiece, priority) -} + select (priority) { + if (this.length === 0) return + this._torrent.select(this._startPiece, this._endPiece, priority) + } -File.prototype.deselect = function () { - if (this.length === 0) return - this._torrent.deselect(this._startPiece, this._endPiece, false) -} + deselect () { + if (this.length === 0) return + this._torrent.deselect(this._startPiece, this._endPiece, false) + } + + createReadStream (opts) { + if (this.length === 0) { + const empty = new stream.PassThrough() + process.nextTick(() => { + empty.end() + }) + return empty + } -File.prototype.createReadStream = function (opts) { - var self = this - if (this.length === 0) { - var empty = new stream.PassThrough() - process.nextTick(function () { - empty.end() + const fileStream = new FileStream(this, opts) + this._torrent.select(fileStream._startPiece, fileStream._endPiece, true, () => { + fileStream._notify() }) - return empty + eos(fileStream, () => { + if (this._destroyed) return + if (!this._torrent.destroyed) { + this._torrent.deselect(fileStream._startPiece, fileStream._endPiece, true) + } + }) + return fileStream } - var fileStream = new FileStream(self, opts) - self._torrent.select(fileStream._startPiece, fileStream._endPiece, true, function () { - fileStream._notify() - }) - eos(fileStream, function () { - if (self._destroyed) return - if (!self._torrent.destroyed) { - self._torrent.deselect(fileStream._startPiece, fileStream._endPiece, true) - } - }) - return fileStream -} + getBuffer (cb) { + streamToBuffer(this.createReadStream(), this.length, cb) + } -File.prototype.getBuffer = function (cb) { - streamToBuffer(this.createReadStream(), this.length, cb) -} + getBlob (cb) { + if (typeof window === 'undefined') throw new Error('browser-only method') + streamToBlob(this.createReadStream(), this._getMimeType(), cb) + } -File.prototype.getBlob = function (cb) { - if (typeof window === 'undefined') throw new Error('browser-only method') - streamToBlob(this.createReadStream(), this._getMimeType(), cb) -} + getBlobURL (cb) { + if (typeof window === 'undefined') throw new Error('browser-only method') + streamToBlobURL(this.createReadStream(), this._getMimeType(), cb) + } -File.prototype.getBlobURL = function (cb) { - if (typeof window === 'undefined') throw new Error('browser-only method') - streamToBlobURL(this.createReadStream(), this._getMimeType(), cb) -} + appendTo (elem, opts, cb) { + if (typeof window === 'undefined') throw new Error('browser-only method') + render.append(this, elem, opts, cb) + } -File.prototype.appendTo = function (elem, opts, cb) { - if (typeof window === 'undefined') throw new Error('browser-only method') - render.append(this, elem, opts, cb) -} + renderTo (elem, opts, cb) { + if (typeof window === 'undefined') throw new Error('browser-only method') + render.render(this, elem, opts, cb) + } -File.prototype.renderTo = function (elem, opts, cb) { - if (typeof window === 'undefined') throw new Error('browser-only method') - render.render(this, elem, opts, cb) -} + _getMimeType () { + return render.mime[path.extname(this.name).toLowerCase()] + } -File.prototype._getMimeType = function () { - return render.mime[path.extname(this.name).toLowerCase()] + _destroy () { + this._destroyed = true + this._torrent = null + } } -File.prototype._destroy = function () { - this._destroyed = true - this._torrent = null -} +module.exports = File From 6386a6e75d260929098f68ccdc59603554e6bc3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20W=C3=A4rting?= Date: Sun, 19 Aug 2018 06:28:00 +0200 Subject: [PATCH 2/4] wrapped in parentheses --- lib/file.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/file.js b/lib/file.js index f9adb063..4d7d71a3 100644 --- a/lib/file.js +++ b/lib/file.js @@ -1,12 +1,12 @@ -const eos = require('end-of-stream') const {EventEmitter} = require('events') -const FileStream = require('./file-stream') +const {PassThrough} = require('readable-stream') +const eos = require('end-of-stream') const path = require('path') const render = require('render-media') -const stream = require('readable-stream') const streamToBlob = require('stream-to-blob') const streamToBlobURL = require('stream-to-blob-url') const streamToBuffer = require('stream-with-known-length-to-buffer') +const FileStream = require('./file-stream') class File extends EventEmitter { constructor (torrent, file) { @@ -40,7 +40,7 @@ class File extends EventEmitter { for (let index = this._startPiece; index <= this._endPiece; ++index) { if (this._torrent.bitfield.get(index)) { // verified data - downloaded += index === this._endPiece ? this._torrent.lastPieceLength : this._torrent.pieceLength + downloaded += (index === this._endPiece) ? this._torrent.lastPieceLength : this._torrent.pieceLength } else { // "in progress" data const piece = this._torrent.pieces[index] @@ -66,7 +66,7 @@ class File extends EventEmitter { createReadStream (opts) { if (this.length === 0) { - const empty = new stream.PassThrough() + const empty = new PassThrough() process.nextTick(() => { empty.end() }) From 84a69f7d5d921c417d8ed9e96615d5de3d23a060 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20W=C3=A4rting?= Date: Sun, 19 Aug 2018 15:17:14 +0200 Subject: [PATCH 3/4] no message --- lib/file.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/lib/file.js b/lib/file.js index 4d7d71a3..23a65766 100644 --- a/lib/file.js +++ b/lib/file.js @@ -36,18 +36,30 @@ class File extends EventEmitter { get downloaded () { if (!this._torrent.bitfield) return 0 - let downloaded = 0 - for (let index = this._startPiece; index <= this._endPiece; ++index) { - if (this._torrent.bitfield.get(index)) { + + const { pieces, bitfield, pieceLength } = this._torrent + const { _startPiece:start, _endPiece:end } = this + const piece = pieces[start] + + // Calculate first piece diffrently, it sometimes have a offset + let downloaded = bitfield.get(start) + ? pieceLength - this.offset + : Math.max(piece.length - piece.missing - this.offset, 0) + + for (let index = start + 1; index <= end; ++index) { + if (bitfield.get(index)) { // verified data - downloaded += (index === this._endPiece) ? this._torrent.lastPieceLength : this._torrent.pieceLength + downloaded += pieceLength } else { // "in progress" data - const piece = this._torrent.pieces[index] + const piece = pieces[index] downloaded += piece.length - piece.missing } } - return downloaded + + // We don't have a end-offset and one small file can fith in the middle + // of one chunk, so return this.length if it's oversized + return Math.min(downloaded, this.length) } get progress () { From 3f89fe9c1ed4643d9a9f8099b43e39fb297e85ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jimmy=20W=C3=A4rting?= Date: Sun, 19 Aug 2018 15:20:24 +0200 Subject: [PATCH 4/4] standard style --- lib/file.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/file.js b/lib/file.js index 23a65766..2715fbb9 100644 --- a/lib/file.js +++ b/lib/file.js @@ -38,7 +38,7 @@ class File extends EventEmitter { if (!this._torrent.bitfield) return 0 const { pieces, bitfield, pieceLength } = this._torrent - const { _startPiece:start, _endPiece:end } = this + const { _startPiece: start, _endPiece: end } = this const piece = pieces[start] // Calculate first piece diffrently, it sometimes have a offset