Skip to content

Commit

Permalink
streams2: Use StringDecoder.end
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Dec 14, 2012
1 parent cf0b4ba commit f624ccb
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit f624ccb

Please sign in to comment.