stream: fix Readable.wrap objectMode falsy values
authorJames Halliday <mail@substack.net>
Mon, 9 Jun 2014 01:58:53 +0000 (18:58 -0700)
committerTrevor Norris <trev.norris@gmail.com>
Fri, 1 Aug 2014 20:01:23 +0000 (13:01 -0700)
A streams1 stream will have its falsy values such as 0, false, or ""
eaten by the upgrade to streams2, even when objectMode is enabled.

Include test for said cases.

Reviewed-by: isaacs <i@izs.me>
Reviewed-by: Trevor Norris <trev.norris@gmail.com>
lib/_stream_readable.js
test/simple/test-stream2-readable-wrap.js

index ae04f22..1b29676 100755 (executable)
@@ -810,7 +810,12 @@ Readable.prototype.wrap = function(stream) {
   stream.on('data', function(chunk) {
     if (state.decoder)
       chunk = state.decoder.write(chunk);
-    if (!chunk || !state.objectMode && !chunk.length)
+
+    // don't skip over falsy values in objectMode
+    //if (state.objectMode && util.isNullOrUndefined(chunk))
+    if (state.objectMode && (chunk === null || chunk === undefined))
+      return;
+    else if (!state.objectMode && (!chunk || !chunk.length))
       return;
 
     var ret = self.push(chunk);
index 6b272be..233e7fd 100644 (file)
@@ -102,6 +102,9 @@ function runTest(highWaterMark, objectMode, produce) {
 runTest(10, false, function(){ return new Buffer('xxxxxxxxxx'); });
 runTest(1, true, function(){ return { foo: 'bar' }; });
 
+var objectChunks = [ 5, 'a', false, 0, '', 'xyz', { x: 4 }, 7, [], 555 ];
+runTest(1, true, function(){ return objectChunks.shift() });
+
 process.on('exit', function() {
   assert.equal(testRuns, completedRuns);
   console.log('ok');