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.
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);
});
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);
}
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 = [];
// 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');
+});