Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: some simplification #32900

Closed
wants to merge 4 commits into from
Closed
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
43 changes: 22 additions & 21 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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);
}
};

Expand All @@ -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,
Expand All @@ -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'));
Expand Down Expand Up @@ -409,7 +410,6 @@ function onwrite(stream, er) {
return;
}

state.writing = false;
state.writecb = null;
state.length -= state.writelen;
state.writelen = 0;
Expand All @@ -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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition is a duplicate of what's inside of clearBuffer. However, it is here on purpose since clearBuffer is not inlineable.

clearBuffer(stream, state);
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;

Expand Down Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -621,7 +622,7 @@ function needFinish(state) {
!state.errored &&
state.bufferedRequest === null &&
!state.finished &&
!state.writing);
!state.writecb);
}

function callFinal(stream, state) {
Expand Down