{"record":{"id":"1af1468eb2d77fca","repo":"denoland/deno","slug":"value-must-fit-in-a-64-bit-unsigned-integer","errorCode":null,"errorMessage":"Value must fit in a 64-bit unsigned integer","messagePattern":"Value must fit in a 64-bit unsigned integer","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"ext/kv/01_db.ts","lineNumber":740,"sourceCode":"}\n\nconst MIN_U64 = BigInt(\"0\");\nconst MAX_U64 = BigInt(\"0xffffffffffffffff\");\n\nclass KvU64 {\n  value: bigint;\n\n  constructor(value: bigint) {\n    if (typeof value !== \"bigint\") {\n      throw new TypeError(`Value must be a bigint: received ${typeof value}`);\n    }\n    if (value < MIN_U64) {\n      throw new RangeError(\n        `Value must be a positive bigint: received ${value}`,\n      );\n    }\n    if (value > MAX_U64) {\n      throw new RangeError(\"Value must fit in a 64-bit unsigned integer\");\n    }\n    this.value = value;\n    ObjectFreeze(this);\n  }\n\n  valueOf() {\n    return this.value;\n  }\n\n  toString() {\n    return BigIntPrototypeToString(this.value);\n  }\n\n  get [SymbolToStringTag]() {\n    return \"Deno.KvU64\";\n  }\n\n  [SymbolFor(\"Deno.privateCustomInspect\")](inspect, inspectOptions) {","sourceCodeStart":722,"sourceCodeEnd":758,"githubUrl":"https://github.com/denoland/deno/blob/9ad36f7a2cce60488e6ec52283efb32efddaf93a/ext/kv/01_db.ts#L722-L758","documentation":"KvU64 must fit the unsigned 64-bit range; a bigint above 0xffffffffffffffff (2^64 - 1, MAX_U64 in the source) throws RangeError('Value must fit in a 64-bit unsigned integer'). Overflow is rejected rather than wrapped because silent wrapping would corrupt counters.","triggerScenarios":"`.sum(key, 2n ** 64n)`; accumulated products (price * quantity) exceeding 2^64; bit math like 1n << 64n; importing 128-bit identifiers or hashes as counters.","commonSituations":"Aggregations over huge magnitudes (wei/gwei token amounts, nanosecond timestamps multiplied by counts); migrating 128-bit values from other stores into Kv counters.","solutions":["Bound-check before the mutation: value <= 0xffff_ffff_ffff_ffffn.","Re-model big magnitudes: store as bytes, split into hi/lo counters, or track in multiple keys.","Scale units down (store milliseconds instead of nanoseconds, whole tokens instead of base units) so sums stay in range."],"exampleFix":"// before\nawait kv.atomic().sum([\"volume\"], weiAmount).commit(); // may exceed 2^64-1\n\n// after\nconst MAX_U64 = 0xffffffffffffffffn;\nif (weiAmount > MAX_U64) throw new RangeError(\"amount exceeds u64\");\nawait kv.atomic().sum([\"volume\"], weiAmount).commit();","handlingStrategy":"validation","validationCode":"const MAX_U64 = 0xffffffffffffffffn;\nfunction assertU64(v: bigint) {\n  if (v > MAX_U64) throw new RangeError(`value exceeds 64-bit unsigned range: ${v}`);\n  if (v < 0n) throw new RangeError(`value must be unsigned: ${v}`);\n}\nassertU64(total);\nawait kv.atomic().sum(key, total).commit();","typeGuard":"const fitsU64 = (v: bigint): boolean => v >= 0n && v <= 0xffffffffffffffffn;","tryCatchPattern":null,"preventionTips":["Check magnitude against 2^64-1 before summing large products","Scale units (ms not ns, whole tokens not base units) to stay in range","Store >64-bit magnitudes as bytes or split hi/lo counters instead of forcing u64"],"tags":["deno-kv","bigint","u64","rangeerror","overflow"],"backgroundTag":"integer-overflow","analyzedSha":"9ad36f7a2cce60488e6ec52283efb32efddaf93a","analyzedAt":"2026-08-20T13:07:44.778Z","contentChangedAt":"2026-08-20T13:07:44.778Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}