Skip to content

Commit 6a8a33c

Browse files
ronagtargos
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 4c317ce commit 6a8a33c

File tree

4 files changed

+45
-7
lines changed

4 files changed

+45
-7
lines changed

doc/api/stream.md

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

505+
##### `writable.writableCorked`
506+
<!-- YAML
507+
added: REPLACEME
508+
-->
509+
510+
* {integer}
511+
512+
Number of times [`writable.uncork()`][stream-uncork] needs to be
513+
called in order to fully uncork the stream.
514+
505515
##### `writable.writableFinished`
506516
<!-- YAML
507517
added: v12.6.0
@@ -2842,6 +2852,7 @@ contain multi-byte characters.
28422852
[stream-push]: #stream_readable_push_chunk_encoding
28432853
[stream-read]: #stream_readable_read_size
28442854
[stream-resume]: #stream_readable_resume
2855+
[stream-uncork]: #stream_writable_uncork
28452856
[stream-write]: #stream_writable_write_chunk_encoding_callback
28462857
[Stream Three States]: #stream_three_states
28472858
[writable-_destroy]: #stream_writable_destroy_err_callback

lib/_http_outgoing.js

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

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

@@ -100,7 +98,6 @@ function OutgoingMessage() {
10098

10199
this.finished = false;
102100
this._headerSent = false;
103-
this[kIsCorked] = false;
104101

105102
this.socket = null;
106103
this.connection = null;
@@ -619,9 +616,8 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
619616
['string', 'Buffer'], chunk);
620617
}
621618

622-
if (!fromEnd && msg.connection && !msg[kIsCorked]) {
619+
if (!fromEnd && msg.connection && !msg.connection.writableCorked) {
623620
msg.connection.cork();
624-
msg[kIsCorked] = true;
625621
process.nextTick(connectionCorkNT, msg, msg.connection);
626622
}
627623

@@ -651,8 +647,7 @@ function writeAfterEndNT(msg, err, callback) {
651647
}
652648

653649

654-
function connectionCorkNT(msg, conn) {
655-
msg[kIsCorked] = false;
650+
function connectionCorkNT(conn) {
656651
conn.uncork();
657652
}
658653

lib/_stream_writable.js

+10
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,16 @@ Object.defineProperty(Writable.prototype, 'writableHighWaterMark', {
379379
}
380380
});
381381

382+
Object.defineProperty(Writable.prototype, 'writableCorked', {
383+
// Making it explicit this property is not enumerable
384+
// because otherwise some prototype manipulation in
385+
// userland will fail
386+
enumerable: false,
387+
get: function() {
388+
return this._writableState ? this._writableState.corked : 0;
389+
}
390+
});
391+
382392
// If we're already writing something, then just put this
383393
// in the queue, and wait our turn. Otherwise, call _write
384394
// 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)