Skip to content

Commit de11913

Browse files
ronagaddaleax
authored andcommitted
stream: add writableCorked property
PR-URL: #29012 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 0747300 commit de11913

File tree

4 files changed

+46
-8
lines changed

4 files changed

+46
-8
lines changed

doc/api/stream.md

+11
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,16 @@ Is `true` after [`writable.end()`][] has been called. This property
505505
does not indicate whether the data has been flushed, for this use
506506
[`writable.writableFinished`][] instead.
507507

508+
##### writable.writableCorked
509+
<!-- YAML
510+
added: REPLACEME
511+
-->
512+
513+
* {integer}
514+
515+
Number of times [`writable.uncork()`][stream-uncork] needs to be
516+
called in order to fully uncork the stream.
517+
508518
##### writable.writableFinished
509519
<!-- YAML
510520
added: v12.6.0
@@ -2838,6 +2848,7 @@ contain multi-byte characters.
28382848
[stream-push]: #stream_readable_push_chunk_encoding
28392849
[stream-read]: #stream_readable_read_size
28402850
[stream-resume]: #stream_readable_resume
2851+
[stream-uncork]: #stream_writable_uncork
28412852
[stream-write]: #stream_writable_write_chunk_encoding_callback
28422853
[Stream Three States]: #stream_three_states
28432854
[writable-_destroy]: #stream_writable_destroy_err_callback

lib/_http_outgoing.js

+3-8
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,6 @@ const { validateString } = require('internal/validators');
5656
const HIGH_WATER_MARK = getDefaultHighWaterMark();
5757
const { CRLF, debug } = common;
5858

59-
const kIsCorked = Symbol('isCorked');
60-
6159
const RE_CONN_CLOSE = /(?:^|\W)close(?:$|\W)/i;
6260
const RE_TE_CHUNKED = common.chunkExpression;
6361

@@ -101,7 +99,6 @@ function OutgoingMessage() {
10199

102100
this.finished = false;
103101
this._headerSent = false;
104-
this[kIsCorked] = false;
105102

106103
this.socket = null;
107104
this._header = null;
@@ -628,10 +625,9 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
628625
['string', 'Buffer'], chunk);
629626
}
630627

631-
if (!fromEnd && msg.socket && !msg[kIsCorked]) {
628+
if (!fromEnd && msg.socket && !msg.socket.writableCorked) {
632629
msg.socket.cork();
633-
msg[kIsCorked] = true;
634-
process.nextTick(connectionCorkNT, msg, msg.socket);
630+
process.nextTick(connectionCorkNT, msg.socket);
635631
}
636632

637633
var len, ret;
@@ -660,8 +656,7 @@ function writeAfterEndNT(msg, err, callback) {
660656
}
661657

662658

663-
function connectionCorkNT(msg, conn) {
664-
msg[kIsCorked] = false;
659+
function connectionCorkNT(conn) {
665660
conn.uncork();
666661
}
667662

lib/_stream_writable.js

+10
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,16 @@ Object.defineProperty(Writable.prototype, 'writableHighWaterMark', {
385385
}
386386
});
387387

388+
Object.defineProperty(Writable.prototype, 'writableCorked', {
389+
// Making it explicit this property is not enumerable
390+
// because otherwise some prototype manipulation in
391+
// userland will fail
392+
enumerable: false,
393+
get: function() {
394+
return this._writableState ? this._writableState.corked : 0;
395+
}
396+
});
397+
388398
// If we're already writing something, then just put this
389399
// in the queue, and wait our turn. Otherwise, call _write
390400
// If we return false, then we need a drain event, so set that flag.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
5+
const { Writable } = require('stream');
6+
7+
{
8+
const w = new Writable();
9+
assert.strictEqual(w.writableCorked, 0);
10+
w.uncork();
11+
assert.strictEqual(w.writableCorked, 0);
12+
w.cork();
13+
assert.strictEqual(w.writableCorked, 1);
14+
w.cork();
15+
assert.strictEqual(w.writableCorked, 2);
16+
w.uncork();
17+
assert.strictEqual(w.writableCorked, 1);
18+
w.uncork();
19+
assert.strictEqual(w.writableCorked, 0);
20+
w.uncork();
21+
assert.strictEqual(w.writableCorked, 0);
22+
}

0 commit comments

Comments
 (0)