diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 25235166d9fa7d..dde0abee2d19f1 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -120,9 +120,6 @@ function WritableState(options, stream, isDuplex) { // socket or file. this.length = 0; - // A flag to see when we're in the middle of a write. - this.writing = false; - // When true all writes will be buffered until .uncork() call this.corked = 0; @@ -188,6 +185,15 @@ function WritableState(options, stream, isDuplex) { this.corkedRequestsFree = corkReq; } +ObjectDefineProperties(WritableState.prototype, { + // Backwards compat. + writing: { + get() { + return !!this.writecb; + } + } +}); + WritableState.prototype.getBuffer = function getBuffer() { let current = this.bufferedRequest; const out = []; @@ -318,11 +324,7 @@ Writable.prototype.uncork = function() { if (state.corked) { state.corked--; - if (!state.writing && - !state.corked && - !state.bufferProcessing && - state.bufferedRequest) - clearBuffer(this, state); + clearBuffer(this, state); } }; @@ -349,7 +351,7 @@ function writeOrBuffer(stream, state, chunk, encoding, cb) { if (!ret) state.needDrain = true; - if (state.writing || state.corked || state.errored) { + if (state.writecb || state.corked || state.errored) { const last = state.lastBufferedRequest; state.lastBufferedRequest = { chunk, @@ -375,7 +377,6 @@ function writeOrBuffer(stream, state, chunk, encoding, cb) { function doWrite(stream, state, writev, len, chunk, encoding, cb) { state.writelen = len; state.writecb = cb; - state.writing = true; state.sync = true; if (state.destroyed) state.onwrite(new ERR_STREAM_DESTROYED('write')); @@ -409,7 +410,6 @@ function onwrite(stream, er) { return; } - state.writing = false; state.writecb = null; state.length -= state.writelen; state.writelen = 0; @@ -429,13 +429,7 @@ function onwrite(stream, er) { onwriteError(stream, state, er, cb); } } else { - // Check if we're actually ready to finish, but don't emit yet - const finished = needFinish(state) || stream.destroyed; - - if (!finished && - !state.corked && - !state.bufferProcessing && - state.bufferedRequest) { + if (state.bufferedRequest) { clearBuffer(stream, state); } @@ -484,7 +478,7 @@ function afterWrite(stream, state, count, cb) { // If there's something in the buffer waiting, then invoke callbacks. function errorBuffer(state, err) { - if (state.writing || !state.bufferedRequest) { + if (state.writecb || !state.bufferedRequest) { return; } @@ -500,6 +494,13 @@ function errorBuffer(state, err) { // If there's something in the buffer waiting, then process it function clearBuffer(stream, state) { + if (state.writecb || + state.corked || + state.bufferProcessing || + !state.bufferedRequest) { + return; + } + state.bufferProcessing = true; let entry = state.bufferedRequest; @@ -551,7 +552,7 @@ function clearBuffer(stream, state) { // it means that we need to wait until it does. // also, that means that the chunk and cb are currently // being processed, so move the buffer counter past them. - if (state.writing) { + if (state.writecb) { break; } } @@ -621,7 +622,7 @@ function needFinish(state) { !state.errored && state.bufferedRequest === null && !state.finished && - !state.writing); + !state.writecb); } function callFinal(stream, state) {