Skip to content

Commit

Permalink
Should not allow 'end' events to happen more than once
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed May 18, 2018
1 parent bcb7f53 commit 55ed167
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,20 +265,22 @@ module.exports = class MiniPass extends EE {
if (this.pipes.length)
this.pipes.forEach(p => p.dest.write(data) || this.pause())
} else if (ev === 'end') {
this[EMITTED_END] = true
this.readable = false

if (this[DECODER]) {
data = this[DECODER].end()
if (data) {
this.pipes.forEach(p => p.dest.write(data))
super.emit('data', data)
}
}

this.pipes.forEach(p => {
p.dest.removeListener('drain', p.ondrain)
if (!p.opts || p.opts.end !== false)
p.dest.end()
})
this[EMITTED_END] = true
this.readable = false
} else if (ev === 'close') {
this[CLOSED] = true
// don't emit close before 'end' and 'finish'
Expand Down
21 changes: 21 additions & 0 deletions test/end-twice.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'
const t = require('tap')
const MP = require('../')

const butterfly = Buffer.from([0x61, 0xf0, 0x9f, 0xa6, 0x8b, 0xf0])
const mp = new MP({ encoding: 'utf8' })

let sawEnd = 0
mp.on('end', () =>
t.equal(sawEnd++, 0, 'should not have seen the end yet'))

mp.once('data', () => {
mp.once('data', () => {
mp.once('data', () => mp.end())
mp.end()
})
mp.end(butterfly.slice(3))
})
mp.end(butterfly.slice(0, 3))

t.equal(sawEnd, 1, 'should see end exactly once')

0 comments on commit 55ed167

Please sign in to comment.