Skip to content

Commit

Permalink
Fixes bug with writing values with an offset of zero (false-y fun)
Browse files Browse the repository at this point in the history
Fixes bug with writeBufferNT where the null character would ignore buffers written at an offset.
Bump version to 1.0.11
  • Loading branch information
JoshGlazebrook committed May 20, 2016
1 parent 5518ebe commit 56ded62
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
12 changes: 6 additions & 6 deletions lib/smart-buffer.js
Expand Up @@ -68,12 +68,12 @@ var SmartBuffer = (function () {


SmartBuffer.prototype._ensureWritable = function (len, offset) {
this._ensureCapacity(this.length + len + (offset || 0));
this._ensureCapacity(this.length + len + (typeof offset === 'number' ? offset : 0));

if (typeof offset === 'number') {
this.buff.copy(this.buff, offset + len, offset, this.buff.length);
}
this.length = Math.max(this.length + len, (offset || 0) + len);
this.length = Math.max(this.length + len, (typeof offset === 'number' ? offset : 0) + len);
};

SmartBuffer.prototype._ensureCapacity = function (minlen) {
Expand Down Expand Up @@ -171,7 +171,7 @@ var SmartBuffer = (function () {
* @returns {Buffer} Buffer containing the read bytes.
*/
SmartBuffer.prototype.readBuffer = function (len) {
var endpoint = Math.min(this.length, this._readOffset + (len || this.length));
var endpoint = Math.min(this.length, this._readOffset + (typeof len === 'number' ? len : this.length));
var ret = this.buff.slice(this._readOffset, endpoint);
this._readOffset = endpoint;
return ret;
Expand Down Expand Up @@ -271,7 +271,7 @@ var SmartBuffer = (function () {
SmartBuffer.prototype.writeBuffer = function (value, offset) {
var len = value.length;
this._ensureWritable(len, offset);
value.copy(this.buff, offset || this._writeOffset);
value.copy(this.buff, typeof offset === 'number' ? offset : this._writeOffset);
this._writeOffset += len;
return this;
};
Expand All @@ -285,8 +285,8 @@ var SmartBuffer = (function () {
SmartBuffer.prototype.writeBufferNT = function (value, offset) {
var len = value.length;
this._ensureWritable(len, offset);
value.copy(this.buff, offset || this._writeOffset);
this.buff[this._writeOffset + len] = 0x00;
value.copy(this.buff, typeof offset === 'number' ? offset : this._writeOffset);
this.buff[(typeof offset === 'number' ? offset : this._writeOffset) + len] = 0x00;
this._writeOffset += len + 1;
return this;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "smart-buffer",
"version": "1.0.10",
"version": "1.0.11",
"description": "A smarter Buffer that keeps track of its own read and write positions while growing endlessly.",
"main": "lib/smart-buffer.js",
"homepage": "https://github.com/JoshGlazebrook/smart-buffer/",
Expand Down
29 changes: 28 additions & 1 deletion test/smart-buffer.test.js
Expand Up @@ -193,6 +193,33 @@ describe('Reading/Writing To/From SmartBuffer', function () {
});

describe('Buffer Values', function () {
describe('Writing buffer to position 0', function () {
var buff = new SmartBuffer();
var frontBuff = new Buffer([1, 2, 3, 4, 5, 6]);
buff.writeStringNT('hello');
buff.writeBuffer(frontBuff, 0);

it('should write the buffer to the front of the smart buffer instance', function () {
var readBuff = buff.readBuffer(frontBuff.length);
assert.deepEqual(readBuff, frontBuff);
});
});

describe('Writing null terminated buffer to position 0', function () {
var buff = new SmartBuffer();
var frontBuff = new Buffer([1, 2, 3, 4, 5, 6]);
buff.writeStringNT('hello');
buff.writeBufferNT(frontBuff, 0);

console.log(buff);

it('should write the buffer to the front of the smart buffer instance', function () {
var readBuff = buff.readBufferNT();
console.log(readBuff);
assert.deepEqual(readBuff, frontBuff);
});
});

describe('Explicit lengths', function () {
var buff = new Buffer([0x01, 0x02, 0x04, 0x08, 0x16, 0x32, 0x64]);
var reader = new SmartBuffer();
Expand Down Expand Up @@ -236,7 +263,7 @@ describe('Reading/Writing To/From SmartBuffer', function () {
var read1 = buff.readBufferNT();

it('Should read the correct null terminated buffer data.', function () {
assert.equal(read1.length, 4);
assert.equal(read1.length, 4);
});

})
Expand Down

0 comments on commit 56ded62

Please sign in to comment.