streams2: Do not allow hwm < lwm
authorisaacs <i@izs.me>
Tue, 15 Jan 2013 00:03:38 +0000 (16:03 -0800)
committerisaacs <i@izs.me>
Tue, 15 Jan 2013 00:03:38 +0000 (16:03 -0800)
There was previously an assert() in there, but this part of the code is
so high-volume that the added cost made a measurable dent in http_simple.

Just checking inline is fine, though, and prevents a lot of potential
hazards.

lib/_stream_readable.js
lib/_stream_writable.js
test/simple/test-stream2-basic.js

index 7ad3974..a0caa3b 100644 (file)
@@ -48,6 +48,9 @@ function ReadableState(options, stream) {
   this.lowWaterMark = ~~this.lowWaterMark;
   this.highWaterMark = ~~this.highWaterMark;
 
+  if (this.lowWaterMark > this.highWaterMark)
+    throw new Error('lowWaterMark cannot be higher than highWaterMark');
+
   this.buffer = [];
   this.length = 0;
   this.pipes = null;
index 1a907da..2d63c4d 100644 (file)
@@ -49,6 +49,9 @@ function WritableState(options, stream) {
   this.lowWaterMark = ~~this.lowWaterMark;
   this.highWaterMark = ~~this.highWaterMark;
 
+  if (this.lowWaterMark > this.highWaterMark)
+    throw new Error('lowWaterMark cannot be higher than highWaterMark');
+
   this.needDrain = false;
   // at the start of calling end()
   this.ending = false;
index 0b4f4cf..dff3340 100644 (file)
@@ -318,3 +318,18 @@ test('multipipe', function(t) {
     r.pipe(w[2]);
   });
 });
+
+assert.throws(function() {
+  var bad = new R({
+    highWaterMark: 10,
+    lowWaterMark: 1000
+  });
+});
+
+assert.throws(function() {
+  var W = require('stream').Writable;
+  var bad = new W({
+    highWaterMark: 10,
+    lowWaterMark: 1000
+  });
+});