mirror of
https://github.com/nodejs/node.git
synced 2026-11-27 17:52:27 -05:00
stream: add writableCorked property
PR-URL: https://github.com/nodejs/node/pull/29012 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
1 parent
df1e183e3f
commit
af960d7f22
4 files changed
+45
-7
No files matched your search
@@ -502,6 +502,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}
|
||||
|
||||
Number of times [`writable.uncork()`][stream-uncork] needs to be
|
||||
called in order to fully uncork the stream.
|
||||
|
||||
##### `writable.writableFinished`
|
||||
<!-- YAML
|
||||
added: v12.6.0
|
||||
@@ -2842,6 +2852,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
|
||||
[Stream Three States]: #stream_three_states
|
||||
[writable-_destroy]: #stream_writable_destroy_err_callback
|
||||
|
||||
@@ -55,8 +55,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;
|
||||
|
||||
@@ -100,7 +98,6 @@ function OutgoingMessage() {
|
||||
|
||||
this.finished = false;
|
||||
this._headerSent = false;
|
||||
this[kIsCorked] = false;
|
||||
|
||||
this.socket = null;
|
||||
this.connection = null;
|
||||
@@ -619,9 +616,8 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
|
||||
['string', 'Buffer'], chunk);
|
||||
}
|
||||
|
||||
if (!fromEnd && msg.connection && !msg[kIsCorked]) {
|
||||
if (!fromEnd && msg.connection && !msg.connection.writableCorked) {
|
||||
msg.connection.cork();
|
||||
msg[kIsCorked] = true;
|
||||
process.nextTick(connectionCorkNT, msg, msg.connection);
|
||||
}
|
||||
|
||||
@@ -651,8 +647,7 @@ function writeAfterEndNT(msg, err, callback) {
|
||||
}
|
||||
|
||||
|
||||
function connectionCorkNT(msg, conn) {
|
||||
msg[kIsCorked] = false;
|
||||
function connectionCorkNT(conn) {
|
||||
conn.uncork();
|
||||
}
|
||||
|
||||
|
||||
@@ -379,6 +379,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.
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
'use strict';
|
||||
require('../common');
|
||||
const assert = require('assert');
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user