streams2: Use StringDecoder.end
authorisaacs <i@izs.me>
Fri, 12 Oct 2012 18:45:17 +0000 (11:45 -0700)
committerisaacs <i@izs.me>
Fri, 14 Dec 2012 01:00:29 +0000 (17:00 -0800)
lib/_stream_readable.js

index 06797c4..51201e8 100644 (file)
@@ -153,6 +153,13 @@ Readable.prototype.read = function(n) {
       if (!chunk || !chunk.length) {
         // eof
         state.ended = true;
+        if (state.decoder) {
+          chunk = state.decoder.end();
+          if (chunk && chunk.length) {
+            state.buffer.push(chunk);
+            state.length += chunk.length;
+          }
+        }
         // if we've ended and we have some data left, then emit
         // 'readable' now to make sure it gets picked up.
         if (!sync) {
@@ -395,11 +402,26 @@ Readable.prototype.wrap = function(stream) {
 
   stream.on('end', function() {
     state.ended = true;
-    if (state.length === 0)
+    if (state.decoder) {
+      var chunk = state.decoder.end();
+      if (chunk && chunk.length) {
+        state.buffer.push(chunk);
+        state.length += chunk.length;
+      }
+    }
+
+    if (state.length > 0)
+      this.emit('readable');
+    else
       endReadable(this);
   }.bind(this));
 
   stream.on('data', function(chunk) {
+    if (state.decoder)
+      chunk = state.decoder.write(chunk);
+    if (!chunk || !chunk.length)
+      return;
+
     state.buffer.push(chunk);
     state.length += chunk.length;
     this.emit('readable');