From 5a6b7dd1a71c836fbeca569ffedce31b06c8ef78 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 12 Jun 2019 18:12:34 +0200 Subject: [PATCH] Added support for .destroy(err, cb) signature --- index.js | 20 ++++++-------------- test.js | 13 +++++++++++++ 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 18b6dd1..7e26db9 100644 --- a/index.js +++ b/index.js @@ -173,22 +173,14 @@ 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) { @@ -196,7 +188,7 @@ Duplexify.prototype._destroy = function(err) { if (this._writable && this._writable.destroy) this._writable.destroy() } - this.emit('close') + cb(err) } Duplexify.prototype._write = function(data, enc, cb) { diff --git a/test.js b/test.js index a01c126..5277c94 100644 --- a/test.js +++ b/test.js @@ -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) + }) +})