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 authored and Matteo Collina committed 2017-12-18 14:41:43 +01:00
1 parent 0a28f94df6
commit 36ffa21af2
16 files changed
+75 -17

No files matched your search

+18 -1
View File
@@ -441,10 +441,18 @@ See also: [`writable.cork()`][].
<!-- YAML <!-- YAML
added: v9.3.0 added: v9.3.0
--> -->
Return the value of `highWaterMark` passed when constructing this Return the value of `highWaterMark` passed when constructing this
`Writable`. `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]) ##### writable.write(chunk[, encoding][, callback])
<!-- YAML <!-- YAML
added: v0.9.4 added: v0.9.4
@@ -945,6 +953,15 @@ event will also be emitted.
*Note*: Calling [`stream.read([size])`][stream-read] after the [`'end'`][] *Note*: Calling [`stream.read([size])`][stream-read] after the [`'end'`][]
event has been emitted will return `null`. No runtime error will be raised. 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() ##### readable.resume()
<!-- YAML <!-- YAML
added: v0.9.4 added: v0.9.4
+15 -1
View File
@@ -69,7 +69,7 @@ Object.defineProperty(Duplex.prototype, 'writableHighWaterMark', {
// because otherwise some prototype manipulation in // because otherwise some prototype manipulation in
// userland will fail // userland will fail
enumerable: false, enumerable: false,
get: function() { get() {
return this._writableState.highWaterMark; 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 // the no-half-open enforcer
function onend() { function onend() {
// if we allow half-open state, or if the writable side ended, // if we allow half-open state, or if the writable side ended,
@@ -101,6 +111,10 @@ function onEndNT(self) {
} }
Object.defineProperty(Duplex.prototype, 'destroyed', { 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() { get() {
if (this._readableState === undefined || if (this._readableState === undefined ||
this._writableState === undefined) { this._writableState === undefined) {
+14
View File
@@ -159,6 +159,10 @@ function Readable(options) {
} }
Object.defineProperty(Readable.prototype, 'destroyed', { 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() { get() {
if (this._readableState === undefined) { if (this._readableState === undefined) {
return false; return false;
@@ -953,6 +957,16 @@ Object.defineProperty(Readable.prototype, 'readableFlowing', {
// exposed for testing purposes only. // exposed for testing purposes only.
Readable._fromList = fromList; 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. // Pluck off n bytes from an array of buffers.
// Length is the combined lengths of all the buffers in the list. // 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 // This function is designed to be inlinable, so please take care when making
+13
View File
@@ -583,6 +583,15 @@ Writable.prototype.end = function(chunk, encoding, cb) {
endWritable(this, state, 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) { function needFinish(state) {
return (state.ending && return (state.ending &&
@@ -657,6 +666,10 @@ function onCorkedFinish(corkReq, state, err) {
} }
Object.defineProperty(Writable.prototype, 'destroyed', { 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() { get() {
if (this._writableState === undefined) { if (this._writableState === undefined) {
return false; return false;
+1 -1
View File
@@ -531,7 +531,7 @@ function CleartextStream(pair, options) {
self._reading = false; self._reading = false;
}, },
readStart: function() { readStart: function() {
if (self._reading && self._readableState.length > 0) return; if (self._reading && self.readableLength > 0) return;
self._reading = true; self._reading = true;
self.read(0); self.read(0);
if (self._opposite.readable) self._opposite.read(0); if (self._opposite.readable) self._opposite.read(0);
+1 -1
View File
@@ -267,7 +267,7 @@ function initRead(tls, wrapped) {
return; return;
// Socket already has some buffered data - emulate receiving it // Socket already has some buffered data - emulate receiving it
if (wrapped && wrapped._readableState && wrapped._readableState.length) { if (wrapped && wrapped.readableLength) {
var buf; var buf;
while ((buf = wrapped.read()) !== null) while ((buf = wrapped.read()) !== null)
tls._handle.receive(buf); tls._handle.receive(buf);
+3 -3
View File
@@ -473,7 +473,7 @@ Object.defineProperty(Socket.prototype, 'readyState', {
Object.defineProperty(Socket.prototype, 'bufferSize', { Object.defineProperty(Socket.prototype, 'bufferSize', {
get: function() { get: function() {
if (this._handle) { 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.writable &&
!socket.destroyed && !socket.destroyed &&
!socket.connecting && !socket.connecting &&
!socket._writableState.length) { !socket.writableLength) {
socket.destroy(); socket.destroy();
} }
} }
@@ -627,7 +627,7 @@ function onread(nread, buffer) {
// `end` -> `close` // `end` -> `close`
self.push(null); self.push(null);
if (self._readableState.length === 0) { if (self.readableLength === 0) {
self.readable = false; self.readable = false;
maybeDestroy(self); maybeDestroy(self);
} }
+1 -1
View File
@@ -57,7 +57,7 @@ function httpsTest() {
const test = common.mustCall(function(res) { const test = common.mustCall(function(res) {
res.on('end', common.mustCall(function() { res.on('end', common.mustCall(function() {
assert.strictEqual(res._readableState.length, 0); assert.strictEqual(res.readableLength, 0);
assert.strictEqual(bytes, data.length); assert.strictEqual(bytes, data.length);
})); }));
@@ -70,7 +70,7 @@ process.on('exit', function(code) {
// we pushed up the high water mark // we pushed up the high water mark
assert.strictEqual(stream.readableHighWaterMark, 8192); assert.strictEqual(stream.readableHighWaterMark, 8192);
// length is 0 right now, because we pulled it all out. // 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(!code);
assert.strictEqual(depth, 0); assert.strictEqual(depth, 0);
console.log('ok'); console.log('ok');
+1 -1
View File
@@ -48,7 +48,7 @@ stream.on('end', function() {
source.on('data', function(chunk) { source.on('data', function(chunk) {
const ret = stream.push(chunk); const ret = stream.push(chunk);
console.error('data', stream._readableState.length); console.error('data', stream.readableLength);
if (!ret) if (!ret)
readStop(); readStop();
}); });
@@ -36,8 +36,8 @@ r._read = function(n) {
}; };
r.on('readable', function onReadable() { r.on('readable', function onReadable() {
if (!(r._readableState.length % 256)) if (!(r.readableLength % 256))
console.error('readable', r._readableState.length); console.error('readable', r.readableLength);
r.read(N * 2); r.read(N * 2);
}); });
+1 -1
View File
@@ -43,7 +43,7 @@ const Transform = require('_stream_transform');
} }
tx.end(); tx.end();
assert.strictEqual(tx._readableState.length, 10); assert.strictEqual(tx.readableLength, 10);
assert.strictEqual(transformed, 10); assert.strictEqual(transformed, 10);
assert.strictEqual(tx._transformState.writechunk.length, 5); assert.strictEqual(tx._transformState.writechunk.length, 5);
assert.deepStrictEqual(tx.writableBuffer.map(function(c) { assert.deepStrictEqual(tx.writableBuffer.map(function(c) {
+1 -1
View File
@@ -68,6 +68,6 @@ console.error(src._readableState);
process.on('exit', function() { process.on('exit', function() {
src.readableBuffer.length = 0; src.readableBuffer.length = 0;
console.error(src._readableState); console.error(src._readableState);
assert(src._readableState.length >= src.readableHighWaterMark); assert(src.readableLength >= src.readableHighWaterMark);
console.log('ok'); console.log('ok');
}); });
+1 -1
View File
@@ -110,7 +110,7 @@ for (let i = 0; i < chunks.length; i++) {
} while (ret !== false && i < chunks.length); } while (ret !== false && i < chunks.length);
if (i < chunks.length) { if (i < chunks.length) {
assert(tw._writableState.length >= 50); assert(tw.writableLength >= 50);
tw.once('drain', W); tw.once('drain', W);
} else { } else {
tw.end(); tw.end();
+1 -1
View File
@@ -54,7 +54,7 @@ server.listen(common.PORT, function() {
setTimeout(function() { setTimeout(function() {
// Read buffer should be somewhere near high watermark // Read buffer should be somewhere near high watermark
// (i.e. should not leak) // (i.e. should not leak)
assert(res._readableState.length < 100 * 1024); assert(res.readableLength < 100 * 1024);
process.exit(0); process.exit(0);
}, 2000); }, 2000);
}); });
+1 -1
View File
@@ -40,7 +40,7 @@ const server = net.createServer(function(connection) {
assert.strictEqual(false, connection.write(body.slice(2 * part_N, N))); assert.strictEqual(false, connection.write(body.slice(2 * part_N, N)));
console.log(`bufferSize: ${connection.bufferSize}`, 'expecting', N); console.log(`bufferSize: ${connection.bufferSize}`, 'expecting', N);
assert.ok(0 <= connection.bufferSize && assert.ok(0 <= connection.bufferSize &&
connection._writableState.length <= N); connection.writableLength <= N);
connection.end(); connection.end();
}); });