diff options
author | Mathias Buus <mathiasbuus@gmail.com> | 2018-01-29 19:32:34 +0100 |
---|---|---|
committer | Matteo Collina <hello@matteocollina.com> | 2018-03-06 13:31:56 +0100 |
commit | 5e3f51648ed5de36b01d53bde13fb6fb7b965667 (patch) | |
tree | a134fb92b8b846e195ea3f960131fcc9147533e6 /lib/_stream_writable.js | |
parent | acac0f852a02c2b129adbc51e0bd8bd482d791af (diff) | |
download | android-node-v8-5e3f51648ed5de36b01d53bde13fb6fb7b965667.tar.gz android-node-v8-5e3f51648ed5de36b01d53bde13fb6fb7b965667.tar.bz2 android-node-v8-5e3f51648ed5de36b01d53bde13fb6fb7b965667.zip |
stream: updated streams error handling
This improves error handling for streams in a few ways.
1. It ensures that no user defined methods (_read, _write, ...) are run
after .destroy has been called.
2. It introduces an explicit error to tell the user if they are write to
write, etc to the stream after it has been destroyed.
3. It makes streams always emit close as the last thing after they have
been destroyed
4. Changes the default _destroy to not gracefully end streams.
It also updates net, http2, zlib and fs to the new error handling.
PR-URL: https://github.com/nodejs/node/pull/18438
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Diffstat (limited to 'lib/_stream_writable.js')
-rw-r--r-- | lib/_stream_writable.js | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 2b76588135..d5cfe07f17 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -134,6 +134,9 @@ function WritableState(options, stream) { // True if the error was already emitted and should not be thrown again this.errorEmitted = false; + // Should close be emitted on destroy. Defaults to true. + this.emitClose = options.emitClose !== false; + // count buffered requests this.bufferedRequestCount = 0; @@ -390,7 +393,9 @@ function doWrite(stream, state, writev, len, chunk, encoding, cb) { state.writecb = cb; state.writing = true; state.sync = true; - if (writev) + if (state.destroyed) + state.onwrite(new errors.Error('ERR_STREAM_DESTROYED', 'write')); + else if (writev) stream._writev(chunk, state.onwrite); else stream._write(chunk, encoding, state.onwrite); @@ -604,7 +609,7 @@ function callFinal(stream, state) { } function prefinish(stream, state) { if (!state.prefinished && !state.finalCalled) { - if (typeof stream._final === 'function') { + if (typeof stream._final === 'function' && !state.destroyed) { state.pendingcb++; state.finalCalled = true; process.nextTick(callFinal, stream, state); @@ -681,6 +686,5 @@ Object.defineProperty(Writable.prototype, 'destroyed', { Writable.prototype.destroy = destroyImpl.destroy; Writable.prototype._undestroy = destroyImpl.undestroy; Writable.prototype._destroy = function(err, cb) { - this.end(); cb(err); }; |