Skip to content

Commit

Permalink
stream: make readable & writable computed
Browse files Browse the repository at this point in the history
This makes readable and writable automatically computed based
on the stream state.

Effectivly deprecating/discouraging manual management of this.

Makes the properties  more consistent and easier to reason about.

Fixes: #29377

PR-URL: #31197
Refs: #29377
Reviewed-By: Ruben Bridgewater <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
ronag committed Feb 8, 2020
1 parent 9c753b3 commit e559842
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 15 deletions.
6 changes: 4 additions & 2 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,8 @@ added: v11.4.0

* {boolean}

Is `true` if it is safe to call [`writable.write()`][stream-write].
Is `true` if it is safe to call [`writable.write()`][stream-write], which means
the stream has not been destroyed, errored or ended.

##### `writable.writableEnded`
<!-- YAML
Expand Down Expand Up @@ -1134,7 +1135,8 @@ added: v11.4.0

* {boolean}

Is `true` if it is safe to call [`readable.read()`][stream-read].
Is `true` if it is safe to call [`readable.read()`][stream-read], which means
the stream has not been destroyed or emitted `'error'` or `'end'`.

##### `readable.readableEncoding`
<!-- YAML
Expand Down
2 changes: 0 additions & 2 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ function IncomingMessage(socket) {
this.trailers = {};
this.rawTrailers = [];

this.readable = true;

this.aborted = false;

this.upgrade = null;
Expand Down
2 changes: 2 additions & 0 deletions lib/_stream_duplex.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

const {
ObjectDefineProperties,
ObjectGetOwnPropertyDescriptor,
ObjectKeys,
ObjectSetPrototypeOf,
} = primordials;
Expand Down Expand Up @@ -71,6 +72,7 @@ function Duplex(options) {
}

ObjectDefineProperties(Duplex.prototype, {
writable: ObjectGetOwnPropertyDescriptor(Writable.prototype, 'writable'),

destroyed: {
get() {
Expand Down
19 changes: 15 additions & 4 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

const {
ArrayIsArray,
Boolean,
NumberIsInteger,
NumberIsNaN,
ObjectDefineProperties,
Expand Down Expand Up @@ -181,9 +182,6 @@ function Readable(options) {

this._readableState = new ReadableState(options, this, isDuplex);

// legacy
this.readable = true;

if (options) {
if (typeof options.read === 'function')
this._read = options.read;
Expand Down Expand Up @@ -1057,6 +1055,20 @@ Readable.prototype[SymbolAsyncIterator] = function() {
// because otherwise some prototype manipulation in
// userland will fail
ObjectDefineProperties(Readable.prototype, {
readable: {
get() {
const r = this._readableState;
if (!r) return false;
if (r.readable !== undefined) return r.readable && !r.endEmitted;
return Boolean(!r.destroyed && !r.errorEmitted && !r.endEmitted);
},
set(val) {
// Backwards compat.
if (this._readableState) {
this._readableState.readable = !!val;
}
}
},

readableHighWaterMark: {
enumerable: false,
Expand Down Expand Up @@ -1198,7 +1210,6 @@ function endReadableNT(state, stream) {
// Check that we didn't get one last unshift.
if (!state.errorEmitted && !state.endEmitted && state.length === 0) {
state.endEmitted = true;
stream.readable = false;
stream.emit('end');

if (state.autoDestroy) {
Expand Down
19 changes: 16 additions & 3 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

const {
Array,
Boolean,
FunctionPrototype,
ObjectDefineProperty,
ObjectSetPrototypeOf,
Expand Down Expand Up @@ -233,9 +234,6 @@ function Writable(options) {

this._writableState = new WritableState(options, this, isDuplex);

// legacy.
this.writable = true;

if (options) {
if (typeof options.write === 'function')
this._write = options.write;
Expand Down Expand Up @@ -776,6 +774,21 @@ function onFinished(stream, state, cb) {
stream.prependListener('error', onerror);
}

ObjectDefineProperty(Writable.prototype, 'writable', {
get() {
const w = this._writableState;
if (!w) return false;
if (w.writable !== undefined) return w.writable;
return Boolean(!w.destroyed && !w.errored && !w.ending);
},
set(val) {
// Backwards compat.
if (this._writableState) {
this._writableState.writable = !!val;
}
}
});

ObjectDefineProperty(Writable.prototype, 'destroyed', {
// Making it explicit this property is not enumerable
// because otherwise some prototype manipulation in
Expand Down
12 changes: 8 additions & 4 deletions test/parallel/test-http2-socket-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,14 @@ server.on('stream', common.mustCall(function(stream, headers) {

stream.respond();

socket.writable = 0;
socket.readable = 0;
assert.strictEqual(socket.writable, 0);
assert.strictEqual(socket.readable, 0);
socket.writable = true;
socket.readable = true;
assert.strictEqual(socket.writable, true);
assert.strictEqual(socket.readable, true);
socket.writable = false;
socket.readable = false;
assert.strictEqual(socket.writable, false);
assert.strictEqual(socket.readable, false);

stream.end();

Expand Down
45 changes: 45 additions & 0 deletions test/parallel/test-stream-readable-readable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
'use strict';
const common = require('../common');
const assert = require('assert');

const { Readable } = require('stream');

{
const r = new Readable({
read() {}
});
assert.strictEqual(r.readable, true);
r.destroy();
assert.strictEqual(r.readable, false);
}

{
const mustNotCall = common.mustNotCall();
const r = new Readable({
read() {}
});
assert.strictEqual(r.readable, true);
r.on('end', mustNotCall);
r.resume();
r.push(null);
assert.strictEqual(r.readable, true);
r.off('end', mustNotCall);
r.on('end', common.mustCall(() => {
assert.strictEqual(r.readable, false);
}));
}

{
const r = new Readable({
read: common.mustCall(() => {
process.nextTick(() => {
r.destroy(new Error());
assert.strictEqual(r.readable, false);
});
})
});
r.resume();
r.on('error', common.mustCall(() => {
assert.strictEqual(r.readable, false);
}));
}
49 changes: 49 additions & 0 deletions test/parallel/test-stream-writable-writable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'use strict';
const common = require('../common');
const assert = require('assert');

const { Writable } = require('stream');

{
const w = new Writable({
write() {}
});
assert.strictEqual(w.writable, true);
w.destroy();
assert.strictEqual(w.writable, false);
}

{
const w = new Writable({
write: common.mustCall((chunk, encoding, callback) => {
callback(new Error());
})
});
assert.strictEqual(w.writable, true);
w.write('asd');
assert.strictEqual(w.writable, false);
w.on('error', common.mustCall());
w.destroy();
}

{
const w = new Writable({
write: common.mustCall((chunk, encoding, callback) => {
process.nextTick(() => {
callback(new Error());
assert.strictEqual(w.writable, false);
});
})
});
w.write('asd');
w.on('error', common.mustCall());
}

{
const w = new Writable({
write: common.mustNotCall()
});
assert.strictEqual(w.writable, true);
w.end();
assert.strictEqual(w.writable, false);
}

0 comments on commit e559842

Please sign in to comment.