Skip to content

Commit

Permalink
fix block stream looking at same dep multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
mafintosh committed Feb 9, 2025
1 parent ba921bc commit 7779a69
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions lib/block-dependency-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ module.exports = class BlockStream extends Readable {
if (this._consumed > this.core.dependencies.length) return

const deps = this.core.dependencies

const index = findDependencyIndex(deps, this.end, this._consumed++, this.reverse)
const index = this._findDependencyIndex(deps)

const curr = deps[index]
const prev = deps[index - 1]
Expand All @@ -38,6 +37,19 @@ module.exports = class BlockStream extends Readable {
this._makeStream(core.block(ptr, start), core.block(ptr, end))
}

_findDependencyIndex (deps) {
if (!this.reverse) return this._consumed++

let i = deps.length - this._consumed++
while (i > 0) {
if (deps[i - 1].length <= this.end) return i
i--
this._consumed++
}

return 0
}

_predestroy () {
if (this._stream !== null) this._stream.destroy()
}
Expand Down Expand Up @@ -83,7 +95,7 @@ module.exports = class BlockStream extends Readable {
// empty the current stream
if (this._onreadable() === true) this._drained = true

this._stream = this._start = this._end = null
this._stream = null

this._update()
this._maybeDrain()
Expand All @@ -98,13 +110,3 @@ module.exports = class BlockStream extends Readable {
}

function noop () {}

function findDependencyIndex (deps, end, offset, reverse) {
if (!reverse) return offset

for (let i = deps.length - offset; i > 0; i--) {
if (deps[i - 1].length <= end) return i
}

return 0
}

0 comments on commit 7779a69

Please sign in to comment.