From c63bd5741e400c3021427ffac1c4e3db9de4ccf6 Mon Sep 17 00:00:00 2001 From: xuezu Date: Wed, 29 Mar 2017 15:09:35 +0800 Subject: [PATCH] fix: should set size and limit correctly when options.array passed in --- lib/byte.js | 3 +++ test/byte.test.js | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/lib/byte.js b/lib/byte.js index f7d97e9..7484845 100644 --- a/lib/byte.js +++ b/lib/byte.js @@ -15,6 +15,7 @@ * Module dependencies. */ +var assert = require('assert'); var Long = require('long'); var debug = require('debug')('byte'); var numbers = require('./number'); @@ -33,7 +34,9 @@ function ByteBuffer(options) { this._limit = this._size; var array = options.array; if (array) { + assert(array instanceof Buffer, 'options.array should be a Buffer'); this._bytes = array; + this._size = this._limit = array.length; } else { this._bytes = new Buffer(this._size); } diff --git a/test/byte.test.js b/test/byte.test.js index cd25293..868c23a 100644 --- a/test/byte.test.js +++ b/test/byte.test.js @@ -26,6 +26,11 @@ describe('byte.test.js', function () { bytes.array().should.length(0); bytes.position().should.equal(0); }); + + it('should correct set size when passed array', function () { + var bytes = new ByteBuffer({ array: new Buffer(10) }); + bytes.limit().should.equal(10); + }); }); describe('putString(), getString()', function () {