Debian Codegraph @

node/@bufbuild/protobuf/dist/esm/proto-int64.js

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