Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down Expand Up @@ -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'))
})

Expand Down
36 changes: 36 additions & 0 deletions test/bigint-aliases.js
Original file line number Diff line number Diff line change
@@ -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()
})