Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
@agoric/nat
Advanced tools
Ensures that a number is within the natural numbers (0, 1, 2...) or throws a RangeError
Nat(value)
returns its argument if it represents a non-negative
integer (i.e. a "natural number") that can be accurately represented
as a JavaScript
BigInt
,
a built-in object that can be used to represent arbitrarily large integers.
If the argument is not a BigInt
or the argument is negative, an exception is thrown. This makes
it easy to use Nat()
on incoming arguments, or as an assertion on generated
values.
You can think of Nat()
as a type enforcement.
Nat()
can be used to enforce desired properties on account balances, where precision is important.
For instance, in a deposit scenario, you would want to defend against someone "depositing" a negative value. Use Nat
to validate the amount to be deposited before proceeding:
deposit: function(amount) {
amount = Nat(amount);
...
}
Any addition or subtraction expressions dealing with monetary amounts should protected with Nat()
to guard against overflow/underflow errors. Without this check, the two balances might both be safe, but their sum might be too large to represent accurately, causing precision errors in subsequent computation:
Nat(myOldBal + amount);
const srcNewBal = Nat(srcOldBal - amount);
Array indexes can be wrapped with Nat()
, to guard against the surprising string coercion of non-integral index values:
const a = [2,4,6]
function add(index, value) {
a[Nat(index)] = value;
}
add(3, 8); // works
add(2.5, 7); // throws rather than add a key named "2.5"
Nat can be used even in cases where it is not strictly necessary, for extra protection against human error.
Because Nat
uses JavaScript's upcoming BigInt
standard, the range of accurately-representable integers is effectively unbounded.
Nat comes from the Google Caja project, which tested whether a number was a primitive integer within the range of continguously representable non-negative integers.
For more, see the discussion in TC39 notes
FAQs
Ensures that a number is within the natural numbers (0, 1, 2...) or throws a RangeError
We found that @agoric/nat demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers 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
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.