Skip to content

Commit de43314

Browse files
committed
Buffer support for LengthCodedString
1 parent ad9db25 commit de43314

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

lib/protocol/types/LengthCodedBinary.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,21 @@ var IEEE_754_BINARY_64_PRECISION = Math.pow(2, 53);
2525
module.exports = LengthCodedBinary;
2626
function LengthCodedBinary(value) {
2727
this.value = value;
28-
29-
if (value === null || value <= 250) {
30-
this.length = 1;
31-
} else if (value <= BIT_16) {
32-
this.length = 3;
33-
} else if (value <= BIT_24) {
34-
this.length = 4;
35-
} else if (value < IEEE_754_BINARY_64_PRECISION) {
36-
this.length = 9;
37-
} else {
38-
throw new Error(
39-
'LengthCodedBinary.SizeExceeded: JS precision range exceeded, your ' +
40-
'number is > 53 bit.'
41-
);
42-
}
28+
this.length = LengthCodedBinary.byteLength(value);
4329
}
4430

31+
LengthCodedBinary.byteLength = function(value) {
32+
if (value === null || value <= 250) return 1;
33+
if (value <= BIT_16) return 3;
34+
if (value <= BIT_24) return 4;
35+
if (value < IEEE_754_BINARY_64_PRECISION) return 9;
36+
37+
throw new Error(
38+
'LengthCodedBinary.SizeExceeded: JS precision range exceeded, your ' +
39+
'number is > 53 bit: "' + value + '"'
40+
);
41+
};
42+
4543
LengthCodedBinary.prototype.copy = function(buffer, offset) {
4644
// Optimization: Reduce property lookups
4745
var value = this.value;

lib/protocol/types/LengthCodedString.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ var LengthCodedBinary = require('./LengthCodedBinary');
1313
module.exports = LengthCodedString;
1414
function LengthCodedString(value) {
1515
this.value = value;
16-
this.length = Buffer.byteLength(value, 'utf-8');
16+
this.length = (Buffer.isBuffer(value))
17+
? value.length
18+
: Buffer.byteLength(value, 'utf-8');
1719

1820
this._lengthCodedBinary = new LengthCodedBinary(this.length);
1921
this.length = this._lengthCodedBinary.length + this.length;
@@ -24,5 +26,9 @@ LengthCodedString.prototype.copy = function(buffer, offset) {
2426

2527
offset += this._lengthCodedBinary.length;
2628

27-
buffer.write(this.value, offset, 'utf-8');
29+
if (Buffer.isBuffer(this.value)) {
30+
this.value.copy(buffer, offset);
31+
} else {
32+
buffer.write(this.value, offset, 'utf-8');
33+
}
2834
};

test/unit/protocol/types/test-LengthCodedString.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ test('LengthCodedString', {
1818
assert.equal(lengthCodedInteger.length, 4);
1919
},
2020

21+
'Buffer "ab"': function() {
22+
var lengthCodedInteger = new LengthCodedString(new Buffer('ab'));
23+
var buffer = new Buffer(lengthCodedInteger.length);
24+
25+
lengthCodedInteger.copy(buffer, 0);
26+
27+
assert.deepEqual(buffer, new Buffer([0x02, 0x61, 0x62]));
28+
},
29+
2130
'offset': function() {
2231
var lengthCodedInteger = new LengthCodedString('ab');
2332
var buffer = new Buffer([0, 0, 0, 0]);

0 commit comments

Comments
 (0)