From dfd11e8dcbffc3847e235d18f897675be554555b Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Fri, 10 Apr 2015 21:22:36 +0200 Subject: [PATCH] v.1.1.1: Fix compatibility with Node.js v0.12 by filtering out nulls from source streams --- lib/combined-stream2.coffee | 8 +++++++- lib/combined-stream2.js | 7 ++++++- package.json | 3 ++- test.coffee | 9 +++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 test.coffee diff --git a/lib/combined-stream2.coffee b/lib/combined-stream2.coffee index 785b859..4cf30ad 100644 --- a/lib/combined-stream2.coffee +++ b/lib/combined-stream2.coffee @@ -131,6 +131,7 @@ class CombinedStream extends stream.Readable @_currentSource = @_sources.shift()[0] @_currentIsStream = isStream @_currentSource + debug "switching to new source (stream = %s): %s", @_currentIsStream, @_currentSource.toString().replace(/\n/g, "\\n").replace(/\r/g, "\\r") if @_currentIsStream @_currentSource.once "end", => @@ -181,7 +182,12 @@ class CombinedStream extends stream.Readable Promise.try => @_sourceDataAvailable = false @_wantData = false - @push @_currentSource.read() + chunk = @_currentSource.read() + + # Since Node.js v0.12, a stream will apparently return null when it is finished... we need to filter this out, to prevent it from ending our combined stream prematurely. + if chunk? + @push chunk + Promise.resolve() # Public module API diff --git a/lib/combined-stream2.js b/lib/combined-stream2.js index 75567f5..425db4a 100644 --- a/lib/combined-stream2.js +++ b/lib/combined-stream2.js @@ -190,6 +190,7 @@ CombinedStream = (function(_super) { } this._currentSource = this._sources.shift()[0]; this._currentIsStream = isStream(this._currentSource); + debug("switching to new source (stream = %s): %s", this._currentIsStream, this._currentSource.toString().replace(/\n/g, "\\n").replace(/\r/g, "\\r")); if (this._currentIsStream) { this._currentSource.once("end", (function(_this) { return function() { @@ -243,9 +244,13 @@ CombinedStream = (function(_super) { CombinedStream.prototype._doStreamRead = function() { return Promise["try"]((function(_this) { return function() { + var chunk; _this._sourceDataAvailable = false; _this._wantData = false; - _this.push(_this._currentSource.read()); + chunk = _this._currentSource.read(); + if (chunk != null) { + _this.push(chunk); + } return Promise.resolve(); }; })(this)); diff --git a/package.json b/package.json index 3a2f1be..8fb9cb9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "combined-stream2", - "version": "1.1.0", + "version": "1.1.1", "description": "A drop-in Streams2-compatible replacement for combined-stream.", "main": "index.js", "scripts": { @@ -19,6 +19,7 @@ "author": "Sven Slootweg", "license": "WTFPL", "devDependencies": { + "dev-null": "^0.1.1", "gulp": "~3.8.0", "gulp-cached": "~0.0.3", "gulp-coffee": "~2.0.1", diff --git a/test.coffee b/test.coffee new file mode 100644 index 0000000..a7b2f09 --- /dev/null +++ b/test.coffee @@ -0,0 +1,9 @@ +fs = require "fs" +CombinedStream = require "./" +devNull = require "dev-null" + +combinedStream = CombinedStream.create() +combinedStream.append fs.createReadStream("./package.json") +combinedStream.append fs.createReadStream("./test.coffee") +#combinedStream.pipe devNull() +combinedStream.pipe process.stdout