Skip to content

Commit c5c4a53

Browse files
calvinmetcalfMylesBorins
authored andcommitted
stream: rm {writeable/readable}State.length
As part of the readableState/writableState mega issue #445, this removes all of the references to .length on those properties and replaces them with a readableLength and writableLength getter. See: #445 PR-URL: #12857 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent d9190c1 commit c5c4a53

16 files changed

+75
-17
lines changed

doc/api/stream.md

+18-1
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,18 @@ See also: [`writable.cork()`][].
441441
<!-- YAML
442442
added: v9.3.0
443443
-->
444-
445444
Return the value of `highWaterMark` passed when constructing this
446445
`Writable`.
447446

447+
##### writable.writableLength
448+
<!-- YAML
449+
added: REPLACEME
450+
-->
451+
452+
This property contains the number of bytes (or objects) in the queue
453+
ready to be written. The value provides introspection data regarding
454+
the status of the `highWaterMark`.
455+
448456
##### writable.write(chunk[, encoding][, callback])
449457
<!-- YAML
450458
added: v0.9.4
@@ -945,6 +953,15 @@ event will also be emitted.
945953
*Note*: Calling [`stream.read([size])`][stream-read] after the [`'end'`][]
946954
event has been emitted will return `null`. No runtime error will be raised.
947955

956+
##### readable.readableLength
957+
<!-- YAML
958+
added: REPLACEME
959+
-->
960+
961+
This property contains the number of bytes (or objects) in the queue
962+
ready to be read. The value provides introspection data regarding
963+
the status of the `highWaterMark`.
964+
948965
##### readable.resume()
949966
<!-- YAML
950967
added: v0.9.4

lib/_stream_duplex.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', {
6969
// because otherwise some prototype manipulation in
7070
// userland will fail
7171
enumerable: false,
72-
get: function() {
72+
get() {
7373
return this._writableState.highWaterMark;
7474
}
7575
});
@@ -84,6 +84,16 @@ Object.defineProperty(Duplex.prototype, 'writableBuffer', {
8484
}
8585
});
8686

87+
Object.defineProperty(Duplex.prototype, 'writableLength', {
88+
// making it explicit this property is not enumerable
89+
// because otherwise some prototype manipulation in
90+
// userland will fail
91+
enumerable: false,
92+
get() {
93+
return this._writableState.length;
94+
}
95+
});
96+
8797
// the no-half-open enforcer
8898
function onend() {
8999
// if we allow half-open state, or if the writable side ended,
@@ -101,6 +111,10 @@ function onEndNT(self) {
101111
}
102112

103113
Object.defineProperty(Duplex.prototype, 'destroyed', {
114+
// making it explicit this property is not enumerable
115+
// because otherwise some prototype manipulation in
116+
// userland will fail
117+
enumerable: false,
104118
get() {
105119
if (this._readableState === undefined ||
106120
this._writableState === undefined) {

lib/_stream_readable.js

+14
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,10 @@ function Readable(options) {
159159
}
160160

161161
Object.defineProperty(Readable.prototype, 'destroyed', {
162+
// making it explicit this property is not enumerable
163+
// because otherwise some prototype manipulation in
164+
// userland will fail
165+
enumerable: false,
162166
get() {
163167
if (this._readableState === undefined) {
164168
return false;
@@ -953,6 +957,16 @@ Object.defineProperty(Readable.prototype, 'readableFlowing', {
953957
// exposed for testing purposes only.
954958
Readable._fromList = fromList;
955959

960+
Object.defineProperty(Readable.prototype, 'readableLength', {
961+
// making it explicit this property is not enumerable
962+
// because otherwise some prototype manipulation in
963+
// userland will fail
964+
enumerable: false,
965+
get() {
966+
return this._readableState.length;
967+
}
968+
});
969+
956970
// Pluck off n bytes from an array of buffers.
957971
// Length is the combined lengths of all the buffers in the list.
958972
// This function is designed to be inlinable, so please take care when making

lib/_stream_writable.js

+13
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,15 @@ Writable.prototype.end = function(chunk, encoding, cb) {
583583
endWritable(this, state, cb);
584584
};
585585

586+
Object.defineProperty(Writable.prototype, 'writableLength', {
587+
// making it explicit this property is not enumerable
588+
// because otherwise some prototype manipulation in
589+
// userland will fail
590+
enumerable: false,
591+
get() {
592+
return this._writableState.length;
593+
}
594+
});
586595

587596
function needFinish(state) {
588597
return (state.ending &&
@@ -657,6 +666,10 @@ function onCorkedFinish(corkReq, state, err) {
657666
}
658667

659668
Object.defineProperty(Writable.prototype, 'destroyed', {
669+
// making it explicit this property is not enumerable
670+
// because otherwise some prototype manipulation in
671+
// userland will fail
672+
enumerable: false,
660673
get() {
661674
if (this._writableState === undefined) {
662675
return false;

lib/_tls_legacy.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -531,7 +531,7 @@ function CleartextStream(pair, options) {
531531
self._reading = false;
532532
},
533533
readStart: function() {
534-
if (self._reading && self._readableState.length > 0) return;
534+
if (self._reading && self.readableLength > 0) return;
535535
self._reading = true;
536536
self.read(0);
537537
if (self._opposite.readable) self._opposite.read(0);

lib/_tls_wrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ function initRead(tls, wrapped) {
267267
return;
268268

269269
// Socket already has some buffered data - emulate receiving it
270-
if (wrapped && wrapped._readableState && wrapped._readableState.length) {
270+
if (wrapped && wrapped.readableLength) {
271271
var buf;
272272
while ((buf = wrapped.read()) !== null)
273273
tls._handle.receive(buf);

lib/net.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ Object.defineProperty(Socket.prototype, 'readyState', {
473473
Object.defineProperty(Socket.prototype, 'bufferSize', {
474474
get: function() {
475475
if (this._handle) {
476-
return this._handle.writeQueueSize + this._writableState.length;
476+
return this._handle.writeQueueSize + this.writableLength;
477477
}
478478
}
479479
});
@@ -519,7 +519,7 @@ function maybeDestroy(socket) {
519519
!socket.writable &&
520520
!socket.destroyed &&
521521
!socket.connecting &&
522-
!socket._writableState.length) {
522+
!socket.writableLength) {
523523
socket.destroy();
524524
}
525525
}
@@ -627,7 +627,7 @@ function onread(nread, buffer) {
627627
// `end` -> `close`
628628
self.push(null);
629629

630-
if (self._readableState.length === 0) {
630+
if (self.readableLength === 0) {
631631
self.readable = false;
632632
maybeDestroy(self);
633633
}

test/parallel/test-https-truncate.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function httpsTest() {
5757

5858
const test = common.mustCall(function(res) {
5959
res.on('end', common.mustCall(function() {
60-
assert.strictEqual(res._readableState.length, 0);
60+
assert.strictEqual(res.readableLength, 0);
6161
assert.strictEqual(bytes, data.length);
6262
}));
6363

test/parallel/test-stream-readable-flow-recursion.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ process.on('exit', function(code) {
7070
// we pushed up the high water mark
7171
assert.strictEqual(stream.readableHighWaterMark, 8192);
7272
// length is 0 right now, because we pulled it all out.
73-
assert.strictEqual(stream._readableState.length, 0);
73+
assert.strictEqual(stream.readableLength, 0);
7474
assert(!code);
7575
assert.strictEqual(depth, 0);
7676
console.log('ok');

test/parallel/test-stream2-push.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ stream.on('end', function() {
4848

4949
source.on('data', function(chunk) {
5050
const ret = stream.push(chunk);
51-
console.error('data', stream._readableState.length);
51+
console.error('data', stream.readableLength);
5252
if (!ret)
5353
readStop();
5454
});

test/parallel/test-stream2-read-sync-stack.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ r._read = function(n) {
3636
};
3737

3838
r.on('readable', function onReadable() {
39-
if (!(r._readableState.length % 256))
40-
console.error('readable', r._readableState.length);
39+
if (!(r.readableLength % 256))
40+
console.error('readable', r.readableLength);
4141
r.read(N * 2);
4242
});
4343

test/parallel/test-stream2-transform.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const Transform = require('_stream_transform');
4343
}
4444
tx.end();
4545

46-
assert.strictEqual(tx._readableState.length, 10);
46+
assert.strictEqual(tx.readableLength, 10);
4747
assert.strictEqual(transformed, 10);
4848
assert.strictEqual(tx._transformState.writechunk.length, 5);
4949
assert.deepStrictEqual(tx.writableBuffer.map(function(c) {

test/parallel/test-stream2-unpipe-leak.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ console.error(src._readableState);
6868
process.on('exit', function() {
6969
src.readableBuffer.length = 0;
7070
console.error(src._readableState);
71-
assert(src._readableState.length >= src.readableHighWaterMark);
71+
assert(src.readableLength >= src.readableHighWaterMark);
7272
console.log('ok');
7373
});

test/parallel/test-stream2-writable.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ for (let i = 0; i < chunks.length; i++) {
110110
} while (ret !== false && i < chunks.length);
111111

112112
if (i < chunks.length) {
113-
assert(tw._writableState.length >= 50);
113+
assert(tw.writableLength >= 50);
114114
tw.once('drain', W);
115115
} else {
116116
tw.end();

test/pummel/test-https-no-reader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ server.listen(common.PORT, function() {
5454
setTimeout(function() {
5555
// Read buffer should be somewhere near high watermark
5656
// (i.e. should not leak)
57-
assert(res._readableState.length < 100 * 1024);
57+
assert(res.readableLength < 100 * 1024);
5858
process.exit(0);
5959
}, 2000);
6060
});

test/pummel/test-net-throttle.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const server = net.createServer(function(connection) {
4040
assert.strictEqual(false, connection.write(body.slice(2 * part_N, N)));
4141
console.log(`bufferSize: ${connection.bufferSize}`, 'expecting', N);
4242
assert.ok(0 <= connection.bufferSize &&
43-
connection._writableState.length <= N);
43+
connection.writableLength <= N);
4444
connection.end();
4545
});
4646

0 commit comments

Comments
 (0)