bin-protocol
Advanced tools
Comparing version 2.0.3 to 2.0.4
@@ -28,5 +28,3 @@ 'use strict'; | ||
function Protocol(options) { | ||
this.options = _.defaults(options || {}, { | ||
writerBufSize: 1024, | ||
}); | ||
this.options = options || {}; | ||
} | ||
@@ -71,4 +69,4 @@ | ||
this.reader = new _Reader(); | ||
this.writer = new _Writer(this.options.writerBufSize); | ||
this.reader = new _Reader(this.options); | ||
this.writer = new _Writer(this.options); | ||
@@ -75,0 +73,0 @@ if (typeof constructor === 'function') { |
@@ -5,6 +5,11 @@ 'use strict'; | ||
function Writer(bufferSize) { | ||
function Writer(options) { | ||
var self = this; | ||
self.buffer = new Buffer(bufferSize || 1024); | ||
self.options = _.defaults(options || {}, { | ||
bufferSize: 8 * 1024, | ||
resultCopy: false // set to true if you want write().result to return a copy of the Buffer instead of the slice | ||
}); | ||
self.buffer = new Buffer(self.options.bufferSize); | ||
self.offset = 0; | ||
@@ -99,4 +104,10 @@ | ||
enumerable: true, get: function () { | ||
var copy; | ||
if (this.options.resultCopy) { | ||
copy = new Buffer(this.offset); | ||
this.buffer.copy(copy, 0, 0, this.offset); | ||
return copy; | ||
} | ||
return this.buffer.slice(0, this.offset); | ||
} | ||
}); |
@@ -9,3 +9,3 @@ { | ||
}, | ||
"version": "2.0.3", | ||
"version": "2.0.4", | ||
"main": "./lib/index.js", | ||
@@ -12,0 +12,0 @@ "keywords": ["buffer", "raw", "binary", "protocol", "parser", "reader", "builder"], |
@@ -199,3 +199,3 @@ 'use strict'; | ||
var protocol = new Protocol({ | ||
writerBufSize: 1 | ||
bufferSize: 1 | ||
}); | ||
@@ -205,2 +205,14 @@ protocol.writer.buffer.length.should.be.eql(1); | ||
}); | ||
it('should return a copy of buffer with resultCopy = true', function () { | ||
var protocol = new Protocol({ | ||
resultCopy: true | ||
}); | ||
var buffer = protocol.write().Int8(1).Int8(2).result; | ||
buffer.should.be.eql(new Buffer([1, 2])); | ||
buffer.writeInt8(10, 0); | ||
buffer.should.be.eql(new Buffer([10, 2])); | ||
protocol.writer.buffer.slice(0, 2).should.be.eql(new Buffer([1, 2])); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
136525
2080