From fd0d7059df45012d513f083c01efb289f870343b Mon Sep 17 00:00:00 2001 From: Douglas Brown Date: Fri, 9 Sep 2016 18:05:25 -0400 Subject: [PATCH] Prevent Infinite Loop in m.prop.combine Passing a non-stream value to combine or merge will result in uncaught errors that are not descriptive enough to be helpful. --- util/stream.js | 2 ++ util/tests/test-stream.js | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/util/stream.js b/util/stream.js index a3833526..526ab1c8 100644 --- a/util/stream.js +++ b/util/stream.js @@ -116,6 +116,7 @@ module.exports = function(log) { return initDependency(self, [stream], derive, recover) } function combine(fn, streams) { + if (streams.length > streams.filter(valid).length) throw new Error("Ensure that each item passed to m.prop.combine/m.prop.merge is a stream") return initDependency(createStream(), streams, function() { var failed = streams.filter(errored) if (failed.length > 0) throw {__error: failed[0]._state.error} @@ -177,6 +178,7 @@ module.exports = function(log) { function valueOf() {return this._state.value} function toJSON() {return this._state.value != null && typeof this._state.value.toJSON === "function" ? this._state.value.toJSON() : this._state.value} + function valid(stream) {return stream._state } function active(stream) {return stream._state.state === 1} function changed(stream) {return stream._state.changed} function notEnded(stream) {return stream._state.state !== 2} diff --git a/util/tests/test-stream.js b/util/tests/test-stream.js index bd187d77..fc29e396 100644 --- a/util/tests/test-stream.js +++ b/util/tests/test-stream.js @@ -171,6 +171,20 @@ o.spec("stream", function() { o(b()).equals(undefined) }) + o("combine will throw with a helpful error if given non-stream values", function () { + var spy = o.spy() + var a = Stream(1) + var thrown = null; + try { + var b = Stream.combine(spy, [a, '']) + } catch (e) { + thrown = e + } + + o(thrown).notEquals(null) + o(thrown.constructor === TypeError).equals(false) + o(spy.callCount).equals(0) + }) }) o.spec("merge", function() { o("transforms an array of streams to an array of values", function() {