Skip to content

Commit

Permalink
Added Y2K38 safety
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorMcKay committed Mar 15, 2022
1 parent 985e191 commit f8f5e96
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,17 @@ exports.generateConfirmationKey = exports.getConfirmationKey = function(identity
}

let buffer = Buffer.allocUnsafe(dataLen);
// This will be a problem in 2038. Hopefully by then we'll be able to require node v10.20.0 or later, which adds
// Buffer#writeUInt64BE. For now, let's just pretend there won't be a problem because I don't want to add a bigint
// module dependency.
buffer.writeUInt32BE(0, 0);
buffer.writeUInt32BE(time, 4);

// Auto-detect whether we have support for Buffer#writeUInt64BE and use it if we can. If we have writeUInt64BE
// then we also definitely have BigInt.
if (buffer.writeBigUInt64BE) {
buffer.writeBigUInt64BE(BigInt(time), 0);
} else {
// Fall back to old Y2K38-unsafe behavior.
// If you're still using Node.js <10.20.0 in 2038, you only have yourself to blame.
buffer.writeUInt32BE(0, 0);
buffer.writeUInt32BE(time, 4);
}

if (tag) {
buffer.write(tag, 8);
Expand Down

0 comments on commit f8f5e96

Please sign in to comment.