@gsknnft/bigint-buffer 1.4.4 → 1.4.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -90
- package/build/Release/bigint_buffer.iobj +0 -0
- package/build/Release/bigint_buffer.ipdb +0 -0
- package/build/Release/bigint_buffer.node +0 -0
- package/build/Release/bigint_buffer.pdb +0 -0
- package/build/Release/obj/bigint_buffer/src/bigint-buffer.obj +0 -0
- package/build/Release/obj/bigint_buffer/win_delay_load_hook.obj +0 -0
- package/build/binding.sln +3 -3
- package/build/conversion/index.js +18 -0
- package/build/conversion/index.js.map +1 -0
- package/build/conversion/src/ts/fixedPoint/index.d.ts +12 -0
- package/build/conversion/src/ts/fixedPoint/index.js +79 -0
- package/build/conversion/src/ts/fixedPoint/index.js.map +1 -0
- package/build/conversion/src/ts/index.d.ts +169 -0
- package/build/conversion/src/ts/index.js +453 -0
- package/build/conversion/src/ts/index.js.map +1 -0
- package/build/conversion/test/bigintToBase64.test.js +72 -0
- package/build/conversion/test/bigintToBase64.test.js.map +1 -0
- package/build/conversion/test/bigintToBuf.test.js +77 -0
- package/build/conversion/test/bigintToBuf.test.js.map +1 -0
- package/build/conversion/test/bigintToHex.test.js +86 -0
- package/build/conversion/test/bigintToHex.test.js.map +1 -0
- package/build/conversion/test/bigintToText.test.js +63 -0
- package/build/conversion/test/bigintToText.test.js.map +1 -0
- package/build/conversion/test/bufToBigint.test.js +55 -0
- package/build/conversion/test/bufToBigint.test.js.map +1 -0
- package/build/conversion/test/hexToBigint.test.js +56 -0
- package/build/conversion/test/hexToBigint.test.js.map +1 -0
- package/build/conversion/test/hexToBuf.test.js +73 -0
- package/build/conversion/test/hexToBuf.test.js.map +1 -0
- package/build/conversion/test/parseHex.test.js +69 -0
- package/build/conversion/test/parseHex.test.js.map +1 -0
- package/build/conversion/test/textToBuf.test.js +59 -0
- package/build/conversion/test/textToBuf.test.js.map +1 -0
- package/build/index.js +242 -0
- package/build/index.js.map +1 -0
- package/dist/index.cjs +1 -1
- package/dist/index.js +257 -200
- package/dist/index.umd.js +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/types/conversion/src/ts/fixedPoint/index.d.ts +12 -0
- package/dist/types/conversion/src/ts/index.d.ts +1 -0
- package/package.json +132 -132
- package/src/conversion/docs/README.md +9 -1
- package/src/conversion/src/docs/index.md +6 -1
- package/src/conversion/src/ts/fixedPoint/index.ts +87 -0
- package/src/conversion/src/ts/index.ts +15 -0
- /package/dist/types/dist/dist/{types/index.d.ts → index.d.ts} +0 -0
package/dist/index.js
CHANGED
|
@@ -1,91 +1,138 @@
|
|
|
1
1
|
import { Buffer as o } from "buffer";
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
const
|
|
2
|
+
import l from "path";
|
|
3
|
+
import N from "fs";
|
|
4
|
+
import { hexToBigint as R, bigintToHex as F } from "#pkg";
|
|
5
|
+
const m = 9, d = "0x0", U = (t) => t.startsWith("0x") || t.startsWith("0X") ? t : `0x${t}`, B = (t) => {
|
|
6
|
+
if (t === 0n)
|
|
7
|
+
return d;
|
|
8
|
+
const e = t < 0n, n = e ? -t : t, r = F(n);
|
|
9
|
+
return `${e ? "-" : ""}0x${r}`;
|
|
10
|
+
}, s = (t) => {
|
|
11
|
+
if (!t)
|
|
12
|
+
return 0n;
|
|
13
|
+
const e = t.trim();
|
|
14
|
+
if (e.length === 0)
|
|
15
|
+
return 0n;
|
|
16
|
+
const n = e.startsWith("-"), r = n ? e.slice(1) : e, i = U(r), c = R(i);
|
|
17
|
+
return n ? -c : c;
|
|
18
|
+
};
|
|
19
|
+
function O(t, e = m) {
|
|
20
|
+
if (!Number.isFinite(t))
|
|
21
|
+
return d;
|
|
22
|
+
const n = Math.pow(10, e), r = BigInt(Math.round(t * n));
|
|
23
|
+
return B(r);
|
|
24
|
+
}
|
|
25
|
+
function H(t, e = m) {
|
|
26
|
+
const n = s(t);
|
|
27
|
+
if (n === 0n)
|
|
28
|
+
return 0;
|
|
29
|
+
const r = Math.pow(10, e);
|
|
30
|
+
return Number(n) / r;
|
|
31
|
+
}
|
|
32
|
+
function L(t, e) {
|
|
33
|
+
return B(s(t) + s(e));
|
|
34
|
+
}
|
|
35
|
+
function M(t, e) {
|
|
36
|
+
return B(s(t) - s(e));
|
|
37
|
+
}
|
|
38
|
+
function P(t) {
|
|
39
|
+
if (t.length === 0)
|
|
40
|
+
return d;
|
|
41
|
+
const e = t.reduce((n, r) => n + s(r), 0n);
|
|
42
|
+
return B(e / BigInt(t.length));
|
|
43
|
+
}
|
|
44
|
+
function V(t, e) {
|
|
45
|
+
const n = s(t) - s(e);
|
|
46
|
+
return n === 0n ? 0 : n > 0n ? 1 : -1;
|
|
47
|
+
}
|
|
48
|
+
function z(t) {
|
|
49
|
+
return s(t);
|
|
50
|
+
}
|
|
51
|
+
let h = !1, f, b;
|
|
52
|
+
const a = typeof globalThis < "u" && typeof globalThis.document < "u", I = [
|
|
6
53
|
// when running from dist/
|
|
7
|
-
|
|
54
|
+
l.resolve(__dirname, ".."),
|
|
8
55
|
// when running from build/conversion/src/ts
|
|
9
|
-
|
|
56
|
+
l.resolve(__dirname, "../../.."),
|
|
10
57
|
// when running from src/conversion/src/ts
|
|
11
|
-
|
|
12
|
-
],
|
|
13
|
-
for (const
|
|
14
|
-
const
|
|
15
|
-
if (
|
|
58
|
+
l.resolve(__dirname, "../../../../")
|
|
59
|
+
], W = () => {
|
|
60
|
+
for (const t of I) {
|
|
61
|
+
const e = l.join(t, "build", "Release", "bigint_buffer.node");
|
|
62
|
+
if (N.existsSync(e)) return t;
|
|
16
63
|
}
|
|
17
|
-
return
|
|
64
|
+
return I[0];
|
|
18
65
|
};
|
|
19
|
-
function
|
|
66
|
+
function D() {
|
|
20
67
|
try {
|
|
21
|
-
const
|
|
22
|
-
return
|
|
68
|
+
const t = require("bindings"), e = W();
|
|
69
|
+
return t({
|
|
23
70
|
bindings: "bigint_buffer",
|
|
24
|
-
module_root:
|
|
71
|
+
module_root: e
|
|
25
72
|
});
|
|
26
|
-
} catch (
|
|
27
|
-
|
|
73
|
+
} catch (t) {
|
|
74
|
+
b = t;
|
|
28
75
|
return;
|
|
29
76
|
}
|
|
30
77
|
}
|
|
31
|
-
|
|
78
|
+
a || (f = D(), h = f !== void 0, !h && b !== void 0 && process.env?.BIGINT_BUFFER_SILENT_NATIVE_FAIL !== "1" && console.warn(
|
|
32
79
|
"bigint-buffer: Failed to load native bindings; using pure JS fallback. Run npm run rebuild to restore native.",
|
|
33
|
-
|
|
80
|
+
b
|
|
34
81
|
));
|
|
35
82
|
f === void 0 && (f = {
|
|
36
|
-
toBigInt: (
|
|
37
|
-
const n = o.from(
|
|
38
|
-
|
|
83
|
+
toBigInt: (t, e = !0) => {
|
|
84
|
+
const n = o.from(t);
|
|
85
|
+
e || n.reverse();
|
|
39
86
|
const r = n.toString("hex");
|
|
40
87
|
return r.length === 0 ? 0n : BigInt(`0x${r}`);
|
|
41
88
|
},
|
|
42
|
-
fromBigInt: (
|
|
43
|
-
const r =
|
|
44
|
-
return n ||
|
|
89
|
+
fromBigInt: (t, e, n = !0) => {
|
|
90
|
+
const r = t.toString(16), i = e.length, c = r.padStart(i * 2, "0").slice(0, i * 2), p = o.from(c, "hex");
|
|
91
|
+
return n || p.reverse(), p.copy(e), e;
|
|
45
92
|
}
|
|
46
93
|
});
|
|
47
|
-
function v(
|
|
48
|
-
if (
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
const n =
|
|
94
|
+
function v(t) {
|
|
95
|
+
if (a || f === void 0) {
|
|
96
|
+
const e = o.from(t);
|
|
97
|
+
e.reverse();
|
|
98
|
+
const n = e.toString("hex");
|
|
52
99
|
return n.length === 0 ? BigInt(0) : BigInt(`0x${n}`);
|
|
53
100
|
}
|
|
54
|
-
return f.toBigInt(
|
|
101
|
+
return f.toBigInt(t, !1);
|
|
55
102
|
}
|
|
56
|
-
function
|
|
103
|
+
function j() {
|
|
57
104
|
try {
|
|
58
105
|
return v(o.from([1, 0])) === BigInt(1);
|
|
59
106
|
} catch {
|
|
60
107
|
return !1;
|
|
61
108
|
}
|
|
62
109
|
}
|
|
63
|
-
function
|
|
64
|
-
if (
|
|
65
|
-
const
|
|
66
|
-
return
|
|
110
|
+
function X(t) {
|
|
111
|
+
if (a || f === void 0) {
|
|
112
|
+
const e = t.toString("hex");
|
|
113
|
+
return e.length === 0 ? BigInt(0) : BigInt(`0x${e}`);
|
|
67
114
|
}
|
|
68
|
-
return f.toBigInt(
|
|
115
|
+
return f.toBigInt(t, !0);
|
|
69
116
|
}
|
|
70
|
-
function
|
|
71
|
-
if (
|
|
72
|
-
const n =
|
|
73
|
-
n.padStart(
|
|
117
|
+
function Z(t, e) {
|
|
118
|
+
if (a || f === void 0) {
|
|
119
|
+
const n = t.toString(16), r = o.from(
|
|
120
|
+
n.padStart(e * 2, "0").slice(0, e * 2),
|
|
74
121
|
"hex"
|
|
75
122
|
);
|
|
76
123
|
return r.reverse(), r;
|
|
77
124
|
}
|
|
78
|
-
return f.fromBigInt(
|
|
125
|
+
return f.fromBigInt(t, o.allocUnsafe(e), !1);
|
|
79
126
|
}
|
|
80
|
-
function
|
|
81
|
-
if (
|
|
82
|
-
const n =
|
|
83
|
-
return o.from(n.padStart(
|
|
127
|
+
function q(t, e) {
|
|
128
|
+
if (a || f === void 0) {
|
|
129
|
+
const n = t.toString(16);
|
|
130
|
+
return o.from(n.padStart(e * 2, "0").slice(0, e * 2), "hex");
|
|
84
131
|
}
|
|
85
|
-
return f.fromBigInt(
|
|
132
|
+
return f.fromBigInt(t, o.allocUnsafe(e), !0);
|
|
86
133
|
}
|
|
87
|
-
function
|
|
88
|
-
const r =
|
|
134
|
+
function g(t, e = !1, n) {
|
|
135
|
+
const r = t.match(/^(0x)?([\da-fA-F]+)$/);
|
|
89
136
|
if (r == null)
|
|
90
137
|
throw new RangeError(
|
|
91
138
|
"input must be a hexadecimal string, e.g. '0x124fe3a' or '0214f1b2'"
|
|
@@ -100,249 +147,259 @@ function a(e, t = !1, n) {
|
|
|
100
147
|
);
|
|
101
148
|
i = i.padStart(n * 2, "0");
|
|
102
149
|
}
|
|
103
|
-
return
|
|
150
|
+
return e ? "0x" + i : i;
|
|
104
151
|
}
|
|
105
|
-
function
|
|
106
|
-
if (
|
|
152
|
+
function w(t, e = !1) {
|
|
153
|
+
if (t < 0)
|
|
107
154
|
throw RangeError(
|
|
108
155
|
"a should be a non-negative integer. Negative values are not supported"
|
|
109
156
|
);
|
|
110
|
-
return
|
|
157
|
+
return T(y(t), e);
|
|
111
158
|
}
|
|
112
|
-
function
|
|
113
|
-
let
|
|
114
|
-
ArrayBuffer.isView(
|
|
159
|
+
function E(t) {
|
|
160
|
+
let e = 8n;
|
|
161
|
+
ArrayBuffer.isView(t) ? e = BigInt(t.BYTES_PER_ELEMENT * 8) : t = new Uint8Array(t);
|
|
115
162
|
let n = 0n;
|
|
116
|
-
for (const r of
|
|
163
|
+
for (const r of t.values()) {
|
|
117
164
|
const i = BigInt(r);
|
|
118
|
-
n = (n <<
|
|
165
|
+
n = (n << e) + i;
|
|
119
166
|
}
|
|
120
167
|
return n;
|
|
121
168
|
}
|
|
122
|
-
function
|
|
123
|
-
if (
|
|
169
|
+
function y(t, e = !1, n) {
|
|
170
|
+
if (t < 0)
|
|
124
171
|
throw RangeError(
|
|
125
172
|
"a should be a non-negative integer. Negative values are not supported"
|
|
126
173
|
);
|
|
127
|
-
return
|
|
174
|
+
return g(t.toString(16), e, n);
|
|
128
175
|
}
|
|
129
|
-
function
|
|
130
|
-
return BigInt(
|
|
176
|
+
function S(t) {
|
|
177
|
+
return BigInt(g(t, !0));
|
|
131
178
|
}
|
|
132
|
-
function
|
|
133
|
-
if (
|
|
179
|
+
function J(t) {
|
|
180
|
+
if (t < 0)
|
|
134
181
|
throw RangeError(
|
|
135
182
|
"a should be a non-negative integer. Negative values are not supported"
|
|
136
183
|
);
|
|
137
|
-
return
|
|
138
|
-
}
|
|
139
|
-
function
|
|
140
|
-
return
|
|
141
|
-
}
|
|
142
|
-
function
|
|
143
|
-
if (o.isBuffer(
|
|
144
|
-
return
|
|
145
|
-
if (ArrayBuffer.isView(
|
|
146
|
-
return o.from(
|
|
147
|
-
if (
|
|
148
|
-
return o.from(new Uint8Array(
|
|
184
|
+
return $(T(t.toString(16)));
|
|
185
|
+
}
|
|
186
|
+
function k(t) {
|
|
187
|
+
return S(A(_(t)));
|
|
188
|
+
}
|
|
189
|
+
function x(t) {
|
|
190
|
+
if (o.isBuffer(t))
|
|
191
|
+
return t;
|
|
192
|
+
if (ArrayBuffer.isView(t))
|
|
193
|
+
return o.from(t.buffer, t.byteOffset, t.byteLength);
|
|
194
|
+
if (t instanceof ArrayBuffer)
|
|
195
|
+
return o.from(new Uint8Array(t));
|
|
149
196
|
throw new TypeError("Unsupported input type for Buffer.from");
|
|
150
197
|
}
|
|
151
|
-
function
|
|
152
|
-
const
|
|
153
|
-
return
|
|
198
|
+
function $(t) {
|
|
199
|
+
const e = x(t);
|
|
200
|
+
return a ? new TextDecoder().decode(new Uint8Array(e)) : o.from(e).toString();
|
|
154
201
|
}
|
|
155
|
-
function
|
|
156
|
-
return !
|
|
202
|
+
function _(t, e = !1) {
|
|
203
|
+
return !a && !e ? o.from(new TextEncoder().encode(t).buffer) : new TextEncoder().encode(t).buffer;
|
|
157
204
|
}
|
|
158
|
-
function
|
|
159
|
-
if (
|
|
205
|
+
function A(t, e = !1, n) {
|
|
206
|
+
if (a) {
|
|
160
207
|
let r = "";
|
|
161
208
|
const i = "0123456789abcdef";
|
|
162
|
-
return ArrayBuffer.isView(
|
|
163
|
-
|
|
164
|
-
) :
|
|
165
|
-
r += i[
|
|
166
|
-
}),
|
|
209
|
+
return ArrayBuffer.isView(t) ? t = new Uint8Array(
|
|
210
|
+
t.buffer.slice(t.byteOffset, t.byteOffset + t.byteLength)
|
|
211
|
+
) : t = new Uint8Array(t), t.forEach((c) => {
|
|
212
|
+
r += i[c >> 4] + i[c & 15];
|
|
213
|
+
}), g(r, e, n);
|
|
167
214
|
} else {
|
|
168
|
-
const r =
|
|
169
|
-
return ArrayBuffer.isView(r) && (
|
|
215
|
+
const r = x(t);
|
|
216
|
+
return ArrayBuffer.isView(r) && (t = new Uint8Array(
|
|
170
217
|
r.buffer.slice(
|
|
171
218
|
r.byteOffset,
|
|
172
219
|
r.byteOffset + r.byteLength
|
|
173
220
|
)
|
|
174
|
-
)),
|
|
175
|
-
o.from(
|
|
176
|
-
|
|
221
|
+
)), g(
|
|
222
|
+
o.from(x(t)).toString("hex"),
|
|
223
|
+
e,
|
|
177
224
|
n
|
|
178
225
|
);
|
|
179
226
|
}
|
|
180
227
|
}
|
|
181
|
-
function
|
|
182
|
-
let n =
|
|
183
|
-
if (n =
|
|
228
|
+
function T(t, e = !1) {
|
|
229
|
+
let n = g(t);
|
|
230
|
+
if (n = g(t, !1, Math.ceil(n.length / 2)), a)
|
|
184
231
|
return Uint8Array.from(
|
|
185
232
|
n.match(/[\da-fA-F]{2}/g).map((r) => +("0x" + r))
|
|
186
233
|
).buffer;
|
|
187
234
|
{
|
|
188
235
|
const r = o.from(n, "hex");
|
|
189
|
-
return
|
|
236
|
+
return e ? r.buffer.slice(r.byteOffset, r.byteOffset + r.byteLength) : r;
|
|
190
237
|
}
|
|
191
238
|
}
|
|
192
|
-
function
|
|
193
|
-
if (
|
|
239
|
+
function C(t, e = !1, n = !0) {
|
|
240
|
+
if (t < 0n)
|
|
194
241
|
throw new RangeError("negative bigint");
|
|
195
|
-
const r =
|
|
242
|
+
const r = w(t);
|
|
196
243
|
let i = o.isBuffer(r) ? r.toString("base64") : o.from(r).toString("base64");
|
|
197
|
-
return
|
|
244
|
+
return e && (i = i.replace(/\+/g, "-").replace(/\//g, "_")), n || (i = i.replace(/=+$/, "")), i;
|
|
198
245
|
}
|
|
199
|
-
function
|
|
200
|
-
if (!
|
|
246
|
+
function G(t) {
|
|
247
|
+
if (!t || t.trim() === "")
|
|
201
248
|
return 0n;
|
|
202
|
-
const
|
|
203
|
-
if (!/^[A-Za-z0-9+/=_-]*$/.test(
|
|
249
|
+
const e = t.trim();
|
|
250
|
+
if (!/^[A-Za-z0-9+/=_-]*$/.test(e))
|
|
204
251
|
throw new RangeError("invalid base64");
|
|
205
|
-
let n =
|
|
252
|
+
let n = e.replace(/-/g, "+").replace(/_/g, "/");
|
|
206
253
|
for (; n.length % 4 !== 0; )
|
|
207
254
|
n += "=";
|
|
208
255
|
const r = o.from(n, "base64");
|
|
209
|
-
return
|
|
256
|
+
return E(r);
|
|
210
257
|
}
|
|
211
|
-
const
|
|
258
|
+
const st = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
212
259
|
__proto__: null,
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
260
|
+
FIXED_POINT_DECIMALS: m,
|
|
261
|
+
ZERO_FIXED_POINT: d,
|
|
262
|
+
addFixedPoint: L,
|
|
263
|
+
averageFixedPoint: P,
|
|
264
|
+
base64ToBigint: G,
|
|
265
|
+
bigintToBase64: C,
|
|
266
|
+
bigintToBuf: w,
|
|
267
|
+
bigintToHex: y,
|
|
268
|
+
bigintToText: J,
|
|
269
|
+
bufToBigint: E,
|
|
270
|
+
bufToHex: A,
|
|
271
|
+
bufToText: $,
|
|
272
|
+
compareFixedPoint: V,
|
|
273
|
+
fixedPointToBigInt: z,
|
|
274
|
+
fromFixedPoint: H,
|
|
275
|
+
hexToBigint: S,
|
|
276
|
+
hexToBuf: T,
|
|
223
277
|
get isNative() {
|
|
224
|
-
return
|
|
278
|
+
return h;
|
|
225
279
|
},
|
|
226
|
-
parseHex:
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
280
|
+
parseHex: g,
|
|
281
|
+
subtractFixedPoint: M,
|
|
282
|
+
textToBigint: k,
|
|
283
|
+
textToBuf: _,
|
|
284
|
+
toBigIntBE: X,
|
|
230
285
|
toBigIntLE: v,
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
286
|
+
toBigIntValue: s,
|
|
287
|
+
toBufferBE: q,
|
|
288
|
+
toBufferLE: Z,
|
|
289
|
+
toFixedPoint: O,
|
|
290
|
+
validateBigIntBuffer: j
|
|
234
291
|
}, Symbol.toStringTag, { value: "Module" }));
|
|
235
|
-
let
|
|
292
|
+
let u, Y = !1;
|
|
236
293
|
if (typeof window > "u")
|
|
237
294
|
try {
|
|
238
|
-
|
|
239
|
-
} catch (
|
|
295
|
+
u = require("bindings")("bigint_buffer"), Y = u !== void 0;
|
|
296
|
+
} catch (t) {
|
|
240
297
|
console.warn(
|
|
241
298
|
"bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)",
|
|
242
|
-
|
|
299
|
+
t
|
|
243
300
|
);
|
|
244
301
|
}
|
|
245
|
-
function
|
|
246
|
-
if (typeof window < "u" ||
|
|
247
|
-
const
|
|
248
|
-
|
|
249
|
-
const n =
|
|
302
|
+
function K(t) {
|
|
303
|
+
if (typeof window < "u" || u === void 0) {
|
|
304
|
+
const e = Buffer.from(t);
|
|
305
|
+
e.reverse();
|
|
306
|
+
const n = e.toString("hex");
|
|
250
307
|
return n.length === 0 ? BigInt(0) : BigInt(`0x${n}`);
|
|
251
308
|
}
|
|
252
|
-
return
|
|
309
|
+
return u.toBigInt(t, !1);
|
|
253
310
|
}
|
|
254
|
-
function
|
|
311
|
+
function ut() {
|
|
255
312
|
try {
|
|
256
|
-
return
|
|
313
|
+
return K(Buffer.from([1, 0])) === BigInt(1);
|
|
257
314
|
} catch {
|
|
258
315
|
return !1;
|
|
259
316
|
}
|
|
260
317
|
}
|
|
261
|
-
function
|
|
262
|
-
if (typeof window < "u" ||
|
|
263
|
-
const
|
|
264
|
-
return
|
|
318
|
+
function Q(t) {
|
|
319
|
+
if (typeof window < "u" || u === void 0) {
|
|
320
|
+
const e = t.toString("hex");
|
|
321
|
+
return e.length === 0 ? BigInt(0) : BigInt(`0x${e}`);
|
|
265
322
|
}
|
|
266
|
-
return
|
|
323
|
+
return u.toBigInt(t, !0);
|
|
267
324
|
}
|
|
268
|
-
function
|
|
269
|
-
if (typeof window < "u" ||
|
|
270
|
-
const n =
|
|
271
|
-
n.padStart(
|
|
325
|
+
function at(t, e) {
|
|
326
|
+
if (typeof window < "u" || u === void 0) {
|
|
327
|
+
const n = t.toString(16), r = Buffer.from(
|
|
328
|
+
n.padStart(e * 2, "0").slice(0, e * 2),
|
|
272
329
|
"hex"
|
|
273
330
|
);
|
|
274
331
|
return r.reverse(), r;
|
|
275
332
|
}
|
|
276
|
-
return
|
|
333
|
+
return u.fromBigInt(t, Buffer.allocUnsafe(e), !1);
|
|
277
334
|
}
|
|
278
|
-
function
|
|
279
|
-
if (typeof window < "u" ||
|
|
280
|
-
const n =
|
|
281
|
-
return Buffer.from(n.padStart(
|
|
335
|
+
function tt(t, e) {
|
|
336
|
+
if (typeof window < "u" || u === void 0) {
|
|
337
|
+
const n = t.toString(16);
|
|
338
|
+
return Buffer.from(n.padStart(e * 2, "0").slice(0, e * 2), "hex");
|
|
282
339
|
}
|
|
283
|
-
return
|
|
340
|
+
return u.fromBigInt(t, Buffer.allocUnsafe(e), !0);
|
|
284
341
|
}
|
|
285
|
-
function
|
|
286
|
-
if (
|
|
342
|
+
function et(t) {
|
|
343
|
+
if (t < BigInt(0))
|
|
287
344
|
throw new Error("bigintToBuf: negative bigint values are not supported");
|
|
288
|
-
if (
|
|
345
|
+
if (t === BigInt(0))
|
|
289
346
|
return Buffer.from([0]);
|
|
290
|
-
const
|
|
291
|
-
return
|
|
347
|
+
const e = t.toString(16), n = Math.ceil(e.length / 2);
|
|
348
|
+
return tt(t, n);
|
|
292
349
|
}
|
|
293
|
-
function
|
|
294
|
-
return
|
|
350
|
+
function nt(t) {
|
|
351
|
+
return Q(t);
|
|
295
352
|
}
|
|
296
|
-
function
|
|
297
|
-
if (
|
|
353
|
+
function ct(t) {
|
|
354
|
+
if (t < BigInt(0))
|
|
298
355
|
throw new Error("bigintToHex: negative bigint values are not supported");
|
|
299
|
-
const
|
|
300
|
-
return
|
|
356
|
+
const e = t.toString(16);
|
|
357
|
+
return e.length % 2 === 0 ? e : "0" + e;
|
|
301
358
|
}
|
|
302
|
-
function
|
|
303
|
-
const
|
|
304
|
-
return
|
|
359
|
+
function gt(t) {
|
|
360
|
+
const e = t.startsWith("0x") ? t.slice(2) : t;
|
|
361
|
+
return e.length === 0 ? BigInt(0) : BigInt(`0x${e}`);
|
|
305
362
|
}
|
|
306
|
-
function
|
|
307
|
-
return
|
|
363
|
+
function lt(t) {
|
|
364
|
+
return t.toString(10);
|
|
308
365
|
}
|
|
309
|
-
function
|
|
310
|
-
if (!
|
|
366
|
+
function dt(t) {
|
|
367
|
+
if (!t?.trim())
|
|
311
368
|
throw new Error("textToBigint: input string cannot be empty");
|
|
312
369
|
try {
|
|
313
|
-
return BigInt(
|
|
314
|
-
} catch (
|
|
315
|
-
throw new Error(`textToBigint: invalid decimal string "${
|
|
370
|
+
return BigInt(t);
|
|
371
|
+
} catch (e) {
|
|
372
|
+
throw new Error(`textToBigint: invalid decimal string "${t}" ${e instanceof Error ? e.message : String(e)}`);
|
|
316
373
|
}
|
|
317
374
|
}
|
|
318
|
-
function
|
|
319
|
-
if (
|
|
375
|
+
function Bt(t) {
|
|
376
|
+
if (t < BigInt(0))
|
|
320
377
|
throw new Error("bigintToBase64: negative bigint values are not supported");
|
|
321
|
-
return
|
|
378
|
+
return et(t).toString("base64");
|
|
322
379
|
}
|
|
323
|
-
function
|
|
324
|
-
if (!
|
|
380
|
+
function ht(t) {
|
|
381
|
+
if (!t?.trim())
|
|
325
382
|
throw new Error("base64ToBigint: input string cannot be empty");
|
|
326
|
-
const
|
|
327
|
-
if (!/^[A-Za-z0-9+/]+=*$/.test(
|
|
383
|
+
const e = t.trim();
|
|
384
|
+
if (!/^[A-Za-z0-9+/]+=*$/.test(e))
|
|
328
385
|
throw new Error("base64ToBigint: invalid base64 string format");
|
|
329
|
-
const n = Buffer.from(
|
|
330
|
-
return
|
|
386
|
+
const n = Buffer.from(e, "base64");
|
|
387
|
+
return nt(n);
|
|
331
388
|
}
|
|
332
389
|
export {
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
390
|
+
ht as base64ToBigint,
|
|
391
|
+
Bt as bigintToBase64,
|
|
392
|
+
et as bigintToBuf,
|
|
393
|
+
ct as bigintToHex,
|
|
394
|
+
lt as bigintToText,
|
|
395
|
+
nt as bufToBigint,
|
|
396
|
+
st as conversionUtils,
|
|
397
|
+
gt as hexToBigint,
|
|
398
|
+
Y as isNative,
|
|
399
|
+
dt as textToBigint,
|
|
400
|
+
Q as toBigIntBE,
|
|
401
|
+
K as toBigIntLE,
|
|
402
|
+
tt as toBufferBE,
|
|
403
|
+
at as toBufferLE,
|
|
404
|
+
ut as validateBigIntBuffer
|
|
348
405
|
};
|
package/dist/index.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(i,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("buffer"),require("path"),require("fs")):typeof define=="function"&&define.amd?define(["exports","buffer","path","fs"],o):(i=typeof globalThis<"u"?globalThis:i||self,o(i["bigint-buffer"]={},i.Buffer,i.path,i.fs))})(this,(function(i,o,
|
|
1
|
+
(function(i,o){typeof exports=="object"&&typeof module<"u"?o(exports,require("buffer"),require("path"),require("fs"),require("#pkg")):typeof define=="function"&&define.amd?define(["exports","buffer","path","fs","#pkg"],o):(i=typeof globalThis<"u"?globalThis:i||self,o(i["bigint-buffer"]={},i.Buffer,i.path,i.fs,i._pkg))})(this,(function(i,o,l,L,x){"use strict";const M=t=>t.startsWith("0x")||t.startsWith("0X")?t:`0x${t}`,d=t=>{if(t===0n)return"0x0";const e=t<0n,n=e?-t:t,r=x.bigintToHex(n);return`${e?"-":""}0x${r}`},s=t=>{if(!t)return 0n;const e=t.trim();if(e.length===0)return 0n;const n=e.startsWith("-"),r=n?e.slice(1):e,f=M(r),B=x.hexToBigint(f);return n?-B:B};function D(t,e=9){if(!Number.isFinite(t))return"0x0";const n=Math.pow(10,e),r=BigInt(Math.round(t*n));return d(r)}function U(t,e=9){const n=s(t);if(n===0n)return 0;const r=Math.pow(10,e);return Number(n)/r}function H(t,e){return d(s(t)+s(e))}function V(t,e){return d(s(t)-s(e))}function X(t){if(t.length===0)return"0x0";const e=t.reduce((n,r)=>n+s(r),0n);return d(e/BigInt(t.length))}function Z(t,e){const n=s(t)-s(e);return n===0n?0:n>0n?1:-1}function q(t){return s(t)}let h=!1,u,b;const g=typeof globalThis<"u"&&typeof globalThis.document<"u",E=[l.resolve(__dirname,".."),l.resolve(__dirname,"../../.."),l.resolve(__dirname,"../../../../")],j=()=>{for(const t of E){const e=l.join(t,"build","Release","bigint_buffer.node");if(L.existsSync(e))return t}return E[0]};function z(){try{const t=require("bindings"),e=j();return t({bindings:"bigint_buffer",module_root:e})}catch(t){b=t;return}}g||(u=z(),h=u!==void 0,!h&&b!==void 0&&process.env?.BIGINT_BUFFER_SILENT_NATIVE_FAIL!=="1"&&console.warn("bigint-buffer: Failed to load native bindings; using pure JS fallback. Run npm run rebuild to restore native.",b)),u===void 0&&(u={toBigInt:(t,e=!0)=>{const n=o.Buffer.from(t);e||n.reverse();const r=n.toString("hex");return r.length===0?0n:BigInt(`0x${r}`)},fromBigInt:(t,e,n=!0)=>{const r=t.toString(16),f=e.length,B=r.padStart(f*2,"0").slice(0,f*2),P=o.Buffer.from(B,"hex");return n||P.reverse(),P.copy(e),e}});function m(t){if(g||u===void 0){const e=o.Buffer.from(t);e.reverse();const n=e.toString("hex");return n.length===0?BigInt(0):BigInt(`0x${n}`)}return u.toBigInt(t,!1)}function W(){try{return m(o.Buffer.from([1,0]))===BigInt(1)}catch{return!1}}function C(t){if(g||u===void 0){const e=t.toString("hex");return e.length===0?BigInt(0):BigInt(`0x${e}`)}return u.toBigInt(t,!0)}function k(t,e){if(g||u===void 0){const n=t.toString(16),r=o.Buffer.from(n.padStart(e*2,"0").slice(0,e*2),"hex");return r.reverse(),r}return u.fromBigInt(t,o.Buffer.allocUnsafe(e),!1)}function J(t,e){if(g||u===void 0){const n=t.toString(16);return o.Buffer.from(n.padStart(e*2,"0").slice(0,e*2),"hex")}return u.fromBigInt(t,o.Buffer.allocUnsafe(e),!0)}function c(t,e=!1,n){const r=t.match(/^(0x)?([\da-fA-F]+)$/);if(r==null)throw new RangeError("input must be a hexadecimal string, e.g. '0x124fe3a' or '0214f1b2'");let f=r[2];if(n!==void 0){if(n<f.length/2)throw new RangeError(`expected byte length ${n} < input hex byte length ${Math.ceil(f.length/2)}`);f=f.padStart(n*2,"0")}return e?"0x"+f:f}function v(t,e=!1){if(t<0)throw RangeError("a should be a non-negative integer. Negative values are not supported");return T(y(t),e)}function w(t){let e=8n;ArrayBuffer.isView(t)?e=BigInt(t.BYTES_PER_ELEMENT*8):t=new Uint8Array(t);let n=0n;for(const r of t.values()){const f=BigInt(r);n=(n<<e)+f}return n}function y(t,e=!1,n){if(t<0)throw RangeError("a should be a non-negative integer. Negative values are not supported");return c(t.toString(16),e,n)}function p(t){return BigInt(c(t,!0))}function G(t){if(t<0)throw RangeError("a should be a non-negative integer. Negative values are not supported");return S(T(t.toString(16)))}function Y(t){return p($(_(t)))}function I(t){if(o.Buffer.isBuffer(t))return t;if(ArrayBuffer.isView(t))return o.Buffer.from(t.buffer,t.byteOffset,t.byteLength);if(t instanceof ArrayBuffer)return o.Buffer.from(new Uint8Array(t));throw new TypeError("Unsupported input type for Buffer.from")}function S(t){const e=I(t);return g?new TextDecoder().decode(new Uint8Array(e)):o.Buffer.from(e).toString()}function _(t,e=!1){return!g&&!e?o.Buffer.from(new TextEncoder().encode(t).buffer):new TextEncoder().encode(t).buffer}function $(t,e=!1,n){if(g){let r="";const f="0123456789abcdef";return ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer.slice(t.byteOffset,t.byteOffset+t.byteLength)):t=new Uint8Array(t),t.forEach(B=>{r+=f[B>>4]+f[B&15]}),c(r,e,n)}else{const r=I(t);return ArrayBuffer.isView(r)&&(t=new Uint8Array(r.buffer.slice(r.byteOffset,r.byteOffset+r.byteLength))),c(o.Buffer.from(I(t)).toString("hex"),e,n)}}function T(t,e=!1){let n=c(t);if(n=c(t,!1,Math.ceil(n.length/2)),g)return Uint8Array.from(n.match(/[\da-fA-F]{2}/g).map(r=>+("0x"+r))).buffer;{const r=o.Buffer.from(n,"hex");return e?r.buffer.slice(r.byteOffset,r.byteOffset+r.byteLength):r}}function K(t,e=!1,n=!0){if(t<0n)throw new RangeError("negative bigint");const r=v(t);let f=o.Buffer.isBuffer(r)?r.toString("base64"):o.Buffer.from(r).toString("base64");return e&&(f=f.replace(/\+/g,"-").replace(/\//g,"_")),n||(f=f.replace(/=+$/,"")),f}function Q(t){if(!t||t.trim()==="")return 0n;const e=t.trim();if(!/^[A-Za-z0-9+/=_-]*$/.test(e))throw new RangeError("invalid base64");let n=e.replace(/-/g,"+").replace(/_/g,"/");for(;n.length%4!==0;)n+="=";const r=o.Buffer.from(n,"base64");return w(r)}const tt=Object.freeze(Object.defineProperty({__proto__:null,FIXED_POINT_DECIMALS:9,ZERO_FIXED_POINT:"0x0",addFixedPoint:H,averageFixedPoint:X,base64ToBigint:Q,bigintToBase64:K,bigintToBuf:v,bigintToHex:y,bigintToText:G,bufToBigint:w,bufToHex:$,bufToText:S,compareFixedPoint:Z,fixedPointToBigInt:q,fromFixedPoint:U,hexToBigint:p,hexToBuf:T,get isNative(){return h},parseHex:c,subtractFixedPoint:V,textToBigint:Y,textToBuf:_,toBigIntBE:C,toBigIntLE:m,toBigIntValue:s,toBufferBE:J,toBufferLE:k,toFixedPoint:D,validateBigIntBuffer:W},Symbol.toStringTag,{value:"Module"}));let a;if(i.isNative=!1,typeof window>"u")try{a=require("bindings")("bigint_buffer"),i.isNative=a!==void 0}catch(t){console.warn("bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)",t)}function N(t){if(typeof window<"u"||a===void 0){const e=Buffer.from(t);e.reverse();const n=e.toString("hex");return n.length===0?BigInt(0):BigInt(`0x${n}`)}return a.toBigInt(t,!1)}function et(){try{return N(Buffer.from([1,0]))===BigInt(1)}catch{return!1}}function O(t){if(typeof window<"u"||a===void 0){const e=t.toString("hex");return e.length===0?BigInt(0):BigInt(`0x${e}`)}return a.toBigInt(t,!0)}function nt(t,e){if(typeof window<"u"||a===void 0){const n=t.toString(16),r=Buffer.from(n.padStart(e*2,"0").slice(0,e*2),"hex");return r.reverse(),r}return a.fromBigInt(t,Buffer.allocUnsafe(e),!1)}function F(t,e){if(typeof window<"u"||a===void 0){const n=t.toString(16);return Buffer.from(n.padStart(e*2,"0").slice(0,e*2),"hex")}return a.fromBigInt(t,Buffer.allocUnsafe(e),!0)}function A(t){if(t<BigInt(0))throw new Error("bigintToBuf: negative bigint values are not supported");if(t===BigInt(0))return Buffer.from([0]);const e=t.toString(16),n=Math.ceil(e.length/2);return F(t,n)}function R(t){return O(t)}function rt(t){if(t<BigInt(0))throw new Error("bigintToHex: negative bigint values are not supported");const e=t.toString(16);return e.length%2===0?e:"0"+e}function it(t){const e=t.startsWith("0x")?t.slice(2):t;return e.length===0?BigInt(0):BigInt(`0x${e}`)}function ot(t){return t.toString(10)}function ft(t){if(!t?.trim())throw new Error("textToBigint: input string cannot be empty");try{return BigInt(t)}catch(e){throw new Error(`textToBigint: invalid decimal string "${t}" ${e instanceof Error?e.message:String(e)}`)}}function ut(t){if(t<BigInt(0))throw new Error("bigintToBase64: negative bigint values are not supported");return A(t).toString("base64")}function st(t){if(!t?.trim())throw new Error("base64ToBigint: input string cannot be empty");const e=t.trim();if(!/^[A-Za-z0-9+/]+=*$/.test(e))throw new Error("base64ToBigint: invalid base64 string format");const n=Buffer.from(e,"base64");return R(n)}i.base64ToBigint=st,i.bigintToBase64=ut,i.bigintToBuf=A,i.bigintToHex=rt,i.bigintToText=ot,i.bufToBigint=R,i.conversionUtils=tt,i.hexToBigint=it,i.textToBigint=ft,i.toBigIntBE=O,i.toBigIntLE=N,i.toBufferBE=F,i.toBufferLE=nt,i.validateBigIntBuffer=et,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
|