Skip to content

Commit

Permalink
fix only emit finish on clean end. always emit close
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Apr 25, 2018
1 parent 8739e68 commit 456c36d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 13 deletions.
35 changes: 29 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,18 @@ function SonicBoom (fd, minLength) {
return
}

if (this.destroyed) {
return
}

var len = this._buf.length
if (this._buf.length > 0 && len > this.minLength) {
actualWrite(this)
} else if (this._ending === true) {
} else if (this._ending) {
if (len > 0) {
actualWrite(this)
} else {
this._writing = false
actualClose(this)
}
} else {
Expand All @@ -74,28 +79,40 @@ SonicBoom.prototype.write = function (data) {
}
this._buf += data
var len = this._buf.length
if (this._writing === false && len > this.minLength) {
if (!this._writing && len > this.minLength) {
actualWrite(this)
}
return len < 16384
}

SonicBoom.prototype.end = function () {
if (this.destroyed) {
throw new Error('SonicBoom destroyed')
}

if (this._ending) {
return
}

this._ending = true

if (!this._writing && this._buf.length > 0 && this.fd >= 0) {
actualWrite(this)
this._ending = true
return
}

if (this._writing === true) {
this._ending = true
if (this._writing) {
return
}

actualClose(this)
}

SonicBoom.prototype.flushSync = function () {
if (this.destroyed) {
throw new Error('SonicBoom destroyed')
}

if (this.fd < 0) {
throw new Error('sonic boom is not ready yet')
}
Expand All @@ -106,6 +123,9 @@ SonicBoom.prototype.flushSync = function () {
}

SonicBoom.prototype.destroy = function () {
if (this.destroyed) {
return
}
actualClose(this)
}

Expand All @@ -124,7 +144,10 @@ function actualClose (sonic) {
return
}

sonic.emit('finish')
if (sonic._ending && !sonic._writing) {
sonic.emit('finish')
}
sonic.emit('close')
})
sonic.destroyed = true
sonic._buf = ''
Expand Down
42 changes: 35 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ tearDown(() => {
})

test('write things to a file descriptor', (t) => {
t.plan(5)
t.plan(6)

const dest = file()
const fd = fs.openSync(dest, 'w')
Expand All @@ -47,10 +47,13 @@ test('write things to a file descriptor', (t) => {
t.equal(data, 'hello world\nsomething else\n')
})
})
stream.on('close', () => {
t.pass('close emitted')
})
})

test('write things in a streaming fashion', (t) => {
t.plan(7)
t.plan(8)

const dest = file()
const fd = fs.openSync(dest, 'w')
Expand All @@ -77,10 +80,13 @@ test('write things in a streaming fashion', (t) => {
stream.on('finish', () => {
t.pass('finish emitted')
})
stream.on('close', () => {
t.pass('close emitted')
})
})

test('can be piped into', (t) => {
t.plan(3)
t.plan(4)

const dest = file()
const fd = fs.openSync(dest, 'w')
Expand All @@ -98,10 +104,13 @@ test('can be piped into', (t) => {
})
})
})
stream.on('close', () => {
t.pass('close emitted')
})
})

test('write things to a file', (t) => {
t.plan(5)
t.plan(6)

const dest = file()
const stream = new SonicBoom(dest)
Expand All @@ -121,10 +130,13 @@ test('write things to a file', (t) => {
t.equal(data, 'hello world\nsomething else\n')
})
})
stream.on('close', () => {
t.pass('close emitted')
})
})

test('flushSync', (t) => {
t.plan(3)
t.plan(4)

const dest = file()
const fd = fs.openSync(dest, 'w')
Expand All @@ -138,10 +150,14 @@ test('flushSync', (t) => {

const data = fs.readFileSync(dest, 'utf8')
t.equal(data, 'hello world\nsomething else\n')

stream.on('close', () => {
t.pass('close emitted')
})
})

test('destroy', (t) => {
t.plan(4)
t.plan(5)

const dest = file()
const fd = fs.openSync(dest, 'w')
Expand All @@ -155,10 +171,18 @@ test('destroy', (t) => {
t.error(err)
t.equal(data, 'hello world\n')
})

stream.on('finish', () => {
t.fail('finish emitted')
})

stream.on('close', () => {
t.pass('close emitted')
})
})

test('minLength', (t) => {
t.plan(7)
t.plan(8)

const dest = file()
const stream = new SonicBoom(dest, 4096)
Expand Down Expand Up @@ -190,4 +214,8 @@ test('minLength', (t) => {
})
})
}, 100)

stream.on('close', () => {
t.pass('close emitted')
})
})

0 comments on commit 456c36d

Please sign in to comment.