Skip to content
Open
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
20 changes: 6 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,30 +173,22 @@ Duplexify.prototype._forward = function() {
this._forwarding = false
}

Duplexify.prototype.destroy = function(err) {
if (this.destroyed) return
this.destroyed = true

var self = this
process.nextTick(function() {
self._destroy(err)
})
}

Duplexify.prototype._destroy = function(err) {
Duplexify.prototype._destroy = function(err, cb) {
if (err) {
var ondrain = this._ondrain
this._ondrain = null
if (ondrain) ondrain(err)
else this.emit('error', err)
if (ondrain) {
ondrain(err)
err = null
}
}

if (this._forwardDestroy) {
if (this._readable && this._readable.destroy) this._readable.destroy()
if (this._writable && this._writable.destroy) this._writable.destroy()
}

this.emit('close')
cb(err)
}

Duplexify.prototype._write = function(data, enc, cb) {
Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,16 @@ tape('works with node native streams (net)', function(t) {
dup.write(HELLO_WORLD)
})
})

tape('destroy with a callback', function(t) {
t.plan(1)

var write = through()
var read = through()
var dup = duplexify(write, read)

var e = new Error('kaboom')
dup.destroy(e, function (err) {
t.strictEqual(err, e)
})
})