Skip to content

Commit

Permalink
fix: use streamx instead of stream
Browse files Browse the repository at this point in the history
  • Loading branch information
ThaUnknown committed Jun 26, 2022
1 parent 4be86f5 commit 8b97ee8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
24 changes: 10 additions & 14 deletions lib/file-stream.js
@@ -1,6 +1,5 @@
const stream = require('stream')
const { Readable } = require('streamx')
const debugFactory = require('debug')
const eos = require('end-of-stream')

const debug = debugFactory('webtorrent:file-stream')

Expand All @@ -12,7 +11,7 @@ const debug = debugFactory('webtorrent:file-stream')
* @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)
*/
class FileStream extends stream.Readable {
class FileStream extends Readable {
constructor (file, opts) {
super(opts)

Expand All @@ -39,26 +38,22 @@ class FileStream extends stream.Readable {
this._torrent.select(this._startPiece, this._endPiece, true, () => {
this._notify()
})

// Ensure that cleanup happens even if destroy() is never called (readable-stream v3 currently doesn't call it automaticallly)
eos(this, (err) => {
this.destroy(err)
})
}

_read () {
_read (cb) {
if (this._reading) return
this._reading = true
this._notify()
this._notify(cb)
}

_notify () {
if (!this._reading || this._missing === 0) return
_notify (cb = () => {}) {
if (!this._reading || this._missing === 0) return cb()
if (!this._torrent.bitfield.get(this._piece)) {
cb()
return this._torrent.critical(this._piece, this._piece + this._criticalLength)
}

if (this._notifying) return
if (this._notifying) return cb()
this._notifying = true

if (this._torrent.destroyed) return this.destroy(new Error('Torrent removed'))
Expand Down Expand Up @@ -92,11 +87,12 @@ class FileStream extends stream.Readable {
this.push(buffer)

if (this._missing === 0) this.push(null)
cb()
})
this._piece += 1
}

_destroy (err, cb) {
_destroy (cb, err) {
if (!this._torrent.destroyed) {
this._torrent.deselect(this._startPiece, this._endPiece, true)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/file.js
@@ -1,5 +1,5 @@
const EventEmitter = require('events')
const { PassThrough } = require('stream')
const { PassThrough } = require('streamx')
const path = require('path')
const render = require('render-media')
const streamToBlob = require('stream-to-blob')
Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -74,6 +74,7 @@
"stream-to-blob": "^2.0.1",
"stream-to-blob-url": "^3.0.2",
"stream-with-known-length-to-buffer": "^1.0.4",
"streamx": "^2.12.4",
"throughput": "^1.0.1",
"torrent-discovery": "^9.4.13",
"torrent-piece": "^2.0.1",
Expand Down

0 comments on commit 8b97ee8

Please sign in to comment.