Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

stream: switch _writableState.buffer to queue #8826

Closed
Changes from 1 commit
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
Prev Previous commit
eliminate bufferLength; style fix on deprecation
  • Loading branch information
chrisdickinson committed Dec 5, 2014
commit d8a2be545dc8be7f259bf21fdbae7d45b6587430
18 changes: 7 additions & 11 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ function WritableState(options, stream) {

this.bufferedRequest = null;
this.lastBufferedRequest = null;
this.bufferLength = 0;

// number of pending user-supplied write callbacks
// this must be 0 before 'finish' can be emitted
Expand All @@ -140,7 +139,8 @@ WritableState.prototype.getBuffer = function writableStateGetBuffer() {
Object.defineProperty(WritableState.prototype, 'buffer', {
get: util.deprecate(function() {
return this.getBuffer();
}, '_writableState.buffer is deprecated. Use _writableState.getBuffer() instead.')
}, '_writableState.buffer is deprecated. Use ' +
'_writableState.getBuffer() instead.')
});

Choose a reason for hiding this comment

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

This is internal/private/undocumented state, I'm not sure we need to do this deprecation warning as we've never really explained to people how to interact with this.

Copy link
Author

Choose a reason for hiding this comment

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

Cool -- I was mostly concerned with supporting projects like vstream that reach into ReadableState for metrics. I figured that if there's one I know about, there's probably more that I don't know about :)


function Writable(options) {
Expand Down Expand Up @@ -277,7 +277,6 @@ function writeOrBuffer(stream, state, chunk, encoding, cb) {

if (state.writing || state.corked) {
var last = state.lastBufferedRequest;
++state.bufferLength;
state.lastBufferedRequest = new WriteReq(chunk, encoding, cb);
if (last) {
last.next = state.lastBufferedRequest;
Expand Down Expand Up @@ -377,10 +376,9 @@ function onwriteDrain(stream, state) {
// if there's something in the buffer waiting, then process it
function clearBuffer(stream, state) {
state.bufferProcessing = true;
var bufferLength = state.bufferLength;
var entry = state.bufferedRequest;

if (stream._writev && bufferLength > 1) {
if (stream._writev && entry && entry.next) {
// Fast case, write everything using _writev()
var buffer = [];
var cbs = [];
Expand All @@ -393,7 +391,7 @@ function clearBuffer(stream, state) {
// count the one we are adding, as well.
// TODO(isaacs) clean this up
state.pendingcb++;
bufferLength = 0;
state.lastBufferedRequest = null;
doWrite(stream, state, true, state.length, buffer, '', function(err) {
for (var i = 0; i < cbs.length; i++) {
state.pendingcb--;
Expand All @@ -405,7 +403,6 @@ function clearBuffer(stream, state) {
} else {
// Slow case, write chunks one-by-one
while (entry) {
--bufferLength;
var chunk = entry.chunk;
var encoding = entry.encoding;
var cb = entry.callback;
Expand All @@ -421,12 +418,11 @@ function clearBuffer(stream, state) {
break;
}
}

if (entry === null)
state.lastBufferedRequest = null;
}
state.bufferedRequest = entry;
if (!bufferLength) {
state.lastBufferedRequest = null;
}
state.bufferLength = bufferLength;
state.bufferProcessing = false;
}

Expand Down