Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer chunk after flushing buffer in write if paused #28

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,12 @@ module.exports = class Minipass extends Stream {
// because we're mid-write, so that'd be bad.
if (this[BUFFERLENGTH] !== 0)
this[FLUSH](true)
this.emit('data', chunk)

// if we are still flowing after flushing the buffer we can emit the
// chunk otherwise we have to buffer it.
this.flowing
? this.emit('data', chunk)
: this[BUFFERPUSH](chunk)
} else
this[BUFFERPUSH](chunk)

Expand Down
35 changes: 35 additions & 0 deletions test/buffer-chunk-when-flowing-stops.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Reproduces an issue where write is called while the stream is flowing but a
// destination cannot accept all of the buffered chunks. minipass should add
// the chunk to the end of the buffer instead of emitting it before the buffer
// is cleared.
//
// This caused issues when piping make-fetch-happen stream to tar.extract
// https://github.com/npm/cli/issues/3884
const Minipass = require('../')
const t = require('tap')

class Pauser extends Minipass {
write (chunk, encoding, callback) {
super.write(chunk, encoding, callback)
return false
}
}

const src = new Minipass({encoding: 'utf8'})
const pauser = new Pauser({encoding: 'utf8'})

// queue up two chunks while the src is buffering
src.write('1')
src.write('2')

// when the src starts flowing write a third chunk
src.once('resume', () => src.write('3'))

// pipe the src to the pauser which will request the src stops after the first
// chunk.
src.pipe(pauser)

src.end()

// we should expect the chunks in the original order.
t.resolveMatch(pauser.collect(), ['1', '2', '3'], '123')