Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ pro.reset = function() {
// read length part of package
pro._readLength = function(data, offset, end) {
var b, i, length = this.length, finish;
for(i=0; i<end; i++) {
b = data.readUInt8(i + offset);
for(i=offset; i<end; i++) {
b = data.readUInt8(i);
length *= LEFT_SHIFT_BITS; // left shift only within 32 bits
length += (b & 0x7f);

Expand All @@ -133,7 +133,7 @@ pro._readLength = function(data, offset, end) {
this.left = this.length;
this.buf = new Buffer(this.length);
}
return i + offset;
return i;
};

// read data part of package
Expand Down
28 changes: 28 additions & 0 deletions test/composer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,32 @@ describe('#Composer', function() {
// should come here
should.be.ok(false);
});

it('should compose and decompose the separated packages', function (done) {
var src1 = 'hello';
var src2 = 'world' + new Array(127).join(".");
var comp1 = new Composer();
var comp2 = new Composer();

var res1 = comp1.compose(src1);
var res2 = comp1.compose(src2);

// assumeed that packets are separated by TCP.
var packet1 = Buffer.concat([res1, res2.slice(0, 1)]);
var packet2 = res2.slice(1);

var first = true;
comp2.on('data', function (data) {
var str = data.toString('utf-8');
if (first) {
str.should.equal(src1);
first = false;
} else {
str.should.equal(src2);
done();
}
});
comp2.feed(packet1);
comp2.feed(packet2);
});
});