Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
stream: Never call decoder.end() multiple times
Browse files Browse the repository at this point in the history
Updated version that does what it says without assigning state.decoder.
  • Loading branch information
kanongil authored and isaacs committed Mar 14, 2013
1 parent 1f53cfd commit e8f80bf
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,14 +342,14 @@ function chunkInvalid(state, chunk) {


function onEofChunk(stream, state) {
state.ended = true;
if (state.decoder) {
if (state.decoder && !state.ended) {
var chunk = state.decoder.end();
if (chunk && chunk.length) {
state.buffer.push(chunk);
state.length += state.objectMode ? 1 : chunk.length;
}
}
state.ended = true;

// if we've ended and we have some data left, then emit
// 'readable' now to make sure it gets picked up.
Expand Down Expand Up @@ -733,12 +733,12 @@ Readable.prototype.wrap = function(stream) {

var self = this;
stream.on('end', function() {
state.ended = true;
if (state.decoder) {
if (state.decoder && !state.ended) {
var chunk = state.decoder.end();
if (chunk && chunk.length)
self.push(chunk);
}
state.ended = true;

self.push(null);
});
Expand Down
73 changes: 73 additions & 0 deletions test/simple/test-stream2-set-encoding.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,15 @@ TestReader.prototype._read = function(n) {
setTimeout(function() {

if (this.pos >= this.len) {
// double push(null) to test eos handling
this.push(null);
return this.push(null);
}

n = Math.min(n, this.len - this.pos);
if (n <= 0) {
// double push(null) to test eos handling
this.push(null);
return this.push(null);
}

Expand Down Expand Up @@ -204,6 +208,41 @@ test('setEncoding hex with read(13)', function(t) {
tr.emit('readable');
});

test('setEncoding base64', function(t) {
var tr = new TestReader(100);
tr.setEncoding('base64');
var out = [];
var expect =
[ 'YWFhYWFhYW',
'FhYWFhYWFh',
'YWFhYWFhYW',
'FhYWFhYWFh',
'YWFhYWFhYW',
'FhYWFhYWFh',
'YWFhYWFhYW',
'FhYWFhYWFh',
'YWFhYWFhYW',
'FhYWFhYWFh',
'YWFhYWFhYW',
'FhYWFhYWFh',
'YWFhYWFhYW',
'FhYQ==' ];

tr.on('readable', function flow() {
var chunk;
while (null !== (chunk = tr.read(10)))
out.push(chunk);
});

tr.on('end', function() {
t.same(out, expect);
t.end();
});

// just kick it off.
tr.emit('readable');
});

test('encoding: utf8', function(t) {
var tr = new TestReader(100, { encoding: 'utf8' });
var out = [];
Expand Down Expand Up @@ -310,3 +349,37 @@ test('encoding: hex with read(13)', function(t) {
// just kick it off.
tr.emit('readable');
});

test('encoding: base64', function(t) {
var tr = new TestReader(100, { encoding: 'base64' });
var out = [];
var expect =
[ 'YWFhYWFhYW',
'FhYWFhYWFh',
'YWFhYWFhYW',
'FhYWFhYWFh',
'YWFhYWFhYW',
'FhYWFhYWFh',
'YWFhYWFhYW',
'FhYWFhYWFh',
'YWFhYWFhYW',
'FhYWFhYWFh',
'YWFhYWFhYW',
'FhYWFhYWFh',
'YWFhYWFhYW',
'FhYQ==' ];

tr.on('readable', function flow() {
var chunk;
while (null !== (chunk = tr.read(10)))
out.push(chunk);
});

tr.on('end', function() {
t.same(out, expect);
t.end();
});

// just kick it off.
tr.emit('readable');
});

0 comments on commit e8f80bf

Please sign in to comment.