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: https://github.com/nodejs/node/issues/445
PR-URL: https://github.com/nodejs/node/pull/12857
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Calvin Metcalf 2017-05-05 14:42:21 +02:00 committed by Matteo Collina
parent 0a28f94df6
commit 36ffa21af2
16 changed files with 75 additions and 17 deletions

View File

@ -441,10 +441,18 @@ See also: [`writable.cork()`][].
<!-- YAML
added: v9.3.0
-->
Return the value of `highWaterMark` passed when constructing this
`Writable`.
##### writable.writableLength
<!-- YAML
added: REPLACEME
-->
This property contains the number of bytes (or objects) in the queue
ready to be written. The value provides introspection data regarding
the status of the `highWaterMark`.
##### writable.write(chunk[, encoding][, callback])
<!-- YAML
added: v0.9.4
@ -945,6 +953,15 @@ event will also be emitted.
*Note*: Calling [`stream.read([size])`][stream-read] after the [`'end'`][]
event has been emitted will return `null`. No runtime error will be raised.
##### readable.readableLength
<!-- YAML
added: REPLACEME
-->
This property contains the number of bytes (or objects) in the queue
ready to be read. The value provides introspection data regarding
the status of the `highWaterMark`.
##### readable.resume()
<!-- YAML
added: v0.9.4

View File

@ -69,7 +69,7 @@ Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', {
// because otherwise some prototype manipulation in
// userland will fail
enumerable: false,
get: function() {
get() {
return this._writableState.highWaterMark;
}
});
@ -84,6 +84,16 @@ Object.defineProperty(Duplex.prototype, 'writableBuffer', {
}
});
Object.defineProperty(Duplex.prototype, 'writableLength', {
// making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
enumerable: false,
get() {
return this._writableState.length;
}
});
// the no-half-open enforcer
function onend() {
// if we allow half-open state, or if the writable side ended,
@ -101,6 +111,10 @@ function onEndNT(self) {
}
Object.defineProperty(Duplex.prototype, 'destroyed', {
// making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
enumerable: false,
get() {
if (this._readableState === undefined ||
this._writableState === undefined) {

View File

@ -159,6 +159,10 @@ function Readable(options) {
}
Object.defineProperty(Readable.prototype, 'destroyed', {
// making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
enumerable: false,
get() {
if (this._readableState === undefined) {
return false;
@ -953,6 +957,16 @@ Object.defineProperty(Readable.prototype, 'readableFlowing', {
// exposed for testing purposes only.
Readable._fromList = fromList;
Object.defineProperty(Readable.prototype, 'readableLength', {
// making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
enumerable: false,
get() {
return this._readableState.length;
}
});
// Pluck off n bytes from an array of buffers.
// Length is the combined lengths of all the buffers in the list.
// This function is designed to be inlinable, so please take care when making

View File

@ -583,6 +583,15 @@ Writable.prototype.end = function(chunk, encoding, cb) {
endWritable(this, state, cb);
};
Object.defineProperty(Writable.prototype, 'writableLength', {
// making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
enumerable: false,
get() {
return this._writableState.length;
}
});
function needFinish(state) {
return (state.ending &&
@ -657,6 +666,10 @@ function onCorkedFinish(corkReq, state, err) {
}
Object.defineProperty(Writable.prototype, 'destroyed', {
// making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
// userland will fail
enumerable: false,
get() {
if (this._writableState === undefined) {
return false;

View File

@ -531,7 +531,7 @@ function CleartextStream(pair, options) {
self._reading = false;
},
readStart: function() {
if (self._reading && self._readableState.length > 0) return;
if (self._reading && self.readableLength > 0) return;
self._reading = true;
self.read(0);
if (self._opposite.readable) self._opposite.read(0);

View File

@ -267,7 +267,7 @@ function initRead(tls, wrapped) {
return;
// Socket already has some buffered data - emulate receiving it
if (wrapped && wrapped._readableState && wrapped._readableState.length) {
if (wrapped && wrapped.readableLength) {
var buf;
while ((buf = wrapped.read()) !== null)
tls._handle.receive(buf);

View File

@ -473,7 +473,7 @@ Object.defineProperty(Socket.prototype, 'readyState', {
Object.defineProperty(Socket.prototype, 'bufferSize', {
get: function() {
if (this._handle) {
return this._handle.writeQueueSize + this._writableState.length;
return this._handle.writeQueueSize + this.writableLength;
}
}
});
@ -519,7 +519,7 @@ function maybeDestroy(socket) {
!socket.writable &&
!socket.destroyed &&
!socket.connecting &&
!socket._writableState.length) {
!socket.writableLength) {
socket.destroy();
}
}
@ -627,7 +627,7 @@ function onread(nread, buffer) {
// `end` -> `close`
self.push(null);
if (self._readableState.length === 0) {
if (self.readableLength === 0) {
self.readable = false;
maybeDestroy(self);
}

View File

@ -57,7 +57,7 @@ function httpsTest() {
const test = common.mustCall(function(res) {
res.on('end', common.mustCall(function() {
assert.strictEqual(res._readableState.length, 0);
assert.strictEqual(res.readableLength, 0);
assert.strictEqual(bytes, data.length);
}));

View File

@ -70,7 +70,7 @@ process.on('exit', function(code) {
// we pushed up the high water mark
assert.strictEqual(stream.readableHighWaterMark, 8192);
// length is 0 right now, because we pulled it all out.
assert.strictEqual(stream._readableState.length, 0);
assert.strictEqual(stream.readableLength, 0);
assert(!code);
assert.strictEqual(depth, 0);
console.log('ok');

View File

@ -48,7 +48,7 @@ stream.on('end', function() {
source.on('data', function(chunk) {
const ret = stream.push(chunk);
console.error('data', stream._readableState.length);
console.error('data', stream.readableLength);
if (!ret)
readStop();
});

View File

@ -36,8 +36,8 @@ r._read = function(n) {
};
r.on('readable', function onReadable() {
if (!(r._readableState.length % 256))
console.error('readable', r._readableState.length);
if (!(r.readableLength % 256))
console.error('readable', r.readableLength);
r.read(N * 2);
});

View File

@ -43,7 +43,7 @@ const Transform = require('_stream_transform');
}
tx.end();
assert.strictEqual(tx._readableState.length, 10);
assert.strictEqual(tx.readableLength, 10);
assert.strictEqual(transformed, 10);
assert.strictEqual(tx._transformState.writechunk.length, 5);
assert.deepStrictEqual(tx.writableBuffer.map(function(c) {

View File

@ -68,6 +68,6 @@ console.error(src._readableState);
process.on('exit', function() {
src.readableBuffer.length = 0;
console.error(src._readableState);
assert(src._readableState.length >= src.readableHighWaterMark);
assert(src.readableLength >= src.readableHighWaterMark);
console.log('ok');
});

View File

@ -110,7 +110,7 @@ for (let i = 0; i < chunks.length; i++) {
} while (ret !== false && i < chunks.length);
if (i < chunks.length) {
assert(tw._writableState.length >= 50);
assert(tw.writableLength >= 50);
tw.once('drain', W);
} else {
tw.end();

View File

@ -54,7 +54,7 @@ server.listen(common.PORT, function() {
setTimeout(function() {
// Read buffer should be somewhere near high watermark
// (i.e. should not leak)
assert(res._readableState.length < 100 * 1024);
assert(res.readableLength < 100 * 1024);
process.exit(0);
}, 2000);
});

View File

@ -40,7 +40,7 @@ const server = net.createServer(function(connection) {
assert.strictEqual(false, connection.write(body.slice(2 * part_N, N)));
console.log(`bufferSize: ${connection.bufferSize}`, 'expecting', N);
assert.ok(0 <= connection.bufferSize &&
connection._writableState.length <= N);
connection.writableLength <= N);
connection.end();
});