
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
int64-native
Advanced tools
int64-native
is a simple uint64_t
wrapper for JavaScript, enabling the
use of 64-bit unsigned integers from node.
int64-native
was originally developed to support reasonable handling of
64-bit ID columns in databases. There are other 64-bit integer modules out
there, but AFAICT all of them are pure JavaScript; native uint64_t
seemed
like a better way to handle this!
The one caveat is that you won't be able to use this browser-side. However, you can use the string representation to pass 64-bit values from server to client.
npm install int64-native
git clone git://github.com/candu/node-int64-native.git
cd node-int64-native
npm install
int64-native
is built using node-gyp
.
All of the following examples are borrowed from test/int64.js
, which you
can run via
npm test
require()
gives you direct access to the constructor:
var Int64 = require('int64-native');
You can create an Int64
as follows:
var x = new Int64(),
y = new Int64(42),
z = new Int64(0xfedcba98, 0x76543210),
w = new Int64('0xfedcba9876543210')
expect(x.toString()).to.equal('0x0000000000000000');
expect(y.toString()).to.equal('0x000000000000002a');
expect(z.toString()).to.equal('0xfedcba9876543210');
expect(w.toString()).to.equal('0xfedcba9876543210');
The last two methods allow you to represent uint64_t
values larger than
(1 << 53) - 1
.
Int64
exposes toNumber()
, valueOf()
for converting to numeric values:
var a = new Int64(2),
b = new Int64(3);
expect(a + b).to.equal(5);
var x = new Int64(),
y = new Int64(42),
z = new Int64(0xfedcba98, 0x76543210),
w = new Int64('0xfedcba9876543210')
expect(+x).to.equal(0);
expect(+y).to.equal(42);
expect(+z).to.equal(Infinity);
expect(+w).to.equal(Infinity);
Values larger than (1 << 53) - 1
will be converted to Infinity
, since
they cannot be accurately represented using JavaScript's Number
type.
As you can see from the examples so far, toString()
produces the hex string
corresponding to an Int64
.
Int64
also exposes toSignedDecimalString()
and toUnsignedDecimalString()
for converting to decimal strings:
var minusOne = new Int64('0xFFFFFFFFFFFFFFFF');
expect(minusOne.toSignedDecimalString()).to.equal('-1');
expect(minusOne.toUnsignedDecimalString()).to.equal('18446744073709551615');
For cases where you wish to sort or compare Int64
values, equals()
and
compare()
are provided:
var a = new Int64(2),
b = new Int64(3);
expect(a.equals(a)).to.be.true;
expect(a.equals(b)).to.be.false;
expect(a.compare(a)).to.equal(0);
expect(a.compare(b)).to.equal(-1);
expect(b.compare(a)).to.equal(1);
There are several operations for bit-level manipulation of Int64
values:
var x = new Int64('0xfedcba9876543210');
expect(x.high32().toString(16)).to.equal('fedcba98');
expect(x.low32().toString(16)).to.equal('76543210');
var y = x.and(new Int64(0xffff)),
z = x.or(new Int64(0xffff)),
w = x.xor(new Int64('fffffffffffffffff'));
expect(y.toString()).to.equal('0x0000000000003210');
expect(z.toString()).to.equal('0xfedcba987654ffff');
expect(w.toString()).to.equal('0x0123456789abcdef');
var a = new Int64(7),
b = a.shiftLeft(1),
c = a.shiftRight(1);
expect(b.toString()).to.equal('0x000000000000000e');
expect(c.toString()).to.equal('0x0000000000000003');
FAQs
A simple uint64_t wrapper for JavaScript
The npm package int64-native receives a total of 22 weekly downloads. As such, int64-native popularity was classified as not popular.
We found that int64-native demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.