fix(events): prevent float precision loss in nanosecond timestamp conversion#4315
fix(events): prevent float precision loss in nanosecond timestamp conversion#4315okxint wants to merge 1 commit into
Conversation
…version Multiplying milliseconds by 1_000_000 before BigInt conversion loses precision because the result (~1.7e18) exceeds Number.MAX_SAFE_INTEGER. Switch all three call sites to use convertDateToNanoseconds(), which does BigInt(ms) * BigInt(1_000_000) and keeps the arithmetic in BigInt-land throughout.
|
|
Hi @okxint, thanks for your interest in contributing! This project requires that pull request authors are vouched, and you are not in the list of vouched users. This PR will be closed automatically. See https://github.com/triggerdotdev/trigger.dev/blob/main/CONTRIBUTING.md for more details. |
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
WalkthroughThe change replaces floating-point millisecond-to-nanosecond conversions with ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Fixes #3292
new Date().getTime() * 1_000_000is computed as a JS number before being passed toBigInt(). The result (~1.7e18) exceedsNumber.MAX_SAFE_INTEGER(9.007e15), so IEEE 754 precision loss corrupts the timestamp before the BigInt is even created.The correct helper
convertDateToNanoseconds()already exists incommon.server.tsand usesBigInt(date.getTime()) * BigInt(1_000_000)— converting to BigInt first, then multiplying — which preserves full precision.Changes:
common.server.tsgetNowInNanoseconds: replace inline multiply withconvertDateToNanoseconds(new Date())common.server.tscalculateDurationFromStart: replace inline multiply withconvertDateToNanoseconds($endtime)index.server.tsrecordRunEvent: replace inline multiply withconvertDateToNanoseconds(startTime ?? new Date())+ import the helper