int64-buffer 64-bit Integer Library
int64-buffer is a pure JavaScript library providing classes for reliable manipulation of 64-bit signed and unsigned integers (Int64BE, Uint64BE, Int64LE, Uint64LE). Unlike standard JavaScript numbers, which are IEEE-754 double-precision floats limited to 53 bits of integer precision, this package ensures full 64-bit accuracy. Currently at version 1.1.0, the library appears stable with infrequent but targeted updates, and has no notable release cadence. It stands out by explicitly *not* offering mathematical operations like addition or multiplication; instead, it focuses solely on efficient storage and conversion of 64-bit integers on `Buffer`, `Uint8Array`, or raw `Array` storage. It supports both big-endian (BE) and little-endian (LE) representations and operates without external dependencies, making it a lightweight (3KB minified) and portable solution for scenarios requiring precise 64-bit integer handling, such as parsing binary data streams or network protocols.
Common errors
-
TypeError: big.add is not a function
cause Attempting to perform arithmetic operations (e.g., add, subtract) directly on an `int64-buffer` instance, which the library does not support.fixThis library is for storage and conversion only, not arithmetic. Use `toString()` to get the value and perform arithmetic with a dedicated bignum library, or implement manual operations on the high/low parts. -
Value displayed as 9223372036854776000 instead of 9223372036854775808
cause The 64-bit integer was implicitly or explicitly converted to a standard JavaScript `number`, losing precision for values above `Number.MAX_SAFE_INTEGER` (2^53 - 1).fixAlways use the `toString()` method (e.g., `big.toString(10)`) when you need to retrieve or display the full 64-bit value to maintain accuracy. -
ReferenceError: Buffer is not defined
cause Attempting to use `Buffer.from(...)` or `Buffer.alloc(...)` directly in a browser environment without a Node.js `Buffer` polyfill.fixIn browser environments, prefer `Uint8Array` or `Array` for byte array inputs/outputs. If `Buffer` functionality is required, ensure a compatible polyfill is included in your project.
Warnings
- gotcha This library deliberately does not provide mathematical operations (e.g., add, subtract, multiply, divide) for 64-bit integers. Users requiring arithmetic operations must implement them manually or use a different 'bignum' library.
- gotcha Direct coercion of `int64-buffer` instances to JavaScript's standard `number` type (e.g., `big - 0`) will result in loss of precision for values exceeding `Number.MAX_SAFE_INTEGER` (2^53 - 1). The internal 64-bit precision will be lost.
- gotcha When initializing `Uint64BE` or `Int64BE` with a JavaScript `number` that already exceeds 53 bits of precision, the input `number` itself might already be corrupted, leading to incorrect 64-bit values even before the library processes it.
- gotcha The library internally uses `Buffer` on Node.js and `Uint8Array` in web browsers for storage. While this is handled transparently for most operations, advanced scenarios involving direct buffer manipulation or specific environment dependencies might require awareness of the underlying type.
Install
-
npm install int64-buffer -
yarn add int64-buffer -
pnpm add int64-buffer
Imports
- Int64BE
const Int64BE = require('int64-buffer').Int64BE;import { Int64BE } from 'int64-buffer'; - Uint64BE
const Uint64BE = require('int64-buffer');import { Uint64BE } from 'int64-buffer'; - Int64LE
import * as Int64LE from 'int64-buffer';
import { Int64LE } from 'int64-buffer';
Quickstart
import { Int64BE, Uint64BE, Uint64LE } from 'int64-buffer';
// Working with a signed 64-bit integer (Big Endian)
const signedBig = new Int64BE(-1);
console.log(`Signed Int64BE: ${signedBig.toString(10)}`);
console.log(`Buffer representation: ${signedBig.toBuffer().toString('hex')}`);
// Working with an unsigned 64-bit integer from high/low 32-bit parts (Big Endian)
const unsignedBig = new Uint64BE(0x12345678, 0x9abcdef0);
console.log(`Unsigned Uint64BE (hex): ${unsignedBig.toString(16)}`);
console.log(`Unsigned Uint64BE (decimal string): ${unsignedBig.toString(10)}`);
// Demonstrating precision loss when coercing to standard JavaScript Number
const largeNumberJs = Math.pow(2, 63); // JavaScript Number, often loses precision
const largeUint64BE = new Uint64BE('9223372036854775808', 10); // Constructing from string for accuracy
console.log(`JS number (potential precision loss): ${largeNumberJs}`);
console.log(`Uint64BE (correct value): ${largeUint64BE.toString(10)}`);
// Creating from a byte array (Little Endian example)
const littleEndianBytes = new Uint8Array([0xF0, 0xDE, 0xBC, 0x9A, 0x78, 0x56, 0x34, 0x12]);
const littleEndianBig = new Uint64LE(littleEndianBytes);
console.log(`Uint64LE from bytes (hex): ${littleEndianBig.toString(16)}`);
// Example of writing a value into an existing buffer at an offset
const targetBuffer = Buffer.alloc(16); // Requires Node.js Buffer or a polyfill
const valueToWrite = new Uint64BE(0xCAFEBABE, 0xDEDDECAF);
new Uint64BE(targetBuffer, 8, valueToWrite.high, valueToWrite.low); // Write into buffer at offset 8
console.log(`Buffer with written value at offset 8: ${targetBuffer.toString('hex')}`);