Skip to content

Commit

Permalink
fixing #12
Browse files Browse the repository at this point in the history
  • Loading branch information
DNizovtsev committed Mar 4, 2015
1 parent 08ee789 commit 6d57b08
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 4 additions & 1 deletion index.js
Expand Up @@ -48,6 +48,7 @@ MemoryDuplexStream.prototype.init = function init (data, options) {
}
self.queue.push(chunk);
});

}

options = options || {};
Expand Down Expand Up @@ -104,7 +105,6 @@ MemoryDuplexStream.prototype._read = function _read (n) {
var self = this,
frequence = self.frequence || 0,
wait_data = this instanceof STREAM.Duplex && ! this._writableState.finished ? true : false;

if ( ! this.queue.length && ! wait_data) {
this.push(null);// finish stream
} else if (this.queue.length) {
Expand Down Expand Up @@ -144,6 +144,9 @@ MemoryDuplexStream.prototype._write = function _write (chunk, encoding, cb) {
}

if (this instanceof STREAM.Duplex) {
while (this.queue.length) {
this.push(this.queue.shift());
}
this.push(decoded_chunk);
} else {
this.queue.push(decoded_chunk);
Expand Down
23 changes: 23 additions & 0 deletions test/memorystream.test.js
Expand Up @@ -276,6 +276,29 @@ describe('Test memory streams', function() {
done();
});
});

it("should write/read data with init buffer", function (done) {

var l = Math.floor(test_data.length / 2);

var test_data1 = test_data.substr(0, l),
test_data2 = test_data.substr(l);

var mem_stream = new MemoryStream(test_data1.split(''));

var data = '';
mem_stream.on('data',function(chunk){
data += chunk;
});

writeToStream2(mem_stream, test_data2);

mem_stream.on('end', function() {
expect(data).to.be(test_data);
done();
});

});


it("should piping data", function (done) {
Expand Down

0 comments on commit 6d57b08

Please sign in to comment.