varInt -> varint
I don't like having to capitalize things unnecessarily
This commit is contained in:
parent
30c96721a0
commit
4bb9105b2d
|
@ -79,7 +79,7 @@ BufferReader.prototype.readUInt64LEBN = function() {
|
|||
return bn;
|
||||
};
|
||||
|
||||
BufferReader.prototype.readVarIntNum = function() {
|
||||
BufferReader.prototype.readVarintNum = function() {
|
||||
var first = this.readUInt8();
|
||||
switch (first) {
|
||||
case 0xFD:
|
||||
|
@ -92,13 +92,13 @@ BufferReader.prototype.readVarIntNum = function() {
|
|||
if (n <= Math.pow(2, 53))
|
||||
return n;
|
||||
else
|
||||
throw new Error('number too large to retain precision - use readVarIntBN');
|
||||
throw new Error('number too large to retain precision - use readVarintBN');
|
||||
default:
|
||||
return first;
|
||||
}
|
||||
};
|
||||
|
||||
BufferReader.prototype.readVarIntBuf = function() {
|
||||
BufferReader.prototype.readVarintBuf = function() {
|
||||
var first = this.buf.readUInt8(this.pos);
|
||||
switch (first) {
|
||||
case 0xFD:
|
||||
|
@ -112,7 +112,7 @@ BufferReader.prototype.readVarIntBuf = function() {
|
|||
}
|
||||
};
|
||||
|
||||
BufferReader.prototype.readVarIntBN = function() {
|
||||
BufferReader.prototype.readVarintBN = function() {
|
||||
var first = this.readUInt8();
|
||||
switch (first) {
|
||||
case 0xFD:
|
||||
|
|
|
@ -71,19 +71,19 @@ BufferWriter.prototype.writeUInt64LEBN = function(bn) {
|
|||
return this;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeVarIntNum = function(n) {
|
||||
var buf = BufferWriter.varIntBufNum(n);
|
||||
BufferWriter.prototype.writeVarintNum = function(n) {
|
||||
var buf = BufferWriter.varintBufNum(n);
|
||||
this.write(buf);
|
||||
return this;
|
||||
};
|
||||
|
||||
BufferWriter.prototype.writeVarIntBN = function(bn) {
|
||||
var buf = BufferWriter.varIntBufBN(bn);
|
||||
BufferWriter.prototype.writeVarintBN = function(bn) {
|
||||
var buf = BufferWriter.varintBufBN(bn);
|
||||
this.write(buf);
|
||||
return this;
|
||||
};
|
||||
|
||||
BufferWriter.varIntBufNum = function(n) {
|
||||
BufferWriter.varintBufNum = function(n) {
|
||||
var buf = undefined;
|
||||
if (n < 253) {
|
||||
buf = new Buffer(1);
|
||||
|
@ -105,7 +105,7 @@ BufferWriter.varIntBufNum = function(n) {
|
|||
return buf;
|
||||
};
|
||||
|
||||
BufferWriter.varIntBufBN = function(bn) {
|
||||
BufferWriter.varintBufBN = function(bn) {
|
||||
var buf = undefined;
|
||||
var n = bn.toNumber();
|
||||
if (n < 253) {
|
||||
|
|
|
@ -29,9 +29,9 @@ Message.magicHash = function(messagebuf) {
|
|||
if (!Buffer.isBuffer(messagebuf))
|
||||
throw new Error('messagebuf must be a buffer');
|
||||
var bw = new BufferWriter();
|
||||
bw.writeVarIntNum(Message.magicBytes.length);
|
||||
bw.writeVarintNum(Message.magicBytes.length);
|
||||
bw.write(Message.magicBytes);
|
||||
bw.writeVarIntNum(messagebuf.length);
|
||||
bw.writeVarintNum(messagebuf.length);
|
||||
bw.write(messagebuf);
|
||||
var buf = bw.concat();
|
||||
|
||||
|
|
|
@ -168,99 +168,99 @@ describe('BufferReader', function() {
|
|||
|
||||
});
|
||||
|
||||
describe('#readVarIntBuf', function() {
|
||||
describe('#readVarintBuf', function() {
|
||||
|
||||
it('should read a 1 byte varint', function() {
|
||||
var buf = new Buffer([50]);
|
||||
var br = new BufferReader({buf: buf});
|
||||
br.readVarIntBuf().length.should.equal(1);
|
||||
br.readVarintBuf().length.should.equal(1);
|
||||
});
|
||||
|
||||
it('should read a 3 byte varint', function() {
|
||||
var buf = new Buffer([253, 253, 0]);
|
||||
var br = new BufferReader({buf: buf});
|
||||
br.readVarIntBuf().length.should.equal(3);
|
||||
br.readVarintBuf().length.should.equal(3);
|
||||
});
|
||||
|
||||
it('should read a 5 byte varint', function() {
|
||||
var buf = new Buffer([254, 0, 0, 0, 0]);
|
||||
buf.writeUInt32LE(50000, 1);
|
||||
var br = new BufferReader({buf: buf});
|
||||
br.readVarIntBuf().length.should.equal(5);
|
||||
br.readVarintBuf().length.should.equal(5);
|
||||
});
|
||||
|
||||
it('should read a 9 byte varint', function() {
|
||||
var buf = BufferWriter().writeVarIntBN(BN(Math.pow(2, 54).toString())).concat();
|
||||
var buf = BufferWriter().writeVarintBN(BN(Math.pow(2, 54).toString())).concat();
|
||||
var br = new BufferReader({buf: buf});
|
||||
br.readVarIntBuf().length.should.equal(9);
|
||||
br.readVarintBuf().length.should.equal(9);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#readVarIntNum', function() {
|
||||
describe('#readVarintNum', function() {
|
||||
|
||||
it('should read a 1 byte varint', function() {
|
||||
var buf = new Buffer([50]);
|
||||
var br = new BufferReader({buf: buf});
|
||||
br.readVarIntNum().should.equal(50);
|
||||
br.readVarintNum().should.equal(50);
|
||||
});
|
||||
|
||||
it('should read a 3 byte varint', function() {
|
||||
var buf = new Buffer([253, 253, 0]);
|
||||
var br = new BufferReader({buf: buf});
|
||||
br.readVarIntNum().should.equal(253);
|
||||
br.readVarintNum().should.equal(253);
|
||||
});
|
||||
|
||||
it('should read a 5 byte varint', function() {
|
||||
var buf = new Buffer([254, 0, 0, 0, 0]);
|
||||
buf.writeUInt32LE(50000, 1);
|
||||
var br = new BufferReader({buf: buf});
|
||||
br.readVarIntNum().should.equal(50000);
|
||||
br.readVarintNum().should.equal(50000);
|
||||
});
|
||||
|
||||
it('should throw an error on a 9 byte varint over the javascript uint precision limit', function() {
|
||||
var buf = BufferWriter().writeVarIntBN(BN(Math.pow(2, 54).toString())).concat();
|
||||
var buf = BufferWriter().writeVarintBN(BN(Math.pow(2, 54).toString())).concat();
|
||||
var br = new BufferReader({buf: buf});
|
||||
(function() {
|
||||
br.readVarIntNum();
|
||||
}).should.throw('number too large to retain precision - use readVarIntBN');
|
||||
br.readVarintNum();
|
||||
}).should.throw('number too large to retain precision - use readVarintBN');
|
||||
});
|
||||
|
||||
it('should not throw an error on a 9 byte varint not over the javascript uint precision limit', function() {
|
||||
var buf = BufferWriter().writeVarIntBN(BN(Math.pow(2, 53).toString())).concat();
|
||||
var buf = BufferWriter().writeVarintBN(BN(Math.pow(2, 53).toString())).concat();
|
||||
var br = new BufferReader({buf: buf});
|
||||
(function() {
|
||||
br.readVarIntNum();
|
||||
}).should.not.throw('number too large to retain precision - use readVarIntBN');
|
||||
br.readVarintNum();
|
||||
}).should.not.throw('number too large to retain precision - use readVarintBN');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#readVarIntBN', function() {
|
||||
describe('#readVarintBN', function() {
|
||||
|
||||
it('should read a 1 byte varint', function() {
|
||||
var buf = new Buffer([50]);
|
||||
var br = new BufferReader({buf: buf});
|
||||
br.readVarIntBN().toNumber().should.equal(50);
|
||||
br.readVarintBN().toNumber().should.equal(50);
|
||||
});
|
||||
|
||||
it('should read a 3 byte varint', function() {
|
||||
var buf = new Buffer([253, 253, 0]);
|
||||
var br = new BufferReader({buf: buf});
|
||||
br.readVarIntBN().toNumber().should.equal(253);
|
||||
br.readVarintBN().toNumber().should.equal(253);
|
||||
});
|
||||
|
||||
it('should read a 5 byte varint', function() {
|
||||
var buf = new Buffer([254, 0, 0, 0, 0]);
|
||||
buf.writeUInt32LE(50000, 1);
|
||||
var br = new BufferReader({buf: buf});
|
||||
br.readVarIntBN().toNumber().should.equal(50000);
|
||||
br.readVarintBN().toNumber().should.equal(50000);
|
||||
});
|
||||
|
||||
it('should read a 9 byte varint', function() {
|
||||
var buf = Buffer.concat([new Buffer([255]), new Buffer('ffffffffffffffff', 'hex')]);
|
||||
var br = new BufferReader({buf: buf});
|
||||
br.readVarIntBN().toNumber().should.equal(Math.pow(2, 64));
|
||||
br.readVarintBN().toNumber().should.equal(Math.pow(2, 64));
|
||||
});
|
||||
|
||||
});
|
||||
|
|
|
@ -107,66 +107,66 @@ describe('BufferWriter', function() {
|
|||
|
||||
});
|
||||
|
||||
describe('#writeVarInt', function() {
|
||||
describe('#writeVarint', function() {
|
||||
|
||||
it('should write a 1 byte varInt', function() {
|
||||
it('should write a 1 byte varint', function() {
|
||||
var bw = new BufferWriter();
|
||||
bw.writeVarIntNum(1);
|
||||
bw.writeVarintNum(1);
|
||||
bw.concat().length.should.equal(1);
|
||||
});
|
||||
|
||||
it('should write a 3 byte varInt', function() {
|
||||
it('should write a 3 byte varint', function() {
|
||||
var bw = new BufferWriter();
|
||||
bw.writeVarIntNum(1000);
|
||||
bw.writeVarintNum(1000);
|
||||
bw.concat().length.should.equal(3);
|
||||
});
|
||||
|
||||
it('should write a 5 byte varInt', function() {
|
||||
it('should write a 5 byte varint', function() {
|
||||
var bw = new BufferWriter();
|
||||
bw.writeVarIntNum(Math.pow(2, 16 + 1));
|
||||
bw.writeVarintNum(Math.pow(2, 16 + 1));
|
||||
bw.concat().length.should.equal(5);
|
||||
});
|
||||
|
||||
it('should write a 9 byte varInt', function() {
|
||||
it('should write a 9 byte varint', function() {
|
||||
var bw = new BufferWriter();
|
||||
bw.writeVarIntNum(Math.pow(2, 32 + 1));
|
||||
bw.writeVarintNum(Math.pow(2, 32 + 1));
|
||||
bw.concat().length.should.equal(9);
|
||||
});
|
||||
|
||||
it('should read back the same value it wrote for a 9 byte varInt', function() {
|
||||
it('should read back the same value it wrote for a 9 byte varint', function() {
|
||||
var bw = new BufferWriter();
|
||||
var n = Math.pow(2, 53);
|
||||
n.should.equal(n + 1); //javascript number precision limit
|
||||
bw.writeVarIntNum(n);
|
||||
bw.writeVarintNum(n);
|
||||
var br = new BufferReader({buf: bw.concat()});
|
||||
br.readVarIntBN().toNumber().should.equal(n);
|
||||
br.readVarintBN().toNumber().should.equal(n);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#writeVarIntBN', function() {
|
||||
describe('#writeVarintBN', function() {
|
||||
|
||||
it('should write a 1 byte varInt', function() {
|
||||
it('should write a 1 byte varint', function() {
|
||||
var bw = new BufferWriter();
|
||||
bw.writeVarIntBN(BN(1));
|
||||
bw.writeVarintBN(BN(1));
|
||||
bw.concat().length.should.equal(1);
|
||||
});
|
||||
|
||||
it('should write a 3 byte varInt', function() {
|
||||
it('should write a 3 byte varint', function() {
|
||||
var bw = new BufferWriter();
|
||||
bw.writeVarIntBN(BN(1000));
|
||||
bw.writeVarintBN(BN(1000));
|
||||
bw.concat().length.should.equal(3);
|
||||
});
|
||||
|
||||
it('should write a 5 byte varInt', function() {
|
||||
it('should write a 5 byte varint', function() {
|
||||
var bw = new BufferWriter();
|
||||
bw.writeVarIntBN(BN(Math.pow(2, 16 + 1)));
|
||||
bw.writeVarintBN(BN(Math.pow(2, 16 + 1)));
|
||||
bw.concat().length.should.equal(5);
|
||||
});
|
||||
|
||||
it('should write a 9 byte varInt', function() {
|
||||
it('should write a 9 byte varint', function() {
|
||||
var bw = new BufferWriter();
|
||||
bw.writeVarIntBN(BN(Math.pow(2, 32 + 1)));
|
||||
bw.writeVarintBN(BN(Math.pow(2, 32 + 1)));
|
||||
bw.concat().length.should.equal(9);
|
||||
});
|
||||
|
||||
|
|
Loading…
Reference in New Issue