Comparing version 1.1.3 to 1.2.0
@@ -111,2 +111,4 @@ /*! | ||
exports.readU = _readn(encoding.readU); | ||
exports.readBigU256 = _read(encoding.readBigU256, 32); | ||
exports.readBigU128 = _read(encoding.readBigU128, 16); | ||
exports.readBigU64 = _read(encoding.readBigU64, 8); | ||
@@ -124,2 +126,4 @@ exports.readBigU56 = _read(encoding.readBigU56, 7); | ||
exports.readUBE = _readn(encoding.readUBE); | ||
exports.readBigU256BE = _read(encoding.readBigU256BE, 32); | ||
exports.readBigU128BE = _read(encoding.readBigU128BE, 16); | ||
exports.readBigU64BE = _read(encoding.readBigU64BE, 8); | ||
@@ -164,2 +168,4 @@ exports.readBigU56BE = _read(encoding.readBigU56BE, 7); | ||
exports.writeU = _writen(encoding.writeU); | ||
exports.writeBigU256 = _write(encoding.writeBigU256, 32); | ||
exports.writeBigU128 = _write(encoding.writeBigU128, 16); | ||
exports.writeBigU64 = _write(encoding.writeBigU64, 8); | ||
@@ -177,2 +183,4 @@ exports.writeBigU56 = _write(encoding.writeBigU56, 7); | ||
exports.writeUBE = _writen(encoding.writeUBE); | ||
exports.writeBigU256BE = _write(encoding.writeBigU256BE, 32); | ||
exports.writeBigU128BE = _write(encoding.writeBigU128BE, 16); | ||
exports.writeBigU64BE = _write(encoding.writeBigU64BE, 8); | ||
@@ -179,0 +187,0 @@ exports.writeBigU56BE = _write(encoding.writeBigU56BE, 7); |
@@ -30,2 +30,9 @@ /*! | ||
// eslint-disable-next-line | ||
const BI = typeof BigInt === 'function' ? BigInt : (function(x) { return 0; }); | ||
const BIG_U56_MAX = (BI(1) << BI(56)) - BI(1); | ||
const BIG_U64_MAX = (BI(1) << BI(64)) - BI(1); | ||
const BIG_U128_MAX = (BI(1) << BI(128)) - BI(1); | ||
const BIG_U256_MAX = (BI(1) << BI(256)) - BI(1); | ||
/* | ||
@@ -58,2 +65,16 @@ * Read Unsigned LE | ||
function readBigU256(data, off) { | ||
const hi = readBigU128(data, off + 16); | ||
const lo = readBigU128(data, off); | ||
return (hi << BigInt(128)) | lo; | ||
} | ||
function readBigU128(data, off) { | ||
const hi = readBigU64(data, off + 8); | ||
const lo = readBigU64(data, off); | ||
return (hi << BigInt(64)) | lo; | ||
} | ||
function readBigU64(data, off) { | ||
@@ -156,2 +177,16 @@ const hi = readU32(data, off + 4); | ||
function readBigU256BE(data, off) { | ||
const hi = readBigU128BE(data, off); | ||
const lo = readBigU128BE(data, off + 16); | ||
return (hi << BigInt(128)) | lo; | ||
} | ||
function readBigU128BE(data, off) { | ||
const hi = readBigU64BE(data, off); | ||
const lo = readBigU64BE(data, off + 8); | ||
return (hi << BigInt(64)) | lo; | ||
} | ||
function readBigU64BE(data, off) { | ||
@@ -500,2 +535,32 @@ const hi = readU32BE(data, off); | ||
function writeBigU256(dst, num, off) { | ||
// eslint-disable-next-line valid-typeof | ||
enforce(typeof num === 'bigint', 'num', 'bigint'); | ||
num &= BIG_U256_MAX; | ||
const hi = num >> BigInt(128); | ||
const lo = num & BIG_U128_MAX; | ||
off = writeBigU128(dst, lo, off); | ||
off = writeBigU128(dst, hi, off); | ||
return off; | ||
} | ||
function writeBigU128(dst, num, off) { | ||
// eslint-disable-next-line valid-typeof | ||
enforce(typeof num === 'bigint', 'num', 'bigint'); | ||
num &= BIG_U128_MAX; | ||
const hi = num >> BigInt(64); | ||
const lo = num & BIG_U64_MAX; | ||
off = writeBigU64(dst, lo, off); | ||
off = writeBigU64(dst, hi, off); | ||
return off; | ||
} | ||
function writeBigU64(dst, num, off) { | ||
@@ -505,3 +570,3 @@ // eslint-disable-next-line valid-typeof | ||
num &= BigInt('0xffffffffffffffff'); | ||
num &= BIG_U64_MAX; | ||
@@ -521,3 +586,3 @@ const hi = Number(num >> BigInt(32)); | ||
num &= BigInt('0xffffffffffffff'); | ||
num &= BIG_U56_MAX; | ||
@@ -648,2 +713,32 @@ const hi = Number(num >> BigInt(32)); | ||
function writeBigU256BE(dst, num, off) { | ||
// eslint-disable-next-line valid-typeof | ||
enforce(typeof num === 'bigint', 'num', 'bigint'); | ||
num &= BIG_U256_MAX; | ||
const hi = num >> BigInt(128); | ||
const lo = num & BIG_U128_MAX; | ||
off = writeBigU128BE(dst, hi, off); | ||
off = writeBigU128BE(dst, lo, off); | ||
return off; | ||
} | ||
function writeBigU128BE(dst, num, off) { | ||
// eslint-disable-next-line valid-typeof | ||
enforce(typeof num === 'bigint', 'num', 'bigint'); | ||
num &= BIG_U128_MAX; | ||
const hi = num >> BigInt(64); | ||
const lo = num & BIG_U64_MAX; | ||
off = writeBigU64BE(dst, hi, off); | ||
off = writeBigU64BE(dst, lo, off); | ||
return off; | ||
} | ||
function writeBigU64BE(dst, num, off) { | ||
@@ -653,3 +748,3 @@ // eslint-disable-next-line valid-typeof | ||
num &= BigInt('0xffffffffffffffff'); | ||
num &= BIG_U64_MAX; | ||
@@ -669,3 +764,3 @@ const hi = Number(num >> BigInt(32)); | ||
num &= BigInt('0xffffffffffffff'); | ||
num &= BIG_U56_MAX; | ||
@@ -1332,2 +1427,12 @@ const hi = Number(num >> BigInt(32)); | ||
function throwNoBigInt() { | ||
throw new Error('BigInt not supported.'); | ||
} | ||
function ensureBigInt(func) { | ||
if (typeof BigInt === 'function') | ||
return func; | ||
return throwNoBigInt; | ||
} | ||
/* | ||
@@ -1338,4 +1443,6 @@ * Expose | ||
exports.readU = readU; | ||
exports.readBigU64 = readBigU64; | ||
exports.readBigU56 = readBigU56; | ||
exports.readBigU256 = ensureBigInt(readBigU256); | ||
exports.readBigU128 = ensureBigInt(readBigU128); | ||
exports.readBigU64 = ensureBigInt(readBigU64); | ||
exports.readBigU56 = ensureBigInt(readBigU56); | ||
exports.readU64 = readU64; | ||
@@ -1351,4 +1458,6 @@ exports.readU56 = readU56; | ||
exports.readUBE = readUBE; | ||
exports.readBigU64BE = readBigU64BE; | ||
exports.readBigU56BE = readBigU56BE; | ||
exports.readBigU256BE = ensureBigInt(readBigU256BE); | ||
exports.readBigU128BE = ensureBigInt(readBigU128BE); | ||
exports.readBigU64BE = ensureBigInt(readBigU64BE); | ||
exports.readBigU56BE = ensureBigInt(readBigU56BE); | ||
exports.readU64BE = readU64BE; | ||
@@ -1363,4 +1472,4 @@ exports.readU56BE = readU56BE; | ||
exports.readI = readI; | ||
exports.readBigI64 = readBigI64; | ||
exports.readBigI56 = readBigI56; | ||
exports.readBigI64 = ensureBigInt(readBigI64); | ||
exports.readBigI56 = ensureBigInt(readBigI56); | ||
exports.readI64 = readI64; | ||
@@ -1376,4 +1485,4 @@ exports.readI56 = readI56; | ||
exports.readIBE = readIBE; | ||
exports.readBigI64BE = readBigI64BE; | ||
exports.readBigI56BE = readBigI56BE; | ||
exports.readBigI64BE = ensureBigInt(readBigI64BE); | ||
exports.readBigI56BE = ensureBigInt(readBigI56BE); | ||
exports.readI64BE = readI64BE; | ||
@@ -1393,4 +1502,6 @@ exports.readI56BE = readI56BE; | ||
exports.writeU = writeU; | ||
exports.writeBigU64 = writeBigU64; | ||
exports.writeBigU56 = writeBigU56; | ||
exports.writeBigU256 = ensureBigInt(writeBigU256); | ||
exports.writeBigU128 = ensureBigInt(writeBigU128); | ||
exports.writeBigU64 = ensureBigInt(writeBigU64); | ||
exports.writeBigU56 = ensureBigInt(writeBigU56); | ||
exports.writeU64 = writeU64; | ||
@@ -1406,4 +1517,6 @@ exports.writeU56 = writeU56; | ||
exports.writeUBE = writeUBE; | ||
exports.writeBigU64BE = writeBigU64BE; | ||
exports.writeBigU56BE = writeBigU56BE; | ||
exports.writeBigU256BE = ensureBigInt(writeBigU256BE); | ||
exports.writeBigU128BE = ensureBigInt(writeBigU128BE); | ||
exports.writeBigU64BE = ensureBigInt(writeBigU64BE); | ||
exports.writeBigU56BE = ensureBigInt(writeBigU56BE); | ||
exports.writeU64BE = writeU64BE; | ||
@@ -1418,4 +1531,4 @@ exports.writeU56BE = writeU56BE; | ||
exports.writeI = writeI; | ||
exports.writeBigI64 = writeBigI64; | ||
exports.writeBigI56 = writeBigI56; | ||
exports.writeBigI64 = ensureBigInt(writeBigI64); | ||
exports.writeBigI56 = ensureBigInt(writeBigI56); | ||
exports.writeI64 = writeI64; | ||
@@ -1431,4 +1544,4 @@ exports.writeI56 = writeI56; | ||
exports.writeIBE = writeIBE; | ||
exports.writeBigI64BE = writeBigI64BE; | ||
exports.writeBigI56BE = writeBigI56BE; | ||
exports.writeBigI64BE = ensureBigInt(writeBigI64BE); | ||
exports.writeBigI56BE = ensureBigInt(writeBigI56BE); | ||
exports.writeI64BE = writeI64BE; | ||
@@ -1435,0 +1548,0 @@ exports.writeI56BE = writeI56BE; |
@@ -448,2 +448,62 @@ /*! | ||
/** | ||
* Read uint128le as a BigInt. | ||
* @returns {BigInt} | ||
*/ | ||
readBigU128() { | ||
this.check(16); | ||
const ret = encoding.readBigU128(this.data, this.offset); | ||
this.offset += 16; | ||
return ret; | ||
} | ||
/** | ||
* Read uint128be as a BigInt. | ||
* @returns {BigInt} | ||
*/ | ||
readBigU128BE() { | ||
this.check(16); | ||
const ret = encoding.readBigU128BE(this.data, this.offset); | ||
this.offset += 16; | ||
return ret; | ||
} | ||
/** | ||
* Read uint256le as a BigInt. | ||
* @returns {BigInt} | ||
*/ | ||
readBigU256() { | ||
this.check(32); | ||
const ret = encoding.readBigU256(this.data, this.offset); | ||
this.offset += 32; | ||
return ret; | ||
} | ||
/** | ||
* Read uint256be as a BigInt. | ||
* @returns {BigInt} | ||
*/ | ||
readBigU256BE() { | ||
this.check(32); | ||
const ret = encoding.readBigU256BE(this.data, this.offset); | ||
this.offset += 32; | ||
return ret; | ||
} | ||
/** | ||
* Read int8. | ||
@@ -450,0 +510,0 @@ * @returns {Number} |
@@ -111,2 +111,11 @@ /*! | ||
/** | ||
* Allocate and encode the final buffer. | ||
* @returns {Buffer} Encoded buffer. | ||
*/ | ||
encode() { | ||
return this.render(); | ||
} | ||
/** | ||
* Slice the final buffer at written offset. | ||
@@ -392,2 +401,46 @@ * @returns {Buffer} Rendered buffer. | ||
/** | ||
* Write uint128le. | ||
* @param {BigInt} value | ||
*/ | ||
writeBigU128(value) { | ||
this.check(16); | ||
this.offset = encoding.writeBigU128(this.data, value, this.offset); | ||
return this; | ||
} | ||
/** | ||
* Write uint128be. | ||
* @param {BigInt} value | ||
*/ | ||
writeBigU128BE(value) { | ||
this.check(16); | ||
this.offset = encoding.writeBigU128BE(this.data, value, this.offset); | ||
return this; | ||
} | ||
/** | ||
* Write uint256le. | ||
* @param {BigInt} value | ||
*/ | ||
writeBigU256(value) { | ||
this.check(32); | ||
this.offset = encoding.writeBigU256(this.data, value, this.offset); | ||
return this; | ||
} | ||
/** | ||
* Write uint256be. | ||
* @param {BigInt} value | ||
*/ | ||
writeBigU256BE(value) { | ||
this.check(32); | ||
this.offset = encoding.writeBigU256BE(this.data, value, this.offset); | ||
return this; | ||
} | ||
/** | ||
* Write int8. | ||
@@ -828,2 +881,5 @@ * @param {Number} value | ||
if (size === 0) | ||
return this; | ||
this.check(size); | ||
@@ -836,2 +892,12 @@ | ||
} | ||
/** | ||
* Pad N bytes with value. | ||
* @param {Number} size | ||
* @param {Number} [value=0x00] | ||
*/ | ||
pad(size, value = 0x00) { | ||
return this.fill(value, size); | ||
} | ||
} | ||
@@ -838,0 +904,0 @@ |
@@ -67,2 +67,6 @@ /*! | ||
const BIG_I64BE = 48; | ||
const BIG_U128 = 49; | ||
const BIG_U128BE = 50; | ||
const BIG_U256 = 51; | ||
const BIG_U256BE = 52; | ||
@@ -244,2 +248,14 @@ /** | ||
break; | ||
case BIG_U128: | ||
off = encoding.writeBigU128(data, op.value, off); | ||
break; | ||
case BIG_U128BE: | ||
off = encoding.writeBigU128BE(data, op.value, off); | ||
break; | ||
case BIG_U256: | ||
off = encoding.writeBigU256(data, op.value, off); | ||
break; | ||
case BIG_U256BE: | ||
off = encoding.writeBigU256BE(data, op.value, off); | ||
break; | ||
default: | ||
@@ -259,2 +275,29 @@ throw new Error('Invalid type.'); | ||
/** | ||
* Allocate and encode the final buffer. | ||
* @returns {Buffer} Encoded buffer. | ||
*/ | ||
encode() { | ||
return this.render(); | ||
} | ||
/** | ||
* Finish rendering the buffer. Fill | ||
* remaining bytes with zeroes. | ||
* @param {Number} size | ||
* @returns {Buffer} Rendered buffer. | ||
*/ | ||
finish(size) { | ||
enforce((size >>> 0) === size, 'size', 'integer'); | ||
if (this.offset > size) | ||
throw new EncodingError(this.offset, 'Out of bounds write'); | ||
this.fill(0x00, size - this.offset); | ||
return this.render(); | ||
} | ||
/** | ||
* Get size of data written so far. | ||
@@ -505,2 +548,46 @@ * @returns {Number} | ||
/** | ||
* Write uint128le. | ||
* @param {BigInt} value | ||
*/ | ||
writeBigU128(value) { | ||
this.offset += 16; | ||
this.ops.push(new BigOp(BIG_U128, value)); | ||
return this; | ||
} | ||
/** | ||
* Write uint128be. | ||
* @param {BigInt} value | ||
*/ | ||
writeBigU128BE(value) { | ||
this.offset += 16; | ||
this.ops.push(new BigOp(BIG_U128BE, value)); | ||
return this; | ||
} | ||
/** | ||
* Write uint256le. | ||
* @param {BigInt} value | ||
*/ | ||
writeBigU256(value) { | ||
this.offset += 32; | ||
this.ops.push(new BigOp(BIG_U256, value)); | ||
return this; | ||
} | ||
/** | ||
* Write uint256be. | ||
* @param {BigInt} value | ||
*/ | ||
writeBigU256BE(value) { | ||
this.offset += 32; | ||
this.ops.push(new BigOp(BIG_U256BE, value)); | ||
return this; | ||
} | ||
/** | ||
* Write int8. | ||
@@ -955,2 +1042,12 @@ * @param {Number} value | ||
} | ||
/** | ||
* Pad N bytes with value. | ||
* @param {Number} size | ||
* @param {Number} [value=0x00] | ||
*/ | ||
pad(size, value = 0x00) { | ||
return this.fill(value, size); | ||
} | ||
} | ||
@@ -957,0 +1054,0 @@ |
{ | ||
"name": "bufio", | ||
"version": "1.1.3", | ||
"version": "1.2.0", | ||
"description": "Buffer and serialization utilities for javascript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
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
105710
4024