diff options
Diffstat (limited to 'lib/_stream_writable.js')
-rw-r--r-- | lib/_stream_writable.js | 24 |
1 files changed, 21 insertions, 3 deletions
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 185407cfd8..41114f41bb 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -299,9 +299,13 @@ Writable.prototype.write = function(chunk, encoding, cb) { if (typeof cb !== 'function') cb = nop; - if (state.ending) + if (state.ending) { writeAfterEnd(this, cb); - else if (isBuf || validChunk(this, state, chunk, cb)) { + } else if (state.destroyed) { + const err = new ERR_STREAM_DESTROYED('write'); + process.nextTick(cb, err); + errorOrDestroy(this, err); + } else if (isBuf || validChunk(this, state, chunk, cb)) { state.pendingcb++; ret = writeOrBuffer(this, state, isBuf, chunk, encoding, cb); } @@ -733,7 +737,21 @@ Object.defineProperty(Writable.prototype, 'writableFinished', { } }); -Writable.prototype.destroy = destroyImpl.destroy; +const destroy = destroyImpl.destroy; +Writable.prototype.destroy = function(err, cb) { + const state = this._writableState; + if (!state.destroyed) { + for (let entry = state.bufferedRequest; entry; entry = entry.next) { + process.nextTick(entry.callback, new ERR_STREAM_DESTROYED('write')); + } + state.bufferedRequest = null; + state.lastBufferedRequest = null; + state.bufferedRequestCount = 0; + } + destroy.call(this, err, cb); + return this; +}; + Writable.prototype._undestroy = destroyImpl.undestroy; Writable.prototype._destroy = function(err, cb) { cb(err); |