Large Integers in JavaScript
As mentioned in Storing RPDE modified
with less than 64-bit integers, the RPDE modified
field can be expected to have values too large to be able to be precisely represented by JavaScript's number type.
In some cases, this may be no issue. If your RPDE feed harvesting implementation does not do anything with the modified
field, then that's fine. However, if your implementation involves asynchronous and concurrent processing in such a way that individual RPDE items can be stored not strictly in the order that they appeared in the feed, you will need to use modified
comparisons in order to determine which RPDE item is the most up to date.
If doing this, the recommended approach is to:
Recommended Approach
Parse the
modified
from the RPDE page using a custom JSON parser, that can handle large integers — we recommend lossless-json.Keep it as a string in memory.
NOTE: The recommended approach is to keep it in memory as a string and NOT a BigInt. This is because most of the JavaScript ecosystem cannot handle BigInts. In a sufficiently complicated app, where you have HTTP requests, database integration, logging, file system read/writes, etc, each one of these integration points will break as soon as it encounters a BigInt. Experience shows that this can be much harder to maintain and so we instead recommend storing this data as strings and temporarily using BigInts for numeric comparison only.
If comparing the
modified
of two different RPDE items, use JavaScript's BigInt type for the numeric comparison e.g.BigInt(aModified) >= BigInt(bModified)
.When storing to a database, store with the database's 64-bit numeric type (
bigint
in PostgreSQL).
Example Code
Here's some example code demonstrating fetching an RPDE page with axios:
Things to note about the above example:
It uses lossless-json, which has been proven to work for this issue (problems with
JSON.parse
'sreviver
and with the json-bigint library are discussed within this GitHub issue).It uses both strings and BigInts at various points to handle the data. Each of these is explained in comments, but broadly it uses the rules defined in the recommended approach.
Last updated