Skip to content

Commit 1c1af81

Browse files
mscdexrvagg
authored andcommittedDec 5, 2015
streams: update .readable/.writable to false
These properties were initially used to determine stream status back in node v0.8 and earlier. Since streams2 however, these properties were *always* true, which can be misleading for example if you are trying to immediately determine whether a Writable stream is still writable or not (to avoid a "write after end" exception). PR-URL: #4083 Reviewed-By: Chris Dickinson <[email protected]>
1 parent cb55c67 commit 1c1af81

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed
 

‎lib/_stream_readable.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ function onEofChunk(stream, state) {
385385
}
386386
}
387387
state.ended = true;
388+
stream.readable = false;
388389

389390
// emit 'readable' now to make sure it gets picked up.
390391
emitReadable(stream);
@@ -670,7 +671,7 @@ Readable.prototype.on = function(ev, fn) {
670671
this.resume();
671672
}
672673

673-
if (ev === 'readable' && this.readable) {
674+
if (ev === 'readable' && !this._readableState.endEmitted) {
674675
var state = this._readableState;
675676
if (!state.readableListening) {
676677
state.readableListening = true;

‎lib/_stream_writable.js

+1
Original file line numberDiff line numberDiff line change
@@ -483,4 +483,5 @@ function endWritable(stream, state, cb) {
483483
stream.once('finish', cb);
484484
}
485485
state.ended = true;
486+
stream.writable = false;
486487
}

‎test/parallel/test-stream2-compatibility.js

+22
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
var common = require('../common');
33
var R = require('_stream_readable');
4+
var W = require('_stream_writable');
45
var assert = require('assert');
56

67
var util = require('util');
@@ -29,4 +30,25 @@ var reader = new TestReader();
2930
setImmediate(function() {
3031
assert.equal(ondataCalled, 1);
3132
console.log('ok');
33+
reader.push(null);
34+
});
35+
36+
function TestWriter() {
37+
W.apply(this);
38+
this.write('foo');
39+
this.end();
40+
}
41+
42+
util.inherits(TestWriter, W);
43+
44+
TestWriter.prototype._write = function(chunk, enc, cb) {
45+
cb();
46+
};
47+
48+
var writer = new TestWriter();
49+
50+
process.on('exit', function() {
51+
assert.strictEqual(reader.readable, false);
52+
assert.strictEqual(writer.writable, false);
53+
console.log('ok');
3254
});

0 commit comments

Comments
 (0)
Please sign in to comment.