Line data Source code
1 : /**
2 : * The generated zod schemas coerce every int64 to `bigint` and accept the
3 : * whole signed range, while the generated types the host reads through say
4 : * `number`. `Number()` on a value past 2^53 rounds silently, so a value the
5 : * host cannot hold exactly is refused rather than reshaped: the read then
6 : * fails as malformed, the way a body the core cannot decode does.
7 : */
8 2356 : const MAX_SAFE = BigInt(Number.MAX_SAFE_INTEGER)
9 :
10 0 : export function asSafeNumber(value: bigint): number | undefined {
11 0 : return value >= -MAX_SAFE && value <= MAX_SAFE ? Number(value) : undefined
12 : }
|