| 1 |
"use strict"; |
| 2 |
// Copyright 2021-2026 Buf Technologies, Inc. |
| 3 |
// |
| 4 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
// you may not use this file except in compliance with the License. |
| 6 |
// You may obtain a copy of the License at |
| 7 |
// |
| 8 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
// |
| 10 |
// Unless required by applicable law or agreed to in writing, software |
| 11 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
// See the License for the specific language governing permissions and |
| 14 |
// limitations under the License. |
| 15 |
Object.defineProperty(exports, "__esModule", { value: true }); |
| 16 |
exports.protoInt64 = void 0; |
| 17 |
const varint_js_1 = require("./wire/varint.js"); |
| 18 |
/** |
| 19 |
* Int64Support for the current environment. |
| 20 |
*/ |
| 21 |
exports.protoInt64 = makeInt64Support(); |
| 22 |
function makeInt64Support() { |
| 23 |
const dv = new DataView(new ArrayBuffer(8)); |
| 24 |
// note that Safari 14 implements BigInt, but not the DataView methods |
| 25 |
const ok = typeof BigInt === "function" && |
| 26 |
typeof dv.getBigInt64 === "function" && |
| 27 |
typeof dv.getBigUint64 === "function" && |
| 28 |
typeof dv.setBigInt64 === "function" && |
| 29 |
typeof dv.setBigUint64 === "function" && |
| 30 |
(!!globalThis.Deno || |
| 31 |
!!globalThis.Bun || |
| 32 |
typeof process != "object" || |
| 33 |
typeof process.env != "object" || |
| 34 |
process.env.BUF_BIGINT_DISABLE !== "1"); |
| 35 |
if (ok) { |
| 36 |
const MIN = BigInt("-9223372036854775808"); |
| 37 |
const MAX = BigInt("9223372036854775807"); |
| 38 |
const UMIN = BigInt("0"); |
| 39 |
const UMAX = BigInt("18446744073709551615"); |
| 40 |
return { |
| 41 |
zero: BigInt(0), |
| 42 |
supported: true, |
| 43 |
parse(value) { |
| 44 |
const bi = typeof value == "bigint" ? value : BigInt(value); |
| 45 |
if (bi > MAX || bi < MIN) { |
| 46 |
throw new Error(`invalid int64: ${value}`); |
| 47 |
} |
| 48 |
return bi; |
| 49 |
}, |
| 50 |
uParse(value) { |
| 51 |
const bi = typeof value == "bigint" ? value : BigInt(value); |
| 52 |
if (bi > UMAX || bi < UMIN) { |
| 53 |
throw new Error(`invalid uint64: ${value}`); |
| 54 |
} |
| 55 |
return bi; |
| 56 |
}, |
| 57 |
enc(value) { |
| 58 |
dv.setBigInt64(0, this.parse(value), true); |
| 59 |
return { |
| 60 |
lo: dv.getInt32(0, true), |
| 61 |
hi: dv.getInt32(4, true), |
| 62 |
}; |
| 63 |
}, |
| 64 |
uEnc(value) { |
| 65 |
dv.setBigInt64(0, this.uParse(value), true); |
| 66 |
return { |
| 67 |
lo: dv.getInt32(0, true), |
| 68 |
hi: dv.getInt32(4, true), |
| 69 |
}; |
| 70 |
}, |
| 71 |
dec(lo, hi) { |
| 72 |
dv.setInt32(0, lo, true); |
| 73 |
dv.setInt32(4, hi, true); |
| 74 |
return dv.getBigInt64(0, true); |
| 75 |
}, |
| 76 |
uDec(lo, hi) { |
| 77 |
dv.setInt32(0, lo, true); |
| 78 |
dv.setInt32(4, hi, true); |
| 79 |
return dv.getBigUint64(0, true); |
| 80 |
}, |
| 81 |
}; |
| 82 |
} |
| 83 |
return { |
| 84 |
zero: "0", |
| 85 |
supported: false, |
| 86 |
parse(value) { |
| 87 |
if (typeof value != "string") { |
| 88 |
value = value.toString(); |
| 89 |
} |
| 90 |
assertInt64String(value); |
| 91 |
return value; |
| 92 |
}, |
| 93 |
uParse(value) { |
| 94 |
if (typeof value != "string") { |
| 95 |
value = value.toString(); |
| 96 |
} |
| 97 |
assertUInt64String(value); |
| 98 |
return value; |
| 99 |
}, |
| 100 |
enc(value) { |
| 101 |
if (typeof value != "string") { |
| 102 |
value = value.toString(); |
| 103 |
} |
| 104 |
assertInt64String(value); |
| 105 |
return (0, varint_js_1.int64FromString)(value); |
| 106 |
}, |
| 107 |
uEnc(value) { |
| 108 |
if (typeof value != "string") { |
| 109 |
value = value.toString(); |
| 110 |
} |
| 111 |
assertUInt64String(value); |
| 112 |
return (0, varint_js_1.int64FromString)(value); |
| 113 |
}, |
| 114 |
dec(lo, hi) { |
| 115 |
return (0, varint_js_1.int64ToString)(lo, hi); |
| 116 |
}, |
| 117 |
uDec(lo, hi) { |
| 118 |
return (0, varint_js_1.uInt64ToString)(lo, hi); |
| 119 |
}, |
| 120 |
}; |
| 121 |
} |
| 122 |
function assertInt64String(value) { |
| 123 |
if (!/^-?[0-9]+$/.test(value)) { |
| 124 |
throw new Error("invalid int64: " + value); |
| 125 |
} |
| 126 |
} |
| 127 |
function assertUInt64String(value) { |
| 128 |
if (!/^[0-9]+$/.test(value)) { |
| 129 |
throw new Error("invalid uint64: " + value); |
| 130 |
} |
| 131 |
} |