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
path: root/lib
diff options
context:
space:
mode:
authorKayleePop <34007889+KayleePop@users.noreply.github.com>2018-08-10 20:06:40 +0300
committerKayleePop <34007889+KayleePop@users.noreply.github.com>2018-08-10 20:06:40 +0300
commit4e7013bcf386cb02442a5580cfc659926366adf6 (patch)
treeddae62ced1fef302de2b3bd257ed730329a9960c /lib
parent2b9f7562a6b64d548e8f937d9ff7bdc0827ca063 (diff)
modernize lib/file-stream.js
Diffstat (limited to 'lib')
-rw-r--r--lib/file-stream.js141
1 files changed, 69 insertions, 72 deletions
diff --git a/lib/file-stream.js b/lib/file-stream.js
index 11514b4..72622b2 100644
--- a/lib/file-stream.js
+++ b/lib/file-stream.js
@@ -1,10 +1,5 @@
-module.exports = FileStream
-
-var debug = require('debug')('webtorrent:file-stream')
-var inherits = require('inherits')
-var stream = require('readable-stream')
-
-inherits(FileStream, stream.Readable)
+const debug = require('debug')('webtorrent:file-stream')
+const stream = require('readable-stream')
/**
* Readable stream of a torrent file
@@ -14,89 +9,91 @@ inherits(FileStream, stream.Readable)
* @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)
*/
-function FileStream (file, opts) {
- stream.Readable.call(this, opts)
+class FileStream extends stream.Readable {
+ constructor (file, opts) {
+ super(opts)
- this.destroyed = false
- this._torrent = file._torrent
+ this.destroyed = false
+ this._torrent = file._torrent
- var start = (opts && opts.start) || 0
- var end = (opts && opts.end && opts.end < file.length)
- ? opts.end
- : file.length - 1
+ const start = (opts && opts.start) || 0
+ const end = (opts && opts.end && opts.end < file.length)
+ ? opts.end
+ : file.length - 1
- var pieceLength = file._torrent.pieceLength
+ const pieceLength = file._torrent.pieceLength
- this._startPiece = (start + file.offset) / pieceLength | 0
- this._endPiece = (end + file.offset) / pieceLength | 0
+ 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._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)
-}
+ this._missing = end - start + 1
+ this._reading = false
+ this._notifying = false
+ this._criticalLength = Math.min((1024 * 1024 / pieceLength) | 0, 2)
+ }
-FileStream.prototype._read = function () {
- if (this._reading) return
- this._reading = true
- this._notify()
-}
+ _read () {
+ if (this._reading) return
+ this._reading = true
+ this._notify()
+ }
-FileStream.prototype._notify = function () {
- var self = this
+ _notify () {
+ if (!this._reading || this._missing === 0) return
+ if (!this._torrent.bitfield.get(this._piece)) {
+ return this._torrent.critical(this._piece, this._piece + this._criticalLength)
+ }
- if (!self._reading || self._missing === 0) return
- if (!self._torrent.bitfield.get(self._piece)) {
- return self._torrent.critical(self._piece, self._piece + self._criticalLength)
- }
+ if (this._notifying) return
+ this._notifying = true
- if (self._notifying) return
- self._notifying = true
+ if (this._torrent.destroyed) return this._destroy(new Error('Torrent removed'))
- if (self._torrent.destroyed) return self._destroy(new Error('Torrent removed'))
+ const p = this._piece
+ this._torrent.store.get(p, (err, buffer) => {
+ this._notifying = false
+ if (this.destroyed) return
+ if (err) return this._destroy(err)
+ debug('read %s (length %s) (err %s)', p, buffer.length, err && err.message)
- var p = self._piece
- self._torrent.store.get(p, function (err, buffer) {
- self._notifying = false
- if (self.destroyed) return
- if (err) return self._destroy(err)
- debug('read %s (length %s) (err %s)', p, buffer.length, err && err.message)
+ if (this._offset) {
+ buffer = buffer.slice(this._offset)
+ this._offset = 0
+ }
- if (self._offset) {
- buffer = buffer.slice(self._offset)
- self._offset = 0
- }
+ if (this._missing < buffer.length) {
+ buffer = buffer.slice(0, this._missing)
+ }
+ this._missing -= buffer.length
- if (self._missing < buffer.length) {
- buffer = buffer.slice(0, self._missing)
- }
- self._missing -= buffer.length
+ debug('pushing buffer of length %s', buffer.length)
+ this._reading = false
+ this.push(buffer)
- debug('pushing buffer of length %s', buffer.length)
- self._reading = false
- self.push(buffer)
+ if (this._missing === 0) this.push(null)
+ })
+ this._piece += 1
+ }
- if (self._missing === 0) self.push(null)
- })
- self._piece += 1
-}
+ destroy (onclose) {
+ this._destroy(null, onclose)
+ }
-FileStream.prototype.destroy = function (onclose) {
- this._destroy(null, onclose)
-}
+ _destroy (err, onclose) {
+ if (this.destroyed) return
+ this.destroyed = true
-FileStream.prototype._destroy = function (err, onclose) {
- if (this.destroyed) return
- this.destroyed = true
+ if (!this._torrent.destroyed) {
+ this._torrent.deselect(this._startPiece, this._endPiece, true)
+ }
- if (!this._torrent.destroyed) {
- this._torrent.deselect(this._startPiece, this._endPiece, true)
+ if (err) this.emit('error', err)
+ this.emit('close')
+ if (onclose) onclose()
}
-
- if (err) this.emit('error', err)
- this.emit('close')
- if (onclose) onclose()
}
+
+module.exports = FileStream