From cc0342a517279c9c52543e3a37e11da3fc6cdb36 Mon Sep 17 00:00:00 2001 From: Brian White Date: Thu, 19 Mar 2015 22:04:07 -0400 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/4083 Reviewed-By: Chris Dickinson --- lib/_stream_readable.js | 3 ++- lib/_stream_writable.js | 1 + test/parallel/test-stream2-compatibility.js | 22 +++++++++++++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 0316a05571080b..ea36e6ed62abf2 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -385,6 +385,7 @@ function onEofChunk(stream, state) { } } state.ended = true; + stream.readable = false; // emit 'readable' now to make sure it gets picked up. emitReadable(stream); @@ -670,7 +671,7 @@ Readable.prototype.on = function(ev, fn) { this.resume(); } - if (ev === 'readable' && this.readable) { + if (ev === 'readable' && !this._readableState.endEmitted) { var state = this._readableState; if (!state.readableListening) { state.readableListening = true; diff --git a/lib/_stream_writable.js b/lib/_stream_writable.js index 73f4d9fb3e193f..10fcae04f5553e 100644 --- a/lib/_stream_writable.js +++ b/lib/_stream_writable.js @@ -483,4 +483,5 @@ function endWritable(stream, state, cb) { stream.once('finish', cb); } state.ended = true; + stream.writable = false; } diff --git a/test/parallel/test-stream2-compatibility.js b/test/parallel/test-stream2-compatibility.js index 9eab7b713bf167..cbb65facfa7073 100644 --- a/test/parallel/test-stream2-compatibility.js +++ b/test/parallel/test-stream2-compatibility.js @@ -1,6 +1,7 @@ 'use strict'; var common = require('../common'); var R = require('_stream_readable'); +var W = require('_stream_writable'); var assert = require('assert'); var util = require('util'); @@ -29,4 +30,25 @@ var reader = new TestReader(); setImmediate(function() { assert.equal(ondataCalled, 1); console.log('ok'); + reader.push(null); +}); + +function TestWriter() { + W.apply(this); + this.write('foo'); + this.end(); +} + +util.inherits(TestWriter, W); + +TestWriter.prototype._write = function(chunk, enc, cb) { + cb(); +}; + +var writer = new TestWriter(); + +process.on('exit', function() { + assert.strictEqual(reader.readable, false); + assert.strictEqual(writer.writable, false); + console.log('ok'); });