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: writableCorked #29012

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,16 @@ Is `true` after [`writable.end()`][] has been called. This property
does not indicate whether the data has been flushed, for this use
[`writable.writableFinished`][] instead.

##### writable.writableCorked
<!-- YAML
added: REPLACEME
-->

* {Integer}
ronag marked this conversation as resolved.
Show resolved Hide resolved

Number of times [`writable.uncork()`][stream-uncork] needs to be
ronag marked this conversation as resolved.
Show resolved Hide resolved
called in order to fully uncork the stream.
Copy link
Member

Choose a reason for hiding this comment

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

minor nit: an example here would be nice


##### writable.writableFinished
<!-- YAML
added: v12.6.0
Expand Down Expand Up @@ -2759,6 +2769,7 @@ contain multi-byte characters.
[stream-push]: #stream_readable_push_chunk_encoding
[stream-read]: #stream_readable_read_size
[stream-resume]: #stream_readable_resume
[stream-uncork]: #stream_writable_uncork
[stream-write]: #stream_writable_write_chunk_encoding_callback
[writable-_destroy]: #stream_writable_destroy_err_callback
[writable-destroy]: #stream_writable_destroy_error
Expand Down
11 changes: 3 additions & 8 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ const { validateString } = require('internal/validators');
const HIGH_WATER_MARK = getDefaultHighWaterMark();
const { CRLF, debug } = common;

const kIsCorked = Symbol('isCorked');

const RE_CONN_CLOSE = /(?:^|\W)close(?:$|\W)/i;
const RE_TE_CHUNKED = common.chunkExpression;

Expand Down Expand Up @@ -101,7 +99,6 @@ function OutgoingMessage() {

this.finished = false;
this._headerSent = false;
this[kIsCorked] = false;

this.socket = null;
this._header = null;
Expand Down Expand Up @@ -628,10 +625,9 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
['string', 'Buffer'], chunk);
}

if (!fromEnd && msg.socket && !msg[kIsCorked]) {
if (!fromEnd && msg.socket && !msg.socket.writableCorked) {
msg.socket.cork();
msg[kIsCorked] = true;
process.nextTick(connectionCorkNT, msg, msg.socket);
process.nextTick(connectionCorkNT, msg.socket);
}

var len, ret;
Expand Down Expand Up @@ -660,8 +656,7 @@ function writeAfterEndNT(msg, err, callback) {
}


function connectionCorkNT(msg, conn) {
msg[kIsCorked] = false;
function connectionCorkNT(conn) {
conn.uncork();
}

Expand Down
10 changes: 10 additions & 0 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ Object.defineProperty(Writable.prototype, 'writableHighWaterMark', {
}
});

Object.defineProperty(Writable.prototype, 'writableCorked', {
// Making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
enumerable: false,
get: function() {
return this._writableState ? this._writableState.corked : 0;
}
});

// If we're already writing something, then just put this
// in the queue, and wait our turn. Otherwise, call _write
// If we return false, then we need a drain event, so set that flag.
Expand Down
22 changes: 22 additions & 0 deletions test/parallel/test-stream-writable-properties.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict';
require('../common');
const assert = require('assert');
ronag marked this conversation as resolved.
Show resolved Hide resolved

const { Writable } = require('stream');

{
const w = new Writable();
assert.strictEqual(w.writableCorked, 0);
w.uncork();
assert.strictEqual(w.writableCorked, 0);
w.cork();
assert.strictEqual(w.writableCorked, 1);
w.cork();
assert.strictEqual(w.writableCorked, 2);
w.uncork();
assert.strictEqual(w.writableCorked, 1);
w.uncork();
assert.strictEqual(w.writableCorked, 0);
w.uncork();
assert.strictEqual(w.writableCorked, 0);
}