diff --git a/index.js b/index.js index bdea604..3c029ce 100644 --- a/index.js +++ b/index.js @@ -1217,7 +1217,7 @@ Buffer.prototype.readUInt32BE = function readUInt32BE (offset, noAssert) { this[offset + 3]) } -Buffer.prototype.readBigUInt64LE = defineBigIntMethod(function readBigUInt64LE (offset) { +Buffer.prototype.readBigUint64LE = Buffer.prototype.readBigUInt64LE = defineBigIntMethod(function readBigUInt64LE (offset) { offset = offset >>> 0 validateNumber(offset, 'offset') const first = this[offset] @@ -1239,7 +1239,7 @@ Buffer.prototype.readBigUInt64LE = defineBigIntMethod(function readBigUInt64LE ( return BigInt(lo) + (BigInt(hi) << BigInt(32)) }) -Buffer.prototype.readBigUInt64BE = defineBigIntMethod(function readBigUInt64BE (offset) { +Buffer.prototype.readBigUint64BE = Buffer.prototype.readBigUInt64BE = defineBigIntMethod(function readBigUInt64BE (offset) { offset = offset >>> 0 validateNumber(offset, 'offset') const first = this[offset] @@ -1547,11 +1547,11 @@ function wrtBigUInt64BE (buf, value, offset, min, max) { return offset + 8 } -Buffer.prototype.writeBigUInt64LE = defineBigIntMethod(function writeBigUInt64LE (value, offset = 0) { +Buffer.prototype.writeBigUint64LE = Buffer.prototype.writeBigUInt64LE = defineBigIntMethod(function writeBigUInt64LE (value, offset = 0) { return wrtBigUInt64LE(this, value, offset, BigInt(0), BigInt('0xffffffffffffffff')) }) -Buffer.prototype.writeBigUInt64BE = defineBigIntMethod(function writeBigUInt64BE (value, offset = 0) { +Buffer.prototype.writeBigUint64BE = Buffer.prototype.writeBigUInt64BE = defineBigIntMethod(function writeBigUInt64BE (value, offset = 0) { return wrtBigUInt64BE(this, value, offset, BigInt(0), BigInt('0xffffffffffffffff')) }) diff --git a/test/bigint-aliases.js b/test/bigint-aliases.js new file mode 100644 index 0000000..33aa7e5 --- /dev/null +++ b/test/bigint-aliases.js @@ -0,0 +1,36 @@ +const test = require('tape') +const Buffer = require('../').Buffer + +test('BigUint64 aliases', function (t) { + if (typeof BigInt === 'undefined') { + t.skip('BigInt is not supported') + t.end() + return + } + + ;['LE', 'BE'].forEach(function (endianness) { + const read = 'readBigUint64' + endianness + const write = 'writeBigUint64' + endianness + t.equal(typeof Buffer.prototype[read], 'function', read + ' exists') + t.equal(typeof Buffer.prototype[write], 'function', write + ' exists') + t.equal(Buffer.prototype[read], Buffer.prototype['readBigUInt64' + endianness], read + ' is an alias') + t.equal(Buffer.prototype[write], Buffer.prototype['writeBigUInt64' + endianness], write + ' is an alias') + if (!Buffer.prototype[read] || !Buffer.prototype[write]) return + + ;['0', '123456789', '18446744073709551615'].forEach(function (value) { + const buf = Buffer.alloc(9) + t.equal(buf[write](BigInt(value), 1), 9, 'write returns end offset') + t.equal(buf[read](1), BigInt(value), 'read returns the unsigned value') + t.equal(buf[0], 0, 'prefix byte is unchanged') + t.equal(buf[write](BigInt(value)), 8, 'write accepts default offset') + t.equal(buf[read](), BigInt(value), 'read accepts default offset') + }) + + const buf = Buffer.alloc(8) + t.throws(function () { buf[write](BigInt(-1)) }, RangeError) + t.throws(function () { buf[write](BigInt('18446744073709551616')) }, RangeError) + t.throws(function () { buf[read](1) }, RangeError) + t.throws(function () { buf[write](BigInt(0), 1) }, RangeError) + }) + t.end() +})