@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.
Files changed (48) hide show
  1. package/README.md +98 -90
  2. package/build/Release/bigint_buffer.iobj +0 -0
  3. package/build/Release/bigint_buffer.ipdb +0 -0
  4. package/build/Release/bigint_buffer.node +0 -0
  5. package/build/Release/bigint_buffer.pdb +0 -0
  6. package/build/Release/obj/bigint_buffer/src/bigint-buffer.obj +0 -0
  7. package/build/Release/obj/bigint_buffer/win_delay_load_hook.obj +0 -0
  8. package/build/binding.sln +3 -3
  9. package/build/conversion/index.js +18 -0
  10. package/build/conversion/index.js.map +1 -0
  11. package/build/conversion/src/ts/fixedPoint/index.d.ts +12 -0
  12. package/build/conversion/src/ts/fixedPoint/index.js +79 -0
  13. package/build/conversion/src/ts/fixedPoint/index.js.map +1 -0
  14. package/build/conversion/src/ts/index.d.ts +169 -0
  15. package/build/conversion/src/ts/index.js +453 -0
  16. package/build/conversion/src/ts/index.js.map +1 -0
  17. package/build/conversion/test/bigintToBase64.test.js +72 -0
  18. package/build/conversion/test/bigintToBase64.test.js.map +1 -0
  19. package/build/conversion/test/bigintToBuf.test.js +77 -0
  20. package/build/conversion/test/bigintToBuf.test.js.map +1 -0
  21. package/build/conversion/test/bigintToHex.test.js +86 -0
  22. package/build/conversion/test/bigintToHex.test.js.map +1 -0
  23. package/build/conversion/test/bigintToText.test.js +63 -0
  24. package/build/conversion/test/bigintToText.test.js.map +1 -0
  25. package/build/conversion/test/bufToBigint.test.js +55 -0
  26. package/build/conversion/test/bufToBigint.test.js.map +1 -0
  27. package/build/conversion/test/hexToBigint.test.js +56 -0
  28. package/build/conversion/test/hexToBigint.test.js.map +1 -0
  29. package/build/conversion/test/hexToBuf.test.js +73 -0
  30. package/build/conversion/test/hexToBuf.test.js.map +1 -0
  31. package/build/conversion/test/parseHex.test.js +69 -0
  32. package/build/conversion/test/parseHex.test.js.map +1 -0
  33. package/build/conversion/test/textToBuf.test.js +59 -0
  34. package/build/conversion/test/textToBuf.test.js.map +1 -0
  35. package/build/index.js +242 -0
  36. package/build/index.js.map +1 -0
  37. package/dist/index.cjs +1 -1
  38. package/dist/index.js +257 -200
  39. package/dist/index.umd.js +1 -1
  40. package/dist/tsconfig.tsbuildinfo +1 -1
  41. package/dist/types/conversion/src/ts/fixedPoint/index.d.ts +12 -0
  42. package/dist/types/conversion/src/ts/index.d.ts +1 -0
  43. package/package.json +132 -132
  44. package/src/conversion/docs/README.md +9 -1
  45. package/src/conversion/src/docs/index.md +6 -1
  46. package/src/conversion/src/ts/fixedPoint/index.ts +87 -0
  47. package/src/conversion/src/ts/index.ts +15 -0
  48. /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 c from "path";
3
- import S from "fs";
4
- let l = !1, f, B;
5
- const u = typeof globalThis < "u" && typeof globalThis.document < "u", x = [
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
- c.resolve(__dirname, ".."),
54
+ l.resolve(__dirname, ".."),
8
55
  // when running from build/conversion/src/ts
9
- c.resolve(__dirname, "../../.."),
56
+ l.resolve(__dirname, "../../.."),
10
57
  // when running from src/conversion/src/ts
11
- c.resolve(__dirname, "../../../../")
12
- ], $ = () => {
13
- for (const e of x) {
14
- const t = c.join(e, "build", "Release", "bigint_buffer.node");
15
- if (S.existsSync(t)) return e;
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 x[0];
64
+ return I[0];
18
65
  };
19
- function _() {
66
+ function D() {
20
67
  try {
21
- const e = require("bindings"), t = $();
22
- return e({
68
+ const t = require("bindings"), e = W();
69
+ return t({
23
70
  bindings: "bigint_buffer",
24
- module_root: t
71
+ module_root: e
25
72
  });
26
- } catch (e) {
27
- B = e;
73
+ } catch (t) {
74
+ b = t;
28
75
  return;
29
76
  }
30
77
  }
31
- u || (f = _(), l = f !== void 0, !l && B !== void 0 && process.env?.BIGINT_BUFFER_SILENT_NATIVE_FAIL !== "1" && console.warn(
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
- B
80
+ b
34
81
  ));
35
82
  f === void 0 && (f = {
36
- toBigInt: (e, t = !0) => {
37
- const n = o.from(e);
38
- t || n.reverse();
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: (e, t, n = !0) => {
43
- const r = e.toString(16), i = t.length, g = r.padStart(i * 2, "0").slice(0, i * 2), b = o.from(g, "hex");
44
- return n || b.reverse(), b.copy(t), t;
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(e) {
48
- if (u || f === void 0) {
49
- const t = o.from(e);
50
- t.reverse();
51
- const n = t.toString("hex");
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(e, !1);
101
+ return f.toBigInt(t, !1);
55
102
  }
56
- function A() {
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 R(e) {
64
- if (u || f === void 0) {
65
- const t = e.toString("hex");
66
- return t.length === 0 ? BigInt(0) : BigInt(`0x${t}`);
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(e, !0);
115
+ return f.toBigInt(t, !0);
69
116
  }
70
- function U(e, t) {
71
- if (u || f === void 0) {
72
- const n = e.toString(16), r = o.from(
73
- n.padStart(t * 2, "0").slice(0, t * 2),
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(e, o.allocUnsafe(t), !1);
125
+ return f.fromBigInt(t, o.allocUnsafe(e), !1);
79
126
  }
80
- function N(e, t) {
81
- if (u || f === void 0) {
82
- const n = e.toString(16);
83
- return o.from(n.padStart(t * 2, "0").slice(0, t * 2), "hex");
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(e, o.allocUnsafe(t), !0);
132
+ return f.fromBigInt(t, o.allocUnsafe(e), !0);
86
133
  }
87
- function a(e, t = !1, n) {
88
- const r = e.match(/^(0x)?([\da-fA-F]+)$/);
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 t ? "0x" + i : i;
150
+ return e ? "0x" + i : i;
104
151
  }
105
- function p(e, t = !1) {
106
- if (e < 0)
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 h(m(e), t);
157
+ return T(y(t), e);
111
158
  }
112
- function T(e) {
113
- let t = 8n;
114
- ArrayBuffer.isView(e) ? t = BigInt(e.BYTES_PER_ELEMENT * 8) : e = new Uint8Array(e);
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 e.values()) {
163
+ for (const r of t.values()) {
117
164
  const i = BigInt(r);
118
- n = (n << t) + i;
165
+ n = (n << e) + i;
119
166
  }
120
167
  return n;
121
168
  }
122
- function m(e, t = !1, n) {
123
- if (e < 0)
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 a(e.toString(16), t, n);
174
+ return g(t.toString(16), e, n);
128
175
  }
129
- function w(e) {
130
- return BigInt(a(e, !0));
176
+ function S(t) {
177
+ return BigInt(g(t, !0));
131
178
  }
132
- function L(e) {
133
- if (e < 0)
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 I(h(e.toString(16)));
138
- }
139
- function O(e) {
140
- return w(E(y(e)));
141
- }
142
- function d(e) {
143
- if (o.isBuffer(e))
144
- return e;
145
- if (ArrayBuffer.isView(e))
146
- return o.from(e.buffer, e.byteOffset, e.byteLength);
147
- if (e instanceof ArrayBuffer)
148
- return o.from(new Uint8Array(e));
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 I(e) {
152
- const t = d(e);
153
- return u ? new TextDecoder().decode(new Uint8Array(t)) : o.from(t).toString();
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 y(e, t = !1) {
156
- return !u && !t ? o.from(new TextEncoder().encode(e).buffer) : new TextEncoder().encode(e).buffer;
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 E(e, t = !1, n) {
159
- if (u) {
205
+ function A(t, e = !1, n) {
206
+ if (a) {
160
207
  let r = "";
161
208
  const i = "0123456789abcdef";
162
- return ArrayBuffer.isView(e) ? e = new Uint8Array(
163
- e.buffer.slice(e.byteOffset, e.byteOffset + e.byteLength)
164
- ) : e = new Uint8Array(e), e.forEach((g) => {
165
- r += i[g >> 4] + i[g & 15];
166
- }), a(r, t, n);
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 = d(e);
169
- return ArrayBuffer.isView(r) && (e = new Uint8Array(
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
- )), a(
175
- o.from(d(e)).toString("hex"),
176
- t,
221
+ )), g(
222
+ o.from(x(t)).toString("hex"),
223
+ e,
177
224
  n
178
225
  );
179
226
  }
180
227
  }
181
- function h(e, t = !1) {
182
- let n = a(e);
183
- if (n = a(e, !1, Math.ceil(n.length / 2)), u)
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 t ? r.buffer.slice(r.byteOffset, r.byteOffset + r.byteLength) : r;
236
+ return e ? r.buffer.slice(r.byteOffset, r.byteOffset + r.byteLength) : r;
190
237
  }
191
238
  }
192
- function F(e, t = !1, n = !0) {
193
- if (e < 0n)
239
+ function C(t, e = !1, n = !0) {
240
+ if (t < 0n)
194
241
  throw new RangeError("negative bigint");
195
- const r = p(e);
242
+ const r = w(t);
196
243
  let i = o.isBuffer(r) ? r.toString("base64") : o.from(r).toString("base64");
197
- return t && (i = i.replace(/\+/g, "-").replace(/\//g, "_")), n || (i = i.replace(/=+$/, "")), i;
244
+ return e && (i = i.replace(/\+/g, "-").replace(/\//g, "_")), n || (i = i.replace(/=+$/, "")), i;
198
245
  }
199
- function H(e) {
200
- if (!e || e.trim() === "")
246
+ function G(t) {
247
+ if (!t || t.trim() === "")
201
248
  return 0n;
202
- const t = e.trim();
203
- if (!/^[A-Za-z0-9+/=_-]*$/.test(t))
249
+ const e = t.trim();
250
+ if (!/^[A-Za-z0-9+/=_-]*$/.test(e))
204
251
  throw new RangeError("invalid base64");
205
- let n = t.replace(/-/g, "+").replace(/_/g, "/");
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 T(r);
256
+ return E(r);
210
257
  }
211
- const k = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
258
+ const st = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
212
259
  __proto__: null,
213
- base64ToBigint: H,
214
- bigintToBase64: F,
215
- bigintToBuf: p,
216
- bigintToHex: m,
217
- bigintToText: L,
218
- bufToBigint: T,
219
- bufToHex: E,
220
- bufToText: I,
221
- hexToBigint: w,
222
- hexToBuf: h,
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 l;
278
+ return h;
225
279
  },
226
- parseHex: a,
227
- textToBigint: O,
228
- textToBuf: y,
229
- toBigIntBE: R,
280
+ parseHex: g,
281
+ subtractFixedPoint: M,
282
+ textToBigint: k,
283
+ textToBuf: _,
284
+ toBigIntBE: X,
230
285
  toBigIntLE: v,
231
- toBufferBE: N,
232
- toBufferLE: U,
233
- validateBigIntBuffer: A
286
+ toBigIntValue: s,
287
+ toBufferBE: q,
288
+ toBufferLE: Z,
289
+ toFixedPoint: O,
290
+ validateBigIntBuffer: j
234
291
  }, Symbol.toStringTag, { value: "Module" }));
235
- let s, M = !1;
292
+ let u, Y = !1;
236
293
  if (typeof window > "u")
237
294
  try {
238
- s = require("bindings")("bigint_buffer"), M = s !== void 0;
239
- } catch (e) {
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
- e
299
+ t
243
300
  );
244
301
  }
245
- function V(e) {
246
- if (typeof window < "u" || s === void 0) {
247
- const t = Buffer.from(e);
248
- t.reverse();
249
- const n = t.toString("hex");
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 s.toBigInt(e, !1);
309
+ return u.toBigInt(t, !1);
253
310
  }
254
- function D() {
311
+ function ut() {
255
312
  try {
256
- return V(Buffer.from([1, 0])) === BigInt(1);
313
+ return K(Buffer.from([1, 0])) === BigInt(1);
257
314
  } catch {
258
315
  return !1;
259
316
  }
260
317
  }
261
- function j(e) {
262
- if (typeof window < "u" || s === void 0) {
263
- const t = e.toString("hex");
264
- return t.length === 0 ? BigInt(0) : BigInt(`0x${t}`);
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 s.toBigInt(e, !0);
323
+ return u.toBigInt(t, !0);
267
324
  }
268
- function G(e, t) {
269
- if (typeof window < "u" || s === void 0) {
270
- const n = e.toString(16), r = Buffer.from(
271
- n.padStart(t * 2, "0").slice(0, t * 2),
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 s.fromBigInt(e, Buffer.allocUnsafe(t), !1);
333
+ return u.fromBigInt(t, Buffer.allocUnsafe(e), !1);
277
334
  }
278
- function z(e, t) {
279
- if (typeof window < "u" || s === void 0) {
280
- const n = e.toString(16);
281
- return Buffer.from(n.padStart(t * 2, "0").slice(0, t * 2), "hex");
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 s.fromBigInt(e, Buffer.allocUnsafe(t), !0);
340
+ return u.fromBigInt(t, Buffer.allocUnsafe(e), !0);
284
341
  }
285
- function q(e) {
286
- if (e < BigInt(0))
342
+ function et(t) {
343
+ if (t < BigInt(0))
287
344
  throw new Error("bigintToBuf: negative bigint values are not supported");
288
- if (e === BigInt(0))
345
+ if (t === BigInt(0))
289
346
  return Buffer.from([0]);
290
- const t = e.toString(16), n = Math.ceil(t.length / 2);
291
- return z(e, n);
347
+ const e = t.toString(16), n = Math.ceil(e.length / 2);
348
+ return tt(t, n);
292
349
  }
293
- function J(e) {
294
- return j(e);
350
+ function nt(t) {
351
+ return Q(t);
295
352
  }
296
- function Y(e) {
297
- if (e < BigInt(0))
353
+ function ct(t) {
354
+ if (t < BigInt(0))
298
355
  throw new Error("bigintToHex: negative bigint values are not supported");
299
- const t = e.toString(16);
300
- return t.length % 2 === 0 ? t : "0" + t;
356
+ const e = t.toString(16);
357
+ return e.length % 2 === 0 ? e : "0" + e;
301
358
  }
302
- function C(e) {
303
- const t = e.startsWith("0x") ? e.slice(2) : e;
304
- return t.length === 0 ? BigInt(0) : BigInt(`0x${t}`);
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 K(e) {
307
- return e.toString(10);
363
+ function lt(t) {
364
+ return t.toString(10);
308
365
  }
309
- function Q(e) {
310
- if (!e?.trim())
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(e);
314
- } catch (t) {
315
- throw new Error(`textToBigint: invalid decimal string "${e}" ${t instanceof Error ? t.message : String(t)}`);
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 X(e) {
319
- if (e < BigInt(0))
375
+ function Bt(t) {
376
+ if (t < BigInt(0))
320
377
  throw new Error("bigintToBase64: negative bigint values are not supported");
321
- return q(e).toString("base64");
378
+ return et(t).toString("base64");
322
379
  }
323
- function ee(e) {
324
- if (!e?.trim())
380
+ function ht(t) {
381
+ if (!t?.trim())
325
382
  throw new Error("base64ToBigint: input string cannot be empty");
326
- const t = e.trim();
327
- if (!/^[A-Za-z0-9+/]+=*$/.test(t))
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(t, "base64");
330
- return J(n);
386
+ const n = Buffer.from(e, "base64");
387
+ return nt(n);
331
388
  }
332
389
  export {
333
- ee as base64ToBigint,
334
- X as bigintToBase64,
335
- q as bigintToBuf,
336
- Y as bigintToHex,
337
- K as bigintToText,
338
- J as bufToBigint,
339
- k as conversionUtils,
340
- C as hexToBigint,
341
- M as isNative,
342
- Q as textToBigint,
343
- j as toBigIntBE,
344
- V as toBigIntLE,
345
- z as toBufferBE,
346
- G as toBufferLE,
347
- D as validateBigIntBuffer
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,c,N){"use strict";let l=!1,u,d;const a=typeof globalThis<"u"&&typeof globalThis.document<"u",T=[c.resolve(__dirname,".."),c.resolve(__dirname,"../../.."),c.resolve(__dirname,"../../../../")],L=()=>{for(const e of T){const t=c.join(e,"build","Release","bigint_buffer.node");if(N.existsSync(t))return e}return T[0]};function O(){try{const e=require("bindings"),t=L();return e({bindings:"bigint_buffer",module_root:t})}catch(e){d=e;return}}a||(u=O(),l=u!==void 0,!l&&d!==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.",d)),u===void 0&&(u={toBigInt:(e,t=!0)=>{const n=o.Buffer.from(e);t||n.reverse();const r=n.toString("hex");return r.length===0?0n:BigInt(`0x${r}`)},fromBigInt:(e,t,n=!0)=>{const r=e.toString(16),f=t.length,B=r.padStart(f*2,"0").slice(0,f*2),U=o.Buffer.from(B,"hex");return n||U.reverse(),U.copy(t),t}});function v(e){if(a||u===void 0){const t=o.Buffer.from(e);t.reverse();const n=t.toString("hex");return n.length===0?BigInt(0):BigInt(`0x${n}`)}return u.toBigInt(e,!1)}function H(){try{return v(o.Buffer.from([1,0]))===BigInt(1)}catch{return!1}}function M(e){if(a||u===void 0){const t=e.toString("hex");return t.length===0?BigInt(0):BigInt(`0x${t}`)}return u.toBigInt(e,!0)}function F(e,t){if(a||u===void 0){const n=e.toString(16),r=o.Buffer.from(n.padStart(t*2,"0").slice(0,t*2),"hex");return r.reverse(),r}return u.fromBigInt(e,o.Buffer.allocUnsafe(t),!1)}function j(e,t){if(a||u===void 0){const n=e.toString(16);return o.Buffer.from(n.padStart(t*2,"0").slice(0,t*2),"hex")}return u.fromBigInt(e,o.Buffer.allocUnsafe(t),!0)}function g(e,t=!1,n){const r=e.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 t?"0x"+f:f}function x(e,t=!1){if(e<0)throw RangeError("a should be a non-negative integer. Negative values are not supported");return b(w(e),t)}function I(e){let t=8n;ArrayBuffer.isView(e)?t=BigInt(e.BYTES_PER_ELEMENT*8):e=new Uint8Array(e);let n=0n;for(const r of e.values()){const f=BigInt(r);n=(n<<t)+f}return n}function w(e,t=!1,n){if(e<0)throw RangeError("a should be a non-negative integer. Negative values are not supported");return g(e.toString(16),t,n)}function m(e){return BigInt(g(e,!0))}function q(e){if(e<0)throw RangeError("a should be a non-negative integer. Negative values are not supported");return y(b(e.toString(16)))}function V(e){return m(p(E(e)))}function h(e){if(o.Buffer.isBuffer(e))return e;if(ArrayBuffer.isView(e))return o.Buffer.from(e.buffer,e.byteOffset,e.byteLength);if(e instanceof ArrayBuffer)return o.Buffer.from(new Uint8Array(e));throw new TypeError("Unsupported input type for Buffer.from")}function y(e){const t=h(e);return a?new TextDecoder().decode(new Uint8Array(t)):o.Buffer.from(t).toString()}function E(e,t=!1){return!a&&!t?o.Buffer.from(new TextEncoder().encode(e).buffer):new TextEncoder().encode(e).buffer}function p(e,t=!1,n){if(a){let r="";const f="0123456789abcdef";return ArrayBuffer.isView(e)?e=new Uint8Array(e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)):e=new Uint8Array(e),e.forEach(B=>{r+=f[B>>4]+f[B&15]}),g(r,t,n)}else{const r=h(e);return ArrayBuffer.isView(r)&&(e=new Uint8Array(r.buffer.slice(r.byteOffset,r.byteOffset+r.byteLength))),g(o.Buffer.from(h(e)).toString("hex"),t,n)}}function b(e,t=!1){let n=g(e);if(n=g(e,!1,Math.ceil(n.length/2)),a)return Uint8Array.from(n.match(/[\da-fA-F]{2}/g).map(r=>+("0x"+r))).buffer;{const r=o.Buffer.from(n,"hex");return t?r.buffer.slice(r.byteOffset,r.byteOffset+r.byteLength):r}}function z(e,t=!1,n=!0){if(e<0n)throw new RangeError("negative bigint");const r=x(e);let f=o.Buffer.isBuffer(r)?r.toString("base64"):o.Buffer.from(r).toString("base64");return t&&(f=f.replace(/\+/g,"-").replace(/\//g,"_")),n||(f=f.replace(/=+$/,"")),f}function P(e){if(!e||e.trim()==="")return 0n;const t=e.trim();if(!/^[A-Za-z0-9+/=_-]*$/.test(t))throw new RangeError("invalid base64");let n=t.replace(/-/g,"+").replace(/_/g,"/");for(;n.length%4!==0;)n+="=";const r=o.Buffer.from(n,"base64");return I(r)}const J=Object.freeze(Object.defineProperty({__proto__:null,base64ToBigint:P,bigintToBase64:z,bigintToBuf:x,bigintToHex:w,bigintToText:q,bufToBigint:I,bufToHex:p,bufToText:y,hexToBigint:m,hexToBuf:b,get isNative(){return l},parseHex:g,textToBigint:V,textToBuf:E,toBigIntBE:M,toBigIntLE:v,toBufferBE:j,toBufferLE:F,validateBigIntBuffer:H},Symbol.toStringTag,{value:"Module"}));let s;if(i.isNative=!1,typeof window>"u")try{s=require("bindings")("bigint_buffer"),i.isNative=s!==void 0}catch(e){console.warn("bigint: Failed to load bindings, pure JS will be used (try npm run rebuild?)",e)}function S(e){if(typeof window<"u"||s===void 0){const t=Buffer.from(e);t.reverse();const n=t.toString("hex");return n.length===0?BigInt(0):BigInt(`0x${n}`)}return s.toBigInt(e,!1)}function W(){try{return S(Buffer.from([1,0]))===BigInt(1)}catch{return!1}}function $(e){if(typeof window<"u"||s===void 0){const t=e.toString("hex");return t.length===0?BigInt(0):BigInt(`0x${t}`)}return s.toBigInt(e,!0)}function Z(e,t){if(typeof window<"u"||s===void 0){const n=e.toString(16),r=Buffer.from(n.padStart(t*2,"0").slice(0,t*2),"hex");return r.reverse(),r}return s.fromBigInt(e,Buffer.allocUnsafe(t),!1)}function _(e,t){if(typeof window<"u"||s===void 0){const n=e.toString(16);return Buffer.from(n.padStart(t*2,"0").slice(0,t*2),"hex")}return s.fromBigInt(e,Buffer.allocUnsafe(t),!0)}function A(e){if(e<BigInt(0))throw new Error("bigintToBuf: negative bigint values are not supported");if(e===BigInt(0))return Buffer.from([0]);const t=e.toString(16),n=Math.ceil(t.length/2);return _(e,n)}function R(e){return $(e)}function k(e){if(e<BigInt(0))throw new Error("bigintToHex: negative bigint values are not supported");const t=e.toString(16);return t.length%2===0?t:"0"+t}function D(e){const t=e.startsWith("0x")?e.slice(2):e;return t.length===0?BigInt(0):BigInt(`0x${t}`)}function G(e){return e.toString(10)}function Y(e){if(!e?.trim())throw new Error("textToBigint: input string cannot be empty");try{return BigInt(e)}catch(t){throw new Error(`textToBigint: invalid decimal string "${e}" ${t instanceof Error?t.message:String(t)}`)}}function C(e){if(e<BigInt(0))throw new Error("bigintToBase64: negative bigint values are not supported");return A(e).toString("base64")}function K(e){if(!e?.trim())throw new Error("base64ToBigint: input string cannot be empty");const t=e.trim();if(!/^[A-Za-z0-9+/]+=*$/.test(t))throw new Error("base64ToBigint: invalid base64 string format");const n=Buffer.from(t,"base64");return R(n)}i.base64ToBigint=K,i.bigintToBase64=C,i.bigintToBuf=A,i.bigintToHex=k,i.bigintToText=G,i.bufToBigint=R,i.conversionUtils=J,i.hexToBigint=D,i.textToBigint=Y,i.toBigIntBE=$,i.toBigIntLE=S,i.toBufferBE=_,i.toBufferLE=Z,i.validateBigIntBuffer=W,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})}));
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"})}));